Commit 06fa894b1 for llama.cpp
commit 06fa894b15f744d952df69357d4d2591cddac0d3
Author: Niklas Wenzel <dev@nikwen.de>
Date: Tue Dec 1 15:48:04 2026 +0100
metal : detect Neural Accelerators without hardcoding chips (#33272)
So that applications built with today's version of llama.cpp will properly use the tensor API on chips released in the future without requiring a llama.cpp update.
diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m
index e87187a12..50e8fd938 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.m
+++ b/ggml/src/ggml-metal/ggml-metal-device.m
@@ -28,6 +28,9 @@
static const NSInteger MTLGPUFamilyMetal3_GGML = 5001;
static const NSInteger MTLGPUFamilyMetal4_GGML = 5002;
+// overload of MTLGPUFamilyAppleX (not available in some environments)
+static const NSInteger MTLGPUFamilyApple10_GGML = 1010;
+
// MTLLanguageVersion4_0 is not present in older SDKs
static const NSUInteger MTLLanguageVersion4_0_GGML = 4 << 16;
@@ -1109,6 +1112,33 @@ const char * ggml_metal_device_id_token(enum ggml_metal_device_id id) {
return "GGML_METAL_DEVICE_GENERIC";
}
+static bool ggml_metal_has_tensor(id<MTLDevice> device) {
+ if (![device supportsFamily:MTLGPUFamilyMetal4_GGML]) {
+ return false;
+ }
+
+ if (getenv("GGML_METAL_TENSOR_DISABLE") != NULL) {
+ return false;
+ }
+
+ if (getenv("GGML_METAL_TENSOR_ENABLE") != NULL) {
+ return true;
+ }
+
+ // enable the tensor API by default for M5+ and A19+
+ // disable it for older chips because with the current implementation it is not useful
+ // - M2 Ultra: ~5% slower
+ // - M4, M4 Max: no significant difference
+ //
+ // TODO: try to update the tensor API kernels to at least match the simdgroup performance
+ if ([device supportsFamily:MTLGPUFamilyApple10_GGML]) {
+ return true;
+ }
+
+ GGML_LOG_INFO("%s: tensor API disabled for pre-M5 and pre-A19 devices\n", __func__);
+ return false;
+}
+
ggml_metal_device_t ggml_metal_device_init(int device, int n_devices) {
ggml_metal_device_t dev = calloc(1, sizeof(struct ggml_metal_device));
@@ -1145,24 +1175,7 @@ ggml_metal_device_t ggml_metal_device_init(int device, int n_devices) {
dev->props.has_bfloat = false;
}
- dev->props.has_tensor = [dev->mtl_device supportsFamily:MTLGPUFamilyMetal4_GGML];
- if (getenv("GGML_METAL_TENSOR_DISABLE") != NULL) {
- dev->props.has_tensor = false;
- }
-
- // note: disable the tensor API by default for old chips because with the current implementation it is not useful
- // - M2 Ultra: ~5% slower
- // - M4, M4 Max: no significant difference
- //
- // TODO: try to update the tensor API kernels to at least match the simdgroup performance
- if (getenv("GGML_METAL_TENSOR_ENABLE") == NULL &&
- ![[dev->mtl_device name] containsString:@"M5"] &&
- ![[dev->mtl_device name] containsString:@"M6"] &&
- ![[dev->mtl_device name] containsString:@"A19"] &&
- ![[dev->mtl_device name] containsString:@"A20"]) {
- GGML_LOG_INFO("%s: tensor API disabled for pre-M5 and pre-A19 devices\n", __func__);
- dev->props.has_tensor = false;
- }
+ dev->props.has_tensor = ggml_metal_has_tensor(dev->mtl_device);
// double-check that the tensor API compiles
if (dev->props.has_tensor) {