Post

Replies

Boosts

Views

Activity

MLModelAsset(specification:blobMapping:) with mlprogram model: correct predictions but drastically slower inference than compiled .mlmodelc path
I'm distributing an encrypted .mlpackage to my app and want to load it entirely in memory without ever writing decrypted weights to disk. I tried MLModelAsset(specification:blobMapping:) as the path to achieve this, but ran into a significant inference performance gap compared to the compiled code path. What I'm trying to do The encrypted .enc file is a serialized FileWrapper of the full .mlpackage, sealed with AES-GCM. At runtime I decrypt it in memory, deserialize the FileWrapper, extract the spec and weight blob, and load via MLModelAsset: static func loadEncryptedPackage(url: URL, configuration: MLModelConfiguration) async throws -> MLModel { // AES-GCM decryption → decryptedData (full serialized .mlpackage) guard let wrapper = FileWrapper(serializedRepresentation: decryptedData) else { throw ... } guard let (specWrapper, specParent) = findSpecWrapper(in: wrapper), let spec = specWrapper.regularFileContents else { throw ... } var blobs: [URL: Data] = [:] collectBlobs(in: specParent, relativePath: "", excluding: specWrapper, into: &blobs) // keys built as URL(fileURLWithPath: rel), e.g. "weights/weight.bin" let asset = try MLModelAsset(specification: spec, blobMapping: blobs) let model = try await MLModel.load(asset: asset, configuration: configuration) // See observation #3 below — must retain these for the model's lifetime objc_setAssociatedObject(model, &retentionKey, Retainer(spec: spec, blobs: blobs), .OBJC_ASSOCIATION_RETAIN) return model } What I observed Predictions are accurate. The blobs are found, weights are applied, and the model produces correct results. Inference is drastically slower than the compiled code path. The same model loaded via MLModel.compileModel(at:) + MLModel.load(contentsOf:) runs inference much faster on the same device with the same MLModelConfiguration (computeUnits = .all). With MLModelAsset the slowdown is consistent across every prediction call, not just the first one. The spec and blob Data objects must stay alive for the model's lifetime. Without retaining them via objc_setAssociatedObject, inference produces NaN outputs or crashes. This suggests Core ML holds a reference back into those Data buffers beyond the load() call, rather than copying them into its own memory during loading. Using the exact blob URI from the spec as the blobMapping key triggers a compilation error. The spec (inspected via strings on the .mlmodel protobuf) stores blob references as @model_path/weights/weight.bin. When I key the blobMapping with URL(string: "@model_path/weights/weight.bin"), MLModel.load(asset:) throws: compiler error: Encountered an error while compiling a model: validator error: The in-memory ML Program must not have a blob file reference but found a reference to mem://weights/weight.bin. With other key formats (e.g. URL(fileURLWithPath: "weights/weight.bin")), this error does not appear — the model loads and predictions are accurate, but inference is slow as in observation #2. The working alternative (which I want to avoid) Decrypting to a temporary directory, calling MLModel.compileModel(at:), loading from the compiled .mlmodelc, then deleting the temp files produces fast inference. Same model, same device, same configuration. The only difference is the compilation step — and the fact that decrypted weights touch disk, which I want to avoid for security reasons. Questions Is MLModelAsset(specification:blobMapping:) expected to produce inference performance equivalent to loading from a compiled .mlmodelc? If not, is the performance gap fundamental to the API or something that can be addressed? Is there any supported way to load an mlprogram model with external weight blobs entirely in memory and achieve inference performance comparable to the compiled code path — i.e. without writing decrypted model data to disk at any point? The validator error "in-memory ML Program must not have a blob file reference" is a hard block when Core ML successfully resolves the blobs and attempts mlprogram compilation. Is this an intended constraint, and does it mean MLModelAsset(specification:blobMapping:) is not the right API for this use case?
1
1
425
Jun ’26
My app has a music mode. Is there a way to bypass all voice processing, even bypass the mic modes in control center while my app uses kAudioUnitSubType_VoiceProcessingIO?
If I set the kAUVoiceIOProperty_BypassVoiceProcessing parameter to true on the voice processing audio unit, it appears that the microphone mode setting in the Control Center still takes effect. For example, if the user has selected the voice isolation mode, the audio unit will still function in voice isolation mode. However, on the iPhone 14, the microphone mode is not displayed in the Control Center when kAUVoiceIOProperty_BypassVoiceProcessing is set to true, unlike on other iPhone models such as the iPhone 11 and iPhone 12. As a result, after setting kAUVoiceIOProperty_BypassVoiceProcessing to true, the voice processing unit will continue to use the last selected microphone mode for my app, and it cannot be changed on the iPhone 14. I have the following questions about this: Is it intended that microphone modes in the Control Center take precedence over the kAUVoiceIOProperty_BypassVoiceProcessing setting on the audio unit? My app has a music mode that captures music through mic. Is there a way to avoid all voice processing, even bypass the mic modes in control center? Why is the microphone mode not displayed in the Control Center on the iPhone 14 Pro when kAUVoiceIOProperty_BypassVoiceProcessing is set to true on the voice processing audio unit, while other iPhone models display it?
0
0
1k
Apr ’23
AVAudioEngine setup doesn't work with iPhone 14 Pro + Airpods Pro combination
I have an AVAudioEngine setup with VoiceProcessingEnabled that takes the input from mic and plays it on speaker. This works great with Airpods on other iPhone models except iPhone 14 (specifically tested on iPhone 14 Pro). In case of iPhone 14 Pro, there is no audio on the speaker at all. I am linking to a minimal sample app below to reproduce the issue. Please run this sample on iPhone 11 using Airpods Pro you will hear yourself speaking. But when you run the app on iPhone 14 Pro with Airpods Pro connected, you can't hear yourself. Any help will be appreciated. Cheers Sample app: https://www.icloud.com/iclouddrive/07bC_sAZW8MAA2u5lHeHOoFPg#AudioEngineAirpodsTest
0
0
972
Nov ’22
[iOS 16 Crash] Crash while getting mach port from CFMessagePortRef
I create a local CFMessagePortRef using CFMessagePortCreateLocal and then use CFMachPortGetPort() to try to get the mach port from it like below: NSString *portIdentifier = [[groupName stringByAppendingString:@"."] stringByAppendingString:sdkId]; NSString *portName = [[portIdentifier stringByAppendingString:@"."] stringByAppendingString:@"mach.port"]; CFMessagePortContext context = {0,(__bridge void *)self,nil,nil,nil}; self.sendPort = CFMessagePortCreateLocal(kCFAllocatorDefault, (__bridge CFStringRef)portName, &callback, &context, false); CFMachPortGetPort(ms->_port); It works till iOS 15 but crashes on iOS 16. Can anyone help? I have defined below definition of __CFMessagePort: struct __CFMessagePort {     CFRuntimeBase _base;     CFLock_t _lock;     CFStringRef _name;     CFMachPortRef _port;        /* immutable; invalidated */     CFMutableDictionaryRef _replies;     int32_t _convCounter;     int32_t _perPID;            /* zero if not per-pid, else pid */     CFMachPortRef _replyPort;        /* only used by remote port; immutable once created; invalidated */     CFRunLoopSourceRef _source;        /* only used by local port; immutable once created; invalidated */     dispatch_source_t _dispatchSource;  /* only used by local port; invalidated */     dispatch_queue_t _dispatchQ;    /* only used by local port */     CFMessagePortInvalidationCallBack _icallout;     CFMessagePortCallBack _callout;    /* only used by local port; immutable */     CFMessagePortCallBackEx _calloutEx;    /* only used by local port; immutable */     CFMessagePortContext _context;    /* not part of remote port; immutable; invalidated */ };
4
0
2.0k
Sep ’22
MLModelAsset(specification:blobMapping:) with mlprogram model: correct predictions but drastically slower inference than compiled .mlmodelc path
I'm distributing an encrypted .mlpackage to my app and want to load it entirely in memory without ever writing decrypted weights to disk. I tried MLModelAsset(specification:blobMapping:) as the path to achieve this, but ran into a significant inference performance gap compared to the compiled code path. What I'm trying to do The encrypted .enc file is a serialized FileWrapper of the full .mlpackage, sealed with AES-GCM. At runtime I decrypt it in memory, deserialize the FileWrapper, extract the spec and weight blob, and load via MLModelAsset: static func loadEncryptedPackage(url: URL, configuration: MLModelConfiguration) async throws -> MLModel { // AES-GCM decryption → decryptedData (full serialized .mlpackage) guard let wrapper = FileWrapper(serializedRepresentation: decryptedData) else { throw ... } guard let (specWrapper, specParent) = findSpecWrapper(in: wrapper), let spec = specWrapper.regularFileContents else { throw ... } var blobs: [URL: Data] = [:] collectBlobs(in: specParent, relativePath: "", excluding: specWrapper, into: &blobs) // keys built as URL(fileURLWithPath: rel), e.g. "weights/weight.bin" let asset = try MLModelAsset(specification: spec, blobMapping: blobs) let model = try await MLModel.load(asset: asset, configuration: configuration) // See observation #3 below — must retain these for the model's lifetime objc_setAssociatedObject(model, &retentionKey, Retainer(spec: spec, blobs: blobs), .OBJC_ASSOCIATION_RETAIN) return model } What I observed Predictions are accurate. The blobs are found, weights are applied, and the model produces correct results. Inference is drastically slower than the compiled code path. The same model loaded via MLModel.compileModel(at:) + MLModel.load(contentsOf:) runs inference much faster on the same device with the same MLModelConfiguration (computeUnits = .all). With MLModelAsset the slowdown is consistent across every prediction call, not just the first one. The spec and blob Data objects must stay alive for the model's lifetime. Without retaining them via objc_setAssociatedObject, inference produces NaN outputs or crashes. This suggests Core ML holds a reference back into those Data buffers beyond the load() call, rather than copying them into its own memory during loading. Using the exact blob URI from the spec as the blobMapping key triggers a compilation error. The spec (inspected via strings on the .mlmodel protobuf) stores blob references as @model_path/weights/weight.bin. When I key the blobMapping with URL(string: "@model_path/weights/weight.bin"), MLModel.load(asset:) throws: compiler error: Encountered an error while compiling a model: validator error: The in-memory ML Program must not have a blob file reference but found a reference to mem://weights/weight.bin. With other key formats (e.g. URL(fileURLWithPath: "weights/weight.bin")), this error does not appear — the model loads and predictions are accurate, but inference is slow as in observation #2. The working alternative (which I want to avoid) Decrypting to a temporary directory, calling MLModel.compileModel(at:), loading from the compiled .mlmodelc, then deleting the temp files produces fast inference. Same model, same device, same configuration. The only difference is the compilation step — and the fact that decrypted weights touch disk, which I want to avoid for security reasons. Questions Is MLModelAsset(specification:blobMapping:) expected to produce inference performance equivalent to loading from a compiled .mlmodelc? If not, is the performance gap fundamental to the API or something that can be addressed? Is there any supported way to load an mlprogram model with external weight blobs entirely in memory and achieve inference performance comparable to the compiled code path — i.e. without writing decrypted model data to disk at any point? The validator error "in-memory ML Program must not have a blob file reference" is a hard block when Core ML successfully resolves the blobs and attempts mlprogram compilation. Is this an intended constraint, and does it mean MLModelAsset(specification:blobMapping:) is not the right API for this use case?
Replies
1
Boosts
1
Views
425
Activity
Jun ’26
My app has a music mode. Is there a way to bypass all voice processing, even bypass the mic modes in control center while my app uses kAudioUnitSubType_VoiceProcessingIO?
If I set the kAUVoiceIOProperty_BypassVoiceProcessing parameter to true on the voice processing audio unit, it appears that the microphone mode setting in the Control Center still takes effect. For example, if the user has selected the voice isolation mode, the audio unit will still function in voice isolation mode. However, on the iPhone 14, the microphone mode is not displayed in the Control Center when kAUVoiceIOProperty_BypassVoiceProcessing is set to true, unlike on other iPhone models such as the iPhone 11 and iPhone 12. As a result, after setting kAUVoiceIOProperty_BypassVoiceProcessing to true, the voice processing unit will continue to use the last selected microphone mode for my app, and it cannot be changed on the iPhone 14. I have the following questions about this: Is it intended that microphone modes in the Control Center take precedence over the kAUVoiceIOProperty_BypassVoiceProcessing setting on the audio unit? My app has a music mode that captures music through mic. Is there a way to avoid all voice processing, even bypass the mic modes in control center? Why is the microphone mode not displayed in the Control Center on the iPhone 14 Pro when kAUVoiceIOProperty_BypassVoiceProcessing is set to true on the voice processing audio unit, while other iPhone models display it?
Replies
0
Boosts
0
Views
1k
Activity
Apr ’23
AVAudioEngine setup doesn't work with iPhone 14 Pro + Airpods Pro combination
I have an AVAudioEngine setup with VoiceProcessingEnabled that takes the input from mic and plays it on speaker. This works great with Airpods on other iPhone models except iPhone 14 (specifically tested on iPhone 14 Pro). In case of iPhone 14 Pro, there is no audio on the speaker at all. I am linking to a minimal sample app below to reproduce the issue. Please run this sample on iPhone 11 using Airpods Pro you will hear yourself speaking. But when you run the app on iPhone 14 Pro with Airpods Pro connected, you can't hear yourself. Any help will be appreciated. Cheers Sample app: https://www.icloud.com/iclouddrive/07bC_sAZW8MAA2u5lHeHOoFPg#AudioEngineAirpodsTest
Replies
0
Boosts
0
Views
972
Activity
Nov ’22
[iOS 16 Crash] Crash while getting mach port from CFMessagePortRef
I create a local CFMessagePortRef using CFMessagePortCreateLocal and then use CFMachPortGetPort() to try to get the mach port from it like below: NSString *portIdentifier = [[groupName stringByAppendingString:@"."] stringByAppendingString:sdkId]; NSString *portName = [[portIdentifier stringByAppendingString:@"."] stringByAppendingString:@"mach.port"]; CFMessagePortContext context = {0,(__bridge void *)self,nil,nil,nil}; self.sendPort = CFMessagePortCreateLocal(kCFAllocatorDefault, (__bridge CFStringRef)portName, &callback, &context, false); CFMachPortGetPort(ms->_port); It works till iOS 15 but crashes on iOS 16. Can anyone help? I have defined below definition of __CFMessagePort: struct __CFMessagePort {     CFRuntimeBase _base;     CFLock_t _lock;     CFStringRef _name;     CFMachPortRef _port;        /* immutable; invalidated */     CFMutableDictionaryRef _replies;     int32_t _convCounter;     int32_t _perPID;            /* zero if not per-pid, else pid */     CFMachPortRef _replyPort;        /* only used by remote port; immutable once created; invalidated */     CFRunLoopSourceRef _source;        /* only used by local port; immutable once created; invalidated */     dispatch_source_t _dispatchSource;  /* only used by local port; invalidated */     dispatch_queue_t _dispatchQ;    /* only used by local port */     CFMessagePortInvalidationCallBack _icallout;     CFMessagePortCallBack _callout;    /* only used by local port; immutable */     CFMessagePortCallBackEx _calloutEx;    /* only used by local port; immutable */     CFMessagePortContext _context;    /* not part of remote port; immutable; invalidated */ };
Replies
4
Boosts
0
Views
2.0k
Activity
Sep ’22