Skip to content

Commit ad5734c

Browse files
committed
Extend how Llama.cpp locates metal resources
* It searches the resource file in the directory where the current binary is located as well. * Resolves symbolic links. Rationale: When we plug this dependency into a Bazel build and run it in the context of Bazel (e.g. testing): * the execution directory is often very different from where the files are located and no direct control over this (Bazel sandboxing), * the Bazel sandbox often use symbolic links to make files available. With this patch, we can have the resource file added to the target, can build and run tests in the context of Bazel.
1 parent cc98896 commit ad5734c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,34 @@ @implementation GGMLMetalClass
507507
#endif
508508

509509
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
510+
if (path_lib == nil) {
511+
// Try to find the resource in the directory where the current binary located.
512+
NSString *currentBinary = [[NSProcessInfo processInfo] arguments][0];
513+
NSString *binDir = [currentBinary stringByDeletingLastPathComponent];
514+
NSString *defaultMetalibPath = [NSString pathWithComponents:@[binDir, @"default.metallib"]];
515+
NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:defaultMetalibPath error:&error];
516+
if (error) {
517+
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
518+
return NULL;
519+
}
520+
if (!atts) {
521+
GGML_METAL_LOG_ERROR("%s: error: src path attribute is NULL\n", __func__);
522+
return NULL;
523+
}
524+
if (atts[NSFileType] == NSFileTypeSymbolicLink) {
525+
// Optionally, if this is a symlink, try to resolve it.
526+
defaultMetalibPath = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:defaultMetalibPath error:&error];
527+
if (error) {
528+
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
529+
return NULL;
530+
}
531+
if (defaultMetalibPath && [defaultMetalibPath length] > 0 && ![[defaultMetalibPath substringToIndex:1] isEqualToString:@"/"]) {
532+
defaultMetalibPath = [NSString pathWithComponents:@[binDir, defaultMetalibPath]];
533+
}
534+
}
535+
path_lib = defaultMetalibPath;
536+
}
537+
510538
if (try_metallib && path_lib != nil) {
511539
// pre-compiled library found
512540
NSURL * libURL = [NSURL fileURLWithPath:path_lib];

0 commit comments

Comments
 (0)