Add coreml_compute_plan.py: report which CoreML ops dispatch to ANE / GPU / CPU#19252
Add coreml_compute_plan.py: report which CoreML ops dispatch to ANE / GPU / CPU#19252john-rocky wants to merge 2 commits into
Conversation
CoreML decides at compile/load time which device each MIL operation will execute on; that decision is exposed through MLComputePlan in coremltools 9.0+. This script wraps it so users can answer 'why isn't my model running on the ANE?' without writing Swift, which is the recurring question behind issues like pytorch#4091, pytorch#11541, and pytorch#8439. Inputs supported: * .pte — extracts every Core ML partition first. * .mlpackage — compiles to .mlmodelc in a tempdir. * .mlmodelc — analyzed directly. Reports per-op dispatch (ANE / GPU / CPU), an aggregate breakdown, and optionally the op types that did not get assigned to the ANE (--show_non_ane). Authored with Claude.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19252
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: apple" |
|
I thought what op runs where is decided at compile time. Is this being extracted from AOT compile or just from lowering? |
|
@kimishpatel Compile time, not runtime — Quoting coremltools' own docs:
So no AOT extraction or runtime profiling involved; just a wrapper that walks |
| ) | ||
|
|
||
|
|
||
| def _extract_models_from_pte(pte_path: str, out_dir: str) -> List[str]: |
There was a problem hiding this comment.
Can we reuse utilties in the extract_coreml_model script in the same folder?
There was a problem hiding this comment.
Done — _extract_models_from_pte is gone. extract_coreml_models.extract_coreml_models now takes an optional out_dir and returns the list of extracted paths, and coreml_compute_plan.py imports it via the executorch.examples.apple.coreml.scripts namespace.
I think it's being extracted from modelc compilation (so won't work on linux), see the _ensure_compile call. |
| import coremltools as ct | ||
| import torch | ||
|
|
||
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
There was a problem hiding this comment.
Done — the sys.path.insert is gone. The test imports through the same namespace path (from executorch.examples.apple.coreml.scripts.coreml_compute_plan import ...).
|
Looks good @john-rocky ! Main comment is to reuse or consolidate the existing utilities in extract_coreml_models script in the same folder. Can you also test this works with multi-function? |
Move the .pte extraction logic into extract_coreml_models.extract_coreml_models, which now takes an optional out_dir and returns the list of extracted paths; coreml_compute_plan.py imports and uses it instead of carrying its own copy. Multifunction .mlpackage inputs are now analyzed function-by-function: each function is projected as the `main` of a temp single-function copy so MLComputePlan.load_from_path covers it (coremltools 9.0 only exposes the plan for the default function otherwise). test_coreml_compute_plan.py uses the executorch.examples.apple.coreml.scripts namespace import directly instead of mutating sys.path, and adds two tests confirming both functions of a multifunction package are surfaced.
|
Thanks for the review! Addressed both inline comments and added real multifunction support (rather than just a test that pins the limitation). What changed (
Tests The two new tests build a multifunction package ( Net diff: +154 / −122 across 3 files. |
Summary
CoreML decides at compile/load time which device each MIL operation will
execute on, and coremltools 9.0+ exposes that through
MLComputePlan.The recurring question on the issue tracker is "why isn't my model
running fully on the ANE?" — for example:
llama model is not fully lowered to ANECoreML model is crashing on iPhone GPU, but not on iPhone CPU or macOS GPUANE compile OOMs on certain input shapesCPU Overhead After ANE ExecutionToday the only way for an ExecuTorch user to answer it is to break out
Swift / Xcode. This PR adds a Python wrapper around
MLComputePlansothe answer is one shell command:
Inputs supported:
.pte.mlpackage.mlmodelcin a tempdir, then analyze..mlmodelcThe PTE path reuses the same JSON/named-data extraction logic that
extract_coreml_models.pyuses, and is inlined into the script so it canbe run against a plain CoreML model without depending on the executorch
package.
Test plan
Added
test_coreml_compute_plan.pycovering:_device_name(...)forNoneand a stubMLNeuralEngineComputeDevice._COMPUTE_UNIT_CHOICESmapping (cpu_and_ne/all).analyze_one(...)end-to-end on a tinyrelu(x @ x.T) + x.sum()mlpackage built with
coremltools.convert(...): returns rows forevery dispatched op, with a
mainfunction and the expected MIL optypes (
matmul,relu,add,reduce_sum).I also ran the script against a few hand-built
.mlpackageand.mlmodelcfiles on macOS 26 with coremltools 9.0 and verified theoutput matches what
MLComputePlanreturns directly.Authored with Claude.
cc @kimishpatel @YifanShenSZ @cymbalrush @metascroy