Skip to content

Fix unsafe Slice/MatMul reordering with broadcasted operands - #367

Open
HowieYin wants to merge 1 commit into
onnx:mainfrom
HowieYin:fix/slice-matmul-rhs-rank-signed
Open

HowieYin wants to merge 1 commit into
onnx:mainfrom
HowieYin:fix/slice-matmul-rhs-rank-signed

Conversation

@HowieYin

@HowieYin HowieYin commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

adjust_slice_and_matmul can 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 by B[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

  • Allow reordering only when the RHS is a rank-2 matrix and the original data has rank at least 2. Read ranks from the constant Tensors, including when intermediate shape metadata is absent.
  • Read int32/int64 axes without a fatal fallback, validate their range, and skip reordering if any axis is the contracting axis.
  • Add regressions for broadcasting, vector RHS, safe matrix RHS, dynamic indices, negative/reverse slices, shared inputs, and unavailable constants.

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:

  • Focused tests: 9 passed, 39 subtests passed; 22 graph variants still reorder. Original and optimized graphs pass full checking and strict shape inference, and match independent NumPy results in ReferenceEvaluator and ONNX Runtime with runtime graph optimization disabled.
  • Full project suite: 197 passed, 5 skipped, 6 xfailed. The skipped tests require torchvision; the xfails are existing Slice and Softmax/Log tests.
  • CMake build with ONNX_BUILD_TESTS=ON and CTest: 2/2 passed.
  • Ruff with the project configuration, clang-format for the changed header, and git diff --check: passed.

CI results are pending.

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants