metal : fix kernel init + fix compile options

This commit is contained in:
Georgi Gerganov 2024-01-06 14:05:13 +02:00
parent 8437ff5ee2
commit cd160f85f1
No known key found for this signature in database
GPG key ID: 449E073F9DC10735

View file

@ -26,7 +26,7 @@
#define GGML_MAX_CONCUR (2*GGML_DEFAULT_GRAPH_SIZE) #define GGML_MAX_CONCUR (2*GGML_DEFAULT_GRAPH_SIZE)
#define GGML_METAL_MAX_KERNELS 1024 #define GGML_METAL_MAX_KERNELS 256
struct ggml_metal_buffer { struct ggml_metal_buffer {
const char * name; const char * name;
@ -294,16 +294,16 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {
return NULL; return NULL;
} }
MTLCompileOptions* options = nil; // dictionary of preprocessor macros
NSMutableDictionary * prep = [NSMutableDictionary dictionary];
#ifdef GGML_QKK_64 #ifdef GGML_QKK_64
options = [MTLCompileOptions new]; prep[@"QK_K"] = @(64);
options.preprocessorMacros = @{ @"QK_K" : @(64) };
#endif #endif
// try to disable fast-math
// NOTE: this seems to have no effect whatsoever MTLCompileOptions* options = [MTLCompileOptions new];
// instead, in order to disable fast-math, we have to build default.metallib from the command line options.preprocessorMacros = prep;
// using xcrun -sdk macosx metal -fno-fast-math -c ggml-metal.metal -o ggml-metal.air
// and go through the "pre-compiled library found" path above
//[options setFastMathEnabled:false]; //[options setFastMathEnabled:false];
ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error]; ctx->library = [ctx->device newLibraryWithSource:src options:options error:&error];
@ -355,6 +355,11 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {
{ {
NSError * error = nil; NSError * error = nil;
for (int i = 0; i < GGML_METAL_MAX_KERNELS; ++i) {
ctx->kernels[i].function = nil;
ctx->kernels[i].pipeline = nil;
}
#define GGML_METAL_ADD_KERNEL(e, name, family) \ #define GGML_METAL_ADD_KERNEL(e, name, family) \
if ([ctx->device supportsFamily:(family)]) { \ if ([ctx->device supportsFamily:(family)]) { \
struct ggml_metal_kernel * kernel = &ctx->kernels[e]; \ struct ggml_metal_kernel * kernel = &ctx->kernels[e]; \