@@ -280,19 +280,33 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
280280 id <MTLLibrary > metal_library;
281281
282282 // load library
283+ //
284+ // - first check if the library is embedded
285+ // - then check if the library is in the bundle
286+ // - if not found, load the source and compile it
287+ // - if that fails, return NULL
283288 {
284289 NSBundle * bundle = nil ;
285290#ifdef SWIFT_PACKAGE
286291 bundle = SWIFTPM_MODULE_BUNDLE;
287292#else
288293 bundle = [NSBundle bundleForClass: [GGMLMetalClass class ]];
289294#endif
295+
290296 NSError * error = nil ;
291- NSString * libPath = [bundle pathForResource: @" default" ofType: @" metallib" ];
292- if (libPath != nil ) {
297+
298+ #if GGML_METAL_EMBED_LIBRARY
299+ const bool try_metallib = false ;
300+ #else
301+ const bool try_metallib = true ;
302+ #endif
303+
304+ NSString * path_lib = [bundle pathForResource: @" default" ofType: @" metallib" ];
305+ if (try_metallib && path_lib != nil ) {
293306 // pre-compiled library found
294- NSURL * libURL = [NSURL fileURLWithPath: libPath];
295- GGML_METAL_LOG_INFO (" %s : loading '%s '\n " , __func__, [libPath UTF8String ]);
307+ NSURL * libURL = [NSURL fileURLWithPath: path_lib];
308+ GGML_METAL_LOG_INFO (" %s : loading '%s '\n " , __func__, [path_lib UTF8String ]);
309+
296310 metal_library = [ctx->device newLibraryWithURL: libURL error: &error];
297311 if (error) {
298312 GGML_METAL_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
@@ -305,31 +319,34 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
305319 extern const char ggml_metallib_start[];
306320 extern const char ggml_metallib_end[];
307321
308- NSString * src = [[NSString alloc ] initWithBytes: ggml_metallib_start length: (ggml_metallib_end-ggml_metallib_start) encoding: NSUTF8StringEncoding];
322+ NSString * src = [[NSString alloc ] initWithBytes: ggml_metallib_start length: (ggml_metallib_end-ggml_metallib_start) encoding: NSUTF8StringEncoding];
309323#else
310324 GGML_METAL_LOG_INFO (" %s : default.metallib not found, loading from source\n " , __func__);
311325
312- NSString * sourcePath ;
313- NSString * ggmlMetalPathResources = [[NSProcessInfo processInfo ].environment objectForKey: @" GGML_METAL_PATH_RESOURCES" ];
326+ NSString * path_source ;
327+ NSString * path_resource = [[NSProcessInfo processInfo ].environment objectForKey: @" GGML_METAL_PATH_RESOURCES" ];
314328
315- GGML_METAL_LOG_INFO (" %s : GGML_METAL_PATH_RESOURCES = %s \n " , __func__, ggmlMetalPathResources ? [ggmlMetalPathResources UTF8String ] : " nil" );
329+ GGML_METAL_LOG_INFO (" %s : GGML_METAL_PATH_RESOURCES = %s \n " , __func__, path_resource ? [path_resource UTF8String ] : " nil" );
316330
317- if (ggmlMetalPathResources ) {
318- sourcePath = [ggmlMetalPathResources stringByAppendingPathComponent: @" ggml-metal.metal" ];
331+ if (path_resource ) {
332+ path_source = [path_resource stringByAppendingPathComponent: @" ggml-metal.metal" ];
319333 } else {
320- sourcePath = [bundle pathForResource: @" ggml-metal" ofType: @" metal" ];
334+ path_source = [bundle pathForResource: @" ggml-metal" ofType: @" metal" ];
321335 }
322- if (sourcePath == nil ) {
336+
337+ if (path_source == nil ) {
323338 GGML_METAL_LOG_WARN (" %s : error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n " , __func__);
324- sourcePath = @" ggml-metal.metal" ;
339+ path_source = @" ggml-metal.metal" ;
325340 }
326- GGML_METAL_LOG_INFO (" %s : loading '%s '\n " , __func__, [sourcePath UTF8String ]);
327- NSString * src = [NSString stringWithContentsOfFile: sourcePath encoding: NSUTF8StringEncoding error: &error];
341+
342+ GGML_METAL_LOG_INFO (" %s : loading '%s '\n " , __func__, [path_source UTF8String ]);
343+
344+ NSString * src = [NSString stringWithContentsOfFile: path_source encoding: NSUTF8StringEncoding error: &error];
328345 if (error) {
329346 GGML_METAL_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
330347 return NULL ;
331348 }
332- #endif
349+ #endif // GGML_METAL_EMBED_LIBRARY
333350
334351 @autoreleasepool {
335352 // dictionary of preprocessor macros
0 commit comments