[Fix][Relax][Frontend][ONNX] Support Pad mode="wrap" and axes input for op… - #20152
Open
siyiweigeHEW wants to merge 1 commit into
Open
[Fix][Relax][Frontend][ONNX] Support Pad mode="wrap" and axes input for op…#20152siyiweigeHEW wants to merge 1 commit into
siyiweigeHEW wants to merge 1 commit into
Conversation
…set 18 The ONNX Pad spec adds mode="wrap" (circular padding) and the optional axes input in opset 18. The Relax ONNX frontend only handled these in _impl_v19 (added by apache#19827), which dispatches for opset >= 19; models with opset 18 still resolve to _impl_v11, which rejects wrap with OpAttributeInvalid and ignores axes (padding the full rank instead), so legal opset-18 Pad models fail to import. Add Pad._impl_v18 with the same wrap/axes handling as _impl_v19: expand the axes input into full-rank pads and dispatch mode="wrap" to topi.nn.circular_pad, which already implements circular padding. Validated by differential testing against onnxruntime over 81 legal Pad models (3 input shapes x all modes x positive/negative pads, plus axes cases): the 22 previously-rejected cases (wrap x19, constant+axes x3) now all import and match onnxruntime; no regression on the 59 already-supported cases. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Fixes: #20150
Summary
The Relax ONNX frontend rejected legal opset-18 Pad models using
mode="wrap"(circular padding) or the optionalaxesinput. Both areONNX Pad-18 features, are accepted by
onnx.checker/onnx.reference/onnxruntime, and
topi.nn.circular_padalready implements circularpadding — this is purely a frontend dispatch gap.
Root cause
Upstream #19827 added
Pad._impl_v19withwrap/axessupport, butget_converterdispatches on the highest_impl_v{N}withN <= opset,so that method is only reached for models with opset >= 19. A model
with opset 18 — the version that actually introduced
wrapandaxes— still resolves to_impl_v11, which:["constant", "edge", "reflect"], somode="wrap"raises
OpAttributeInvalid("Value wrap ... is invalid for operator Pad.");inputs[3](theaxesinput), so an axes model is paddedon the full rank instead of the specified axes and fails with
ValueError("Input dimension and pad_before dismatch ...").Fix
Add
Pad._impl_v18, mirroring #19827's_impl_v19but for opset 18:expand the
axesinput into full-rank pads via_get_known_tensor_rank/_normalize_constant_axes, extend the modewhitelist to include
"wrap", and dispatchwraptotopi.nn.circular_pad:_impl_v2(opset 2, pads as attribute) and_impl_v11(opset 11-17,neither
wrapnoraxeslegal) are left untouched.Validation
Differential test (Relax
from_onnx+relax.build+VirtualMachinevs onnxruntime) over 81 legal Pad models: 3 input shapes × all modes ×
positive/negative pads, plus
axescases. Verified on the familyfuzzlocked build
262c6d2e0via runtime monkey-patch(
results/.../onnx_Pad/verify_patch.py, no source files modified).constant/edge/reflect(opset 11/13)constant/edgeopset-18, no axeswrappositive pads (opset 18, 19)OpAttributeInvalid)max|diff| = 0constant+axes(opset 18, incl. negative axis)ValueError)max|diff| = 0The 5 documented deviations are
wrapwith negative pads, where theimplementations disagree:
onnx.reference(np.padmode"wrap") errorsout entirely, onnxruntime uses its own crop-window semantics, and
topi.nn.circular_padfollows the ONNX mod formula(
out[i] = in[(i - pad_before) mod dim]), matching upstream #19827'sidentical implementation. Positive-pad
wrap(the actual use case)agrees exactly across onnxrt / onnx.reference / TVM.
Run:
Files changed
python/tvm/relax/frontend/onnx/onnx_frontend.py— addPad._impl_v18with
wrap/axessupport for opset 18 (same handling as [Relax][Frontend][ONNX] Add support for Pad mode="wrap" for opset 19 #19827's_impl_v19), closing the opset-18 gap.