Fix ReplicationPad3d failing at CoreML ML Program runtime#2697
Fix ReplicationPad3d failing at CoreML ML Program runtime#2697tritolol wants to merge 3 commits into
Conversation
413106c to
608c8dd
Compare
|
@TobyRoseman, @tritolol -- I went through this carefully (was sent here from #2701). The approach looks right to me and is strictly better than what I had in #2701 (which only raised earlier); this one actually makes the case work. Why the split is semantically safeSequential 1D reflect/replicate along independent axes is provably equivalent to a single multi-axis op, because each axis operation is index-independent:
So the chain in Pipeline placementConfirmed by reading Tests look right, plus a couple of suggestionsThe two new
Minor
#2701Happy to close #2701 once this lands -- they cover the same issues (#2576 / #2571) and this is the cleaner fix. Nice work, @tritolol. |
CoreML's ML Program runtime rejects non-constant padding modes (reflect, replicate) when more than 2 dimensions are padded, with: "Padding for more than two dimensions only supports constant mode". Previously, torch.nn.ReplicationPad3d converted silently but failed at prediction time. Add a new mil_backend pass `split_non_constant_pads` that decomposes such pad ops into sequential pads each covering at most 2 dimensions, which CoreML supports. The pass runs in _BACKEND_MIL_PASSES after merge_consecutive_paddings to prevent splits from being merged back. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Extend test_pad_reflect_replicate parametrization to rank 5 to cover the functional path for both reflect and replicate modes on >2 dims - Add test_reflection_pad3d and test_reflection_pad3d_asymmetric regression tests (reflect mode was handled by the pass but untested) - Add docstring note to split_non_constant_pads about the known limitation with dynamic (runtime-computed) pad values Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Explain why the skip applies only to rank 5: the NN backend's add_padding (op_mapping.py:2604) requires all padding outside the last 2 dimensions to be zero. Rank-5 with 3 spatial dims padded violates this; ranks 3 and 4 pad only 1-2 spatial dims and pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
608c8dd to
847ad7e
Compare
|
Thanks for the thorough review @LeSingh1! Addressed all points: Tests added
Docstring
Clarified the NN backend skip
Also rebased onto the latest |
|
@tritolol - can you please explain, in your own words (i.e. not output from Claude), why this is implemented as a pass. Why can't this be implemented at op conversion time? That would be a much more self-contained and straightforward change. |
Summary
torch.nn.ReplicationPad3dconverted silently but failed at prediction time with"Padding for more than two dimensions only supports constant mode"split_non_constant_padsthat decomposes non-constant pad ops (reflect/replicate) covering >2 dimensions into sequential pads of ≤2 dimensions each, which CoreML supports_BACKEND_MIL_PASSESaftermerge_consecutive_paddingsso splits aren't merged backTest plan
TestPad::test_replication_pad3d— regression test for symmetric 3D replication paddingTestPad::test_replication_pad3d_asymmetric— regression test for asymmetric 3D replication paddingmil/passes/tests/test_passes.py -kpad tests pass (215 passed)🤖 Generated with Claude Code