Skip to content

Commit 89a96fd

Browse files
committed
Metal support for Swift
1 parent ebc9608 commit 89a96fd

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import PackageDescription
44

55
let package = Package(
66
name: "llama",
7+
platforms: [.macOS(.v11)],
78
products: [
89
.library(name: "llama", targets: ["llama"]),
910
],
1011
targets: [
1112
.target(
1213
name: "llama",
1314
path: ".",
14-
exclude: ["ggml-metal.metal"],
1515
sources: [
1616
"ggml.c",
1717
"llama.cpp",
1818
"ggml-alloc.c",
19-
"k_quants.c"
19+
"k_quants.c",
20+
"ggml-metal.m",
2021
],
2122
publicHeadersPath: "spm-headers",
2223
cSettings: [
2324
.unsafeFlags(["-Wno-shorten-64-to-32"]),
25+
.unsafeFlags(["-fno-objc-arc"]),
26+
.define("GGML_SWIFT"),
27+
.define("GGML_USE_METAL"),
2428
.define("GGML_USE_K_QUANTS"),
2529
.define("GGML_USE_ACCELERATE")
2630
],

ggml-metal.m

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,22 @@ @implementation GGMLMetalClass
141141

142142
ctx->d_queue = dispatch_queue_create("llama.cpp", DISPATCH_QUEUE_CONCURRENT);
143143

144-
#if 0
145-
// compile from source string and show compile log
144+
MTLCompileOptions* options = [MTLCompileOptions new];
145+
options.preprocessorMacros = @{ @"QK_K" : @(64) };
146+
147+
#ifdef GGML_SWIFT
148+
// load the default.metallib file
146149
{
147150
NSError * error = nil;
148151

149-
ctx->library = [ctx->device newLibraryWithSource:msl_library_source options:nil error:&error];
152+
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
153+
NSString * llamaBundlePath = [bundle pathForResource:@"llama_llama" ofType:@"bundle"];
154+
NSBundle * llamaBundle = [NSBundle bundleWithPath:llamaBundlePath];
155+
NSString * libPath = [llamaBundle pathForResource:@"default" ofType:@"metallib"];
156+
157+
// Load the metallib file into a Metal library
158+
ctx->library = [ctx->device newLibraryWithFile:libPath error:&error];
159+
150160
if (error) {
151161
metal_printf("%s: error: %s\n", __func__, [[error description] UTF8String]);
152162
return NULL;
@@ -171,8 +181,6 @@ @implementation GGMLMetalClass
171181
}
172182

173183
#ifdef GGML_QKK_64
174-
MTLCompileOptions* options = [MTLCompileOptions new];
175-
options.preprocessorMacros = @{ @"QK_K" : @(64) };
176184
ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
177185
#else
178186
ctx->library = [ctx->device newLibraryWithSource:src options:nil error:&error];

0 commit comments

Comments
 (0)