Support PyTorch 2.9
Summary
Starting with torch 2.9, torch.export.export() returns an ExportedProgram in the new TRAINING IR dialect by default (it used to be ATEN). The PyTorch frontend only accepts ATEN/EDGE, so _validate_conversion_arguments rejects every torch.export-based model on torch 2.9 before conversion even starts:
NotImplementedError: Conversion for models with only ATEN or EDGE dialect is supported/tested.
Provided Dialect: TRAINING. Run '.run_decompositions({})' on your exported PyTorch Model prior to conversion.
This isn't one broken op — it breaks essentially every ct.convert(exported_program, ...) call the moment you upgrade to torch 2.9.
Part of #2615.
The error message already tells the user the remedy (run_decompositions({})), and the converter's own testing_utils runs exactly that after every torch.export.export(...). This PR just moves that one step inside convert() so existing user code keeps working without changes.
Fix
- In
convert(), before the argument validation, if the model is anExportedProgramwhose dialect is notATEN/EDGE, lower it withmodel.run_decompositions({}). - The lowered (
ATEN) program then flows through validation and intomil_convertunchanged. - No-op for torch <= 2.8 (already
ATEN) and forEDGE(ExecuTorch), so those paths are untouched.
Test
Adds TestPyTorchConverterExamples.test_convert_exported_program_training_dialect: it exports a small Linear+ReLU model and calls ct.convert(...) directly, with no manual run_decompositions(). On torch 2.9 the exported program is in the TRAINING dialect (so this is the regression guard); on older torch it's ATEN and the test still passes.
Verification
Built against coremltools 9.0 + torch 2.9.0 on macOS (arm64):
- Without this change: every torch.export conversion I tried — linear/relu, layer_norm, conv1d, sdpa, where, pow, floor_divide, instance_norm3d — fails with the
Provided Dialect: TRAININGerror above. - With this change: the same models convert, and predictions match PyTorch within fp16 tolerance (e.g. layer_norm + linear: max abs diff ~3.5e-4).
- torch <= 2.8 is unaffected; the new branch only fires for a non-ATEN/EDGE dialect.
One thing this PR deliberately leaves alone: _TORCH_MAX_VERSION and reqs/pytorch.pip. A few op-level signature changes in 2.9 still need their own fixes (e.g. hann_window now reports a different arg count, which breaks stft), so I didn't want to claim 2.9 is fully tested. This is just the dialect-level unblock that everything else on 2.9 sits behind.
Makes sense. I'll bump the torch pin to 2.9 and fix the remaining op breakages so CI can test it properly, then update this PR.
Done — pushed full 2.9 support on top of the dialect fix:
- Bumped
_TORCH_MAX_VERSIONand the arm64torchpin to 2.9.0 so CI exercises 2.9. - Fixed the op breakages 2.9's
torch.exportpath surfaces:hann_window: handler required 5/6 positional inputs (TorchScript shape); export passes onlywindow_length(+periodic). Made it per-frontend, and registered thehann_window.periodicoverload (sanitize_op_kind doesn't stripperiodic) — this also unblocksstft.rms_norm: required exactly 4 inputs; export omits optionalweight/epswhen defaulted. Relaxed to min 2.
- Added frontend coverage to
test_hann_window+ a newTestRMSNormso CI validates the export path.
Ran a ~40-op probe against torch 2.9.0 locally: everything converts and matches PyTorch within fp16 tolerance, except hamming/blackman/bartlett/kaiser_window — those were never implemented (not 2.9 regressions). Should be safe to bump CI now.
CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2612956158
Thanks for running CI — went through the 3 failures:
1 & 2 (test_executorch, coremltools_test) — dependency resolution. executorch>=0.7.0 resolved to 1.3.1, which requires torch>=2.12, so the install was ResolutionImpossible against torch 2.9. ExecuTorch 1.0.x is the release built for torch 2.9 (needs torch>=2.9,<2.10 + torchao==0.14.0), so I pinned executorch>=1.0.0,<1.1.0 and bumped torchao 0.12.0 → 0.14.0 (which also clears the test_coreml_quantizer collection error).
3 (test_pytorch_export) — all 208 failures were test_unfold[is_dynamic_hw=True], and it's an upstream torch 2.9 bug. torch 2.9's ExportedProgram.run_decompositions({}) raises NameError: name 'L' is not defined while interpreting the _guards_fn submodule it generates for dynamic-shape exports that carry shape guards (unfold constrains H/W ≥ f(kernel, dilation, padding, stride)). I reproduced it minimally — it fails on both strict=True and strict=False, and it's not specific to the converter (static-shape unfold and every other export op convert fine). Locally, TestUnfold on the export frontend is now 240 passed / 240 skipped / 0 failed.
Since it's a torch regression rather than something we can fix here, I skipped the guarded dynamic-shape unfold cases on torch>=2.9 with a comment to re-enable once torch fixes it. Happy to file/track the torch issue if you'd like it referenced by number instead.
Updated CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2621391035
Pushed updates addressing all review comments: removed the rdar reference, and added a dialect assertion so the training-dialect test provably exercises the auto-lowering path on the installed torch (the CI pin is now 2.9.0). Ready for another look whenever you can re-run CI — thanks!
Updated CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2641356509