Add fuse_consecutive_mul and batched MatMul+Add->Gemm passes - #4
Closed
take-cheeze wants to merge 1 commit into
Closed
Conversation
Implements two passes from the proposed_passes design drafts: fuse_consecutive_mul (default Fuse pass): Folds Mul(Mul(X, C1), C2) -> Mul(X, C1*C2) when C1 and C2 are constants and the inner Mul feeds only the outer one. The combined scale is materialised with numpy-style broadcasting, so the rewrite is numerically equivalent (X*C1*C2 at every position). Covers e.g. PoolFormer LayerScale exported as a per-channel (C,1,1) scale times a scalar factor. fuse_matmul_add_bias_into_gemm_batched (PassType::Other, opt-in only): Rewrites batched MatMul(X[>=3D], W[K,N]) + b into Reshape(->[-1,K]) -> Gemm -> Reshape(->[...,N]), extending the 2-D-only fuse_matmul_add_bias_into_gemm to the rank>=3 activations used by transformer linear layers. Static leading dims emit a constant output shape; dynamic dims rebuild it via Shape/Slice/Concat. Registered as PassType::Other so it stays out of the default fuse set, since it is a graph-shape rewrite that is not guaranteed to be faster. Adds unit tests for both passes (fuse, no-fuse guards, per-channel and dynamic-shape cases) with onnxruntime numeric-equivalence checks. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V63N6PXYEgNEe1BWbKi6cU
take-cheeze
force-pushed
the
claude/onnx-optimizer-passes-tx9y3n
branch
from
July 29, 2026 22:13
55fb61a to
c9a482c
Compare
This was referenced Jul 29, 2026
Member
Author
|
Superseded by #5 ( Note: I'm intentionally not deleting the Generated by Claude Code |
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.
Implements two passes from the
docs/proposed_passesdesign drafts.fuse_consecutive_mul(defaultFusepass)Folds
Mul(Mul(X, C1), C2) -> Mul(X, C1 * C2)whenC1/C2are constants (initializers orConstantnodes) and the innerMulfeeds only the outer one. The combined scale is materialised with numpy-style broadcasting, so the rewrite is numerically identical (X * C1 * C2at every position). Covers e.g. PoolFormer's LayerScale, exported as a per-channel(C,1,1)scale times a scalar factor. Conservatively limited toFLOAT/DOUBLEconstants; other dtypes are left to constant folding.fuse_matmul_add_bias_into_gemm_batched(PassType::Other, opt-in)Rewrites batched
MatMul(X[>=3D], W[K,N]) + binto:extending the 2-D-only
fuse_matmul_add_bias_into_gemmto the rank-≥3 activations transformer linear layers use. Static leading dims emit a constant output shape; dynamic leading dims rebuild it viaShape/Slice/Concat(requires opset ≥ 10).Wmust be a 2-D constant andba 1-D bias broadcastable over theNaxis.Registered as
PassType::Otherso it stays out of the default fuse set (GetFuseAndEliminationPass) — it is a graph-shape rewrite that trades a batched MatMul forReshape + Gemm + Reshapeand is not guaranteed to be faster, so it is invoked explicitly by name.Tests
Adds unit tests for both passes — fuse cases, no-fuse guards (multiple-use, non-constant operand, rank-2, non-constant weight), per-channel and dynamic-shape cases — with onnxruntime numeric-equivalence checks. Full suite: 175 passed, 5 skipped (torchvision, torch not installed), 6 pre-existing xfails.
Generated by Claude Code