[Fix][Relax][Frontend][ONNX] Fix Mean/Sum/Min/Max with all-constant inputs - #20147
Open
siyiweigeHEW wants to merge 2 commits into
Open
[Fix][Relax][Frontend][ONNX] Fix Mean/Sum/Min/Max with all-constant inputs#20147siyiweigeHEW wants to merge 2 commits into
siyiweigeHEW wants to merge 2 commits into
Conversation
The MultiInputBase constant-fold path called numpy_op(*np_inputs) (np.mean/np.sum/np.min/np.max). For these numpy reductions the 2nd and later positional arguments are interpreted as `axis`, not as additional data tensors, so a single constant input returned a 0-d scalar (the global mean instead of the input unchanged) and multiple constant inputs raised TypeError. Fix by mirroring the non-constant path: broadcast each constant to the common shape, stack along a new leading axis, then reduce along it. 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: #20146
Summary
The Relax ONNX frontend mishandles
Mean/Sum/Min/Maxnodes whoseinputs are all constants (model initializers): a single constant input
returns a 0-d scalar (e.g. the global mean
3.5for a(2, 3)input)instead of the input unchanged, and multiple constant inputs raise
TypeError: only integer scalar arrays can be converted to a scalar index.onnxruntime returns correct results in both cases.
Root cause
MultiInputBase._impl_v1's constant-fold path atpython/tvm/relax/frontend/onnx/onnx_frontend.py:2456-2459calledFor
Mean/Sum/Min/Max,numpy_opisnp.mean/np.sum/np.min/np.max. These numpy reductions reduce their first argument andinterpret the 2nd and later positional arguments as the
axisparameter, not asadditional data tensors. So
np.mean(x)reduces the whole tensor to a 0-dscalar, and
np.mean(a, b, ...)passes an array intoaxis→ TypeError.Fix
Mirror the non-constant path: broadcast each constant to the common shape,
stack along a new leading axis, then reduce along it.
A single constant input then reduces a
(1, *shape)stack along axis 0, whichis the identity — matching the ONNX semantics. (
relax.Constantshape elementsare
tvm.tir.IntImm, so they are converted to plain ints fornp.broadcast_to.)Validation
Differential test: Relax (build +
VirtualMachine) vs onnxruntime (andonnx.reference) on the same model, comparing output shapes and max |diff|.(2,3)(2, 3)(2, 3)(5,)(5,)(5,)()()()(2,3)(2, 3)(2, 3)(2,3,4)(2, 3, 4)(2, 3, 4)(2,3)+(3,)(2, 3)(2, 3)(2,3)+(3,)+(2,1)(2, 3)(2, 3)(2,3)(2, 3)(2, 3)(2,3)(2, 3)(2, 3)(2,3)(2, 3)(2, 3)(2,3)+(3,)(2, 3)(2, 3)(2, 3)(2, 3)The two failing cases from the issue now match onnxruntime exactly:
Run:
Files changed
python/tvm/relax/frontend/onnx/onnx_frontend.py—MultiInputBase._impl_v1constant-fold path: broadcast + stack + reduce along the leading axis instead
of
numpy_op(*np_inputs)(fixesMean/Sum/Min/Max).