Ihar Hrachyshka commited on
Commit
4e38ed4
·
1 Parent(s): 9da4d68

metal: Handle null returned from MTLCreateSystemDefaultDevice() (llama/11441)

Browse files

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).

Files changed (1) hide show
  1. ggml/src/ggml-metal/ggml-metal.m +6 -2
ggml/src/ggml-metal/ggml-metal.m CHANGED
@@ -64,7 +64,9 @@ static id<MTLDevice> ggml_backend_metal_device_acq(struct ggml_backend_metal_dev
64
 
65
  if (ctx->mtl_device == nil) {
66
  ctx->mtl_device = MTLCreateSystemDefaultDevice();
 
67
 
 
68
  ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
69
  ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
70
 
@@ -99,8 +101,10 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
99
  ctx->mtl_device_ref_count--;
100
 
101
  if (ctx->mtl_device_ref_count == 0) {
102
- [ctx->mtl_device release];
103
- ctx->mtl_device = nil;
 
 
104
  }
105
  }
106
 
 
64
 
65
  if (ctx->mtl_device == nil) {
66
  ctx->mtl_device = MTLCreateSystemDefaultDevice();
67
+ }
68
 
69
+ if (ctx->mtl_device) {
70
  ctx->has_simdgroup_reduction = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
71
  ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
72
 
 
101
  ctx->mtl_device_ref_count--;
102
 
103
  if (ctx->mtl_device_ref_count == 0) {
104
+ if (ctx->mtl_device) {
105
+ [ctx->mtl_device release];
106
+ ctx->mtl_device = nil;
107
+ }
108
  }
109
  }
110