Skip to content

[Qwen3.5] Use LpNormalization for L2-norm in linear-attention Q/K - #2127

Merged
kunal-vaishnavi merged 2 commits into
mainfrom
xfh/qwen35-l2norm-via-lpnorm
May 18, 2026
Merged

[Qwen3.5] Use LpNormalization for L2-norm in linear-attention Q/K#2127
kunal-vaishnavi merged 2 commits into
mainfrom
xfh/qwen35-l2norm-via-lpnorm

Conversation

@xiaofeihan1

Copy link
Copy Markdown
Contributor

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 single LpNormalization(p=2, axis=-1) — natively supported by all current EPs (CPU, CUDA, WebGPU, ...).

Op count delta (Qwen3.5-0.8B int4)

Op before after
Total nodes 1130 949 (−181)
Mul 198 126 (−72)
ReduceSum 37 1 (−36)
Add 54 18 (−36)
Rsqrt 36 0
LpNormalization 0 36

Perf (greedy, prefill-1000, max_tokens=100, 5-run avg)

GPU Model baseline gen tps new gen tps Δ
NVIDIA RTX 5080 Qwen3.5-0.8B 73.94 83.56 +13.0%
NVIDIA RTX 5080 Qwen3.5-4B 55.24 62.14 +12.5%
Intel iGPU Qwen3.5-0.8B 36.22 38.48 +6.2%
Intel iGPU Qwen3.5-4B 12.26 12.68 +3.4%

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

  • Built Qwen3.5-0.8B and Qwen3.5-4B with int4 quant + WebGPU EP
  • Verified output text quality on NV RTX 5080 and Intel iGPU — outputs are equivalent / nearly identical to baseline (no garbage, no degradation)
  • Decode TPS measured 5×, all 4 configurations show net positive gains

Copilot AI review requested due to automatic review settings May 6, 2026 05:35
@xiaofeihan1
Xiaofei Han (xiaofeihan1) requested a review from a team as a code owner May 6, 2026 05:35

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.

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 single LpNormalization(p=2, axis=-1) node.
  • Updated the _make_l2_normalize helper’s documentation and node construction to match the new approach.

Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py
@xiaofeihan1

Copy link
Copy Markdown
Contributor Author

Copilot help me rebase with latest main branch

Copilot AI commented May 8, 2026

Copy link
Copy Markdown
Contributor

Copilot help me rebase with latest main branch

Updated the branch with the latest main ancestry and pushed it in 23913b4.

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.
Comment thread src/python/py/models/builders/qwen.py Outdated
@kunal-vaishnavi
kunal-vaishnavi merged commit 88af38b into main May 18, 2026
16 of 17 checks passed
@kunal-vaishnavi
kunal-vaishnavi deleted the xfh/qwen35-l2norm-via-lpnorm branch May 18, 2026 08:31
Copilot AI added a commit to xadupre/mbext that referenced this pull request May 24, 2026
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>
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.

4 participants