You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "siyiweigeHEW (via GitHub)" <gi...@apache.org> on 2026/08/27 21:08:57 UTC
[PR] [Fix][Relax][Frontend][ONNX] Squeeze: raise an error when axes select a non-unit dimension [tvm]
siyiweigeHEW opened a new pull request, #20211:
URL: https://github.com/apache/tvm/pull/20211
Fixes: #20185
## Summary
The Relax ONNX frontend silently accepted a `Squeeze` whose `axes` select a
dimension with size **not equal to 1**, returning the input **unchanged** with no
error. The [ONNX Squeeze spec](https://onnx.ai/onnx/operators/onnx__Squeeze.html)
(opset 11+ and earlier) requires the opposite: *"If an axis is selected with shape
entry not equal to one, an error is raised."* Both onnxruntime and
`onnx.reference` reject such models; TVM diverged. The converter now validates
statically-known axes against the input shape and raises a `ValueError` before
delegating to `relax.op.squeeze`.
## Root cause
`Squeeze._squeeze` (`python/tvm/relax/frontend/onnx/onnx_frontend.py`) delegates
the non-constant path directly to `relax.op.squeeze(data, list(axis))`. The
underlying op's shape inference
(`src/relax/op/tensor/manipulate.cc:1269-1284`) deliberately implements PyTorch
semantics — *"If a dimension is not 1, silently skip it (no-op), matching PyTorch
behavior"* (and carries a `Todo(relax-team): revisit here for better check on if
the axis being squeezed has length 1`). So a non-unit selected axis was silently
ignored and the input returned unchanged. The ONNX frontend performed no
`dim == 1` check — unlike its constant path, where `np.squeeze` already raises
the same `ValueError` as `onnx.reference`.
```python
# Before: silently accepted e.g. data(2,3) with axes=[0]
if isinstance(axis, tuple):
return relax.op.squeeze(data, list(axis))
```
## Fix
Validate before delegating: when `axis` is statically known (a tuple) and the
input shape is statically known, every selected (normalized, deduplicated,
bounds-checked) axis must have size 1:
```python
if isinstance(axis, tuple):
cls._validate_axes_unit_dims(data, axis)
return relax.op.squeeze(data, list(axis))
```
`_validate_axes_unit_dims` reuses `_normalize_constant_axes` (negative-axis
normalization, bounds and uniqueness checks) and raises the same `ValueError`
message as `np.squeeze`/`onnx.reference` when a known dim is `!= 1`. Dimensions
that are only known at runtime cannot be validated at import time and keep the
previous behavior — the shared `relax.op.squeeze` is intentionally left unchanged
because other frontends (e.g. PyTorch) rely on its no-op semantics.
## Validation
Differential test: Relax (build + `VirtualMachine`) vs onnxruntime on the same
model, cross-checked with `onnx.reference` (which is the reference for the
rejection semantics and for fp64), across valid, invalid-axis, constant, and
out-of-bounds cases.
| Case | onnxruntime | onnx.reference | TVM (after fix) | Result |
|---|---|---|---|---|
| `(2,3)` `axes=[0]` (non-unit) | ERR | ERR | `ValueError` | OK |
| `(1,2,3)` `axes=[1]` (non-unit) | ERR | ERR | `ValueError` | OK |
| `(1,2,1,3)` `axes=[1]` (only non-unit) | ERR | ERR | `ValueError` | OK |
| `(2,3,1)` `axes=[0,2]` (mixed) | ERR | ERR | `ValueError` | OK |
| `(2,3)` `axes=[-1]` (neg, non-unit) | ERR | ERR | `ValueError` | OK |
| `(1,2,1,3)` `axes=[0,2]` (valid, regression) | `(2,3)` | `(2,3)` | `(2,3)` max\|diff\|=0 | OK |
| `(1,2,1)` `axes=[-1]` (valid neg, regression) | `(1,2)` | `(1,2)` | `(1,2)` max\|diff\|=0 | OK |
| `(1,)` `axes=[0]` (scalar output, regression) | `()` | `()` | `()` max\|diff\|=0 | OK |
Totals: **25/25 OK** (pre-fix: 6/6 legal models diverged — onnxruntime/ref
rejected, TVM silently returned the input unchanged). Valid axes (incl. negative
axes, scalar output, opset 13/21), the constant-data path, and out-of-bounds
rejection are unchanged. Run:
```bash
TVM_LIBRARY_PATH=<tvm23>/build PYTHONPATH=<patched 262c6d2e04 source>/python \
python results/TVM/deepseek-v4-flash/prove_hum/onnx_Squeeze/5修复_差分验证.py
```
New in-tree tests cover the raise behavior for both opset 11 (axes attribute) and
opset 13 (axes input), the constant path, and numeric correctness of valid axes
vs onnxruntime.
## Files changed
- `python/tvm/relax/frontend/onnx/onnx_frontend.py` — `Squeeze._squeeze`: validate
that every selected axis has a size-1 dimension (ONNX spec) before delegating to
`relax.op.squeeze`.
- `tests/python/relax/test_frontend_onnx.py` — add `test_squeeze_non_unit_axis_raises`,
`test_squeeze_constant_non_unit_axis_raises`, `test_squeeze_valid_axes_correctness`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Fix][Relax][Frontend][ONNX] Squeeze: raise an error when axes select a non-unit dimension [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on PR #20211:
URL: https://github.com/apache/tvm/pull/20211#issuecomment-5460768694
Closed as already fixed. Thanks @siyiweigeHEW @OmarAzizi
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Fix][Relax][Frontend][ONNX] Squeeze: raise an error when axes select a non-unit dimension [tvm]
Posted by "OmarAzizi (via GitHub)" <gi...@apache.org>.
OmarAzizi commented on PR #20211:
URL: https://github.com/apache/tvm/pull/20211#issuecomment-5453370515
Fixed in #20188, sorry I didn't know you were working on it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Fix][Relax][Frontend][ONNX] Squeeze: raise an error when axes select a non-unit dimension [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex closed pull request #20211: [Fix][Relax][Frontend][ONNX] Squeeze: raise an error when axes select a non-unit dimension
URL: https://github.com/apache/tvm/pull/20211
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org