]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
metal : add backend function to check device family support (#1547)
authorGeorgi Gerganov <redacted>
Fri, 24 Nov 2023 10:37:08 +0000 (12:37 +0200)
committerGitHub <redacted>
Fri, 24 Nov 2023 10:37:08 +0000 (12:37 +0200)
ggml-metal.h
ggml-metal.m
whisper.cpp

index be2731f8ba476728c50b4971a67a74a9437b981d..de46b8804dc69fcdffb9df4d578efcf4dbaa4f95 100644 (file)
@@ -52,6 +52,11 @@ void ggml_metal_free(struct ggml_metal_context * ctx);
 void * ggml_metal_host_malloc(size_t n);
 void   ggml_metal_host_free  (void * data);
 
+// helper to check if the device supports a specific family
+// ideally, the user code should be doing these checks
+// ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
+bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family);
+
 // set the number of command buffers to use
 void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
 
@@ -100,6 +105,8 @@ GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
 
 GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
 
+GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
+
 #ifdef __cplusplus
 }
 #endif
index 3591b87cae8e63921135ccfd9ce5b4e5127fa076..51382adea57aaf500ef7672995e42a2cc5e54828 100644 (file)
@@ -459,6 +459,10 @@ void ggml_metal_host_free(void * data) {
     free(data);
 }
 
+bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family) {
+    return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
+}
+
 void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
     ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
 }
@@ -1751,3 +1755,9 @@ void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
 
     ggml_metal_set_n_cb(ctx, n_cb);
 }
+
+bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
+    struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
+
+    return ggml_metal_supports_family(ctx, family);
+}
index e2bb1bba3e6297a5da0fbe93ece1c3b71e876e2b..971b8e6f4d712789dd2ad642ddfed4fa10ff2797 100644 (file)
@@ -1078,6 +1078,11 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
         if (!backend_gpu) {
             WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__);
         }
+        if (!ggml_backend_metal_supports_family(backend_gpu, 7)) {
+            WHISPER_LOG_ERROR("%s: Metal GPU does not support family 7 - falling back to CPU\n", __func__);
+            ggml_backend_free(backend_gpu);
+            backend_gpu = NULL;
+        }
     }
 #endif