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

This commit is contained in:
Georgi Gerganov 2023-11-24 12:37:08 +02:00 committed by GitHub
parent 010c8ec3ab
commit 0ba365f958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View 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_malloc(size_t n);
void ggml_metal_host_free (void * data); 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 // set the number of command buffers to use
void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb); 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 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -459,6 +459,10 @@ void ggml_metal_host_free(void * data) {
free(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) { void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_BUFFERS); 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); 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);
}

View File

@ -1078,6 +1078,11 @@ static ggml_backend_t whisper_backend_init(const whisper_context_params & params
if (!backend_gpu) { if (!backend_gpu) {
WHISPER_LOG_ERROR("%s: ggml_backend_metal_init() failed\n", __func__); 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 #endif