diff --git a/Package.swift b/Package.swift index 96f52c4f0..b86e25570 100644 --- a/Package.swift +++ b/Package.swift @@ -4,6 +4,7 @@ import PackageDescription let package = Package( name: "llama", + platforms: [.macOS(.v11)], products: [ .library(name: "llama", targets: ["llama"]), ], @@ -11,16 +12,19 @@ let package = Package( .target( name: "llama", path: ".", - exclude: ["ggml-metal.metal"], sources: [ "ggml.c", "llama.cpp", "ggml-alloc.c", - "k_quants.c" + "k_quants.c", + "ggml-metal.m", ], publicHeadersPath: "spm-headers", cSettings: [ .unsafeFlags(["-Wno-shorten-64-to-32"]), + .unsafeFlags(["-fno-objc-arc"]), + .define("GGML_SWIFT"), + .define("GGML_USE_METAL"), .define("GGML_USE_K_QUANTS"), .define("GGML_USE_ACCELERATE") ], diff --git a/ggml-metal.m b/ggml-metal.m index 7e2355ce6..cd1308955 100644 --- a/ggml-metal.m +++ b/ggml-metal.m @@ -141,12 +141,22 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) { ctx->d_queue = dispatch_queue_create("llama.cpp", DISPATCH_QUEUE_CONCURRENT); -#if 0 - // compile from source string and show compile log + MTLCompileOptions* options = [MTLCompileOptions new]; + options.preprocessorMacros = @{ @"QK_K" : @(64) }; + +#ifdef GGML_SWIFT + // load the default.metallib file { NSError * error = nil; - ctx->library = [ctx->device newLibraryWithSource:msl_library_source options:nil error:&error]; + NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]]; + NSString * llamaBundlePath = [bundle pathForResource:@"llama_llama" ofType:@"bundle"]; + NSBundle * llamaBundle = [NSBundle bundleWithPath:llamaBundlePath]; + NSString * libPath = [llamaBundle pathForResource:@"default" ofType:@"metallib"]; + + // Load the metallib file into a Metal library + ctx->library = [ctx->device newLibraryWithFile:libPath error:&error]; + if (error) { metal_printf("%s: error: %s\n", __func__, [[error description] UTF8String]); return NULL; @@ -171,8 +181,6 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) { } #ifdef GGML_QKK_64 - MTLCompileOptions* options = [MTLCompileOptions new]; - options.preprocessorMacros = @{ @"QK_K" : @(64) }; ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error]; #else ctx->library = [ctx->device newLibraryWithSource:src options:nil error:&error];