-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathLLMModelFactory.swift
More file actions
579 lines (494 loc) · 22.9 KB
/
LLMModelFactory.swift
File metadata and controls
579 lines (494 loc) · 22.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
// Copyright © 2024 Apple Inc.
import Foundation
import Hub
import MLX
import MLXLMCommon
import Tokenizers
/// Creates a function that decodes configuration data and instantiates a model with the proper configuration
private func create<C: Codable, M>(
_ configurationType: C.Type, _ modelInit: @escaping @Sendable (C) -> M
) -> @Sendable (Data) throws -> M {
{ data in
let configuration = try JSONDecoder().decode(C.self, from: data)
return modelInit(configuration)
}
}
/// Registry of model type, e.g 'llama', to functions that can instantiate the model from configuration.
///
/// Typically called via ``LLMModelFactory/load(hub:configuration:progressHandler:)``.
public enum LLMTypeRegistry {
/// Shared instance with default model types.
public static let shared: ModelTypeRegistry = .init(creators: all())
/// All predefined model types.
private static func all() -> [String: @Sendable (Data) throws -> any LanguageModel] {
var models: [String: @Sendable (Data) throws -> any LanguageModel] = [
"mistral": create(LlamaConfiguration.self, LlamaModel.init),
"llama": create(LlamaConfiguration.self, LlamaModel.init),
"phi": create(PhiConfiguration.self, PhiModel.init),
"phi3": create(Phi3Configuration.self, Phi3Model.init),
"phimoe": create(PhiMoEConfiguration.self, PhiMoEModel.init),
"gemma": create(GemmaConfiguration.self, GemmaModel.init),
"gemma2": create(Gemma2Configuration.self, Gemma2Model.init),
"gemma3": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
"gemma3_text": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
"gemma3n": create(Gemma3nTextConfiguration.self, Gemma3nTextModel.init),
"qwen2": create(Qwen2Configuration.self, Qwen2Model.init),
"qwen3": create(Qwen3Configuration.self, Qwen3Model.init),
"qwen3_moe": create(Qwen3MoEConfiguration.self, Qwen3MoEModel.init),
"qwen3_next": create(Qwen3NextConfiguration.self, Qwen3NextModel.init),
"minicpm": create(MiniCPMConfiguration.self, MiniCPMModel.init),
"starcoder2": create(Starcoder2Configuration.self, Starcoder2Model.init),
"cohere": create(CohereConfiguration.self, CohereModel.init),
"openelm": create(OpenElmConfiguration.self, OpenELMModel.init),
"internlm2": create(InternLM2Configuration.self, InternLM2Model.init),
"deepseek_v3": create(DeepseekV3Configuration.self, DeepseekV3Model.init),
"granite": create(GraniteConfiguration.self, GraniteModel.init),
"granitemoehybrid": create(
GraniteMoeHybridConfiguration.self, GraniteMoeHybridModel.init),
"mimo": create(MiMoConfiguration.self, MiMoModel.init),
"mimo_v2_flash": create(MiMoV2FlashConfiguration.self, MiMoV2FlashModel.init),
"minimax": create(MiniMaxConfiguration.self, MiniMaxModel.init),
"glm4": create(GLM4Configuration.self, GLM4Model.init),
"glm4_moe": create(GLM4MoEConfiguration.self, GLM4MoEModel.init),
"glm4_moe_lite": create(GLM4MoELiteConfiguration.self, GLM4MoELiteModel.init),
"acereason": create(Qwen2Configuration.self, Qwen2Model.init),
"falcon_h1": create(FalconH1Configuration.self, FalconH1Model.init),
"smollm3": create(SmolLM3Configuration.self, SmolLM3Model.init),
"ernie4_5": create(Ernie45Configuration.self, Ernie45Model.init),
"lfm2": create(LFM2Configuration.self, LFM2Model.init),
"baichuan_m1": create(BaichuanM1Configuration.self, BaichuanM1Model.init),
"exaone4": create(Exaone4Configuration.self, Exaone4Model.init),
"gpt_oss": create(GPTOSSConfiguration.self, GPTOSSModel.init),
"lille-130m": create(Lille130mConfiguration.self, Lille130mModel.init),
"olmoe": create(OlmoEConfiguration.self, OlmoEModel.init),
"olmo2": create(Olmo2Configuration.self, Olmo2Model.init),
"olmo3": create(Olmo3Configuration.self, Olmo3Model.init),
"bailing_moe": create(BailingMoeConfiguration.self, BailingMoeModel.init),
"lfm2_moe": create(LFM2MoEConfiguration.self, LFM2MoEModel.init),
"nanochat": create(NanoChatConfiguration.self, NanoChatModel.init),
"nemotron_h": create(NemotronHConfiguration.self, NemotronHModel.init),
"afmoe": create(AfMoEConfiguration.self, AfMoEModel.init),
"jamba_3b": create(JambaConfiguration.self, JambaModel.init),
"mistral3": create(Mistral3TextConfiguration.self, Mistral3TextModel.init),
"apertus": create(ApertusConfiguration.self, ApertusModel.init),
]
// Bitnet requires Metal custom kernels and is only available on Apple platforms
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
models["bitnet"] = create(BitnetConfiguration.self, BitnetModel.init)
#endif
return models
}
}
/// Registry of models and any overrides that go with them, e.g. prompt augmentation.
/// If asked for an unknown configuration this will use the model/tokenizer as-is.
///
/// The Python tokenizers have a very rich set of implementations and configuration. The
/// swift-tokenizers code handles a good chunk of that and this is a place to augment that
/// implementation, if needed.
public class LLMRegistry: AbstractModelRegistry, @unchecked Sendable {
/// Shared instance with default model configurations.
public static let shared = LLMRegistry(modelConfigurations: all())
static public let smolLM_135M_4bit = ModelConfiguration(
id: "mlx-community/SmolLM-135M-Instruct-4bit",
defaultPrompt: "Tell me about the history of Spain."
)
static public let mistralNeMo4bit = ModelConfiguration(
id: "mlx-community/Mistral-Nemo-Instruct-2407-4bit",
defaultPrompt: "Explain quaternions."
)
static public let mistral7B4bit = ModelConfiguration(
id: "mlx-community/Mistral-7B-Instruct-v0.3-4bit",
defaultPrompt: "Describe the Swift language."
)
static public let codeLlama13b4bit = ModelConfiguration(
id: "mlx-community/CodeLlama-13b-Instruct-hf-4bit-MLX",
overrideTokenizer: "PreTrainedTokenizer",
defaultPrompt: "func sortArray(_ array: [Int]) -> String { <FILL_ME> }"
)
static public let deepSeekR1_7B_4bit = ModelConfiguration(
id: "mlx-community/DeepSeek-R1-Distill-Qwen-7B-4bit",
defaultPrompt: "Is 9.9 greater or 9.11?"
)
static public let phi4bit = ModelConfiguration(
id: "mlx-community/phi-2-hf-4bit-mlx",
// https://www.promptingguide.ai/models/phi-2
defaultPrompt: "Why is the sky blue?"
)
static public let phi3_5_4bit = ModelConfiguration(
id: "mlx-community/Phi-3.5-mini-instruct-4bit",
defaultPrompt: "What is the gravity on Mars and the moon?",
extraEOSTokens: ["<|end|>"]
)
static public let phi3_5MoE = ModelConfiguration(
id: "mlx-community/Phi-3.5-MoE-instruct-4bit",
defaultPrompt: "What is the gravity on Mars and the moon?",
extraEOSTokens: ["<|end|>"]
) {
prompt in
"<|user|>\n\(prompt)<|end|>\n<|assistant|>\n"
}
static public let gemma2bQuantized = ModelConfiguration(
id: "mlx-community/quantized-gemma-2b-it",
overrideTokenizer: "PreTrainedTokenizer",
// https://www.promptingguide.ai/models/gemma
defaultPrompt: "what is the difference between lettuce and cabbage?"
)
static public let gemma_2_9b_it_4bit = ModelConfiguration(
id: "mlx-community/gemma-2-9b-it-4bit",
overrideTokenizer: "PreTrainedTokenizer",
// https://www.promptingguide.ai/models/gemma
defaultPrompt: "What is the difference between lettuce and cabbage?"
)
static public let gemma_2_2b_it_4bit = ModelConfiguration(
id: "mlx-community/gemma-2-2b-it-4bit",
overrideTokenizer: "PreTrainedTokenizer",
// https://www.promptingguide.ai/models/gemma
defaultPrompt: "What is the difference between lettuce and cabbage?"
)
static public let gemma3_1B_qat_4bit = ModelConfiguration(
id: "mlx-community/gemma-3-1b-it-qat-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?",
extraEOSTokens: ["<end_of_turn>"]
)
static public let gemma3n_E4B_it_lm_bf16 = ModelConfiguration(
id: "mlx-community/gemma-3n-E4B-it-lm-bf16",
defaultPrompt: "What is the difference between a fruit and a vegetable?",
// https://ai.google.dev/gemma/docs/core/prompt-structure
extraEOSTokens: ["<end_of_turn>"]
)
static public let gemma3n_E2B_it_lm_bf16 = ModelConfiguration(
id: "mlx-community/gemma-3n-E2B-it-lm-bf16",
defaultPrompt: "What is the difference between a fruit and a vegetable?",
// https://ai.google.dev/gemma/docs/core/prompt-structure
extraEOSTokens: ["<end_of_turn>"]
)
static public let gemma3n_E4B_it_lm_4bit = ModelConfiguration(
id: "mlx-community/gemma-3n-E4B-it-lm-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?",
// https://ai.google.dev/gemma/docs/core/prompt-structure
extraEOSTokens: ["<end_of_turn>"]
)
static public let gemma3n_E2B_it_lm_4bit = ModelConfiguration(
id: "mlx-community/gemma-3n-E2B-it-lm-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?",
// https://ai.google.dev/gemma/docs/core/prompt-structure
extraEOSTokens: ["<end_of_turn>"]
)
static public let qwen205b4bit = ModelConfiguration(
id: "mlx-community/Qwen1.5-0.5B-Chat-4bit",
overrideTokenizer: "PreTrainedTokenizer",
defaultPrompt: "why is the sky blue?"
)
static public let qwen2_5_7b = ModelConfiguration(
id: "mlx-community/Qwen2.5-7B-Instruct-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen2_5_1_5b = ModelConfiguration(
id: "mlx-community/Qwen2.5-1.5B-Instruct-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen3_0_6b_4bit = ModelConfiguration(
id: "mlx-community/Qwen3-0.6B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen3_1_7b_4bit = ModelConfiguration(
id: "mlx-community/Qwen3-1.7B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen3_4b_4bit = ModelConfiguration(
id: "mlx-community/Qwen3-4B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen3_8b_4bit = ModelConfiguration(
id: "mlx-community/Qwen3-8B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let qwen3MoE_30b_a3b_4bit = ModelConfiguration(
id: "mlx-community/Qwen3-30B-A3B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let openelm270m4bit = ModelConfiguration(
id: "mlx-community/OpenELM-270M-Instruct",
// https://huggingface.co/apple/OpenELM
defaultPrompt: "Once upon a time there was"
)
static public let llama3_1_8B_4bit = ModelConfiguration(
id: "mlx-community/Meta-Llama-3.1-8B-Instruct-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?"
)
static public let llama3_8B_4bit = ModelConfiguration(
id: "mlx-community/Meta-Llama-3-8B-Instruct-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?"
)
static public let llama3_2_1B_4bit = ModelConfiguration(
id: "mlx-community/Llama-3.2-1B-Instruct-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?"
)
static public let llama3_2_3B_4bit = ModelConfiguration(
id: "mlx-community/Llama-3.2-3B-Instruct-4bit",
defaultPrompt: "What is the difference between a fruit and a vegetable?"
)
static public let deepseek_r1_4bit = ModelConfiguration(
id: "mlx-community/DeepSeek-R1-4bit",
defaultPrompt: "Tell me about the history of Spain."
)
static public let granite3_3_2b_4bit = ModelConfiguration(
id: "mlx-community/granite-3.3-2b-instruct-4bit",
defaultPrompt: ""
)
static public let mimo_7b_sft_4bit = ModelConfiguration(
id: "mlx-community/MiMo-7B-SFT-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let glm4_9b_4bit = ModelConfiguration(
id: "mlx-community/GLM-4-9B-0414-4bit",
defaultPrompt: "Why is the sky blue?",
toolCallFormat: .glm4
)
static public let acereason_7b_4bit = ModelConfiguration(
id: "mlx-community/AceReason-Nemotron-7B-4bit",
defaultPrompt: ""
)
static public let bitnet_b1_58_2b_4t_4bit = ModelConfiguration(
id: "mlx-community/bitnet-b1.58-2B-4T-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let baichuan_m1_14b_instruct_4bit = ModelConfiguration(
id: "mlx-community/Baichuan-M1-14B-Instruct-4bit-ft",
defaultPrompt: "Why is the sky blue?"
)
static public let smollm3_3b_4bit = ModelConfiguration(
id: "mlx-community/SmolLM3-3B-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let ernie_45_0_3BPT_bf16_ft = ModelConfiguration(
id: "mlx-community/ERNIE-4.5-0.3B-PT-bf16-ft",
defaultPrompt: "Why is the sky blue?"
)
static public let lfm2_1_2b_4bit = ModelConfiguration(
id: "mlx-community/LFM2-1.2B-4bit",
defaultPrompt: "Why is the sky blue?",
toolCallFormat: .lfm2
)
static public let exaone_4_0_1_2b_4bit = ModelConfiguration(
id: "mlx-community/exaone-4.0-1.2b-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let lille_130m_bf16 = ModelConfiguration(
id: "mlx-community/lille-130m-instruct-bf16",
defaultPrompt: "Why is the sky blue?"
)
static public let olmoe_1b_7b_0125_instruct_4bit = ModelConfiguration(
id: "mlx-community/OLMoE-1B-7B-0125-Instruct-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let olmo_2_1124_7B_Instruct_4bit = ModelConfiguration(
id: "mlx-community/OLMo-2-1124-7B-Instruct-4bit",
defaultPrompt: "Why is the sky blue?"
)
static public let ling_mini_2_2bit = ModelConfiguration(
id: "mlx-community/Ling-mini-2.0-2bit-DWQ",
defaultPrompt: "Why is the sky blue?"
)
static public let granite_4_0_h_tiny_4bit_dwq = ModelConfiguration(
id: "mlx-community/Granite-4.0-H-Tiny-4bit-DWQ",
defaultPrompt: ""
)
static public let lfm2_8b_a1b_3bit_mlx = ModelConfiguration(
id: "mlx-community/LFM2-8B-A1B-3bit-MLX",
defaultPrompt: "",
toolCallFormat: .lfm2
)
static public let nanochat_d20_mlx = ModelConfiguration(
id: "dnakov/nanochat-d20-mlx",
defaultPrompt: ""
)
static public let gpt_oss_20b_MXFP4_Q8 = ModelConfiguration(
id: "mlx-community/gpt-oss-20b-MXFP4-Q8",
defaultPrompt: "Why is the sky blue?"
)
static public let jamba_3b = ModelConfiguration(
id: "mlx-community/AI21-Jamba-Reasoning-3B-bf16",
defaultPrompt: ""
)
private static func all() -> [ModelConfiguration] {
[
codeLlama13b4bit,
deepSeekR1_7B_4bit,
gemma2bQuantized,
gemma_2_2b_it_4bit,
gemma_2_9b_it_4bit,
gemma3_1B_qat_4bit,
gemma3n_E4B_it_lm_bf16,
gemma3n_E2B_it_lm_bf16,
gemma3n_E4B_it_lm_4bit,
gemma3n_E2B_it_lm_4bit,
granite3_3_2b_4bit,
granite_4_0_h_tiny_4bit_dwq,
llama3_1_8B_4bit,
llama3_2_1B_4bit,
llama3_2_3B_4bit,
llama3_8B_4bit,
mistral7B4bit,
mistralNeMo4bit,
openelm270m4bit,
phi3_5MoE,
phi3_5_4bit,
phi4bit,
qwen205b4bit,
qwen2_5_7b,
qwen2_5_1_5b,
qwen3_0_6b_4bit,
qwen3_1_7b_4bit,
qwen3_4b_4bit,
qwen3_8b_4bit,
qwen3MoE_30b_a3b_4bit,
smolLM_135M_4bit,
deepseek_r1_4bit,
mimo_7b_sft_4bit,
glm4_9b_4bit,
acereason_7b_4bit,
bitnet_b1_58_2b_4t_4bit,
smollm3_3b_4bit,
ernie_45_0_3BPT_bf16_ft,
lfm2_1_2b_4bit,
baichuan_m1_14b_instruct_4bit,
exaone_4_0_1_2b_4bit,
lille_130m_bf16,
olmoe_1b_7b_0125_instruct_4bit,
olmo_2_1124_7B_Instruct_4bit,
ling_mini_2_2bit,
lfm2_8b_a1b_3bit_mlx,
nanochat_d20_mlx,
gpt_oss_20b_MXFP4_Q8,
jamba_3b,
]
}
}
@available(*, deprecated, renamed: "LLMRegistry", message: "Please use LLMRegistry directly.")
public typealias ModelRegistry = LLMRegistry
private struct LLMUserInputProcessor: UserInputProcessor {
let tokenizer: Tokenizer
let configuration: ModelConfiguration
let messageGenerator: MessageGenerator
internal init(
tokenizer: any Tokenizer, configuration: ModelConfiguration,
messageGenerator: MessageGenerator
) {
self.tokenizer = tokenizer
self.configuration = configuration
self.messageGenerator = messageGenerator
}
func prepare(input: UserInput) throws -> LMInput {
let messages = messageGenerator.generate(from: input)
do {
let promptTokens = try tokenizer.applyChatTemplate(
messages: messages, tools: input.tools, additionalContext: input.additionalContext)
return LMInput(tokens: MLXArray(promptTokens))
} catch TokenizerError.missingChatTemplate {
print(
"No chat template was included or provided, so converting messages to simple text format. This is not optimal for model performance, so applications should provide a chat template if none is included with the model."
)
let prompt =
messages
.compactMap { $0["content"] as? String }
.joined(separator: "\n\n")
let promptTokens = tokenizer.encode(text: prompt)
return LMInput(tokens: MLXArray(promptTokens))
}
}
}
/// Factory for creating new LLMs.
///
/// Callers can use the `shared` instance or create a new instance if custom configuration
/// is required.
///
/// ```swift
/// let modelContainer = try await LLMModelFactory.shared.loadContainer(
/// configuration: LLMRegistry.llama3_8B_4bit)
/// ```
public final class LLMModelFactory: ModelFactory {
public init(typeRegistry: ModelTypeRegistry, modelRegistry: AbstractModelRegistry) {
self.typeRegistry = typeRegistry
self.modelRegistry = modelRegistry
}
/// Shared instance with default behavior.
public static let shared = LLMModelFactory(
typeRegistry: LLMTypeRegistry.shared, modelRegistry: LLMRegistry.shared)
/// registry of model type, e.g. configuration value `llama` -> configuration and init methods
public let typeRegistry: ModelTypeRegistry
/// registry of model id to configuration, e.g. `mlx-community/Llama-3.2-3B-Instruct-4bit`
public let modelRegistry: AbstractModelRegistry
public func _load(
hub: HubApi, configuration: ModelConfiguration,
progressHandler: @Sendable @escaping (Progress) -> Void
) async throws -> ModelContext {
// download weights and config
let modelDirectory = try await downloadModel(
hub: hub, configuration: configuration, progressHandler: progressHandler)
// Load config.json once and decode for both base config and model-specific config
let configurationURL = modelDirectory.appending(component: "config.json")
let configData: Data
do {
configData = try Data(contentsOf: configurationURL)
} catch {
throw ModelFactoryError.configurationFileError(
configurationURL.lastPathComponent, configuration.name, error)
}
let baseConfig: BaseConfiguration
do {
baseConfig = try JSONDecoder().decode(BaseConfiguration.self, from: configData)
} catch let error as DecodingError {
throw ModelFactoryError.configurationDecodingError(
configurationURL.lastPathComponent, configuration.name, error)
}
let model: LanguageModel
do {
model = try await typeRegistry.createModel(
configuration: configData, modelType: baseConfig.modelType)
} catch let error as DecodingError {
throw ModelFactoryError.configurationDecodingError(
configurationURL.lastPathComponent, configuration.name, error)
}
// Load EOS token IDs from config.json, with optional override from generation_config.json
var eosTokenIds = Set(baseConfig.eosTokenIds?.values ?? [])
let generationConfigURL = modelDirectory.appending(component: "generation_config.json")
if let generationData = try? Data(contentsOf: generationConfigURL),
let generationConfig = try? JSONDecoder().decode(
GenerationConfigFile.self, from: generationData),
let genEosIds = generationConfig.eosTokenIds?.values
{
eosTokenIds = Set(genEosIds) // Override per Python mlx-lm behavior
}
// Create mutable configuration with loaded EOS token IDs
var mutableConfiguration = configuration
mutableConfiguration.eosTokenIds = eosTokenIds
// Auto-detect tool call format from model type if not explicitly set
if mutableConfiguration.toolCallFormat == nil {
mutableConfiguration.toolCallFormat = ToolCallFormat.infer(from: baseConfig.modelType)
}
// Load tokenizer and weights in parallel using async let.
async let tokenizerTask = loadTokenizer(configuration: configuration, hub: hub)
try loadWeights(
modelDirectory: modelDirectory, model: model,
perLayerQuantization: baseConfig.perLayerQuantization)
let tokenizer = try await tokenizerTask
let messageGenerator =
if let model = model as? LLMModel {
model.messageGenerator(tokenizer: tokenizer)
} else {
DefaultMessageGenerator()
}
let processor = LLMUserInputProcessor(
tokenizer: tokenizer, configuration: mutableConfiguration,
messageGenerator: messageGenerator)
return .init(
configuration: mutableConfiguration, model: model, processor: processor,
tokenizer: tokenizer)
}
}
public class TrampolineModelFactory: NSObject, ModelFactoryTrampoline {
public static func modelFactory() -> (any MLXLMCommon.ModelFactory)? {
LLMModelFactory.shared
}
}