Skip to content

Commit

Permalink
metal: Handle null returned from MTLCreateSystemDefaultDevice() (#11441)
Browse files Browse the repository at this point in the history
This fixes segmentation fault error when running tests when no metal
devices are available (for example, when not linked with Core Graphics
framework or otherwise).
  • Loading branch information
booxter authored Jan 27, 2025
1 parent caf773f commit acd38ef
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ggml/src/ggml-metal/ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@

if (ctx->mtl_device == nil) {
ctx->mtl_device = MTLCreateSystemDefaultDevice();
}

if (ctx->mtl_device) {
ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];

Expand Down Expand Up @@ -99,8 +101,10 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
ctx->mtl_device_ref_count--;

if (ctx->mtl_device_ref_count == 0) {
[ctx->mtl_device release];
ctx->mtl_device = nil;
if (ctx->mtl_device) {
[ctx->mtl_device release];
ctx->mtl_device = nil;
}
}
}

Expand Down

0 comments on commit acd38ef

Please sign in to comment.