24
24
25
25
#define UNUSED (x ) (void )(x)
26
26
27
- #define GGML_METAL_MAX_KERNELS 256
28
-
29
27
struct ggml_metal_kernel {
30
- id <MTLFunction > function;
31
28
id <MTLComputePipelineState > pipeline;
32
29
};
33
30
159
156
160
157
id <MTLDevice > device;
161
158
id <MTLCommandQueue > queue;
162
- id <MTLLibrary > library;
163
159
164
160
dispatch_queue_t d_queue;
165
161
166
- struct ggml_metal_kernel kernels[GGML_METAL_MAX_KERNELS ];
162
+ struct ggml_metal_kernel kernels[GGML_METAL_KERNEL_TYPE_COUNT ];
167
163
168
164
bool support_simdgroup_reduction;
169
165
bool support_simdgroup_mm;
@@ -246,6 +242,8 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
246
242
ctx->queue = [ctx->device newCommandQueue ];
247
243
ctx->d_queue = dispatch_queue_create (" ggml-metal" , DISPATCH_QUEUE_CONCURRENT);
248
244
245
+ id <MTLLibrary > metal_library;
246
+
249
247
// load library
250
248
{
251
249
NSBundle * bundle = nil ;
@@ -260,7 +258,7 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
260
258
// pre-compiled library found
261
259
NSURL * libURL = [NSURL fileURLWithPath: libPath];
262
260
GGML_METAL_LOG_INFO (" %s : loading '%s '\n " , __func__, [libPath UTF8String ]);
263
- ctx-> library = [ctx->device newLibraryWithURL: libURL error: &error];
261
+ metal_library = [ctx->device newLibraryWithURL: libURL error: &error];
264
262
if (error) {
265
263
GGML_METAL_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
266
264
return NULL ;
@@ -302,7 +300,7 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
302
300
303
301
// [options setFastMathEnabled:false];
304
302
305
- ctx-> library = [ctx->device newLibraryWithSource: src options: options error: &error];
303
+ metal_library = [ctx->device newLibraryWithSource: src options: options error: &error];
306
304
if (error) {
307
305
GGML_METAL_LOG_ERROR (" %s : error: %s \n " , __func__, [[error description ] UTF8String ]);
308
306
return NULL ;
@@ -367,8 +365,7 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
367
365
{
368
366
NSError * error = nil ;
369
367
370
- for (int i = 0 ; i < GGML_METAL_MAX_KERNELS; ++i) {
371
- ctx->kernels [i].function = nil ;
368
+ for (int i = 0 ; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
372
369
ctx->kernels [i].pipeline = nil ;
373
370
}
374
371
@@ -380,10 +377,12 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
380
377
#define GGML_METAL_ADD_KERNEL (e, name, supported ) \
381
378
if (supported) { \
382
379
struct ggml_metal_kernel * kernel = &ctx->kernels [e]; \
383
- kernel->function = [ctx->library newFunctionWithName: @" kernel_" #name]; \
384
- kernel->pipeline = [ctx->device newComputePipelineStateWithFunction: kernel->function error: &error]; \
380
+ id <MTLFunction > metal_function = [metal_library newFunctionWithName: @" kernel_" #name]; \
381
+ kernel->pipeline = [ctx->device newComputePipelineStateWithFunction: metal_function error: &error]; \
382
+ [metal_function release ]; \
385
383
if (error) { \
386
384
GGML_METAL_LOG_ERROR (" %s : error: load pipeline error: %s \n " , __func__, [[error description ] UTF8String ]); \
385
+ [metal_library release ]; \
387
386
return NULL ; \
388
387
} \
389
388
} else { \
@@ -512,23 +511,17 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
512
511
GGML_METAL_ADD_KERNEL (GGML_METAL_KERNEL_TYPE_SUM_ROWS, sum_rows, true );
513
512
}
514
513
514
+ [metal_library release ];
515
515
return ctx;
516
516
}
517
517
518
518
static void ggml_metal_free (struct ggml_metal_context * ctx) {
519
519
GGML_METAL_LOG_INFO (" %s : deallocating\n " , __func__);
520
520
521
- for (int i = 0 ; i < GGML_METAL_MAX_KERNELS; ++i) {
522
- if (ctx->kernels [i].pipeline ) {
523
- [ctx->kernels[i].pipeline release ];
524
- }
525
-
526
- if (ctx->kernels [i].function ) {
527
- [ctx->kernels[i].function release ];
528
- }
521
+ for (int i = 0 ; i < GGML_METAL_KERNEL_TYPE_COUNT; ++i) {
522
+ [ctx->kernels[i].pipeline release ];
529
523
}
530
524
531
- [ctx->library release ];
532
525
[ctx->queue release ];
533
526
[ctx->device release ];
534
527
0 commit comments