coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Raise NotImplementedError at conversion time for reflect/replicate pad >2D (fixes #2576, #2571)

Open LeSingh1 opened this issue 6 months ago • 5 comments

Summary

mb.pad only supports reflect / replicate modes when at most the final two dimensions carry non-zero padding. The generic pad torch converter forwards the mode through unconditionally, so torch.export-decomposed torch.nn.ReflectionPad3d / torch.nn.ReplicationPad3d (which decompose to aten.pad with mode="reflect" / "replicate" and 3 padded dims) currently convert successfully and then explode at predict() time with:

RuntimeError: Error compiling model: "Failed to parse the model specification.
Error: Unable to parse ML Program: in operation pad_cast_fp16:
Padding for more than two dimensions only supports `constant` mode".

The two original issues (#2576 ReflectionPad3D, #2571 ReplicationPad3d) reported this exact failure mode and asked for an ahead-of-time error instead.

Fix

Added a static check inside _translate_torch_args in the pad op converter (coremltools/converters/mil/frontend/torch/ops.py). When mode in ("reflect", "replicate"), the pad list is fully static, and more than two dimensions have non-zero pad, raise NotImplementedError with a message that names the mode, the padded-dim count, and the typical culprit modules.

The check is scoped tightly:

  • only static-list pad (dynamic-padding path was already raising for other reasons);
  • only reflect / replicate modes (constant and circular unaffected);
  • only triggers when padded_dims > 2 — valid 2D cases (ReflectionPad2d, etc.) still convert unchanged.

Tests

Added TestPad::test_pad_reflect_replicate_3d_raises parameterized over both torch.nn.ReflectionPad3d and torch.nn.ReplicationPad3d:

tests/...::test_pad_reflect_replicate_3d_raises[mode=reflect-...]   PASSED
tests/...::test_pad_reflect_replicate_3d_raises[mode=replicate-...] PASSED
======================== 2 passed in 0.64s =========================

I also manually verified torch.nn.ReflectionPad2d(padding=2) on a rank-4 input still passes the new guard and proceeds to the rest of the converter.

Issues

Fixes #2576 Fixes #2571

LeSingh1 avatar May 18 '26 01:05 LeSingh1

CI Run: https://gitlab.com/coremltools1/coremltools/-/pipelines/2535438543

TobyRoseman avatar May 18 '26 21:05 TobyRoseman

Thanks for the review @TobyRoseman! Updated in abb9faa — now raising the Core ML Framework's own wording verbatim ("Padding for more than two dimensions only supports constant mode") and updated the test comment + pytest.raises match to use the same string.

LeSingh1 avatar May 18 '26 22:05 LeSingh1

This may be a better fix: #2697

TobyRoseman avatar May 18 '26 22:05 TobyRoseman

Agreed @TobyRoseman — #2697's approach is better, since it actually makes >2D reflect/replicate pads work via the new split_non_constant_pads backend pass instead of just surfacing the error sooner. I'm happy to close this PR once #2697 lands, or sooner if you'd prefer. Let me know which is cleaner for you.

LeSingh1 avatar May 18 '26 23:05 LeSingh1

@LeSingh1 - Thanks for confirming. If you don't mind, please review #2697. Does the fix look good to you? Are there any additional tests you recommend adding? Feel free to comment directly in that pull request.

cc: @tritolol.

TobyRoseman avatar May 20 '26 00:05 TobyRoseman