fix: skip DQ->MatMulNBits fusion when weight/scale initializer is shared#28326
Open
Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: skip DQ->MatMulNBits fusion when weight/scale initializer is shared#28326Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…r is shared When two DQ nodes reference the same weight or scale initializer (the tied embedding pattern seen in Whisper's decoder_model_merged_uint8.onnx), the first DQ->MatMul fusion consumes the shared initializer. The second fusion then crashes in TransposeDQWeightsForMatMulNBits with "Missing required scale" because the initializer has already been removed from the graph. The existing CheckOutputEdges guard does not catch this because each DQ node has exactly one output edge — the sharing is at the initializer input level, not the DQ output level. Fix: in DQMatMulNodeGroupSelector::Check, after the CheckOutputEdges guard, reject fusion if graph.GetConsumerNodes(weight_arg->Name()).size() > 1 or graph.GetConsumerNodes(scale_arg->Name()).size() > 1. This preserves both DQ nodes as-is when their initializers are shared. Adds regression test DQMatMulNotConvertedToMatMulNBits_SharedWeight covering Int4x2 and UInt4x2 with and without zero-points. Fixes microsoft#28306
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in the QDQ transformer by preventing DequantizeLinear -> MatMul/Gemm from being fused into MatMulNBits when the DQ’s weight or scale initializer is shared across multiple consumers (e.g., tied embeddings), which previously could lead to a crash during TransposeDQWeightsForMatMulNBits.
Changes:
- Add a selector-side guard to skip DQ→MatMulNBits fusion when the weight or scale initializer has multiple consumers.
- Add a regression test that constructs two DQ→MatMul paths sharing the same weight+scale initializers and asserts that no
MatMulNBitsnodes are produced.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selectors.cc | Adds a consumer-count guard in DQMatMulNodeGroupSelector::Check to reject fusion when weight/scale initializers are shared. |
| onnxruntime/test/optimizer/qdq_matmulnbits_transformer_test.cc | Adds a regression test ensuring shared weight/scale initializers prevent fusion and avoid the prior crash. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
TransposeDQWeightsForMatMulNBits("Missing required scale") when loading models like Whisper'sdecoder_model_merged_uint8.onnx.Motivation
Fixes #28306.
ORT 1.25 regressed on quantized Whisper decoder models. PR #27769 broadened the
DQ→MatMulNBitsfusion, but itsCheckOutputEdgesguard only checks DQ output edges; it does not catch the case where two DQ nodes share the same weight + scale initializers at their inputs. The first fusion consumes the shared initializer; the second fusion then asserts inqdq_actions.cc:136because the initializer has been removed from the graph.Changes
onnxruntime/core/optimizer/qdq_transformer/selectors_actions/qdq_selectors.cc: inDQMatMulNodeGroupSelector::Check, after the existingCheckOutputEdgesguard, returnfalseif either the weight initializer or the scale initializer has more than one consumer node. This conservatively preserves both DQ nodes asDequantizeLinear+MatMulrather than attempting a fusion whose first half would invalidate the second.onnxruntime/test/optimizer/qdq_matmulnbits_transformer_test.cc: addsDQMatMulNotConvertedToMatMulNBits_SharedWeight(4 variants: Int4x2/UInt4x2 × with/without zero-points). Builds two DQ nodes pointing at a shared weight + shared scale initializer, runs the QDQ transformer atTransformerLevel::Level2, and asserts noMatMulNBitsis emitted (MatMul=2,DequantizeLinear=2,MatMulNBits=0). Without the fix the second fusion crashes before the assertion runs, so this is a real regression guard.Test Plan
DQMatMulNotConvertedToMatMulNBits_SharedWeightis included inonnxruntime_test_alland will run in CI.lintrunner -aclean on the diff.onnx-community/whisper-tinydecoder_model_merged_uint8.onnxviaInferenceSessionno longer asserts inTransposeDQWeightsForMatMulNBits.Fixes #28306