Raise NotImplementedError for unsupported TF symmetric pad mode#2720
Open
adityasingh2400 wants to merge 1 commit into
Open
Raise NotImplementedError for unsupported TF symmetric pad mode#2720adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Fixes #2280.
When converting a TensorFlow model, both the
PadandMirrorPadop handlers incoremltools/converters/mil/frontend/tensorflow/ops.pysilently rewrite TF'ssymmetricpadding mode to MIL'sreflect:The two modes are not equivalent. TF
symmetricreflects including the edge value, whilereflectexcludes it. For input[1, 2, 3, 4]padded by 2 on each side:[2, 1, | 1, 2, 3, 4 | 4, 3][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
padsupports onlyconstant,reflect, andreplicate(seemil/ops/defs/iOS15/tensor_operation.py), sosymmetriccannot 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 raiseNotImplementedErrornaming the op and the unsupported mode. Thereflectandconstantpaths are unchanged.Testing
Added
TestPad::test_symmetric_unsupported, which asserts that converting atf.pad(mode='SYMMETRIC')graph raisesNotImplementedError. Verified locally with TensorFlow 2.19:pad(..., mode="reflect")for a SYMMETRIC input (silent wrong mapping confirmed)NotImplementedErrorthrough both theMirrorPadpath (tf.padlowers SYMMETRIC toMirrorPad) and thePadhandler branchREFLECTandCONSTANTstill convert correctly and emitmode="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.