[Qwen3.5] Use LpNormalization for L2-norm in linear-attention Q/K - #2127
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Qwen3.5 GatedDeltaNet linear-attention Q/K normalization subgraph in the Python model builder by replacing a multi-node L2-normalization pattern with a single ONNX LpNormalization op, reducing node count and improving decode performance for supported EPs.
Changes:
- Replaced the per-head L2-normalization subgraph (
Square + ReduceSum + Add(eps) + Rsqrt + Mul) with a singleLpNormalization(p=2, axis=-1)node. - Updated the
_make_l2_normalizehelper’s documentation and node construction to match the new approach.
Contributor
Author
|
Copilot help me rebase with latest main branch |
Contributor
Updated the branch with the latest |
The qk_l2norm path used a 5-node subgraph (Square + ReduceSum + Add(eps) + Rsqrt + Mul) per Q/K head per layer. Replace with a single LpNormalization(p=2, axis=-1) op which is natively supported by all current EPs (CPU, CUDA, WebGPU, ...). Net per Qwen3.5-0.8B model: -181 nodes (Mul -72, ReduceSum -36, Add -36, Rsqrt -36, +LpNorm 36). Behavior change: drops the +eps fallback. q/k come from RMSNorm+Proj so vector magnitudes far exceed any sane epsilon; divergence stays within fp16 noise. Verified text quality identical/equivalent on Qwen3.5-0.8B and 4B across NV (RTX 5080) and Intel iGPU. Decode TPS gains (greedy, prefill-1000, max_tokens=100, 5-run avg): NV 0.8B: 73.94 -> 83.56 (+13.0%) NV 4B: 55.24 -> 62.14 (+12.5%) Intel 0.8B: 36.22 -> 38.48 (+6.2%) Intel 4B: 12.26 -> 12.68 (+3.4%) Prompt TPS unchanged.
Xiaofei Han (xiaofeihan1)
force-pushed
the
xfh/qwen35-l2norm-via-lpnorm
branch
from
May 8, 2026 03:33
23913b4 to
64b7df3
Compare
Xiaofei Han (xiaofeihan1)
requested review from
Akshay Sonawane (apsonawane),
Baiju Meswani (baijumeswani) and
kunal-vaishnavi
May 12, 2026 03:20
kunal-vaishnavi
approved these changes
May 18, 2026
Copilot AI
added a commit
to xadupre/mbext
that referenced
this pull request
May 24, 2026
) Agent-Logs-Url: https://github.com/xadupre/mbext/sessions/a42d673d-e551-4caa-86f5-3d007e8bbea4 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Xavier Dupré (xadupre)
added a commit
to xadupre/mbext
that referenced
this pull request
May 24, 2026
#365) * Initial plan * Port qwen qk_l2norm to LpNormalization (microsoft/onnxruntime-genai#2127) Agent-Logs-Url: https://github.com/xadupre/mbext/sessions/a42d673d-e551-4caa-86f5-3d007e8bbea4 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> * Apply black formatting to test_random_qwen3_5.py Agent-Logs-Url: https://github.com/xadupre/mbext/sessions/be3d428c-0277-42ea-ad97-3e6d6081ee17 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
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
Qwen3.5's GatedDeltaNet linear-attention Q/K path uses an L2-normalize step. The current builder emits a 5-node subgraph per Q/K head per layer (
Square + ReduceSum + Add(eps) + Rsqrt + Mul). Replace it with a singleLpNormalization(p=2, axis=-1)— natively supported by all current EPs (CPU, CUDA, WebGPU, ...).Op count delta (Qwen3.5-0.8B int4)
Perf (greedy, prefill-1000, max_tokens=100, 5-run avg)
Prompt TPS unchanged on all configurations.
Behavior change
Drops the `+eps` fallback. q/k come from RMSNorm+Proj so vector magnitudes far exceed any sane epsilon; divergence stays within fp16 noise.
Test plan