Skip to content

Commit 21ac3a1

Browse files
kchro3jhen0409
andauthored
metal : support for Swift (#3078)
* Metal support for Swift * update * add a toggle for arm/arm64 * set minimum versions for all platforms * update to use newLibraryWithURL * bump version Co-authored-by: Jhen-Jie Hong <[email protected]> --------- Co-authored-by: Jhen-Jie Hong <[email protected]>
1 parent 4fd5477 commit 21ac3a1

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

Package.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,54 @@
22

33
import PackageDescription
44

5+
#if arch(arm) || arch(arm64)
6+
let platforms: [SupportedPlatform]? = [
7+
.macOS(.v11),
8+
.iOS(.v14),
9+
.watchOS(.v4),
10+
.tvOS(.v14)
11+
]
12+
let exclude: [String] = []
13+
let additionalSources: [String] = ["ggml-metal.m"]
14+
let additionalSettings: [CSetting] = [
15+
.unsafeFlags(["-fno-objc-arc"]),
16+
.define("GGML_SWIFT"),
17+
.define("GGML_USE_METAL")
18+
]
19+
#else
20+
let platforms: [SupportedPlatform]? = nil
21+
let exclude: [String] = ["ggml-metal.metal"]
22+
let additionalSources: [String] = []
23+
let additionalSettings: [CSetting] = []
24+
#endif
25+
526
let package = Package(
627
name: "llama",
28+
platforms: platforms,
729
products: [
830
.library(name: "llama", targets: ["llama"]),
931
],
1032
targets: [
1133
.target(
1234
name: "llama",
1335
path: ".",
14-
exclude: ["ggml-metal.metal"],
36+
exclude: exclude,
1537
sources: [
1638
"ggml.c",
1739
"llama.cpp",
1840
"ggml-alloc.c",
19-
"k_quants.c"
20-
],
41+
"k_quants.c",
42+
] + additionalSources,
2143
publicHeadersPath: "spm-headers",
2244
cSettings: [
2345
.unsafeFlags(["-Wno-shorten-64-to-32"]),
2446
.define("GGML_USE_K_QUANTS"),
2547
.define("GGML_USE_ACCELERATE")
26-
],
48+
] + additionalSettings,
2749
linkerSettings: [
2850
.linkedFramework("Accelerate")
2951
]
30-
),
52+
)
3153
],
3254
cxxLanguageStandard: .cxx11
3355
)

ggml-metal.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,20 @@ @implementation GGMLMetalClass
144144

145145
ctx->d_queue = dispatch_queue_create("llama.cpp", DISPATCH_QUEUE_CONCURRENT);
146146

147-
#if 0
148-
// compile from source string and show compile log
147+
#ifdef GGML_SWIFT
148+
// load the default.metallib file
149149
{
150150
NSError * error = nil;
151151

152-
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+
NSURL * libURL = [NSURL fileURLWithPath:libPath];
157+
158+
// Load the metallib file into a Metal library
159+
ctx->library = [ctx->device newLibraryWithURL:libURL error:&error];
160+
153161
if (error) {
154162
metal_printf("%s: error: %s\n", __func__, [[error description] UTF8String]);
155163
return NULL;

0 commit comments

Comments
 (0)