@@ -20,20 +20,10 @@ private func create<C: Codable, M>(
20
20
/// Registry of model type, e.g 'llama', to functions that can instantiate the model from configuration.
21
21
///
22
22
/// Typically called via ``LLMModelFactory/load(hub:configuration:progressHandler:)``.
23
- public class ModelTypeRegistry : @unchecked Sendable {
24
-
25
- /// Creates an empty registry.
26
- public init ( ) {
27
- self . creators = [ : ]
28
- }
29
-
30
- /// Creates a registry with given creators.
31
- public init ( creators: [ String : @Sendable ( URL) throws -> any LanguageModel ] ) {
32
- self . creators = creators
33
- }
23
+ public class LLMTypeRegistry : ModelTypeRegistry , @unchecked Sendable {
34
24
35
25
/// Shared instance with default model types.
36
- public static let shared : ModelTypeRegistry = . init( creators: all ( ) )
26
+ public static let shared : LLMTypeRegistry = . init( creators: all ( ) )
37
27
38
28
/// All predefined model types.
39
29
private static func all( ) -> [ String : @Sendable ( URL) throws -> any LanguageModel ] {
@@ -53,32 +43,6 @@ public class ModelTypeRegistry: @unchecked Sendable {
53
43
]
54
44
}
55
45
56
- // Note: using NSLock as we have very small (just dictionary get/set)
57
- // critical sections and expect no contention. this allows the methods
58
- // to remain synchronous.
59
- private let lock = NSLock ( )
60
- private var creators : [ String : @Sendable ( URL) throws -> any LanguageModel ]
61
-
62
- /// Add a new model to the type registry.
63
- public func registerModelType(
64
- _ type: String , creator: @Sendable @escaping ( URL) throws -> any LanguageModel
65
- ) {
66
- lock. withLock {
67
- creators [ type] = creator
68
- }
69
- }
70
-
71
- /// Given a `modelType` and configuration file instantiate a new `LanguageModel`.
72
- public func createModel( configuration: URL , modelType: String ) throws -> LanguageModel {
73
- let creator = lock. withLock {
74
- creators [ modelType]
75
- }
76
- guard let creator else {
77
- throw ModelFactoryError . unsupportedModelType ( modelType)
78
- }
79
- return try creator ( configuration)
80
- }
81
-
82
46
}
83
47
84
48
/// Registry of models and any overrides that go with them, e.g. prompt augmentation.
@@ -87,23 +51,10 @@ public class ModelTypeRegistry: @unchecked Sendable {
87
51
/// The python tokenizers have a very rich set of implementations and configuration. The
88
52
/// swift-tokenizers code handles a good chunk of that and this is a place to augment that
89
53
/// implementation, if needed.
90
- public class ModelRegistry : @unchecked Sendable {
91
-
92
- /// Creates an empty registry.
93
- public init ( ) {
94
- self . registry = Dictionary ( )
95
- }
96
-
97
- /// Creates a new registry with from given model configurations.
98
- public init ( modelConfigurations: [ ModelConfiguration ] ) {
99
- self . registry = Dictionary ( uniqueKeysWithValues: modelConfigurations. map { ( $0. name, $0) } )
100
- }
54
+ public class LLMRegistry : AbstractModelRegistry , @unchecked Sendable {
101
55
102
56
/// Shared instance with default model configurations.
103
- public static let shared = ModelRegistry ( modelConfigurations: all ( ) )
104
-
105
- private let lock = NSLock ( )
106
- private var registry : [ String : ModelConfiguration ]
57
+ public static let shared = LLMRegistry ( modelConfigurations: all ( ) )
107
58
108
59
static public let smolLM_135M_4bit = ModelConfiguration (
109
60
id: " mlx-community/SmolLM-135M-Instruct-4bit " ,
@@ -239,31 +190,11 @@ public class ModelRegistry: @unchecked Sendable {
239
190
]
240
191
}
241
192
242
- public func register( configurations: [ ModelConfiguration ] ) {
243
- lock. withLock {
244
- for c in configurations {
245
- registry [ c. name] = c
246
- }
247
- }
248
- }
249
-
250
- public func configuration( id: String ) -> ModelConfiguration {
251
- lock. withLock {
252
- if let c = registry [ id] {
253
- return c
254
- } else {
255
- return ModelConfiguration ( id: id)
256
- }
257
- }
258
- }
259
-
260
- public var models : some Collection < ModelConfiguration > & Sendable {
261
- lock. withLock {
262
- return registry. values
263
- }
264
- }
265
193
}
266
194
195
+ @available ( * , deprecated, renamed: " LLMRegistry " , message: " Please use LLMRegistry directly. " )
196
+ public typealias ModelRegistry = LLMRegistry
197
+
267
198
private struct LLMUserInputProcessor : UserInputProcessor {
268
199
269
200
let tokenizer : Tokenizer
@@ -304,19 +235,20 @@ private struct LLMUserInputProcessor: UserInputProcessor {
304
235
/// ```
305
236
public class LLMModelFactory : ModelFactory {
306
237
307
- public init ( typeRegistry: ModelTypeRegistry , modelRegistry: ModelRegistry ) {
238
+ public init ( typeRegistry: ModelTypeRegistry , modelRegistry: AbstractModelRegistry ) {
308
239
self . typeRegistry = typeRegistry
309
240
self . modelRegistry = modelRegistry
310
241
}
311
242
312
243
/// Shared instance with default behavior.
313
- public static let shared = LLMModelFactory ( typeRegistry: . shared, modelRegistry: . shared)
244
+ public static let shared = LLMModelFactory (
245
+ typeRegistry: LLMTypeRegistry . shared, modelRegistry: LLMRegistry . shared)
314
246
315
247
/// registry of model type, e.g. configuration value `llama` -> configuration and init methods
316
248
public let typeRegistry : ModelTypeRegistry
317
249
318
250
/// registry of model id to configuration, e.g. `mlx-community/Llama-3.2-3B-Instruct-4bit`
319
- public let modelRegistry : ModelRegistry
251
+ public let modelRegistry : AbstractModelRegistry
320
252
321
253
public func configuration( id: String ) -> ModelConfiguration {
322
254
modelRegistry. configuration ( id: id)
0 commit comments