Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions backends/apple/coreml/runtime/delegate/ETCoreMLModelCompiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have the timeout logic, but it was crashing before, so I think we can go with this for now to mitigate the issue and improve it later.

}

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
}

Expand Down
Loading