Repository navigation
Conversation
Require a matrix RHS and non-contracting Slice axes before reordering. Read ranks from constant Tensors and skip unreadable or invalid axes. Add numerical and structural regressions for broadcasting, vector RHS, safe matrix RHS, dynamic indices and shared inputs. Signed-off-by: Omnishambles <160447654+HowieYin@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The conservative guards address the semantic mismatch, and the regression tests cover both rejected cases and supported rewrites with no unresolved findings.
No open issues 🎉
What changed in this PR
This PR fixes unsafe Slice/MatMul reordering in the explicitly enabled optimizer pass by restricting it to cases that preserve rank, axis positions, and batch semantics.
Changes:
- Require a rank-2 constant RHS and constant data with rank at least 2, using tensor ranks rather than intermediate shape metadata.
- Safely read int32/int64 axes and reject out-of-range or contracting axes.
- Add regression tests for unsafe broadcasts, vector operands, supported rewrites, dynamic indices, and shared inputs.
| File | Description |
|---|---|
onnxoptimizer/test/optimizer_test.py |
Adds structural and numerical regression checks for supported and rejected reorderings. |
onnxoptimizer/passes/adjust_slice_and_matmul.h |
Adds conservative rank guards and safe axis validation before reordering. |
Review effort: Balanced
This branch has not been deployed
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.
Summary
adjust_slice_and_matmulcan change model outputs when the constant RHS has batch dimensions or is a vector. Checking only that Slice avoids the contracting axis does not preserve broadcasting or the meaning of its axes after MatMul.For example, reversing axis 0 of
X[1,3,4]before multiplying byB[2,4,5]leaves X unchanged. Moving that Slice after MatMul reverses the two broadcasted B batches instead. Both graphs pass the full ONNX checker and produce shape[2,3,5], but their values differ.Changes
A matrix RHS preserves the data's rank and non-contracting axis positions without introducing batch pairing changes. Dynamic starts/ends/steps and the existing rewrite remain supported. This conservative guard also skips some safe batched/vector RHS patterns, which may reduce constant-folding opportunities; broader support can be added separately.
This pass is explicitly enabled and is not in the default fuse/elimination pass list.
Testing
Locally on macOS, after rebuilding the native extension:
ONNX_BUILD_TESTS=ONand CTest: 2/2 passed.git diff --check: passed.CI results are pending.