@@ -139,11 +139,8 @@ int ggml_metal_pipeline_max_theads_per_threadgroup(ggml_metal_pipeline_t pipelin
139139};
140140
141141ggml_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
287292void 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