Repository navigation
Rewriter: validate and normalize axis in collapse_slices - #3144
Ti-Tai Wang (titaiwangms) merged 6 commits into
Conversation
|
Hi Ti-Tai Wang (@titaiwangms), Could you please review this PR when you have a moment? It fixes an |
There was a problem hiding this comment.
🟡 Changes recommended
The nonzero-start rejection lacks a regression test with matching shapes, unit steps, valid axes, and starts [0, 1] to ensure the _same_shape fallback does not incorrectly rewrite the Slice as an identity.
1 open issue
Review effort: Balanced
|
The axis-bounds checks fix a real issue: they prevent both an IndexError and the same-shape fallback from eliminating a Slice with a statically known invalid axis. One adjustment I recommend before merging: the new requirement in Regarding IR reuse: in the minimum supported onnx-ir version (1.1.0), Validation: static review of the diff, matcher behavior, IR implementation, and ONNX Slice specification only; tests were not run locally because onnx_ir is unavailable in this environment. |
|
Thanks for the review and suggestions! Ti-Tai Wang (@titaiwangms) I have updated the PR accordingly across four focused commits:
All 17 tests in |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3144 +/- ##
==========================================
+ Coverage 73.25% 73.27% +0.01%
==========================================
Files 269 269
Lines 33441 33524 +83
Branches 3192 3208 +16
==========================================
+ Hits 24498 24565 +67
- Misses 7874 7882 +8
- Partials 1069 1077 +8 ☔ View full report in Codecov by Harness. |
onnx-ir 1.1.0 guarantees that Shape.rank() returns an int, and Shape indexing and is_dynamic() natively accept negative axis indices. Remove redundant rank is not None guards and explicit axis += rank normalization while retaining axis bounds checks. Additionally, update _same_shape to normalize and clamp negative slice starts against static dimension bounds instead of strictly checking for zero. If effective starts evaluate to 0 and all steps are 1 with matching output shapes, the slice can be collapsed as an identity operation. Reject non-zero starts on dynamic dimensions or when effective start != 0. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…collapsing to identity Add regression tests verifying that a Slice with negative starts that normalize (e.g. starts=[-8] on dimension 8) or clamp (e.g. starts=[-10] on dimension 8) to 0 along with matching shapes and unit steps are recognized as identity slices and collapsed into Identity nodes with verified numerical parity. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ction in same_shape Add a regression test for multi-axis Slice with matching input/output shape annotations [4, 8], unit steps [1, 1], valid axes [0, 1], and nonzero starts [0, 1], asserting that no rewrite occurs in _same_shape. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…d multi-axis axis bounds in same_shape Add regression tests verifying that a single-axis Slice with nonzero start on matching shapes does not collapse, and that a multi-axis Slice containing an out-of-bounds axis does not collapse in _same_shape. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
5d7a8e2 to
f41d6b8
Compare
Ti-Tai Wang (titaiwangms)
left a comment
There was a problem hiding this comment.
The revised changes address my previous concerns: negative starts are normalized/clamped correctly, and the regression tests cover the optimization and axis-validation cases. No blocking findings remain in my static review.
Please update the PR description to match the final implementation: negative-axis handling now relies on IR Shape indexing rather than explicit normalization, and the same-shape check accepts starts whose effective value normalizes/clamps to zero rather than requiring every raw start to be zero.
Approval is based on static review; local tests were not run. Please ensure the required CI checks pass before merging.
|
Sure Ti-Tai Wang (@titaiwangms) ! |
152ffbe
into
microsoft:main

Summary
This PR hardens the
_collapse_slicesrewrite rule against invalid axis values that could cause unhandledIndexErrorcrashes or incorrect identity rewrites.It adds axis bounds validation and improves same-shape detection to account for ONNX Slice start normalization and clamping, preserving valid identity-slice optimizations.
Problem
In
_collapse_slices._check_if_redundant_slice(), the axis value was extracted from theSlicenode without validating whether it was within the valid range for the input tensor.This caused two issues:
Out-of-bounds axes: Invalid axes could cause
data.shape.is_dynamic()to raise an unhandledIndexError. Additionally, whenends == _INT64_MAX, the rule could returnTruebefore validating the axis, incorrectly collapsing an invalidSliceinto anIdentity.Incorrect same-shape detection:
_same_shape()could treat matching input and output shapes as sufficient evidence for an identity rewrite without properly validating the slice parameters. Conversely, requiring every rawstartsvalue to equal zero would reject valid identity slices whose negative starts normalize or clamp to zero.Solution
Updated
_collapse_sliceswith the following changes:ends == _INT64_MAXearly return._same_shape()to validate slice parameters rather than relying solely on matching input and output shapes.These changes prevent invalid slices from being incorrectly rewritten while preserving valid identity optimizations.
Tests
Added and updated regression tests in
_collapse_slices_test.py:test_slice_out_of_bounds_axis_does_not_crash_or_collapse: Verifies that a positive out-of-bounds axis is safely rejected.test_slice_negative_out_of_bounds_axis_does_not_crash_or_collapse: Verifies that a negative out-of-bounds axis is safely rejected.test_slice_out_of_bounds_axis_with_int64_max_ends_does_not_collapse: Ensures invalid axes are rejected even whenends == _INT64_MAX.test_slice_valid_negative_axis_collapses: Verifies valid negative-axis handling and numerical equivalence.test_slice_multi_axis_nonzero_start_does_not_collapse: Ensures a nonzero effective start does not produce an incorrect identity rewrite.