@@ -467,11 +467,13 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
467467//        for now it is easier to work in a separate file
468468//  static NSString * const msl_library_source = @"see metal.metal";
469469
470+ #if  !GGML_METAL_EMBED_LIBRARY
470471//  Here to assist with NSBundle Path Hack
471472@interface  GGMLMetalClass  : NSObject 
472473@end 
473474@implementation  GGMLMetalClass 
474475@end 
476+ #endif 
475477
476478static  void  * ggml_metal_host_malloc (size_t  n) {
477479    void  * data = NULL ;
@@ -520,7 +522,7 @@ @implementation GGMLMetalClass
520522
521523    ctx->d_queue  = dispatch_queue_create (" ggml-metal"  , DISPATCH_QUEUE_CONCURRENT);
522524
523-     id <MTLLibrary > metal_library;
525+     id <MTLLibrary > metal_library =  nil ;
524526
525527    //  load library
526528    // 
@@ -529,19 +531,23 @@ @implementation GGMLMetalClass
529531    //  - if not found, load the source and compile it
530532    //  - if that fails, return NULL
531533    {
532-         NSBundle  * bundle = nil ;
533- #ifdef  SWIFT_PACKAGE
534-         bundle = SWIFTPM_MODULE_BUNDLE;
535- #else 
536-         bundle = [NSBundle  bundleForClass: [GGMLMetalClass class ]];
537- #endif 
538- 
539534        NSError  * error = nil ;
535+         NSString  * src = nil ;
540536
541537#if  GGML_METAL_EMBED_LIBRARY
542-         const  bool  try_metallib = false ;
538+         GGML_LOG_INFO (" %s : using embedded metal library\n "  , __func__);
539+ 
540+         extern  const  char  ggml_metallib_start[];
541+         extern  const  char  ggml_metallib_end[];
542+ 
543+         src = [[NSString  alloc ] initWithBytes: ggml_metallib_start length: (ggml_metallib_end-ggml_metallib_start) encoding: NSUTF8StringEncoding];
544+ 
545+ #else 
546+ 
547+ #ifdef  SWIFT_PACKAGE
548+         NSBundle  * bundle = SWIFTPM_MODULE_BUNDLE;
543549#else 
544-         const   bool  try_metallib  = true ;
550+         NSBundle  * bundle  = [ NSBundle   bundleForClass: [GGMLMetalClass  class ]] ;
545551#endif 
546552
547553        NSString  * path_lib = [bundle pathForResource: @" default"   ofType: @" metallib"  ];
@@ -574,7 +580,7 @@ @implementation GGMLMetalClass
574580            path_lib = default_metallib_path;
575581        }
576582
577-         if  (try_metallib &&  path_lib != nil ) {
583+         if  (path_lib != nil ) {
578584            //  pre-compiled library found
579585            NSURL  * libURL = [NSURL  fileURLWithPath: path_lib];
580586            GGML_LOG_INFO (" %s : loading '%s '\n "  , __func__, [path_lib UTF8String ]);
@@ -585,14 +591,6 @@ @implementation GGMLMetalClass
585591                return  NULL ;
586592            }
587593        } else  {
588- #if  GGML_METAL_EMBED_LIBRARY
589-             GGML_LOG_INFO (" %s : using embedded metal library\n "  , __func__);
590- 
591-             extern  const  char  ggml_metallib_start[];
592-             extern  const  char  ggml_metallib_end[];
593- 
594-             NSString  * src = [[NSString  alloc ] initWithBytes: ggml_metallib_start length: (ggml_metallib_end-ggml_metallib_start) encoding: NSUTF8StringEncoding];
595- #else 
596594            GGML_LOG_INFO (" %s : default.metallib not found, loading from source\n "  , __func__);
597595
598596            NSString  * path_source;
@@ -613,13 +611,15 @@ @implementation GGMLMetalClass
613611
614612            GGML_LOG_INFO (" %s : loading '%s '\n "  , __func__, [path_source UTF8String ]);
615613
616-             NSString  *  src = [NSString  stringWithContentsOfFile: path_source encoding: NSUTF8StringEncoding error: &error];
614+             src = [NSString  stringWithContentsOfFile: path_source encoding: NSUTF8StringEncoding error: &error];
617615            if  (error) {
618616                GGML_LOG_ERROR (" %s : error: %s \n "  , __func__, [[error description ] UTF8String ]);
619617                return  NULL ;
620618            }
621- #endif  //  GGML_METAL_EMBED_LIBRARY
619+         }
620+ #endif 
622621
622+         if  (!metal_library) {
623623            @autoreleasepool {
624624                //  dictionary of preprocessor macros
625625                NSMutableDictionary  * prep = [NSMutableDictionary  dictionary ];
@@ -647,10 +647,11 @@ @implementation GGMLMetalClass
647647                [options release ];
648648#endif 
649649            }
650+         }
651+ 
650652#if  GGML_METAL_EMBED_LIBRARY
651-              [src release ];
653+         [src release ];
652654#endif  //  GGML_METAL_EMBED_LIBRARY
653-         }
654655    }
655656
656657    //  print MTL GPU family:
0 commit comments