Skip to content

Commit 111f8d0

Browse files
authored
metal: fix regression when no metal devices are present (ggml-org#15531)
1 parent 5eff6ec commit 111f8d0

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,37 @@
9393
if (ctx->mtl_device == nil) {
9494
ctx->mtl_device = MTLCreateSystemDefaultDevice();
9595

96-
ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
97-
ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
96+
if (ctx->mtl_device) {
97+
ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
98+
ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
9899

99-
ctx->has_simdgroup_mm = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
100+
ctx->has_simdgroup_mm = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
100101

101102
#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
102-
ctx->has_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil;
103+
ctx->has_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil;
103104
#endif
104105

105-
ctx->has_bfloat = [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
106-
ctx->has_bfloat |= [ctx->mtl_device supportsFamily:MTLGPUFamilyApple6];
106+
ctx->has_bfloat = [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
107+
ctx->has_bfloat |= [ctx->mtl_device supportsFamily:MTLGPUFamilyApple6];
107108

108109
#if defined(GGML_METAL_USE_BF16)
109-
ctx->use_bfloat = ctx->has_bfloat;
110+
ctx->use_bfloat = ctx->has_bfloat;
110111
#else
111-
ctx->use_bfloat = false;
112+
ctx->use_bfloat = false;
112113
#endif
113-
ctx->use_fusion = getenv("GGML_METAL_FUSION_DISABLE") == nil;
114+
ctx->use_fusion = getenv("GGML_METAL_FUSION_DISABLE") == nil;
114115

115-
{
116-
const char * val = getenv("GGML_METAL_FUSION_DEBUG");
117-
ctx->debug_fusion = val ? atoi(val) : 0;
118-
}
116+
{
117+
const char * val = getenv("GGML_METAL_FUSION_DEBUG");
118+
ctx->debug_fusion = val ? atoi(val) : 0;
119+
}
119120

120-
memset(ctx->fuse_cnt, 0, sizeof(ctx->fuse_cnt));
121+
memset(ctx->fuse_cnt, 0, sizeof(ctx->fuse_cnt));
121122

122-
ctx->max_size = ctx->mtl_device.maxBufferLength;
123+
ctx->max_size = ctx->mtl_device.maxBufferLength;
123124

124-
strncpy(ctx->name, [[ctx->mtl_device name] UTF8String], sizeof(ctx->name) - 1);
125+
strncpy(ctx->name, [[ctx->mtl_device name] UTF8String], sizeof(ctx->name) - 1);
126+
}
125127
}
126128

127129
ctx->mtl_device_ref_count++;

0 commit comments

Comments
 (0)