Skip to content

Commit c26765a

Browse files
authored
metal : support default.metallib load & reuse code for swift package (#3522)
* metal : support load default.metallib & reuse code for swift package * metal : use SWIFT_PACKAGE def instead of define GGML_SWIFT
1 parent 0e797c2 commit c26765a

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*.gcno
1111
*.gcda
1212
*.dot
13+
*.metallib
1314
.DS_Store
1415
.build/
1516
.cache/

Package.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ let platforms: [SupportedPlatform]? = [
1010
.tvOS(.v14)
1111
]
1212
let exclude: [String] = []
13-
let additionalSources: [String] = ["ggml-metal.m", "ggml-metal.metal"]
13+
let resources: [Resource] = [
14+
.process("ggml-metal.metal")
15+
]
16+
let additionalSources: [String] = ["ggml-metal.m"]
1417
let additionalSettings: [CSetting] = [
1518
.unsafeFlags(["-fno-objc-arc"]),
16-
.define("GGML_SWIFT"),
1719
.define("GGML_USE_METAL")
1820
]
1921
#else
2022
let platforms: [SupportedPlatform]? = nil
2123
let exclude: [String] = ["ggml-metal.metal"]
24+
let resources: [Resource] = []
2225
let additionalSources: [String] = []
2326
let additionalSettings: [CSetting] = []
2427
#endif
@@ -40,6 +43,7 @@ let package = Package(
4043
"ggml-alloc.c",
4144
"k_quants.c",
4245
] + additionalSources,
46+
resources: resources,
4347
publicHeadersPath: "spm-headers",
4448
cSettings: [
4549
.unsafeFlags(["-Wno-shorten-64-to-32"]),

ggml-metal.m

+28-40
Original file line numberDiff line numberDiff line change
@@ -185,56 +185,44 @@ static void ggml_metal_log(enum ggml_log_level level, const char* format, ...){
185185

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

188-
#ifdef GGML_SWIFT
189-
// load the default.metallib file
188+
// load library
190189
{
191-
NSError * error = nil;
192-
193-
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
194-
NSString * llamaBundlePath = [bundle pathForResource:@"llama_llama" ofType:@"bundle"];
195-
NSBundle * llamaBundle = [NSBundle bundleWithPath:llamaBundlePath];
196-
NSString * libPath = [llamaBundle pathForResource:@"default" ofType:@"metallib"];
197-
NSURL * libURL = [NSURL fileURLWithPath:libPath];
198-
199-
// Load the metallib file into a Metal library
200-
ctx->library = [ctx->device newLibraryWithURL:libURL error:&error];
201-
202-
if (error) {
203-
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
204-
return NULL;
205-
}
206-
}
190+
NSBundle * bundle = nil;
191+
#ifdef SWIFT_PACKAGE
192+
bundle = SWIFTPM_MODULE_BUNDLE;
207193
#else
208-
UNUSED(msl_library_source);
209-
210-
// read the source from "ggml-metal.metal" into a string and use newLibraryWithSource
211-
{
194+
bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
195+
#endif
212196
NSError * error = nil;
197+
NSString * libPath = [bundle pathForResource:@"default" ofType:@"metallib"];
198+
if (libPath != nil) {
199+
NSURL * libURL = [NSURL fileURLWithPath:libPath];
200+
GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [libPath UTF8String]);
201+
ctx->library = [ctx->device newLibraryWithURL:libURL error:&error];
202+
} else {
203+
GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
204+
205+
NSString * sourcePath = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
206+
GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [sourcePath UTF8String]);
207+
NSString * src = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:&error];
208+
if (error) {
209+
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
210+
return NULL;
211+
}
213212

214-
//NSString * path = [[NSBundle mainBundle] pathForResource:@"../../examples/metal/metal" ofType:@"metal"];
215-
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
216-
NSString * path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
217-
GGML_METAL_LOG_INFO("%s: loading '%s'\n", __func__, [path UTF8String]);
218-
219-
NSString * src = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
220-
if (error) {
221-
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
222-
return NULL;
223-
}
224-
213+
MTLCompileOptions* options = nil;
225214
#ifdef GGML_QKK_64
226-
MTLCompileOptions* options = [MTLCompileOptions new];
227-
options.preprocessorMacros = @{ @"QK_K" : @(64) };
228-
ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
229-
#else
230-
ctx->library = [ctx->device newLibraryWithSource:src options:nil error:&error];
215+
options = [MTLCompileOptions new];
216+
options.preprocessorMacros = @{ @"QK_K" : @(64) };
231217
#endif
218+
ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
219+
}
220+
232221
if (error) {
233222
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
234223
return NULL;
235224
}
236225
}
237-
#endif
238226

239227
// load kernels
240228
{
@@ -437,7 +425,7 @@ int ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
437425
for (int i = 0; i < ctx->n_buffers; ++i) {
438426
const int64_t ioffs = (int64_t) t->data - (int64_t) ctx->buffers[i].data;
439427

440-
//metal_printf("ioffs = %10ld, tsize = %10ld, sum = %10ld, ctx->buffers[%d].size = %10ld, name = %s\n", ioffs, tsize, ioffs + tsize, i, ctx->buffers[i].size, ctx->buffers[i].name);
428+
//GGML_METAL_LOG_INFO("ioffs = %10ld, tsize = %10ld, sum = %10ld, ctx->buffers[%d].size = %10ld, name = %s\n", ioffs, tsize, ioffs + tsize, i, ctx->buffers[i].size, ctx->buffers[i].name);
441429
if (ioffs >= 0 && ioffs + tsize <= (int64_t) ctx->buffers[i].size) {
442430
*offs = (size_t) ioffs;
443431

0 commit comments

Comments
 (0)