-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathIntegrationTestHelpers.swift
More file actions
614 lines (538 loc) · 21.9 KB
/
IntegrationTestHelpers.swift
File metadata and controls
614 lines (538 loc) · 21.9 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Shared integration test logic for verifying end-to-end model loading and generation.
// Integration packages inject their own Downloader and TokenizerLoader, then call
// these functions which run the test and throw on failure.
import CoreImage
import Foundation
import MLX
import MLXEmbedders
import MLXLLM
import MLXLMCommon
import MLXVLM
// Both MLXLMCommon and MLXEmbedders define ModelContainer.
public typealias LMModelContainer = MLXLMCommon.ModelContainer
public typealias EmbeddingModelContainer = MLXEmbedders.ModelContainer
// MARK: - Error
public struct IntegrationTestFailure: LocalizedError {
public let errorDescription: String?
public init(_ message: String) {
self.errorDescription = message
}
}
private func check(_ condition: Bool, _ message: String) throws {
guard condition else { throw IntegrationTestFailure(message) }
}
// MARK: - Model IDs
public enum IntegrationTestModelIDs {
public static let llm = "mlx-community/Qwen3-4B-Instruct-2507-4bit"
public static let vlm = "mlx-community/Qwen3-VL-4B-Instruct-4bit"
public static let lfm2 = "mlx-community/LFM2-2.6B-Exp-4bit"
public static let glm4 = "mlx-community/GLM-4-9B-0414-4bit"
}
// MARK: - Model Loading
/// Shared model cache that loads each model at most once per test run.
public actor IntegrationTestModels {
private let downloader: any Downloader
private let tokenizerLoader: any TokenizerLoader
private var llmTask: Task<LMModelContainer, Error>?
private var vlmTask: Task<LMModelContainer, Error>?
private var lfm2Task: Task<LMModelContainer, Error>?
private var glm4Task: Task<LMModelContainer, Error>?
public init(downloader: any Downloader, tokenizerLoader: any TokenizerLoader) {
self.downloader = downloader
self.tokenizerLoader = tokenizerLoader
}
public func llmContainer() async throws -> LMModelContainer {
if let task = llmTask {
return try await task.value
}
let downloader = self.downloader
let tokenizerLoader = self.tokenizerLoader
let id = IntegrationTestModelIDs.llm
let task = Task {
print("Loading LLM: \(id)")
let container = try await LLMModelFactory.shared.loadContainer(
from: downloader, using: tokenizerLoader,
configuration: .init(id: id),
progressHandler: logProgress(id)
)
print("Loaded LLM: \(id)")
return container
}
llmTask = task
return try await task.value
}
public func vlmContainer() async throws -> LMModelContainer {
if let task = vlmTask {
return try await task.value
}
let downloader = self.downloader
let tokenizerLoader = self.tokenizerLoader
let id = IntegrationTestModelIDs.vlm
let task = Task {
print("Loading VLM: \(id)")
let container = try await VLMModelFactory.shared.loadContainer(
from: downloader, using: tokenizerLoader,
configuration: .init(id: id),
progressHandler: logProgress(id)
)
print("Loaded VLM: \(id)")
return container
}
vlmTask = task
return try await task.value
}
public func lfm2Container() async throws -> LMModelContainer {
if let task = lfm2Task {
return try await task.value
}
let downloader = self.downloader
let tokenizerLoader = self.tokenizerLoader
let id = IntegrationTestModelIDs.lfm2
let task = Task {
print("Loading LFM2: \(id)")
let container = try await LLMModelFactory.shared.loadContainer(
from: downloader, using: tokenizerLoader,
configuration: .init(id: id),
progressHandler: logProgress(id)
)
print("Loaded LFM2: \(id)")
return container
}
lfm2Task = task
return try await task.value
}
public func glm4Container() async throws -> LMModelContainer {
if let task = glm4Task {
return try await task.value
}
let downloader = self.downloader
let tokenizerLoader = self.tokenizerLoader
let id = IntegrationTestModelIDs.glm4
let task = Task {
print("Loading GLM4: \(id)")
let container = try await LLMModelFactory.shared.loadContainer(
from: downloader, using: tokenizerLoader,
configuration: .init(id: id),
progressHandler: logProgress(id)
)
print("Loaded GLM4: \(id)")
return container
}
glm4Task = task
return try await task.value
}
public func embeddingContainer() async throws -> EmbeddingModelContainer {
let downloader = self.downloader
let tokenizerLoader = self.tokenizerLoader
let id = "nomic_text_v1_5"
print("Loading embedding model: \(id)")
let container = try await MLXEmbedders.loadModelContainer(
from: downloader, using: tokenizerLoader, configuration: .nomic_text_v1_5,
progressHandler: logProgress(id)
)
print("Loaded embedding model: \(id)")
return container
}
}
// MARK: - ChatSession Tests
private let generateParameters = GenerateParameters(maxTokens: 200, temperature: 0)
public enum ChatSessionTests {
public static func oneShot(container: LMModelContainer) async throws {
let session = ChatSession(container, generateParameters: generateParameters)
let result = try await streamAndCollect(
session.streamResponse(
to: "What is 2+2? Reply with just the number."), label: "One-shot")
try check(
result.contains("4") || result.lowercased().contains("four"),
"Expected '4' or 'four' in response, got: \(result)"
)
}
public static func oneShotStream(container: LMModelContainer) async throws {
let session = ChatSession(container, generateParameters: generateParameters)
let result = try await streamAndCollect(
session.streamResponse(
to: "What is 2+2? Reply with just the number."), label: "Stream")
try check(
result.contains("4") || result.lowercased().contains("four"),
"Expected '4' or 'four' in streamed response, got: \(result)"
)
}
public static func multiTurnConversation(container: LMModelContainer) async throws {
let session = ChatSession(
container, instructions: "You are a helpful assistant. Keep responses brief.",
generateParameters: generateParameters)
_ = try await streamAndCollect(
session.streamResponse(
to: "My name is Alice."), label: "Turn 1")
let response2 = try await streamAndCollect(
session.streamResponse(
to: "What is my name?"), label: "Turn 2")
try check(
response2.lowercased().contains("alice"),
"Expected 'Alice' in response, got: \(response2)"
)
}
public static func visionModel(container: LMModelContainer) async throws {
let session = ChatSession(container, generateParameters: generateParameters)
let redImage = CIImage(color: .red).cropped(
to: CGRect(x: 0, y: 0, width: 100, height: 100))
let result = try await streamAndCollect(
session.streamResponse(
to: "What color is this image? Reply with just the color name.",
image: .ciImage(redImage)), label: "Vision")
try check(
result.lowercased().contains("red"),
"Expected 'red' in response, got: \(result)"
)
}
public static func streamDetailsWithTools(container: LMModelContainer) async throws {
let tools: [ToolSpec] = [weatherToolSchema]
let session = ChatSession(container, generateParameters: generateParameters, tools: tools)
var responseText = ""
var toolCalls: [ToolCall] = []
var info: GenerateCompletionInfo?
print("Tools: ", terminator: "")
for try await generation in session.streamDetails(
to: "What is the weather in San Francisco?", images: [], videos: [])
{
switch generation {
case .chunk(let text):
print(text, terminator: "")
responseText += text
case .toolCall(let toolCall):
toolCalls.append(toolCall)
case .info(let completionInfo):
info = completionInfo
}
}
print()
if let info {
print(
"Generation info: \(info.generationTokenCount) tokens, stop reason: \(info.stopReason)"
)
}
if !toolCalls.isEmpty {
print("Tool calls: \(toolCalls)")
}
try check(
!responseText.isEmpty || !toolCalls.isEmpty,
"Expected either text or tool calls, got neither (generated \(info?.generationTokenCount ?? 0) tokens, stop reason: \(String(describing: info?.stopReason)))"
)
// If we got tool calls, feed back a tool result and verify the model responds
if !toolCalls.isEmpty {
let followUp = try await streamAndCollect(
session.streamResponse(
to: "Foggy with a high in the low 60s, clearing later in the day",
role: .tool, images: [], videos: []),
label: "Tool result")
try check(
!followUp.isEmpty,
"Expected a response after providing tool result, got empty string"
)
}
}
public static func toolInvocation(container: LMModelContainer) async throws {
struct EmptyInput: Codable {}
struct TimeOutput: Codable {
let time: String
}
let timeTool = Tool<EmptyInput, TimeOutput>(
name: "get_time",
description: "Get the current date and time including day of week.",
parameters: []
) { _ in
TimeOutput(time: "Wed Feb 18 17:50:43 PST 2026")
}
let session = ChatSession(
container, generateParameters: generateParameters,
tools: [timeTool.schema]
) { toolCall in
if toolCall.function.name == timeTool.name {
return try await toolCall.execute(with: timeTool).toolResult
}
return "Unknown tool: \(toolCall.function.name)"
}
let result = try await streamAndCollect(
session.streamResponse(
to: "What day of week is it?"), label: "Tool invocation")
try check(
result.lowercased().contains("wed") || result.lowercased().contains("wednesday"),
"Expected 'Wed' or 'Wednesday' in response, got: \(result)"
)
}
public static func promptRehydration(container: LMModelContainer) async throws {
let history: [Chat.Message] = [
.system("You are a helpful assistant."),
.user("My name is Bob."),
.assistant("Hello Bob! How can I help you today?"),
]
let session = ChatSession(
container, history: history, generateParameters: generateParameters)
let response = try await streamAndCollect(
session.streamResponse(
to: "What is my name?"), label: "Rehydration")
try check(
response.lowercased().contains("bob"),
"Expected 'Bob' in response (prompt rehydration), got: \(response)"
)
}
}
// MARK: - Stream Helper
private func streamAndCollect(
_ stream: AsyncThrowingStream<String, Error>,
label: String
) async throws -> String {
var result = ""
print("\(label): ", terminator: "")
for try await token in stream {
print(token, terminator: "")
result += token
}
print()
return result
}
// MARK: - Embedder Tests
public enum EmbedderTests {
public static func gemma3Embedder(
downloader: any Downloader, tokenizerLoader: any TokenizerLoader
) async throws {
let modelId = "mlx-community/gemma-3-1b-it-qat-4bit"
print("Loading Gemma 3 embedding model: \(modelId)")
let modelContainer = try await MLXEmbedders.loadModelContainer(
from: downloader, using: tokenizerLoader, configuration: .init(id: modelId),
progressHandler: logProgress(modelId)
)
print("Loaded Gemma 3 embedding model: \(modelId)")
let inputs = [
"The Coca-Cola Company is a soft drink company based in Atlanta, Georgia, USA.",
"In the United States, PepsiCo Inc. is a leading soft drink company.",
]
let resultEmbeddings = await modelContainer.perform {
(model: EmbeddingModel, tokenizer: Tokenizer, pooling: Pooling) -> [[Float]] in
let encoded = inputs.map {
tokenizer.encode(text: $0, addSpecialTokens: true)
}
let maxLength = encoded.reduce(into: 1) { acc, elem in
acc = max(acc, elem.count)
}
let padded = stacked(
encoded.map { elem in
MLXArray(
elem
+ Array(
repeating: tokenizer.eosTokenId ?? 0,
count: maxLength - elem.count))
})
let mask = (padded .!= (tokenizer.eosTokenId ?? 0))
let tokenTypes = MLXArray.zeros(like: padded)
let modelOutput = model(
padded, positionIds: nil, tokenTypeIds: tokenTypes, attentionMask: mask)
let result = pooling(
modelOutput,
normalize: true, applyLayerNorm: true
)
result.eval()
return result.map { $0.asArray(Float.self) }
}
try check(
resultEmbeddings.count == inputs.count,
"Should have one embedding per input, got \(resultEmbeddings.count)"
)
for embedding in resultEmbeddings {
try check(
embedding.count == 1152,
"Gemma 3 1B embedding size should be 1152, got \(embedding.count)"
)
let l2Norm = sqrt(embedding.map { $0 * $0 }.reduce(0, +))
try check(
abs(l2Norm - 1.0) < 0.05,
"Embeddings should be approximately L2-normalized, got L2 norm \(l2Norm)"
)
}
let similarity = zip(resultEmbeddings[0], resultEmbeddings[1]).map(*).reduce(0, +)
try check(
similarity > 0.0,
"Similarity between related sentences should be positive, got \(similarity)"
)
}
public static func readmeExample(container: EmbeddingModelContainer) async throws {
let searchInputs = [
"search_query: Animals in Tropical Climates.",
"search_document: Elephants",
"search_document: Horses",
"search_document: Polar Bears",
]
let resultEmbeddings = await container.perform {
(model: EmbeddingModel, tokenizer: Tokenizer, pooling: Pooling) -> [[Float]] in
let inputs = searchInputs.map {
tokenizer.encode(text: $0, addSpecialTokens: true)
}
let maxLength = inputs.reduce(into: 16) { acc, elem in
acc = max(acc, elem.count)
}
let padded = stacked(
inputs.map { elem in
MLXArray(
elem
+ Array(
repeating: tokenizer.eosTokenId ?? 0,
count: maxLength - elem.count))
})
let mask = (padded .!= tokenizer.eosTokenId ?? 0)
let tokenTypes = MLXArray.zeros(like: padded)
let result = pooling(
model(padded, positionIds: nil, tokenTypeIds: tokenTypes, attentionMask: mask),
normalize: true, applyLayerNorm: true
)
result.eval()
return result.map { $0.asArray(Float.self) }
}
let searchQueryEmbedding = resultEmbeddings[0]
let documentEmbeddings = resultEmbeddings[1...]
let similarities = documentEmbeddings.map { docEmbedding in
zip(searchQueryEmbedding, docEmbedding).map(*).reduce(0, +)
}
let documentNames = searchInputs[1...].map {
$0.replacingOccurrences(of: "search_document: ", with: "")
}
let expectedSimilarities: [Float] = [0.6854175, 0.6644787, 0.63326025]
let tolerance: Float = 1e-4
for (index, resultSimilarity) in similarities.enumerated() {
try check(
abs(resultSimilarity - expectedSimilarities[index]) < tolerance,
"Similarity mismatch for \(documentNames[index]): expected \(expectedSimilarities[index]), got \(resultSimilarity)"
)
}
}
}
// MARK: - Tool Call Tests
public enum ToolCallTests {
public static func lfm2FormatAutoDetection(container: LMModelContainer) async throws {
let config = await container.configuration
try check(
config.toolCallFormat == ToolCallFormat.lfm2,
"Expected .lfm2 tool call format, got: \(String(describing: config.toolCallFormat))"
)
}
public static func lfm2EndToEndGeneration(container: LMModelContainer) async throws {
let (result, toolCalls) = try await generateWithTools(
container: container,
userMessage: "What's the weather in Tokyo?")
print("LFM2 Output:", result)
print("LFM2 Tool Calls:", toolCalls)
if !toolCalls.isEmpty {
let toolCall = toolCalls[0]
try check(
toolCall.function.name == "get_weather",
"Expected tool name 'get_weather', got: \(toolCall.function.name)"
)
if case .string(let location) = toolCall.function.arguments["location"] {
try check(
location.lowercased().contains("tokyo"),
"Expected location containing 'Tokyo', got: \(location)"
)
}
}
}
public static func glm4FormatAutoDetection(container: LMModelContainer) async throws {
let config = await container.configuration
try check(
config.toolCallFormat == ToolCallFormat.glm4,
"Expected .glm4 tool call format, got: \(String(describing: config.toolCallFormat))"
)
}
public static func glm4EndToEndGeneration(container: LMModelContainer) async throws {
let (result, toolCalls) = try await generateWithTools(
container: container,
userMessage: "What's the weather in Paris?")
print("GLM4 Output:", result)
print("GLM4 Tool Calls:", toolCalls)
if !toolCalls.isEmpty {
let toolCall = toolCalls[0]
try check(
toolCall.function.name == "get_weather",
"Expected tool name 'get_weather', got: \(toolCall.function.name)"
)
if case .string(let location) = toolCall.function.arguments["location"] {
try check(
location.lowercased().contains("paris"),
"Expected location containing 'Paris', got: \(location)"
)
}
}
}
private static func generateWithTools(
container: LMModelContainer,
userMessage: String
) async throws -> (text: String, toolCalls: [ToolCall]) {
try await container.perform { context in
let input = UserInput(
chat: [
.system(
"You are a helpful assistant with access to tools. When asked about weather, use the get_weather function."
),
.user(userMessage),
],
tools: [weatherToolSchema]
)
let lmInput = try await context.processor.prepare(input: input)
let stream = try generate(
input: lmInput,
parameters: GenerateParameters(maxTokens: 100),
context: context
)
var text = ""
var toolCalls: [ToolCall] = []
for try await generation in stream {
switch generation {
case .chunk(let chunk):
text += chunk
case .toolCall(let toolCall):
toolCalls.append(toolCall)
case .info:
break
}
}
return (text, toolCalls)
}
}
}
// MARK: - Progress Logging
private func logProgress(_ label: String) -> @Sendable (Progress) -> Void {
let lock = NSLock()
nonisolated(unsafe) var lastThreshold = -1
return { progress in
let pct = Int(progress.fractionCompleted * 100)
let threshold = pct / 5
lock.lock()
let shouldPrint = threshold > lastThreshold
if shouldPrint { lastThreshold = threshold }
lock.unlock()
if shouldPrint {
print(" \(label): \(pct)%")
}
}
}
// MARK: - Shared Constants
private let weatherToolSchema: ToolSpec = [
"type": "function",
"function": [
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": [
"type": "object",
"properties": [
"location": [
"type": "string",
"description": "The city name, e.g. San Francisco",
] as [String: any Sendable],
"unit": [
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit",
] as [String: any Sendable],
] as [String: any Sendable],
"required": ["location"],
] as [String: any Sendable],
] as [String: any Sendable],
]