pacominev commited on
Commit
2094cb7
·
1 Parent(s): c9a49f9

ggml : fix GGMLMetalClass ODR (llama/12200)

Browse files

-- it might happen if ggml is loaded from 2 separate libraries since each one of them will expose the class. This is more of a guard since we want to use only Metal as embedded library and don't care about the other case.

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