Skip to content

npu: qwen3_5 stores two RMSNorm conventions under one model_type, so a zero-centered (1+w) pack is normalised with plain w #901

Closed

Description

@bong-water-water-bong

What

Qwen3.5's Qwen3_5RMSNorm is zero-centered: the weight is initialised to 0 and applied as x/rms * (1 + w)
(transformers 5.17.0, transformers/models/qwen3_5/modeling_qwen3_5.py, class registered as
RMSNormZeroCentered). The engine applied the plain form x/rms * w, because q4nx_semantics only sets
rmsnorm_offset for gemma*:

npu/forward/src/q4nx_semantics.c:216
    s->rmsnorm_offset = (is_gemma && !is_gemma4) ? 1 : 0;

Every d3-lite (vllm-sr/d3-lite, model_type: qwen3_5, the pack engine#796 is about) norm weight is a delta
trained from 0, so the engine scaled every norm output by roughly the weight's own magnitude — layer 0's
input_layernorm has mean 0.2377, min -0.1143, i.e. the engine's xn came out 4.7x too small.

Measured

One prompt (score_three_levels, 94 tokens), causal, bf16 container (weights byte-identical to the pack, so
quantization is not in play), host-GEMM, npu-hold -l npufeat, amdxdna errors 0.

The whole first op, from the pack's own tensors:

numpy on rmsnorm(embed, ·) l2 cos vs engine xn cos vs reference xn
w (what the engine did) 8.4773 1.000000 0.931249
1 + w (what Qwen3.5 means) 39.7198 0.931263 0.999999

After the fix, the same diagnostics, layer 0 stage by stage against hooks on the pack's own runtime:

stage cos rel L2
xn 0.999999 0.0016
qkv (pre-conv) 0.999998 0.0019
co (conv + SiLU) 0.999999 0.0017
a / b 0.999999 0.0013 / 0.0016
z 0.999998 0.0020
core (delta rule + gated RMSNorm) 0.999991 0.0043
out (ssm_out_proj) 0.999995 0.0034

and the per-layer last-position residual (24 layers, same prompt):

before after
first layer with cos < 0.999 0 (cos 0.254) 3
layer 0 / 1 / 2 0.254 / 0.278 / 0.377 0.999968 / 0.999974 / 0.999972
layer 3 (first full attention) 0.139 0.452 — the next defect, separate from this one
worst ` Δp on/v1/systemonevs the pack'sd3_runtime.py` (causal, 9 cases)
choice argmax flips 3/5 2/5

Why it is a probe and not a model_type rule

qwen3_5 is used by two conventions on this box. All seven 27B bonsai2-27b-* containers declare
model_type: qwen3_5 and store plain gains (layer-0 input_layernorm: mean 0.9704, min 0.8711, every value
positive) — including bonsai2-27b-t2s2-serve, which is the owner's live serve. A model_type-only rule would have
silently renormalised the production 27B. The weights decide, so q4nx_semantics sets a probe for qwen3_5* and
the forward resolves the convention from the layer-0 input_layernorm.weight it just loaded (negative value, or mean
below 0.5 → zero-centered), printing the evidence on the [semantics] line:

[semantics] rmsnorm_offset=1  <- model.layers.0.input_layernorm.weight: mean 0.2377 min -0.1143
                                (qwen3_5 ships both the zero-centered (1+w) form and the plain w form; the weight decides)

The GDN gated norm (linear_attn.norm, Qwen3_5RMSNormGated) is exempt by construction: it is initialised to
ones and applied as w, which is what both packs store (0.95 / 0.86).

Fix

  • npu/forward/include/q4nx_semantics.h — rmsnorm_offset_probe field + exported
    q4nx_rmsnorm_offset_from_norm(const float*, int).
  • npu/forward/src/q4nx_semantics.c — probe for qwen3_5*; the rule itself.
  • npu/forward/src/q4nx_forward.cpp — resolve after load_norms(), print the evidence.
  • tests/npu_rmsnorm_offset_test.c — ctest npu_rmsnorm_offset: zero-centered/delta shapes → (1 + w); the 27B's
    gain shape, all-ones, null and empty → plain; both real tensors' heads as constants.
  • docs/npu.md — the convention, both shapes, and the exemption.

No device row is needed for this: the numbers above are host-GEMM, and the 27B path is unchanged (offset 0 on all
seven dirs, verified from the containers).

Activity

  1. added a commit that references this issue on Nov 3, 2026
    01e39c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions