From 8f75eefe45d735169da98adb2c2e48da1b5d8e76 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Thu, 21 Nov 2024 03:41:47 -0800 Subject: [PATCH] Fix compilation crash on iOS<16 Summary: Added conditional compilation to mitigate a crash on iOS<16 where the used function is not present. Fixes https://github.com/pytorch/executorch/issues/6984 Bonus: report back the error in case of compilation errors, which was missing Differential Revision: D66294248 --- .../runtime/delegate/ETCoreMLModelCompiler.mm | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/backends/apple/coreml/runtime/delegate/ETCoreMLModelCompiler.mm b/backends/apple/coreml/runtime/delegate/ETCoreMLModelCompiler.mm index 36249aee6ce..c50bf3002fa 100644 --- a/backends/apple/coreml/runtime/delegate/ETCoreMLModelCompiler.mm +++ b/backends/apple/coreml/runtime/delegate/ETCoreMLModelCompiler.mm @@ -26,25 +26,38 @@ + (nullable NSURL *)compileModelAtURL:(NSURL *)modelURL #else __block NSError *localError = nil; __block NSURL *result = nil; - - dispatch_semaphore_t sema = dispatch_semaphore_create(0); - [MLModel compileModelAtURL:modelURL completionHandler:^(NSURL * _Nullable tempURL, NSError * _Nullable compilationError) { - result = [tempURL copy]; - localError = compilationError; - dispatch_semaphore_signal(sema); - }]; - - long status = dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(maxWaitTimeInSeconds * NSEC_PER_SEC))); - if (status != 0) { + + if (@available(iOS 16, macOS 13, watchOS 9, tvOS 16, *)) { + dispatch_semaphore_t sema = dispatch_semaphore_create(0); + [MLModel compileModelAtURL:modelURL completionHandler:^(NSURL * _Nullable tempURL, NSError * _Nullable compilationError) { + result = [tempURL copy]; + localError = compilationError; + dispatch_semaphore_signal(sema); + }]; + + long status = dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(maxWaitTimeInSeconds * NSEC_PER_SEC))); + if (status != 0) { + ETCoreMLLogErrorAndSetNSError(error, + ETCoreMLErrorCompilationFailed, + "%@: Failed to compile model in %f seconds.", + NSStringFromClass(ETCoreMLModelCompiler.class), + maxWaitTimeInSeconds); + return nil; + } + } else { + result = [MLModel compileModelAtURL:modelURL error:&localError]; + } + + if (localError) { ETCoreMLLogErrorAndSetNSError(error, - ETCoreMLErrorCompilationFailed, - "%@: Failed to compile model in %f seconds.", - NSStringFromClass(ETCoreMLModelCompiler.class), - maxWaitTimeInSeconds); + ETCoreMLErrorCompilationFailed, + "%@: Failed to compile model, error: %@", + NSStringFromClass(ETCoreMLModelCompiler.class), + localError); return nil; + } else { + return result; } - - return result; #endif }