Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync : llama.cpp #1134

Merged
merged 14 commits into from
Mar 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ggml : fix GGMLMetalClass ODR (llama/12200)
-- 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.
pminev authored and ggerganov committed Mar 7, 2025

Verified

This commit was signed with the committer’s verified signature.
ggerganov Georgi Gerganov
commit a60363bd39013833ecb6628ce76a34a886b28da4
47 changes: 24 additions & 23 deletions src/ggml-metal/ggml-metal.m
Original file line number Diff line number Diff line change
@@ -467,11 +467,13 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
// for now it is easier to work in a separate file
// static NSString * const msl_library_source = @"see metal.metal";

#if !GGML_METAL_EMBED_LIBRARY
// Here to assist with NSBundle Path Hack
@interface GGMLMetalClass : NSObject
@end
@implementation GGMLMetalClass
@end
#endif

static void * ggml_metal_host_malloc(size_t n) {
void * data = NULL;
@@ -520,7 +522,7 @@ @implementation GGMLMetalClass

ctx->d_queue = dispatch_queue_create("ggml-metal", DISPATCH_QUEUE_CONCURRENT);

id<MTLLibrary> metal_library;
id<MTLLibrary> metal_library = nil;

// load library
//
@@ -529,19 +531,23 @@ @implementation GGMLMetalClass
// - if not found, load the source and compile it
// - if that fails, return NULL
{
NSBundle * bundle = nil;
#ifdef SWIFT_PACKAGE
bundle = SWIFTPM_MODULE_BUNDLE;
#else
bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
#endif

NSError * error = nil;
NSString * src = nil;

#if GGML_METAL_EMBED_LIBRARY
const bool try_metallib = false;
GGML_LOG_INFO("%s: using embedded metal library\n", __func__);

extern const char ggml_metallib_start[];
extern const char ggml_metallib_end[];

src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];

#else

#ifdef SWIFT_PACKAGE
NSBundle * bundle = SWIFTPM_MODULE_BUNDLE;
#else
const bool try_metallib = true;
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
#endif

NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
@@ -574,7 +580,7 @@ @implementation GGMLMetalClass
path_lib = default_metallib_path;
}

if (try_metallib && path_lib != nil) {
if (path_lib != nil) {
// pre-compiled library found
NSURL * libURL = [NSURL fileURLWithPath:path_lib];
GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
@@ -585,14 +591,6 @@ @implementation GGMLMetalClass
return NULL;
}
} else {
#if GGML_METAL_EMBED_LIBRARY
GGML_LOG_INFO("%s: using embedded metal library\n", __func__);

extern const char ggml_metallib_start[];
extern const char ggml_metallib_end[];

NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
#else
GGML_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);

NSString * path_source;
@@ -613,13 +611,15 @@ @implementation GGMLMetalClass

GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_source UTF8String]);

NSString * src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
if (error) {
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
return NULL;
}
#endif // GGML_METAL_EMBED_LIBRARY
}
#endif

if (!metal_library) {
@autoreleasepool {
// dictionary of preprocessor macros
NSMutableDictionary * prep = [NSMutableDictionary dictionary];
@@ -647,10 +647,11 @@ @implementation GGMLMetalClass
[options release];
#endif
}
}

#if GGML_METAL_EMBED_LIBRARY
[src release];
[src release];
#endif // GGML_METAL_EMBED_LIBRARY
}
}

// print MTL GPU family: