[Relax][ONNX] Fix shape/dynamic restrictions for Squeeze/Unsqueeze and Slice#18955
[Relax][ONNX] Fix shape/dynamic restrictions for Squeeze/Unsqueeze and Slice#18955Aharrypotter wants to merge 5 commits intoapache:mainfrom
Squeeze/Unsqueeze and Slice#18955Conversation
…ctural tests
- fix Slice converter docstring typo ("Splice" -> "Slice") for consistency
- add explicit validation to reject zero step values in Slice
for both constant and dynamic-parameter paths
- add Unsqueeze negative test to reject duplicate axes
- strengthen structural IR test for dynamic Slice to assert
relax.dynamic_strided_slice is used and relax.strided_slice is not
- add Slice negative test for zero-step input
Validation:
- python -m ruff check python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py
- python -m pre_commit run --files python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py
- python -m pytest -n 1 tests/python/relax/test_frontend_onnx.py -k "unsqueeze_dynamic_axes or unsqueeze_duplicate_axes_validation or slice_dynamic_inputs_ir or slice_dynamic_inputs_length_validation or slice_zero_step_validation" -v
Result:
- 7 passed
…ion test - refactor constant Unsqueeze lowering to build target shape then reshape instead of scalar special-casing plus repeated expand_dims - add Unsqueeze scalar-input regression test to cover the new path - restore helper used by structural ONNX frontend IR checks Validation: - python -m ruff check python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py - python -m pre_commit run --files python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py - python -m pytest -n 1 tests/python/relax/test_frontend_onnx.py -k "unsqueeze_scalar_input or unsqueeze_dynamic_axes or unsqueeze_duplicate_axes_validation or slice_dynamic_inputs_ir or slice_dynamic_inputs_length_validation or slice_zero_step_validation" -v Result: - 8 passed
There was a problem hiding this comment.
Code Review
This pull request enhances the ONNX frontend for TVM Relax by implementing support for dynamic axes and parameters in the Unsqueeze, Squeeze, and Slice operators. It introduces several utility functions for handling runtime shape calculations and tensor rank validation, such as _get_known_tensor_rank and _build_unsqueezed_shape_tensor. The changes also include comprehensive new test cases to verify dynamic behavior and input validation. Feedback was provided to simplify the dimension-checking logic in the _get_known_tensor_length helper function to improve readability.
tlopex
left a comment
There was a problem hiding this comment.
-
Please fix the
Squeezebehavior forShapeExprinput whenaxis=None. Per ONNX spec, it should remove all size-1 dimensions, but the current path returns the input unchanged whenlen(data) > 1. -
Please take a look at the
Slicedynamic path. It currently convertsShapeExprto a tensor and returns a tensor fromdynamic_strided_slice, while the constant path returnsShapeExprorrelax.const. That type mismatch may cause downstream issues. -
_get_known_tensor_lengthcould also be simplified a bit — the secondif struct_info.ndim != 1is effectively just thendim == -1case, so usingif struct_info.ndim == -1: return Nonewould be clearer.
| if axis < 0: | ||
| axis += rank | ||
| if axis < 0 or axis >= rank: | ||
| raise ValueError(f"{op_name} axis {axis} is out of range for rank {rank}.") |
There was a problem hiding this comment.
Please preserve the original axis value in _normalize_constant_axes for the error message. Right now it reports the normalized value instead of the user input, which can be confusing.
There was a problem hiding this comment.
Makes sense, I'll fix this.
There was a problem hiding this comment.
Hi @tlopex , thank you for the detailed review! I've updated the PR to address your and gemini-code-assist's feedback
Especially, regarding the Slice dynamic path: I've added an explicit check to reject ShapeExpr inputs. This prevents potential type mismatches between constant and dynamic paths while keeping the implementation clean.
4c5965b to
29dc965
Compare
Summary
Relates to #18945.
This PR improves ONNX frontend handling for dynamic
Unsqueeze/Squeeze/Slice, tightens validation paths, and adds targeted structural/negative regression tests.Unsqueezelowering to use a singlereshapebased on computed target shape.expand_dimsin the constant path.Unsqueeze.Changes
UnsqueezeandSqueeze:relax.reshapewith validated shape rank/length assumptionsSliceconversion robustness:Splice->Slice)UnsqueezeaxesSlice(relax.dynamic_strided_slicepresent,relax.strided_sliceabsent)SliceUnsqueezescalar handling:expand_dimswith one target-shapereshapevalidation
ruff check: passedpre-commit --files: passedpytest: 8 passed