I've hit a very similar issue with CoreML model loading hanging on the MLE5ProgramLibrary.lazyInitQueue after OS updates. A few things that helped me work around it:
1. Pre-compile to .mlmodelc instead of loading .mlpackage at runtime
The AOT recompilation path (which is what's hanging) gets triggered when the on-device compiled cache is invalidated by the OS update. If you ship a pre-compiled .mlmodelc built with the matching Xcode/SDK version, it often skips recompilation entirely:
// Compile once at build time or first launch
let compiledURL = try MLModel.compileModel(at: mlpackageURL)
// Then load from compiled
let model = try MLModel(contentsOf: compiledURL, configuration: config)
2. Load on a background thread with a timeout
Since the hang is on a serial dispatch queue and the C++ exception bypasses Swift error handling, wrapping the load in a Task with a timeout at least lets you fail gracefully instead of getting watchdog-killed:
let loadTask = Task {
try MLModel(contentsOf: modelURL, configuration: config)
}
let result = try await withThrowingTaskGroup(of: MLModel.self) { group in
group.addTask { try await loadTask.value }
group.addTask {
try await Task.sleep(for: .seconds(30))
loadTask.cancel()
throw CancellationError()
}
return try await group.next()!
}
3. Delete the CoreML cache
The stale AOT cache seems to be the trigger. Clearing Library/Caches/com.apple.coreml before loading sometimes forces a clean recompilation that succeeds. Obviously not ideal for production, but useful for diagnosing whether it's a cache corruption issue vs. a compiler bug.
Strongly agree this should be filed as a Feedback — the fact that a C++ exception in espresso/BNNS hangs rather than propagating as an NSError is itself a bug regardless of the AOT issue.
Topic:
Machine Learning & AI
SubTopic:
Core ML
Tags: