Skip to content

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

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

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

Conversation

@HowieYin

@HowieYin HowieYin commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Superseded by #367, which contains the same patch with the required DCO sign-off.

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.
@HowieYin
HowieYin requested review from a team as code owners October 22, 2026 05:12
@HowieYin HowieYin closed this Oct 22, 2026
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.

1 participant