Skip to content

Commit ebc1fad

Browse files
ormandiggerganov
authored andcommitted
metal : Extend how Llama.cpp locates metal resources (ggml-org#10676)
* metal : Extend how Llama.cpp locates metal resources (ggml-org#10675) * 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. * Update ggml/src/ggml-metal/ggml-metal.m Co-authored-by: Georgi Gerganov <[email protected]> * Update ggml/src/ggml-metal/ggml-metal.m Co-authored-by: Georgi Gerganov <[email protected]> --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent a1b52c1 commit ebc1fad

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,35 @@ @implementation GGMLMetalClass
510510
#endif
511511

512512
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
513+
if (path_lib == nil) {
514+
// Try to find the resource in the directory where the current binary located.
515+
NSString * current_binary = [[NSProcessInfo processInfo] arguments][0];
516+
NSString * bin_dir = [current_binary stringByDeletingLastPathComponent];
517+
NSString * default_metallib_path = [NSString pathWithComponents:@[bin_dir, @"default.metallib"]];
518+
if ([[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
519+
GGML_LOG_INFO("%s: found '%s'\n", __func__, [default_metallib_path UTF8String]);
520+
NSDictionary * atts = [[NSFileManager defaultManager] attributesOfItemAtPath:default_metallib_path error:&error];
521+
if (atts && atts[NSFileType] == NSFileTypeSymbolicLink) {
522+
// Optionally, if this is a symlink, try to resolve it.
523+
default_metallib_path = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:default_metallib_path error:&error];
524+
if (default_metallib_path && [default_metallib_path length] > 0 && ![[default_metallib_path substringToIndex:1] isEqualToString:@"/"]) {
525+
// It is a relative path, adding the binary directory as directory prefix.
526+
default_metallib_path = [NSString pathWithComponents:@[bin_dir, default_metallib_path]];
527+
}
528+
if (!default_metallib_path || ![[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
529+
// Link to the resource could not be resolved.
530+
default_metallib_path = nil;
531+
} else {
532+
GGML_LOG_INFO("%s: symlink resolved '%s'\n", __func__, [default_metallib_path UTF8String]);
533+
}
534+
}
535+
} else {
536+
// The resource couldn't be found in the binary's directory.
537+
default_metallib_path = nil;
538+
}
539+
path_lib = default_metallib_path;
540+
}
541+
513542
if (try_metallib && path_lib != nil) {
514543
// pre-compiled library found
515544
NSURL * libURL = [NSURL fileURLWithPath:path_lib];

0 commit comments

Comments
 (0)