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.
This commit is contained in:
parent
cc98896db8
commit
ad5734c34b
1 changed files with 28 additions and 0 deletions
|
@ -507,6 +507,34 @@ static struct ggml_backend_metal_context * ggml_metal_init(ggml_backend_dev_t de
|
|||
#endif
|
||||
|
||||
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
|
||||
if (path_lib == nil) {
|
||||
// Try to find the resource in the directory where the current binary located.
|
||||
NSString *currentBinary = [[NSProcessInfo processInfo] arguments][0];
|
||||
NSString *binDir = [currentBinary stringByDeletingLastPathComponent];
|
||||
NSString *defaultMetalibPath = [NSString pathWithComponents:@[binDir, @"default.metallib"]];
|
||||
NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:defaultMetalibPath error:&error];
|
||||
if (error) {
|
||||
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
||||
return NULL;
|
||||
}
|
||||
if (!atts) {
|
||||
GGML_METAL_LOG_ERROR("%s: error: src path attribute is NULL\n", __func__);
|
||||
return NULL;
|
||||
}
|
||||
if (atts[NSFileType] == NSFileTypeSymbolicLink) {
|
||||
// Optionally, if this is a symlink, try to resolve it.
|
||||
defaultMetalibPath = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:defaultMetalibPath error:&error];
|
||||
if (error) {
|
||||
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
|
||||
return NULL;
|
||||
}
|
||||
if (defaultMetalibPath && [defaultMetalibPath length] > 0 && ![[defaultMetalibPath substringToIndex:1] isEqualToString:@"/"]) {
|
||||
defaultMetalibPath = [NSString pathWithComponents:@[binDir, defaultMetalibPath]];
|
||||
}
|
||||
}
|
||||
path_lib = defaultMetalibPath;
|
||||
}
|
||||
|
||||
if (try_metallib && path_lib != nil) {
|
||||
// pre-compiled library found
|
||||
NSURL * libURL = [NSURL fileURLWithPath:path_lib];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue