Skip to content

Commit 2324beb

Browse files
committed
metal : fix error handling
ggml-ci
1 parent be6f519 commit 2324beb

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,17 @@ ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) {
108108
res->dev = dev;
109109
res->lib = ggml_metal_device_get_library(dev);
110110
if (res->lib == NULL) {
111-
GGML_LOG_ERROR("%s: error: failed to initialize Metal library\n", __func__);
112-
return NULL;
111+
GGML_LOG_WARN("%s: the device does not have a precompiled Metal library - this is unexpected\n", __func__);
112+
GGML_LOG_WARN("%s: will try to compile it on the fly\n", __func__);
113+
114+
res->lib = ggml_metal_library_init(dev);
115+
if (res->lib == NULL) {
116+
GGML_LOG_ERROR("%s: error: failed to initialize the Metal library\n", __func__);
117+
118+
free(res);
119+
120+
return NULL;
121+
}
113122
}
114123

115124
const struct ggml_metal_device_props * props_dev = ggml_metal_device_get_props(dev);

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ int ggml_metal_pipeline_max_theads_per_threadgroup(ggml_metal_pipeline_t pipelin
139139
};
140140

141141
ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
142-
ggml_metal_library_t res = calloc(1, sizeof(struct ggml_metal_library));
143-
144-
res->device = ggml_metal_device_get_obj(dev);
145-
146-
res->pipelines = ggml_metal_pipelines_init();
142+
id<MTLLibrary> library = nil;
143+
id<MTLDevice> device = ggml_metal_device_get_obj(dev);
147144

148145
// load library
149146
//
@@ -166,7 +163,6 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
166163
extern const char ggml_metallib_end[];
167164

168165
src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
169-
170166
#else
171167

172168
#ifdef SWIFT_PACKAGE
@@ -213,9 +209,10 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
213209
NSURL * libURL = [NSURL fileURLWithPath:path_lib];
214210
GGML_LOG_INFO("%s: loading '%s'\n", __func__, [path_lib UTF8String]);
215211

216-
res->obj = [res->device newLibraryWithURL:libURL error:&error];
212+
library = [device newLibraryWithURL:libURL error:&error];
217213
if (error) {
218214
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
215+
return nil;
219216
}
220217
} else {
221218
GGML_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);
@@ -241,11 +238,12 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
241238
src = [NSString stringWithContentsOfFile:path_source encoding:NSUTF8StringEncoding error:&error];
242239
if (error) {
243240
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
241+
return nil;
244242
}
245243
}
246244
#endif
247245

248-
if (!res->obj) {
246+
if (!library) {
249247
@autoreleasepool {
250248
// dictionary of preprocessor macros
251249
NSMutableDictionary * prep = [NSMutableDictionary dictionary];
@@ -263,9 +261,10 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
263261

264262
//[options setFastMathEnabled:false];
265263

266-
res->obj = [res->device newLibraryWithSource:src options:options error:&error];
264+
library = [device newLibraryWithSource:src options:options error:&error];
267265
if (error) {
268266
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
267+
return nil;
269268
}
270269

271270
#if !__has_feature(objc_arc)
@@ -281,16 +280,26 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) {
281280
GGML_LOG_INFO("%s: loaded in %.3f sec\n", __func__, (ggml_time_us() - t_start) / 1e6);
282281
}
283282

283+
ggml_metal_library_t res = calloc(1, sizeof(struct ggml_metal_library));
284+
285+
res->obj = library;
286+
res->device = device;
287+
res->pipelines = ggml_metal_pipelines_init();
288+
284289
return res;
285290
}
286291

287292
void ggml_metal_library_free(ggml_metal_library_t lib) {
288-
if (lib) {
289-
[lib->obj release];
293+
if (!lib) {
294+
return;
295+
}
290296

291-
ggml_metal_pipelines_free(lib->pipelines);
297+
if (lib->obj) {
298+
[lib->obj release];
292299
}
293300

301+
ggml_metal_pipelines_free(lib->pipelines);
302+
294303
free(lib);
295304
}
296305

@@ -322,6 +331,7 @@ ggml_metal_pipeline_t ggml_metal_library_compile_pipeline(ggml_metal_library_t l
322331
if (!mtl_function) {
323332
ggml_critical_section_end();
324333

334+
GGML_LOG_ERROR("%s: error: failed to compile pipeline: base = '%s', name = '%s'\n", __func__, base, name);
325335
GGML_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
326336

327337
return nil;
@@ -466,7 +476,6 @@ ggml_metal_device_t ggml_metal_device_init(void) {
466476
dev->library = ggml_metal_library_init(dev);
467477
if (!dev->library) {
468478
GGML_LOG_ERROR("%s: error: failed to create library\n", __func__);
469-
return NULL;
470479
}
471480

472481
// --------------------------------------------------

0 commit comments

Comments
 (0)