@@ -467,11 +467,13 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
467
467
// for now it is easier to work in a separate file
468
468
// static NSString * const msl_library_source = @"see metal.metal";
469
469
470
+ #if !GGML_METAL_EMBED_LIBRARY
470
471
// Here to assist with NSBundle Path Hack
471
472
@interface GGMLMetalClass : NSObject
472
473
@end
473
474
@implementation GGMLMetalClass
474
475
@end
476
+ #endif
475
477
476
478
static void * ggml_metal_host_malloc (size_t n) {
477
479
void * data = NULL ;
@@ -520,7 +522,7 @@ @implementation GGMLMetalClass
520
522
521
523
ctx->d_queue = dispatch_queue_create (" ggml-metal" , DISPATCH_QUEUE_CONCURRENT);
522
524
523
- id <MTLLibrary > metal_library;
525
+ id <MTLLibrary > metal_library = nil ;
524
526
525
527
// load library
526
528
//
@@ -529,19 +531,23 @@ @implementation GGMLMetalClass
529
531
// - if not found, load the source and compile it
530
532
// - if that fails, return NULL
531
533
{
532
- NSBundle * bundle = nil ;
533
- #ifdef SWIFT_PACKAGE
534
- bundle = SWIFTPM_MODULE_BUNDLE;
535
- #else
536
- bundle = [NSBundle bundleForClass: [GGMLMetalClass class ]];
537
- #endif
538
-
539
534
NSError * error = nil ;
535
+ NSString * src = nil ;
540
536
541
537
#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;
543
549
#else
544
- const bool try_metallib = true ;
550
+ NSBundle * bundle = [ NSBundle bundleForClass: [GGMLMetalClass class ]] ;
545
551
#endif
546
552
547
553
NSString * path_lib = [bundle pathForResource: @" default" ofType: @" metallib" ];
@@ -574,7 +580,7 @@ @implementation GGMLMetalClass
574
580
path_lib = default_metallib_path;
575
581
}
576
582
577
- if (try_metallib && path_lib != nil ) {
583
+ if (path_lib != nil ) {
578
584
// pre-compiled library found
579
585
NSURL * libURL = [NSURL fileURLWithPath: path_lib];
580
586
GGML_LOG_INFO (" %s : loading '%s '\n " , __func__, [path_lib UTF8String ]);
@@ -585,14 +591,6 @@ @implementation GGMLMetalClass
585
591
return NULL ;
586
592
}
587
593
} 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
596
594
GGML_LOG_INFO (" %s : default.metallib not found, loading from source\n " , __func__);
597
595
598
596
NSString * path_source;
@@ -613,13 +611,15 @@ @implementation GGMLMetalClass
613
611
614
612
GGML_LOG_INFO (" %s : loading '%s '\n " , __func__, [path_source UTF8String ]);
615
613
616
- NSString * src = [NSString stringWithContentsOfFile: path_source encoding: NSUTF8StringEncoding error: &error];
614
+ src = [NSString stringWithContentsOfFile: path_source encoding: NSUTF8StringEncoding error: &error];
617
615
if (error) {
618
616
GGML_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
619
617
return NULL ;
620
618
}
621
- #endif // GGML_METAL_EMBED_LIBRARY
619
+ }
620
+ #endif
622
621
622
+ if (!metal_library) {
623
623
@autoreleasepool {
624
624
// dictionary of preprocessor macros
625
625
NSMutableDictionary * prep = [NSMutableDictionary dictionary ];
@@ -647,10 +647,11 @@ @implementation GGMLMetalClass
647
647
[options release ];
648
648
#endif
649
649
}
650
+ }
651
+
650
652
#if GGML_METAL_EMBED_LIBRARY
651
- [src release ];
653
+ [src release ];
652
654
#endif // GGML_METAL_EMBED_LIBRARY
653
- }
654
655
}
655
656
656
657
// print MTL GPU family:
0 commit comments