[Fix][Relax][Frontend][ONNX] Validate Flatten axis range in from_onnx - #20145
Open
siyiweigeHEW wants to merge 1 commit into
Open
[Fix][Relax][Frontend][ONNX] Validate Flatten axis range in from_onnx#20145siyiweigeHEW wants to merge 1 commit into
from_onnx#20145siyiweigeHEW wants to merge 1 commit into
Conversation
…che#20144) Flatten._impl_v13 now normalizes negative axis and rejects axis outside [-r, r] (the rank of the input tensor), matching onnxruntime's ShapeInferenceError. Previously an out-of-range axis (e.g. axis=5 on a rank-3 input) was silently accepted and produced a wrong output shape (24, 1) instead of being rejected. Fixes apache#20144 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: #20144
Summary
The Relax ONNX frontend silently accepted a
Flattennode whoseaxisattribute is outside
[-r, r](whereris the input rank), producing awrong output shape. The ONNX spec requires
axis ∈ [-r, r], and onnxruntimerejects such models with a
ShapeInferenceError. This PR makes the frontendreject out-of-range
axislike onnxruntime.Root cause
Flatten._impl_v13inpython/tvm/relax/frontend/onnx/onnx_frontend.pycomputed the batch size as
data_shape[0:axis]with no range check. BecausePython slicing silently clamps out-of-range indices,
axis=5on a rank-3input
(2, 3, 4)sliced the whole shape, giving(24, -1)→ output(24, 1),instead of raising an error.
Fix
Normalize negative
axis(axis += rank) and raiseValueErrorwhen theresult is outside
[0, rank]:Validation
Differential test: Relax (build +
VirtualMachine) vs onnxruntime.axis=5on(2,3,4)(24, 1)ValueErroraxis=-4on(2,3,4)(1, 24)ValueErrorRegression (all valid
axis ∈ [-r, r]unchanged, 0 differences vs onnxruntime):onnx.checker['N',3,4,5], axis-4..4)Run:
python results/TVM/deepseek-v4-flash/prove_hum/onnx_Flatten/4严格_穷举差分.py python results/TVM/deepseek-v4-flash/prove_hum/onnx_Flatten/minimal_repro.py # expect ValueError nowFiles changed
python/tvm/relax/frontend/onnx/onnx_frontend.py—Flatten._impl_v13:normalize negative
axisand raiseValueErrorforaxis ∉ [-r, r].