Skip to content

Raise NotImplementedError for unsupported TF symmetric pad mode#2720

Open
adityasingh2400 wants to merge 1 commit into
apple:mainfrom
adityasingh2400:fix-tf-pad-symmetric-mode
Open

Raise NotImplementedError for unsupported TF symmetric pad mode#2720
adityasingh2400 wants to merge 1 commit into
apple:mainfrom
adityasingh2400:fix-tf-pad-symmetric-mode

Conversation

@adityasingh2400
Copy link
Copy Markdown

Issue

Fixes #2280.

When converting a TensorFlow model, both the Pad and MirrorPad op handlers in coremltools/converters/mil/frontend/tensorflow/ops.py silently rewrite TF's symmetric padding mode to MIL's reflect:

mode = node.attr.get("mode", "constant").lower()
if mode == "symmetric":
    mode = "reflect"

The two modes are not equivalent. TF symmetric reflects including the edge value, while reflect excludes it. For input [1, 2, 3, 4] padded by 2 on each side:

  • symmetric: [2, 1, | 1, 2, 3, 4 | 4, 3]
  • reflect: [3, 2, | 1, 2, 3, 4 | 3, 2]

So a tf.pad(..., mode='SYMMETRIC') currently converts without any warning and the resulting Core ML model computes different, wrong values. This is a silent miscompilation.

Fix

MIL pad supports only constant, reflect, and replicate (see mil/ops/defs/iOS15/tensor_operation.py), so symmetric cannot be expressed directly. As the reporter suggested, the right behavior is to raise a clear error rather than emit a wrong result. Both handlers now raise NotImplementedError naming the op and the unsupported mode. The reflect and constant paths are unchanged.

Testing

Added TestPad::test_symmetric_unsupported, which asserts that converting a tf.pad(mode='SYMMETRIC') graph raises NotImplementedError. Verified locally with TensorFlow 2.19:

  • before the fix, the TF -> MIL conversion emits pad(..., mode="reflect") for a SYMMETRIC input (silent wrong mapping confirmed)
  • after the fix, SYMMETRIC raises NotImplementedError through both the MirrorPad path (tf.pad lowers SYMMETRIC to MirrorPad) and the Pad handler branch
  • REFLECT and CONSTANT still convert correctly and emit mode="reflect" / mode="constant"

The new test passes for both backends. End-to-end runtime comparison was not run here because this environment lacks the compiled Core ML runtime, but the change is confined to the frontend mode mapping.

TF's symmetric pad mode reflects including the edge values, while MIL's
reflect mode excludes them. Both the Pad and MirrorPad handlers silently
rewrote symmetric to reflect, which produces wrong outputs with no warning.

MIL pad supports only constant, reflect, and replicate, so symmetric cannot
be expressed directly. Raise a clear NotImplementedError naming the op and
mode instead of silently mapping to reflect. The reflect and constant paths
are unchanged.

Fixes apple#2280
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.

CoreML maps tensorflow's pad op's symmetric mode to reflect mode.

1 participant