@@ -48,10 +48,9 @@ public struct BaseProcessorConfiguration: Codable, Sendable {
4848/// Creates a function that loads a configuration file and instantiates a model with the proper configuration
4949private func create< C: Codable , M> (
5050 _ configurationType: C . Type , _ modelInit: @escaping ( C ) -> M
51- ) -> ( URL ) throws -> M {
52- { url in
53- let configuration = try JSONDecoder ( ) . decode (
54- C . self, from: Data ( contentsOf: url) )
51+ ) -> ( Data ) throws -> M {
52+ { data in
53+ let configuration = try JSONDecoder ( ) . decode ( C . self, from: data)
5554 return modelInit ( configuration)
5655 }
5756}
@@ -63,10 +62,9 @@ private func create<C: Codable, P>(
6362 C ,
6463 any Tokenizer
6564 ) -> P
66- ) -> ( URL , any Tokenizer ) throws -> P {
67- { url, tokenizer in
68- let configuration = try JSONDecoder ( ) . decode (
69- C . self, from: Data ( contentsOf: url) )
65+ ) -> ( Data , any Tokenizer ) throws -> P {
66+ { data, tokenizer in
67+ let configuration = try JSONDecoder ( ) . decode ( C . self, from: data)
7068 return processorInit ( configuration, tokenizer)
7169 }
7270}
@@ -247,15 +245,13 @@ public final class VLMModelFactory: ModelFactory {
247245 let modelDirectory = try await downloadModel (
248246 hub: hub, configuration: configuration, progressHandler: progressHandler)
249247
250- // load the generic config to understand which model and how to load the weights
251- let configurationURL = modelDirectory. appending (
252- component: " config.json "
253- )
254-
248+ // Load config.json once and decode for both base config and model-specific config
249+ let configurationURL = modelDirectory. appending ( component: " config.json " )
250+ let configData : Data
255251 let baseConfig : BaseConfiguration
256252 do {
257- baseConfig = try JSONDecoder ( ) . decode (
258- BaseConfiguration . self, from: Data ( contentsOf : configurationURL ) )
253+ configData = try Data ( contentsOf : configurationURL )
254+ baseConfig = try JSONDecoder ( ) . decode ( BaseConfiguration . self, from: configData )
259255 } catch let error as DecodingError {
260256 throw ModelFactoryError . configurationDecodingError (
261257 configurationURL. lastPathComponent, configuration. name, error)
@@ -264,39 +260,30 @@ public final class VLMModelFactory: ModelFactory {
264260 let model : LanguageModel
265261 do {
266262 model = try await typeRegistry. createModel (
267- configuration: configurationURL , modelType: baseConfig. modelType)
263+ configuration: configData , modelType: baseConfig. modelType)
268264 } catch let error as DecodingError {
269265 throw ModelFactoryError . configurationDecodingError (
270266 configurationURL. lastPathComponent, configuration. name, error)
271267 }
272268
273- // apply the weights to the bare model
269+ // Load weights, tokenizer, and processor config in parallel
270+ async let tokenizerTask = loadTokenizer ( configuration: configuration, hub: hub)
271+ async let processorConfigTask : ( Data , BaseProcessorConfiguration ) = {
272+ let url = modelDirectory. appending ( component: " preprocessor_config.json " )
273+ let data = try Data ( contentsOf: url)
274+ let config = try JSONDecoder ( ) . decode ( BaseProcessorConfiguration . self, from: data)
275+ return ( data, config)
276+ } ( )
277+
274278 try loadWeights (
275279 modelDirectory: modelDirectory, model: model,
276280 perLayerQuantization: baseConfig. perLayerQuantization)
277281
278- let tokenizer = try await loadTokenizer (
279- configuration: configuration,
280- hub: hub
281- )
282-
283- let processorConfigurationURL = modelDirectory. appending (
284- component: " preprocessor_config.json "
285- )
286-
287- let baseProcessorConfig : BaseProcessorConfiguration
288- do {
289- baseProcessorConfig = try JSONDecoder ( ) . decode (
290- BaseProcessorConfiguration . self,
291- from: Data ( contentsOf: processorConfigurationURL)
292- )
293- } catch let error as DecodingError {
294- throw ModelFactoryError . configurationDecodingError (
295- processorConfigurationURL. lastPathComponent, configuration. name, error)
296- }
282+ let tokenizer = try await tokenizerTask
283+ let ( processorConfigData, baseProcessorConfig) = try await processorConfigTask
297284
298285 let processor = try await processorRegistry. createModel (
299- configuration: processorConfigurationURL ,
286+ configuration: processorConfigData ,
300287 processorType: baseProcessorConfig. processorClass, tokenizer: tokenizer)
301288
302289 return . init(
0 commit comments