@@ -185,56 +185,44 @@ static void ggml_metal_log(enum ggml_log_level level, const char* format, ...){
185
185
186
186
ctx->d_queue = dispatch_queue_create (" ggml-metal" , DISPATCH_QUEUE_CONCURRENT);
187
187
188
- #ifdef GGML_SWIFT
189
- // load the default.metallib file
188
+ // load library
190
189
{
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;
207
193
#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
212
196
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
+ }
213
212
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 ;
225
214
#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 ) };
231
217
#endif
218
+ ctx->library = [ctx->device newLibraryWithSource: src options: options error: &error];
219
+ }
220
+
232
221
if (error) {
233
222
GGML_METAL_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
234
223
return NULL ;
235
224
}
236
225
}
237
- #endif
238
226
239
227
// load kernels
240
228
{
@@ -437,7 +425,7 @@ int ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
437
425
for (int i = 0 ; i < ctx->n_buffers ; ++i) {
438
426
const int64_t ioffs = (int64_t ) t->data - (int64_t ) ctx->buffers [i].data ;
439
427
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);
441
429
if (ioffs >= 0 && ioffs + tsize <= (int64_t ) ctx->buffers [i].size ) {
442
430
*offs = (size_t ) ioffs;
443
431
0 commit comments