Skip to content

fix: avoid fusing identity transpose into Gemm - #32435

Merged
Xavier Dupré (xadupre) merged 4 commits into
microsoft:mainfrom
LOGO127:codex/onnxruntime-32418-identity-gemm-transpose
Sep 9, 2026
Merged

Xavier Dupré (xadupre) merged 4 commits into
microsoft:mainfrom
LOGO127:codex/onnxruntime-32418-identity-gemm-transpose

Conversation

@LOGO127

@LOGO127 LOGO127 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Description

Prevent GemmTransposeFusion from folding a Transpose into Gemm unless it is a real two-dimensional matrix transpose. Identity transposes are now left unchanged, and default perm handling remains supported for known two-dimensional inputs.

Add a graph-transform regression test covering an identity transpose on the Gemm weight input.

Motivation and Context

Fixes #32418.

An identity Transpose(perm=[0, 1]) feeding a Gemm was incorrectly treated as a matrix transpose. The optimizer removed the node and set transB=1, changing the graph semantics and causing an invalid bias-shape error for non-square weights. The fix keeps the identity transpose in the graph and avoids changing Gemm attributes.

Validation

  • ORT lintrunner passed for both changed C++ files, including clang-format.
  • git diff --check passed.
  • A CPU-wheel baseline reproducer with ONNX Runtime 1.22.1 ran successfully with optimization disabled and reproduced Gemm: Invalid bias shape for broadcast with basic optimization; the optimized graph contained transB=1.
  • The targeted C++ gtest was not run locally because this Windows checkout has no existing ONNX Runtime C++ build; the new test is included for the upstream test environment.

AI assistance disclosure

This patch was developed with AI assistance. The issue, optimizer logic, regression test, and validation evidence were reviewed against the repository source and contribution guidance.

Signed-off-by: luozijian <luozijian0924@gamil.com>
Copilot AI balanced review requested due to automatic review settings September 4, 2026 06:26
@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

The newly guarded Gemm-output transpose path lacks regression coverage.

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

Pull request overview

Prevents GemmTransposeFusion from treating identity transposes as matrix transposes.

Changes:

  • Validates explicit and default transpose permutations before fusion.
  • Adds regression coverage for identity transpose on Gemm input B.
File summaries
File Description
onnxruntime/core/optimizer/gemm_transpose_fusion.cc Restricts fusion to genuine 2D matrix transposes.
onnxruntime/test/optimizer/graph_transform_test.cc Tests preservation of an identity weight transpose.
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 thread onnxruntime/core/optimizer/gemm_transpose_fusion.cc
Signed-off-by: luozijian <luozijian0924@gamil.com>
@LOGO127

LOGO127 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review note in commit 8b7e6ff by adding GemmTransposeFusionDoesNotFuseIdentityTransposeAtOutput. This covers the output-side identity transpose guard; both input- and output-side identity transposes now have regression coverage. lintrunner -a and git diff --check pass locally. The native ONNX Runtime test binary is not available in this checkout, so the gtest itself remains for upstream CI.

@xadupre

Copy link
Copy Markdown
Member

It is probably worth checking it is not happening somewhere else as well.

@LOGO127

LOGO127 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion. I audited the other transpose-related optimizer paths in this checkout. MatmulTransposeFusion parses the permutation and leaves both matrix-transpose flags false for an identity permutation, so removing that identity node is semantics-preserving; DQMatMulNBitsFusion matches an explicit [1, 0] permutation. I did not find another path that rewrites Gemm transA/transB from an identity transpose. The unsafe rewrite was confined to GemmTransposeFusion, and the guard now covers both input- and output-side transposes.

@LOGO127

LOGO127 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the approval and follow-up. The full CI exposed a compile issue in the new regression tests: the shape-only MakeInput calls used a single-brace initializer and did not match ModelTestBuilder's overload. Fixed in commit 031cc67 by using the existing double-brace shape form; the fix is now pushed to the PR branch. The affected build failures all pointed to those four calls. CLA remains pending.

@LOGO127

LOGO127 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@xadupre
Xavier Dupré (xadupre) merged commit b905e94 into microsoft:main Sep 9, 2026
90 of 92 checks passed
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 invalid transB Gemm for identity Transpose weight

3 participants