-
Notifications
You must be signed in to change notification settings - Fork 119
/
LLMModelFactory.swift
291 lines (247 loc) · 10.7 KB
/
LLMModelFactory.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
// Copyright © 2024 Apple Inc.
import Foundation
import Hub
import MLX
import MLXLMCommon
import Tokenizers
/// Creates a function that loads a configuration file and instantiates a model with the proper configuration
private func create<C: Codable, M>(
_ configurationType: C.Type, _ modelInit: @escaping (C) -> M
) -> (URL) throws -> M {
{ url in
let configuration = try JSONDecoder().decode(
C.self, from: Data(contentsOf: url))
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 class ModelTypeRegistry: @unchecked Sendable {
// Note: using NSLock as we have very small (just dictionary get/set)
// critical sections and expect no contention. this allows the methods
// to remain synchronous.
private let lock = NSLock()
private var creators: [String: @Sendable (URL) 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),
"qwen2": create(Qwen2Configuration.self, Qwen2Model.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),
]
/// Add a new model to the type registry.
public func registerModelType(
_ type: String, creator: @Sendable @escaping (URL) throws -> any LanguageModel
) {
lock.withLock {
creators[type] = creator
}
}
/// Given a `modelType` and configuration file instantiate a new `LanguageModel`.
public func createModel(configuration: URL, modelType: String) throws -> LanguageModel {
let creator = lock.withLock {
creators[modelType]
}
guard let creator else {
throw ModelFactoryError.unsupportedModelType(modelType)
}
return try creator(configuration)
}
}
/// 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 ModelRegistry: @unchecked Sendable {
private let lock = NSLock()
private var registry = Dictionary(uniqueKeysWithValues: all().map { ($0.name, $0) })
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 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 qwen205b4bit = ModelConfiguration(
id: "mlx-community/Qwen1.5-0.5B-Chat-4bit",
overrideTokenizer: "PreTrainedTokenizer",
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?"
)
private static func all() -> [ModelConfiguration] {
[
codeLlama13b4bit,
gemma2bQuantized,
gemma_2_2b_it_4bit,
gemma_2_9b_it_4bit,
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,
smolLM_135M_4bit,
]
}
public func register(configurations: [ModelConfiguration]) {
lock.withLock {
for c in configurations {
registry[c.name] = c
}
}
}
public func configuration(id: String) -> ModelConfiguration {
lock.withLock {
if let c = registry[id] {
return c
} else {
return ModelConfiguration(id: id)
}
}
}
}
private struct LLMUserInputProcessor: UserInputProcessor {
let tokenizer: Tokenizer
let configuration: ModelConfiguration
internal init(tokenizer: any Tokenizer, configuration: ModelConfiguration) {
self.tokenizer = tokenizer
self.configuration = configuration
}
func prepare(input: UserInput) throws -> LMInput {
do {
let messages = input.prompt.asMessages()
let promptTokens = try tokenizer.applyChatTemplate(messages: messages)
return LMInput(tokens: MLXArray(promptTokens))
} catch {
// #150 -- it might be a TokenizerError.chatTemplate("No chat template was specified")
// but that is not public so just fall back to text
let prompt = input.prompt
.asMessages()
.compactMap { $0["content"] }
.joined(separator: ". ")
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: ModelRegistry.llama3_8B_4bit)
/// ```
public class LLMModelFactory: ModelFactory {
public static let shared = LLMModelFactory()
/// 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 = ModelRegistry()
public func configuration(id: String) -> ModelConfiguration {
modelRegistry.configuration(id: id)
}
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 the generic config to unerstand which model and how to load the weights
let configurationURL = modelDirectory.appending(component: "config.json")
let baseConfig = try JSONDecoder().decode(
BaseConfiguration.self, from: Data(contentsOf: configurationURL))
let model = try typeRegistry.createModel(
configuration: configurationURL, modelType: baseConfig.modelType)
// apply the weights to the bare model
try loadWeights(
modelDirectory: modelDirectory, model: model, quantization: baseConfig.quantization)
let tokenizer = try await loadTokenizer(configuration: configuration, hub: hub)
return .init(
configuration: configuration, model: model,
processor: LLMUserInputProcessor(tokenizer: tokenizer, configuration: configuration),
tokenizer: tokenizer)
}
}