ggerganov commited on
Commit
c95e649
·
unverified ·
1 Parent(s): cc06e31

metal : add backend function to check device family support (#1547)

Browse files
Files changed (3) hide show
  1. ggml-metal.h +7 -0
  2. ggml-metal.m +10 -0
  3. whisper.cpp +5 -0
ggml-metal.h CHANGED
@@ -52,6 +52,11 @@ void ggml_metal_free(struct ggml_metal_context * ctx);
52
  void * ggml_metal_host_malloc(size_t n);
53
  void ggml_metal_host_free (void * data);
54
 
 
 
 
 
 
55
  // set the number of command buffers to use
56
  void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
57
 
@@ -100,6 +105,8 @@ GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
100
 
101
  GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
102
 
 
 
103
  #ifdef __cplusplus
104
  }
105
  #endif
 
52
  void * ggml_metal_host_malloc(size_t n);
53
  void ggml_metal_host_free (void * data);
54
 
55
+ // helper to check if the device supports a specific family
56
+ // ideally, the user code should be doing these checks
57
+ // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
58
+ bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family);
59
+
60
  // set the number of command buffers to use
61
  void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
62
 
 
105
 
106
  GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
107
 
108
+ GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
109
+
110
  #ifdef __cplusplus
111
  }
112
  #endif
ggml-metal.m CHANGED
@@ -459,6 +459,10 @@ void ggml_metal_host_free(void * data) {
459
  free(data);
460
  }
461
 
 
 
 
 
462
  void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
463
  ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
464
  }
@@ -1751,3 +1755,9 @@ void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
1751
 
1752
  ggml_metal_set_n_cb(ctx, n_cb);
1753
  }
 
 
 
 
 
 
 
459
  free(data);
460
  }
461
 
462
+ bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family) {
463
+ return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
464
+ }
465
+
466
  void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
467
  ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
468
  }
 
1755
 
1756
  ggml_metal_set_n_cb(ctx, n_cb);
1757
  }
1758
+
1759
+ bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) {
1760
+ struct ggml_metal_context * ctx = (struct ggml_metal_context *)backend->context;
1761
+
1762
+ return ggml_metal_supports_family(ctx, family);
1763
+ }
whisper.cpp CHANGED
@@ -1078,6 +1078,11 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
1078
  if (!backend_gpu) {
1079
  WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__);
1080
  }
 
 
 
 
 
1081
  }
1082
  #endif
1083
 
 
1078
  if (!backend_gpu) {
1079
  WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__);
1080
  }
1081
+ if (!ggml_backend_metal_supports_family(backend_gpu, 7)) {
1082
+ WHISPER_LOG_ERROR("%s: Metal GPU does not support family 7 - falling back to CPU\n", __func__);
1083
+ ggml_backend_free(backend_gpu);
1084
+ backend_gpu = NULL;
1085
+ }
1086
  }
1087
  #endif
1088