← All postsBlog

A 200× slowdown: how Core ML model encryption broke ANE acceleration

An AppDelegate engineer working at his monitor

The problem

We run pose estimation on device. Video comes in, and for every frame we analyse, the model predicts a set of body keypoints — no upload, no server, nothing leaves the phone. Before encryption this was fine: the keypoint stage for a 4K clip at 60 fps, up to about a minute long, took around 30 seconds. (The whole analysis has three stages and takes about two minutes; this post is about the keypoint stage.) Every timing in this post was measured on an iPhone 17 Pro.

Then we turned on Core ML model encryption, and inference went to 9.48 seconds per frame.

Nothing failed. No exception, no warning, no error surfaced to us. The model loaded, produced correct keypoints, and was simply 237× slower. Correct output is the worst kind of symptom, because it removes the thing you would normally chase.

Our approach

Instruments told us where the time went before it told us why: the CPU was saturated and the Neural Engine was idle. The model that had been running on the ANE was now running as plain CPU work. So the question stopped being "what got slower" and became "why did Core ML stop using the ANE."

The natural next step was MLComputePlan.load, which reports how Core ML intends to distribute a model across CPU, GPU and ANE. It crashed. Not "returned an empty plan" — crashed, with mremap_encrypted and ENOMEM, on every encrypted model we gave it.

That crash was the first solid lead. Something in the encrypted artifact could not be memory-mapped for inspection, which suggested the compilation step that targets the ANE might be hitting the same wall.

We isolated the variables, because two things had changed at once — the model was now both precompiled on a different machine and encrypted:

Build Inference per frame ANE used
Compiled on device 0.04 s yes
Precompiled on another machine 0.04 s yes
Precompiled and encrypted on another machine 9.48 s no

Precompilation was innocent. Encryption was not. One variable, two builds, a reproducible 237× gap.

Per-frame inference and wall-clock analysis time do not multiply out here, because we only run the model on frames inside detected segments — the rest of a video costs decode and tracking, not inference.

The root cause: --encrypt embeds an encryptionInfo artifact in the .mlmodelc. Ahead-of-time ANE compilation — ANECCompile — needs to read model.mil to build the execution plan for the Neural Engine, and it cannot read it through the encryption layer. It fails with CompilationFailure. Core ML then does exactly what it is designed to do: it falls back to a compute unit that works, and returns the right answer. Silently.

Worth being precise about scope, because we were wrong about this early on: Apple built Core ML encryption to work with the ANE, and our other encrypted models run there fine. The trigger is narrower — this model needs multi-backend partitioning, because parts of its graph are not ANE-compatible, and partitioning requires the same program-structure introspection that encryption blocks. A model that fits entirely on the ANE never needs to partition, so it never hits this. A model that does gets pushed off the ANE altogether.

This is a build-pipeline decision with a runtime performance consequence, and nothing connects the two for you.

The hard decisions and trade-offs

Shipping the model in the clear was never on the table, so the question was never whether to encrypt. It was which encryption, and what to do about the ANE.

Core ML model encryption was our first choice and we wanted to keep it. Xcode generates an encryption key (.mlmodelkey) tied to your development team, xcrun coremlcompiler compile --encrypt applies it at build time, and the device fetches the key from Apple's servers on first load. The model is never in the clear in the bundle or on disk; it is only decrypted in memory, at load time. Nothing we build ourselves gets that property for free.

Envelope encryption against a KMS was the fallback. Encrypt the model with a data key, wrap that key with a master key held in a key management service, fetch and unwrap it on the device, decrypt to a plaintext .mlmodelc on disk, then hand Core ML the file URL. Core ML never knows the model was encrypted, so ANECCompile sees a readable model.mil and the ANE path survives. It would have solved this specific problem.

The reason it stayed the fallback rather than the fix is what it costs elsewhere. The decrypted model has to exist as a complete plaintext directory on disk at load time — for the compiled .mlmodelc we load, decrypting into memory was not an option, because Core ML memory-maps the weights off the file. First launch depends on our own key service instead of Apple's. Key rotation, cache invalidation across app updates and cleanup of the plaintext copy all become ours. We were willing to take that on if we had to. We wanted to find out first whether we had to.

Which meant restructuring the model so the ANE path never needs the operations that break — no partitioning, no introspection, no problem. That is the path we took, with a cost we accepted up front: work that used to live inside the model graph would now live in our Swift code, and we would own it forever.

Our ML engineer rebuilt the model as an ANE-only graph, stripping out the operations the Neural Engine could not handle — reverse, reduce_argmax, select, stack, cast. Those were the decode head. What came out the other side was no longer a model that returns coordinates. It returns raw tensors: for each keypoint, a probability distribution along the X axis and one along the Y axis, plus an occlusion score. Turning those into coordinates became our job.

So did test-time augmentation.

What TTA is, and why it made this harder

Test-time augmentation is a cheap accuracy trick: run inference more than once on transformed versions of the same input, then fuse the results. We run every frame twice — once as-is, once mirrored horizontally. The mirrored view gives the model a second, slightly different look at the same pose, and averaging the two distributions cancels a lot of per-view bias. It matters most exactly where the model is weakest: occluded joints and left/right confusions.

Fusing them is not a plain average. A mirrored image has its anatomy swapped, so keypoint k in the normal pass corresponds to a different keypoint in the flipped pass — an index mapping handles that. And the mirrored X distribution runs backwards, so it has to be reversed before it lines up with the normal one. Y is untouched: a horizontal flip does not move anything vertically.

That is four tensors to reconcile per frame, with an index remap and an axis reversal in the middle.

We wrote it the obvious way first: convert the Float16 values to Float32, reverse the flipped X row, average, take the argmax. Per axis that is well over a hundred thousand elements, and four of those per frame.

It cost 41 ms per frame — on its own, more than the entire inference had cost on the ANE. The reason is not the element count. It is how a scalar loop handles them: each Float16 value converted one at a time, a bounds check and a branch per element, and the vector units left idle.

We had the Neural Engine back and the keypoint stage was still taking two to three minutes. We had moved the bottleneck, not removed it.

The second trade-off was readability. Fixing this meant abandoning idiomatic Swift for raw Float16 pointers into the model output and Accelerate primitives, plus an assumption the compiler cannot check for us — that rows are contiguous, strideW == 1. That assumption is load-bearing, and it is a comment, not a type. We took it, and documented it where someone will actually read it.

The outcome

The fix was Accelerate, Apple's framework for SIMD work — single instruction, multiple data. A scalar loop over a row of Float16 values does one operation per value. A SIMD instruction operates on a whole register at once: at 128 bits that is eight Float16 values per instruction. The saving is not only instruction count — you also drop the per-element bounds check and the loop branch, read straight through contiguous memory, and convert the whole row to Float32 in bulk instead of one value at a time. Accelerate wraps hand-tuned SIMD routines that pick the right path for the chip they run on, so you get this without writing intrinsics yourself.

The decode loop became up to four Accelerate calls per keypoint per axis (Y skips the reversal):

  • vImageConvert_Planar16FtoPlanarF — a whole row of Float16 values to Float32 in one call
  • vDSP_vrvrs — reverse the mirrored row in place
  • vDSP_vasm — fused = (normal + flipped) × 0.5 in a single pass
  • vDSP_maxvi — max value and its index together, so the argmax is free

Scratch buffers are allocated once per call and reused across all keypoints, and kept local rather than shared so frames can decode concurrently without a lock. Occlusion is only one value per keypoint, so it stays scalar — vectorizing it would cost more in setup than it saves.

Two more things came out of the same pass. The mirrored image is produced with .oriented(.upMirrored) on a CIImage, which happens on the GPU with no CPU copy. And the two Vision passes run concurrently through async let rather than Task.detached, which preserves cancellation inheritance: the passes inherit it from the parent task, and if the pass we await throws, the other one is cancelled automatically when the scope exits.

The keypoint stage for a 4K clip at 60 fps, up to about a minute long, is back to roughly 30 seconds — with encryption on, and with TTA now running two inference passes per frame instead of one. The honest framing is not that we made anything faster. We got the Neural Engine back, paid for a second inference pass with the accuracy TTA buys, and came out level with where we started.

What broke along the way

Once the encrypted ANE-only model actually ran, the output needed tuning. We were no longer comparing against a model that hands back coordinates — we were comparing our Swift decode against what the old graph used to produce internally, and the two had to agree. Getting the left/right index mapping, the X-axis reversal and the bin-to-pixel scaling right took several rounds of checking keypoints frame by frame against known-good runs.

Stack & takeaways

Stack: Core ML and Vision for inference, Accelerate (vDSP + vImage) for the SIMD-vectorized decode, Core Image for the GPU-side mirror, Swift Concurrency for the parallel passes and cancellation.

  • Silent fallbacks cost more than crashes. Core ML never told us the ANE was unavailable. It did the work elsewhere and returned the right answer, 237× slower.
  • Verify device assignment explicitly. MLComputePlan is the tool for this — and its crash on encrypted models was more informative than any log line we had.
  • Reduce to one variable before theorising. Two builds differing only by --encrypt, with timings, turned a vague mystery into a filable result. Everything before that step was guessing.
  • Test the artifact you ship, not the one you build. Encryption is applied at compile time; a debug build will never show you this class of problem.
  • Moving work out of the model graph into app code is a legitimate option — as long as you accept that you now maintain it, and that a naive implementation can eat the entire win. Ours did, twice over, before Accelerate.

Related: Resumable ML analysis of 4K/60 video