Skip to content

ort: load single-subgraph Core ML sessions natively - #30

Open
moltenglue wants to merge 1 commit into
zoompilot:mainfrom
moltenglue:ios-thermal-and-driving-mode
Open

moltenglue wants to merge 1 commit into
zoompilot:mainfrom
moltenglue:ios-thermal-and-driving-mode

Conversation

@moltenglue

@moltenglue moltenglue commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Loads single-subgraph Core ML .mlmodelc caches directly through CoreML.MLModel instead of ONNX Runtime's C API wrapper, eliminating duplicate runtime memory while preserving full FP16/FP32 precision (allowLowPrecisionAccumulationOnGPU = false):

  • Build-time native decision (sessions.json): After compiling and verifying the build on ONNX Runtime (OrtBackend.checkCoreML), validates each Core ML cache with CoreMLSession.canRunNatively(...) (single .mlmodelc, 1:1 input/output names, static shapes, and types) and records "native": true/false directly into sessions.json.
    • Load respects "native" (true → CoreML-Native(...), false → OrtSession, legacy caches without "native" probe once so existing v0.10.4 caches still benefit).
  • No RequireStaticInputShapes: Keeps ORT's SessionPlan options unchanged from main so cache directories stay 0_dynamic_mlprogram across --no-native-coreml toggles and existing artifacts.
  • Zero-copy bindings: Binds MLMultiArray(dataPointer:...) inputs and MLPredictionOptions.outputBackings over EngineCore-owned buffers (skipping output copies when Core ML writes directly into the backing pointer).
  • --no-native-coreml CLI flag: Falls back to OrtSession cleanly on any model or CLI invocation.
  • Preserves v0.10.4 pipelining & keep-warm: CoreMLSession / CoreMLBinding conform to EngineSession / EngineBinding, so runAhead() and CPUKeepWarm.workStarted()/workEnded() work identically.

Rebased onto main (214c31c / v0.10.4), picking up d750eed and c523384. iOS UI changes (Driving Mode, Benchmark Dim Screen, and Core ML Placement) have been broken out into a separate PR.


Verification (Apple M5 Max, macOS 26.7.3, Release, 20 Hz, v0.10.4)

With v0.10.4 running all 966 ops (vision 502/502 + policy 464/464) on the Neural Engine under --device ane-both:

Fairy Tale v1.1 (--device ane-both) Sessions Loaded Peak Footprint Max RSS Mean / P99 Latency Over 35 ms
ORT (--no-native-coreml) CoreML(CPUAndNeuralEngine) × 2 759.9 MB 783.1 MB 11.1 / 11.9 ms 0
Native Core ML CoreML-Native(CPUAndNeuralEngine) × 2 62.0 MB (−91.8%) 77.1 MB (−90.2%) 11.2 / 11.9 ms 0
  • Verified sessions.json records "native": true at build time and a subsequent --no-native-coreml run keeps a single compiled_model.mlmodelc per cache (0_dynamic_mlprogram).
  • swift test --package-path JetlinkKit --filter OrtBackendTests: 11/11 passed (covers split-graph rejection, RequireStaticInputShapes == nil, native CPUOnly execution, "native": false, and nativeCoreML: false).
  • xcrun swift-format lint --strict --configuration macos/.swift-format passes across JetlinkKit/Sources and JetlinkKit/Tests.

How to Test

swift build -c release --package-path JetlinkKit --product jetlink-server
JetlinkKit/.build/release/jetlink-server bench --device ane-both --seconds 30
JetlinkKit/.build/release/jetlink-server bench --device ane-both --no-native-coreml --seconds 30

zephleggett added a commit that referenced this pull request Nov 30, 2026
The keep-warm thread was busy from a frame's start until a second after
the last one, through the engine's own work. With the whole model on the
Neural Engine that took time from the frame it was there for. It now rests
from the start of the engine's work to its end (run and runAhead) and is
busy only in the gaps between frames, which also halves its CPU time.

Measured with Cinque Terre V3 at 20 Hz on Neural Engine Only: an M1 Pro
went from 30.1 ms mean and 35.0 p99 to 23.9 and 27.2, an iPhone 13 mini
from 38.0 and 41.8 to 32.9 and 36.8 over three minutes from cool. The
iPhone once hot slid to 48 ms before and holds 34 now. Neural Engine + GPU
on the M1 Pro is unchanged (27.9 to 27.8).

The idea is moltenglue's, from #30.

Co-authored-by: moltenglue <257664494+moltenglue@users.noreply.github.com>
@zephleggett

Copy link
Copy Markdown
Collaborator

Thanks for this and for the detailed numbers. I benchmarked the pieces separately on an M1 Pro and an iPhone 13 mini to see where the gains come from.

The latency win is the keep-warm thread resting while a frame runs. On its own it took Neural Engine Only from 30.1 to 23.9 ms on the M1 Pro and from 38.0 to 32.9 ms on the iPhone. That is on main now in d750eed, with you as co-author.

The native Core ML session does not change frame time here, but it cuts the memory peak while loading (729 MB to 82 MB on the iPhone), and I would like to take it. Two things first: decide native-or-not at build time and record it in sessions.json, and drop RequireStaticInputShapes. That option renames onnxruntime's cache folder, so an existing cache recompiles silently and then never loads natively again.

I could not measure a gain from the IOSurface pool, and the MLState rewriter never ran on the shipped models because their state tensors are float32.

Would you be up for rebasing this down to the native session? The iOS screens would be easier to review as their own PR.

@moltenglue
moltenglue force-pushed the ios-thermal-and-driving-mode branch from b5c95b7 to d90103d Compare November 30, 2026 13:10
@moltenglue moltenglue changed the title Apple Native Core ML runner, thermal/CPU controls, and iOS OLED Driving Mode ort: load single-subgraph Core ML sessions natively Nov 30, 2026
@moltenglue

Copy link
Copy Markdown
Contributor Author

Rebased onto main (214c31c / v0.10.4) and narrowed down to the native Core ML session:

  • Decided native eligibility at build time in OrtBackend.checkCoreML and recorded "native": true/false per Core ML session in sessions.json (legacy v0.10.4 caches without "native" still probe once on load).
  • Removed RequireStaticInputShapes, CoreMLSurfacePool, and CoreMLStateRewriter.
  • Kept EngineSession/EngineBinding compatible with v0.10.4's runAhead() and CPUKeepWarm.workStarted()/workEnded().
  • Verified on v0.10.4 ane-both (Fairy Tale v1.1, 966/966 ANE): 62.0 MB peak footprint / 77.1 MB RSS (11.2 / 11.9 ms) vs. ORT 759.9 MB peak / 783.1 MB RSS (11.1 / 11.9 ms), with 0_dynamic_mlprogram keeping a single .mlmodelc across --no-native-coreml.

Broken the iOS UI changes (OLED Driving Mode, Benchmark Dim Screen, reliable idle timer, and Core ML Placement) out into #31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants