ggerganov commited on
Commit
27311ef
·
unverified ·
1 Parent(s): b5bb3f3

metal : build metallib + fix embed path (llama/6015)

Browse files

* metal : build metallib + fix embed path

ggml-ci

* metal : fix embed build + update library load logic

ggml-ci

* metal : fix embeded library build

ggml-ci

* ci : fix iOS builds to use embedded library

Files changed (2) hide show
  1. ggml-metal.m +33 -16
  2. ggml-metal.metal +0 -3
ggml-metal.m CHANGED
@@ -280,6 +280,11 @@ static struct ggml_metal_context * ggml_metal_init(int n_cb) {
280
  id<MTLLibrary> metal_library;
281
 
282
  // load library
 
 
 
 
 
283
  {
284
  NSBundle * bundle = nil;
285
  #ifdef SWIFT_PACKAGE
@@ -287,12 +292,21 @@ static struct ggml_metal_context * ggml_metal_init(int n_cb) {
287
  #else
288
  bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
289
  #endif
 
290
  NSError * error = nil;
291
- NSString * libPath = [bundle pathForResource:@"default" ofType:@"metallib"];
292
- if (libPath != nil) {
 
 
 
 
 
 
 
293
  // pre-compiled library found
294
- NSURL * libURL = [NSURL fileURLWithPath:libPath];
295
- GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [libPath UTF8String]);
 
296
  metal_library = [ctx->device newLibraryWithURL:libURL error:&error];
297
  if (error) {
298
  GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
@@ -305,31 +319,34 @@ static struct ggml_metal_context * ggml_metal_init(int n_cb) {
305
  extern const char ggml_metallib_start[];
306
  extern const char ggml_metallib_end[];
307
 
308
- NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
309
  #else
310
  GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
311
 
312
- NSString * sourcePath;
313
- NSString * ggmlMetalPathResources = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
314
 
315
- GGML_METAL_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, ggmlMetalPathResources ? [ggmlMetalPathResources UTF8String] : "nil");
316
 
317
- if (ggmlMetalPathResources) {
318
- sourcePath = [ggmlMetalPathResources stringByAppendingPathComponent:@"ggml-metal.metal"];
319
  } else {
320
- sourcePath = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
321
  }
322
- if (sourcePath == nil) {
 
323
  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";
325
  }
326
- GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [sourcePath UTF8String]);
327
- NSString * src = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:&error];
 
 
328
  if (error) {
329
  GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
330
  return NULL;
331
  }
332
- #endif
333
 
334
  @autoreleasepool {
335
  // dictionary of preprocessor macros
 
280
  id<MTLLibrary> metal_library;
281
 
282
  // 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
288
  {
289
  NSBundle * bundle = nil;
290
  #ifdef SWIFT_PACKAGE
 
292
  #else
293
  bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
294
  #endif
295
+
296
  NSError * error = 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) {
306
  // pre-compiled library found
307
+ NSURL * libURL = [NSURL fileURLWithPath:path_lib];
308
+ GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
309
+
310
  metal_library = [ctx->device newLibraryWithURL:libURL error:&error];
311
  if (error) {
312
  GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
 
319
  extern const char ggml_metallib_start[];
320
  extern const char ggml_metallib_end[];
321
 
322
+ NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
323
  #else
324
  GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
325
 
326
+ NSString * path_source;
327
+ NSString * path_resource = [[NSProcessInfo processInfo].environment objectForKey:@"GGML_METAL_PATH_RESOURCES"];
328
 
329
+ GGML_METAL_LOG_INFO("%s: GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");
330
 
331
+ if (path_resource) {
332
+ path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
333
  } else {
334
+ path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
335
  }
336
+
337
+ if (path_source == nil) {
338
  GGML_METAL_LOG_WARN("%s: error: could not use bundle path to find ggml-metal.metal, falling back to trying cwd\n", __func__);
339
+ path_source = @"ggml-metal.metal";
340
  }
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];
345
  if (error) {
346
  GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
347
  return NULL;
348
  }
349
+ #endif // GGML_METAL_EMBED_LIBRARY
350
 
351
  @autoreleasepool {
352
  // dictionary of preprocessor macros
ggml-metal.metal CHANGED
@@ -4,9 +4,6 @@
4
 
5
  #include <metal_stdlib>
6
 
7
- #define GGML_COMMON_IMPL_METAL
8
- #include "ggml-common.h"
9
-
10
  using namespace metal;
11
 
12
  #define MAX(x, y) ((x) > (y) ? (x) : (y))
 
4
 
5
  #include <metal_stdlib>
6
 
 
 
 
7
  using namespace metal;
8
 
9
  #define MAX(x, y) ((x) > (y) ? (x) : (y))