[Fix] Preserve ONNX Squeeze axes attribute for opset < 13#19966
[Fix] Preserve ONNX Squeeze axes attribute for opset < 13#19966OmarAzizi wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for ONNX Squeeze operators prior to opset 13 (where axes is an attribute rather than an input) by introducing _impl_v1 and refactoring the squeeze logic into a helper method _squeeze. A test case is also added to verify this behavior. The reviewer suggests simplifying _impl_v1 by removing the redundant conversion of axes to a tuple of integers, as it is already parsed as a tuple of integers by _parse_attr.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| axes = attr.get("axes", None) | ||
| axis = tuple(int(x) for x in axes) if axes is not None else None | ||
| return cls._squeeze(bb, inputs[0], axis) |
There was a problem hiding this comment.
Since the axes attribute is already parsed as a tuple of integers (or None if not present) by _parse_attr, the explicit conversion tuple(int(x) for x in axes) is redundant. We can directly pass axes to _squeeze.
| axes = attr.get("axes", None) | |
| axis = tuple(int(x) for x in axes) if axes is not None else None | |
| return cls._squeeze(bb, inputs[0], axis) | |
| axes = attr.get("axes", None) | |
| return cls._squeeze(bb, inputs[0], axes) |
Summary
Before opset 13, ONNX
Squeezespecifiesaxesas a node attribute rather than a tensor input. The Relax ONNX importer only implemented_impl_v13, which reads axes from the second input, so for opset < 13 models, the attribute was silently ignored (axisdefaulted toNone) and the importer squeezed every size-1 dimension instead of only the requested one. This produced tensors with the wrong rank, breaking downstream ops likeTransposewhosepermno longer matched the input's actual rank.Added
_impl_v1to readaxesfrom the node attribute for opset < 13, and factored the existing squeeze logic into a shared_squeezehelper used by both_impl_v1and_impl_v13.Test plan
test_squeeze_axes_attributetotests/python/relax/test_frontend_onnx.py, covering an opset-11Squeezenode withaxesas an attribute.pytest tests/python/relax/test_frontend_onnx.py -k squeeze. All 21 tests pass.Squeeze): import fails onmainwithTranspose: number of axes in perm attribute (3) must equal the number of input tensor dimensions (-1), and succeeds with this fix.Real-world reproduction
Fixes (partially) #19965. The shape-Gather and dynamic-TopK issues reported in that issue are separate and not addressed here.