Skip to content

Commit 0ba365f

Browse files
authored
metal : add backend function to check device family support (#1547)
1 parent 010c8ec commit 0ba365f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

ggml-metal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ void ggml_metal_free(struct ggml_metal_context * ctx);
5252
void * ggml_metal_host_malloc(size_t n);
5353
void ggml_metal_host_free (void * data);
5454

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+
5560
// set the number of command buffers to use
5661
void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
5762

@@ -100,6 +105,8 @@ GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
100105

101106
GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
102107

108+
GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
109+
103110
#ifdef __cplusplus
104111
}
105112
#endif

ggml-metal.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ void ggml_metal_host_free(void * data) {
459459
free(data);
460460
}
461461

462+
bool ggml_metal_supports_family(struct ggml_metal_context * ctx, int family) {
463+
return [ctx->device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
464+
}
465+
462466
void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
463467
ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS);
464468
}
@@ -1751,3 +1755,9 @@ void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
17511755

17521756
ggml_metal_set_n_cb(ctx, n_cb);
17531757
}
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,11 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
10781078
if (!backend_gpu) {
10791079
WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__);
10801080
}
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+
}
10811086
}
10821087
#endif
10831088

0 commit comments

Comments
 (0)