Skip to content

Fix GemmTransposeFusion identity transpose handling - #32429

Open
Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 3 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/gemm-transpose-matrix-perm
Open

Fix GemmTransposeFusion identity transpose handling#32429
Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 3 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/gemm-transpose-matrix-perm

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor

Description

Fixes #32417 and #32418.

GemmTransposeFusion currently treats any Transpose directly connected to a Gemm input or output as a matrix transpose and toggles transA/transB. That is only valid when the transpose actually swaps the two matrix axes.

For an identity transpose with perm=[0, 1], the current rewrite removes the transpose and flips the corresponding Gemm transpose attribute, changing the graph semantics. Non-square cases can fail after optimization, while square cases can silently produce different numeric results.

This change only folds rank-2 matrix transposes with perm=[1, 0], or a missing perm where ONNX's default reverse-axis behavior is equivalent to [1, 0] for Gemm matrices. The same guard is applied independently to A, B, and output transposes.

Tests

Adds regression coverage for identity and real matrix transposes on both Gemm A and B inputs. Identity transposes remain intact; [1, 0] transposes continue to fuse.

Copilot AI balanced review requested due to automatic review settings September 3, 2026 19:45
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

🟡 Changes recommended

Add output-side coverage for preserved identity and folded matrix transposes.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Restricts Gemm transpose fusion to genuine rank-2 matrix transposes, preserving identity transposes.

Changes:

  • Validates transpose permutations for Gemm inputs and outputs.
  • Adds regression coverage for A and B input transposes.
File summaries
File Summary
onnxruntime/test/optimizer/gemm_transpose_fusion_test.cc Tests identity and matrix transposes for A/B inputs; output-side cases remain uncovered.
onnxruntime/core/optimizer/gemm_transpose_fusion.cc Guards input and output fusion based on transpose permutation.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +83 to +84
output_node_ptr->OpType() == "Transpose" &&
IsMatrixTranspose(*output_node_ptr)) {
@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor Author

Added the missing output-side regression coverage. The tests now verify that an identity output transpose is preserved and that a true matrix transpose is folded, including the expected transA/transB values and reversed Gemm input order in the folded case. Full ONNX Runtime CI is left to the PR because this sparse local checkout does not contain a complete build tree.

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.

GemmTransposeFusion can create dimension-mismatched Gemm for identity Transpose input

2 participants