[qwen] Port qwen qk_l2norm to LpNormalization (onnxruntime-genai#2127) - #365
Merged
Conversation
) Agent-Logs-Url: https://github.com/xadupre/mbext/sessions/a42d673d-e551-4caa-86f5-3d007e8bbea4 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Import changes from ONNX Runtime PR 2127 and add fast tests
Port qwen qk_l2norm to LpNormalization (onnxruntime-genai#2127)
May 24, 2026
xadupre
marked this pull request as ready for review
May 24, 2026 22:06
📂 Impacted Files3 file(s) changed in this pull request: 🔵 |
Owner
|
@copilot fix style |
Agent-Logs-Url: https://github.com/xadupre/mbext/sessions/be3d428c-0277-42ea-ad97-3e6d6081ee17 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Contributor
Author
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #365 +/- ##
==========================================
- Coverage 78.44% 78.39% -0.06%
==========================================
Files 26 26
Lines 7990 7984 -6
==========================================
- Hits 6268 6259 -9
- Misses 1722 1725 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Imports microsoft/onnxruntime-genai#2127 into this repo and adds a fast test. The Qwen3.5
qk_l2normpath emitted a 5-nodeSquare + ReduceSum + Add(eps) + Rsqrt + Mulsubgraph per Q/K head per linear-attention layer; replace it with a singleLpNormalization(p=2, axis=-1)op, which is natively supported by all current EPs (CPU, CUDA, WebGPU, …).Per the upstream PR, net Qwen3.5-0.8B node delta is −181 (−72 Mul, −36 ReduceSum, −36 Add, −36 Rsqrt, +36 LpNorm), with decode TPS gains of +13.0% (NV 0.8B), +12.5% (NV 4B), +6.2% (Intel 0.8B), +3.4% (Intel 4B). The
+epsfallback is dropped — q/k come fromRMSNorm + Projso magnitudes far exceed1e-6, keeping any divergence within fp16 noise.Changes
modelbuilder/builders/base.py: addmake_lp_normalization(name, root_input, dtype, shape, axis=-1, p=2)helper.modelbuilder/builders/qwen.py: rewriteQwen35TextModel._make_l2_normalizeto emit oneLpNormalizationnode instead of the 5-node subgraph:tests/fast/test_random_qwen3_5.py: addtest_qwen3_5_qk_l2norm_uses_lp_normalizationwhich builds the hybrid (full_attention + linear_attention) config and asserts that the Q/K*l2norm*basenames emitLpNormalizationnodes withaxis=-1,p=2, and that none of the legacy node-name suffixes (/Square/Mul,/SumSq/ReduceSum,/AddEps/Add,/Rsqrt,/Normalize/Mul) remain.Notes
Qwen35TextModel._get_shared_l2_epsis now unused but kept (matching upstream) to minimize the diff.