-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathSmolVLM2.swift
357 lines (307 loc) · 14.6 KB
/
SmolVLM2.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// SmolVLM2.swift
// mlx-swift-examples
//
// Created by Pedro Cuenca on 20/3/25.
//
import CoreImage
import CoreMedia
import Foundation
import MLX
import MLXLMCommon
import Tokenizers
// MARK: - Configuration and modeling are Idefics3
typealias SmolVLM2Configuration = Idefics3Configuration
typealias SmolVLM2 = Idefics3
// MARK: - SmolVLMProcessor and configuration
public struct SmolVLMProcessorConfiguration: Codable, Sendable {
public struct Size: Codable, Sendable {
public let longestEdge: Int
enum CodingKeys: String, CodingKey {
case longestEdge = "longest_edge"
}
}
public struct VideoSampling: Codable, Sendable {
public let fps: Int
public let maxFrames: Int
// Intentionally ignoring videoSize because I believe it's still wrong in the config files
// public let videoSize: Size
enum CodingKeys: String, CodingKey {
case fps
case maxFrames = "max_frames"
}
}
public let imageMean: [CGFloat]
public let imageStd: [CGFloat]
public let size: Size
public let maxImageSize: Size
public let videoSampling: VideoSampling
private let _imageSequenceLength: Int?
// TODO: this does not come in preprocessor_config.json, verify where transformers gets it from
public var imageSequenceLength: Int { _imageSequenceLength ?? 64 }
init(
imageMean: [CGFloat], imageStd: [CGFloat], size: Size, maxImageSize: Size,
videoSampling: VideoSampling, imageSequenceLength: Int?
) {
self.imageMean = imageMean
self.imageStd = imageStd
self.size = size
self.maxImageSize = maxImageSize
self.videoSampling = videoSampling
self._imageSequenceLength = imageSequenceLength
}
public var imageMeanTuple: (CGFloat, CGFloat, CGFloat) {
(imageMean[0], imageMean[1], imageMean[2])
}
public var imageStdTuple: (CGFloat, CGFloat, CGFloat) {
(imageStd[0], imageStd[1], imageStd[2])
}
enum CodingKeys: String, CodingKey {
case imageMean = "image_mean"
case imageStd = "image_std"
case size
case maxImageSize = "max_image_size"
case videoSampling = "video_sampling"
case _imageSequenceLength = "image_seq_len"
}
}
public class SmolVLMProcessor: UserInputProcessor {
private let config: SmolVLMProcessorConfiguration
private let tokenizer: any Tokenizer
// FIXME: hardcoded values for now
// Hardcode this since we can't pass it in or rely on it from the preprocessor config.
let imageTokenId = 49190
let imageToken = "<image>"
let fakeImageToken = "<fake_token_around_image>"
let globalImageToken = "<global-img>"
var maxProcessingImageSize: CGFloat { CGFloat(config.size.longestEdge) } // 2048
var fixedImageSize: CGFloat { CGFloat(config.maxImageSize.longestEdge) } // 384 for big models, 512 for small models (200-500M)
var imageSequenceLength: Int { config.imageSequenceLength }
var maxVideoFrames: Int { 20 /*config.videoSampling.maxFrames*/ }
var targetVideoFPS: Double { Double(config.videoSampling.fps) }
let defaultVideoSystemMessage =
"You are a helpful assistant that can understand videos. Describe what type of video this is and what's happening in it."
public init(
_ config: SmolVLMProcessorConfiguration,
tokenizer: any Tokenizer
) {
self.config = config
self.tokenizer = tokenizer
}
func getVideoPromptString(
frameCount: Int, timeStamps: [String], videoDuration: String, seqLen: Int,
fakeToken: String, imageToken: String, globalImageToken: String
) -> String {
var textSplitFrames =
"You are provided the following series of \(frameCount) frames from a \(videoDuration) [H:MM:SS] video.\n"
for frameIndex in 0 ..< frameCount {
textSplitFrames += "\nFrame from \(timeStamps[frameIndex]):"
textSplitFrames +=
(fakeToken
+ globalImageToken
+ String(repeating: imageToken, count: seqLen)
+ fakeToken)
}
textSplitFrames += "\n\n"
return textSplitFrames
}
func getImagePromptString(
rows: Int, cols: Int, seqLen: Int, fakeToken: String, imageToken: String,
globalImageToken: String
) -> String {
/// Prompt with expanded image tokens for when the image is split into patches.
/// This applies to image processing, not video (I think).
/// This just transliterates this: https://github.com/huggingface/transformers/blob/6a1ab634b6886b6560b0502e7a305c8cd881732e/src/transformers/models/idefics3/processing_idefics3.py#L44
var textSplitImages = ""
for h in 0 ..< rows {
for w in 0 ..< cols {
textSplitImages +=
(fakeToken
+ "<row_\(h + 1)_col_\(w + 1)>"
+ String(repeating: imageToken, count: seqLen))
}
textSplitImages += "\n"
}
textSplitImages +=
("\n"
+ fakeToken
+ globalImageToken
+ String(repeating: imageToken, count: seqLen)
+ fakeToken)
return textSplitImages
}
/// Compute the resize size with `longestEdge` for the given size
/// If `multiple` is not nil, ensures each side is a multiple of that value
func aspectRatioSize(for size: CGSize, longestEdge: CGFloat, multiple: CGFloat? = nil) -> CGSize
{
var targetSize = MediaProcessing.bestFit(
size, in: CGSize(width: longestEdge, height: longestEdge))
guard let multiple = multiple else { return targetSize }
let aspectRatio = targetSize.width / targetSize.height
if size.width >= size.height {
let width = ceil(targetSize.width / multiple) * multiple
var height = width / aspectRatio
height = ceil(height / multiple) * multiple
return CGSize(width: width, height: height)
} else {
let height = ceil(targetSize.height / multiple) * multiple
var width = height * aspectRatio
width = ceil(width / multiple) * multiple
return CGSize(width: width, height: height)
}
}
/// Compute the resize size with `longestEdge` for the given size
/// If `multiple` is not nil, ensures each side is a multiple of that value
func aspectRatioSize(for size: CGSize, longestEdge: Int, multiple: Int? = nil) -> CGSize {
return aspectRatioSize(
for: size, longestEdge: CGFloat(longestEdge), multiple: multiple.flatMap(CGFloat.init))
}
/// Tile image if it's larger than the maxProcessingImageSize, so the model gets to see more of it
/// TODO: disable in video mode
func tiles(from originalImage: CIImage) -> (tiles: [CIImage], rows: Int, cols: Int) {
// The original code resizes to maxProcessingImageSize, then resizes again ensuring multiples of fixedImageSize
// We do both resizes in one go
let processingSize = aspectRatioSize(
for: originalImage.extent.size, longestEdge: maxProcessingImageSize,
multiple: fixedImageSize)
let image = MediaProcessing.resampleLanczos(originalImage, to: processingSize)
var tiles: [CIImage] = []
// Crop nRows x nCols tiles
let nRows = Int(ceil(image.extent.size.height / CGFloat(fixedImageSize)))
let nCols = Int(ceil(image.extent.size.width / CGFloat(fixedImageSize)))
// Warning: in CIImage, y=0 is the bottom side. We reverse the rows to match the transformers processor
let tileEdge = Int(fixedImageSize)
for row in (0 ..< nRows).reversed() {
for col in 0 ..< nCols {
let x0 = col * tileEdge
let y0 = row * tileEdge
let x1 = min(x0 + tileEdge, Int(image.extent.size.width))
let y1 = min(y0 + tileEdge, Int(image.extent.size.height))
let tile = image.cropped(to: CGRect(x: x0, y: y0, width: x1 - x0, height: y1 - y0))
tiles.append(tile)
}
}
return (tiles, nRows, nCols)
}
func formatTimestamp(_ time: CMTime) -> String {
let totalSeconds = Int(ceil(time.seconds))
let hours = totalSeconds / 3600
let minutes = (totalSeconds % 3600) / 60
let seconds = totalSeconds % 60
return String(format: "%d:%02d:%02d", hours, minutes, seconds)
}
public func prepare(input: UserInput) async throws -> LMInput {
let messages = Qwen2VLMessageGenerator().generate(from: input) // TODO: Create SmolVLM2MessageGenerator
if input.images.isEmpty && input.videos.isEmpty {
// No image scenario
let promptTokens = try tokenizer.applyChatTemplate(messages: messages)
let tokensArray = MLXArray(promptTokens).expandedDimensions(axis: 0)
let mask = ones(like: tokensArray)
return LMInput(text: .init(tokens: tokensArray, mask: mask), image: nil)
} else if input.images.count > 0 && input.videos.isEmpty {
// Single image scenario
guard input.images.count == 1 else {
throw VLMError.singleImageAllowed
}
// Unfortunately we don't have a "render" option in Tokenizers yet, so decoding
let promptTokens = try tokenizer.applyChatTemplate(messages: messages)
let decoded = try tokenizer.decode(tokens: promptTokens, skipSpecialTokens: false)
let image = try input.images[0].asCIImage().toSRGB()
let (tiles, imageRows, imageCols) = tiles(from: image)
// Append the resized global image
// Note we are resampling from the original (potentially larger), not the processing size. It shouldn't make much difference.
let images =
tiles + [
image.resampled(
to: CGSize(width: fixedImageSize, height: fixedImageSize), method: .lanczos)
]
let pixelsForImages = images.map {
$0.normalized(mean: config.imageMeanTuple, std: config.imageStdTuple).asMLXArray()
}
// In transformers we have a batch dim plus the number of images per batch, and they get collapsed inside the model.
// Here we provide the compact version.
let pixels = concatenated(pixelsForImages, axis: 0).transposed(0, 2, 3, 1)
let imagePromptString = getImagePromptString(
rows: imageRows,
cols: imageCols,
seqLen: imageSequenceLength,
fakeToken: fakeImageToken,
imageToken: imageToken,
globalImageToken: globalImageToken
)
let splitPrompt = decoded.split(by: imageToken, options: .literal)
let prompt = splitPrompt.joined(separator: imagePromptString)
let finalPromptTokens = try tokenizer.encode(text: prompt)
let promptArray = MLXArray(finalPromptTokens).expandedDimensions(axis: 0)
let mask = ones(like: promptArray)
return LMInput(
text: .init(tokens: promptArray, mask: mask),
image: .init(pixels: pixels)
)
} else {
// Single video scenario
guard input.images.count == 0 else {
throw VLMError.singleMediaTypeAllowed
}
guard input.videos.count == 1 else {
throw VLMError.singleVideoAllowed
}
// Insert a default system message if the input doesn't have one
func messagesWithSystem(_ messages: [Message]) -> [Message] {
guard messages.filter { $0["role"] as? String == "system" }.isEmpty else {
return messages
}
var messagesWithSystem = messages
messagesWithSystem.insert(
[
"role": "system",
"content": [["type": "text", "text": defaultVideoSystemMessage]],
], at: 0)
return messagesWithSystem
}
// Unfortunately we don't have a "render" option in Tokenizers yet, so decoding
let finalMessages = messagesWithSystem(messages)
let promptTokens = try tokenizer.applyChatTemplate(
messages: messagesWithSystem(messages))
let decoded = try tokenizer.decode(tokens: promptTokens, skipSpecialTokens: false)
var video = try input.videos[0].asAVAsset()
let processedFrames = await try MediaProcessing.asProcessedSequence(
video,
maxFrames: maxVideoFrames,
targetFPS: { duration in
// 1 fps for duration >= 10s, apply a multiplier if smaller
max((10 - 0.9 * duration.seconds) * targetVideoFPS, 1)
}
) { frame in
let processedFrame = frame.frame
.toSRGB()
.resampled(
to: CGSize(width: fixedImageSize, height: fixedImageSize), method: .lanczos
)
.normalized(mean: config.imageMeanTuple, std: config.imageStdTuple)
return VideoFrame(frame: processedFrame, timeStamp: frame.timeStamp)
}
let thwFrames = (0 ..< processedFrames.frames.count).map {
THW($0, Int(fixedImageSize), Int(fixedImageSize))
}
let stackedFrames = concatenated(processedFrames.frames, axis: 0)
let transposedFrames = stackedFrames.transposed(0, 2, 3, 1)
let videoPromptString = getVideoPromptString(
frameCount: processedFrames.frames.count,
timeStamps: processedFrames.timestamps.map(formatTimestamp),
videoDuration: formatTimestamp(processedFrames.totalDuration),
seqLen: imageSequenceLength,
fakeToken: fakeImageToken, imageToken: imageToken,
globalImageToken: globalImageToken)
let splitPrompt = decoded.split(by: "User: ", options: .literal)
let prompt = splitPrompt[0] + "User: " + videoPromptString + splitPrompt[1]
let finalPromptTokens = try tokenizer.encode(text: prompt)
let promptArray = MLXArray(finalPromptTokens).expandedDimensions(axis: 0)
let mask = ones(like: promptArray)
return LMInput(
text: .init(tokens: promptArray, mask: mask),
image: .init(pixels: transposedFrames, frames: thwFrames)
)
}
}
}