Repository navigation
npu: resolve qwen3_5's RMSNorm convention from the weights, so a zero-centered (1+w) pack is not normalised with plain w - #902
Conversation
Qwen3.5's Qwen3_5RMSNorm is zero-centered -- the weight is initialised to 0 and applied as x/rms * (1 + w) -- but q4nx_semantics only set rmsnorm_offset for gemma*, so a d3-lite container was normalised with plain w and every norm output inherited the weight's own magnitude (layer 0's input_layernorm is a delta: mean 0.2377, min -0.1143, so xn came out 4.7x small). The model name cannot decide it: this box carries BOTH conventions under model_type qwen3_5 (all seven 27B bonsai2 dirs store plain gains, mean 0.9704, min 0.8711, including the live serve dir), so the convention is a probe resolved from the layer-0 input_layernorm weight the forward just loaded, printed with its evidence. The GDN gated norm (linear_attn.norm) is exempt: it is ones-initialised and applied as w in both packs. Measured (bf16 container, same weights as the pack, causal, host-GEMM, one 94-token prompt): xn cos vs the pack's own runtime 0.931249 -> 0.999999; layer 0 stage by stage (xn/qkv/co/a/b/z/ core/out) >= 0.999991; first layer below cos 0.999 moves from 0 (cos 0.254) to 3; /v1/systemone worst |dp| vs d3_runtime.py 0.6367 -> 0.4140, choice argmax flips 3/5 -> 2/5. The remaining gap starts at the first full-attention layer (cos 0.452), a separate defect tracked on engine#796. ctest npu_rmsnorm_offset covers the rule with both real shapes; docs/npu.md documents the convention and the exemption.
PR Reviewer Guide 🔍(Review updated until commit 518a30d)Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 7d0ecee. I re-derived the evidence from the containers and ran the shipped function on the real tensors, because the whole PR rests on "the weights decide it".
Every number in the docs and the test reproduces from the packs on this box (bf16 read straight out of each .q4nx, the same path load_norms() takes):
d3-lite-bf16 model.layers.0.input_layernorm.weight n=1024 mean=0.2377 min=-0.1143 -> (1+w)
model.layers.0.post_attention_layernorm mean=0.0852 min=-0.3594 -> (1+w)
model.norm.weight mean=3.3092 min=-0.7773 -> (1+w)
model.layers.0.linear_attn.ssm_norm mean=0.9516 min=0.5234 -> plain
bonsai2-27b-t2s2 (the owner's live dir, model_type qwen3_5)
model.layers.0.input_layernorm.weight n=5120 mean=0.9704 min=0.8711 -> plain
model.layers.0.post_attention_layernorm mean=0.7816 min=0.0000 -> plain
model.norm.weight mean=1.9427 min=0.7070 -> plain
model.layers.0.linear_attn.ssm_norm mean=0.8621 min=0.5469 -> plain
The shipped function, fed the full real tensors (not the test's four-value heads), compiled from this head against q4nx_semantics.c:
/tmp/tprs/w_d3.bin n=1024 mean=0.2377 min=-0.1143 q4nx_rmsnorm_offset_from_norm -> 1
/tmp/tprs/w_27b.bin n=5120 mean=0.9704 min=0.8711 q4nx_rmsnorm_offset_from_norm -> 0
So the fix lands on the pack it is for, and the production 27B — the case a name-based flip would have silently renormalised — resolves to the convention it already had.
Scope, checked against every container on the box (the probe fires only for qwen3_5* && !gemma):
7 x bonsai2-27b-* model_type=qwen3_5 mean 0.9704 min 0.8711 probe fires -> plain (production, unchanged)
2 x d3-lite/-bf16/-conv model_type=qwen3_5 mean 0.2377 min -0.1143 probe fires -> (1+w) (the fix)
Qwen3-0.6B model_type=qwen3 mean 0.1748 probe does NOT fire
d3-synth-0.6b model_type=qwen3 mean 0.1748 probe does NOT fire
qwen3-coder-30b-a3b-moe model_type=qwen3moe mean 0.0025 min -0.0352 probe does NOT fire
ctest npu_rmsnorm_offset built and ran here: 9/9 ok, including the two real shapes, and CMakeLists.txt adds the add_dependencies line for ci.yml's explicit target list, which is the #635 trap and it is handled.
The bits I expected to be fragile are the bits that hold:
- Both signals in the rule are load-bearing. The negative test alone is not enough: d3-lite layer 23 has no negatives at all (min 0.0525) and mean 0.267, so only the mean branch classifies it. The 27B's
post_attention_layernormgoes the other way — min exactly 0.0000, no negative, mean 0.78 — and stays plain. A one-signal rule would be wrong on one of these two packs. - Layer 0 is a fair proxy for these packs: sampled layers 0/1/2/3/11/23 on both — d3-lite stays 0.142-0.440 with negatives on five of six, the 27B stays 0.970-1.253 with none. (It is still a sample; see the note below.)
- The GDN gated norm really is exempt, by construction rather than by luck: the gated norm is written out at
q4nx_forward.cpp:1990-1996ascore * inv * gdn_small_[l].norm[vd] * silu(z), reading the weight directly and never consultingsem_.rmsnorm_offset— whose only consumer in the file isrmsnorm()(:3507). And that weight is a plain gain in both packs (0.9516 / 0.8621). - One flag for all layers + the final norm is right here: within each pack the input norms, the post-attention norms and
model.normall fall on the same side, so nothing is split. - Bounds:
in_norm_.assign(NL, std::vector<float>(H))happens insideload_norms()(:1365), which is called at:1250, before the probe readsw0[0..c_.H-1]at:1255— no short-vector read.
Three notes, none of them gates:
- The probe is decided by layer 0 only, and the docs state it as if the weight itself decided per tensor. For these two packs that is measured and safe, but a pack whose layer 0 is plain and whose later layers are deltas would be decided silently by layer 0. One sentence in
docs/npu.md("sample is layer 0; measured to agree with layers … on both packs here") would keep a future reader from over-trusting it. - If
c_.H == 0or the norms did not load, the probe silently leaves the plain convention in place for aqwen3_5pack — i.e. the pre-fix behaviour, with no[semantics]line. Failing closed is right; saying nothing is the only part I would change. - Out of scope but worth its own issue: the same statistic, applied to the three non-
qwen3_5packs above, says they are zero-centered too —Qwen3-0.6Bandd3-synth-0.6bat mean 0.1748, andqwen3-coder-30b-a3b-moeat mean 0.0025 with 864 negatives. I checked the source GGUF of the 0.6B pack: itsblk.0.attn_normis 0.1748 / max 1.0469 andblk.1.attn_normhas 314 negatives, so the container is faithful to it — these are not conversion artefacts. If any of those packs is a real model rather than a synthetic gate-row fixture, it has #796's defect and this PR deliberately does not touch it. The PR is right not to (its rule is measured forqwen3_5only, and "the name cannot decide it" is exactly the argument for measuring rather than extending the scope), but it should be filed while the evidence is fresh.
I ran no device work: the PR's before/after cos tables and the |Δp| numbers are the lane's npu-hold measurements, and the owner's serve holds the NPU lock.
|
Persistent review updated to latest commit 4d3c4cf |
|
Persistent review updated to latest commit 518a30d |
|
Cross-check of the probe's evidence sentence, from a lane that runs a pack this changes (pi-mtploop, MTP drafter on Read straight out of the container's own tensor table ( i.e. 1.8668 / 1.8428, not 0.9704 / 0.8711 as the commit message states for these dirs ( Measured device-free (header parse + BF16 decode, no dequant, no device). If the 0.9704 came from an HF/bf16 checkpoint rather than the q4nx container, saying so in the message would close this. |
|
Retracting my comment above — the error was mine, and your numbers are exactly right. I decoded the container's BF16 norm tensor with Python i.e. 0.9704 / 0.8711, digit for digit what your commit message says, and the device agrees: a run at So there is nothing to reconcile, the probe's evidence is reproducible from the shipped container, and the only real content of my earlier note is the mild, non-actionable observation that the rule is one-sided (a zero-centered pack whose |
Root cause
q4nx_semanticsdecided the RMSNorm convention from the model name, and onlygemma*got the(1 + w)form:Qwen3.5's
Qwen3_5RMSNormis also zero-centered — weight initialised to 0, applied asx/rms * (1 + w)(transformers 5.17.0, kernel name
RMSNormZeroCentered) — so every norm in avllm-sr/d3-litecontainer was scaledby the weight's own magnitude instead of by
1 + that. Layer 0'sinput_layernormis a delta trained from 0(mean 0.2377, min -0.1143), so the engine's
xncame out 4.7x too small and the whole forward inherited it.The name cannot decide it, and that is the reason this is a probe rather than a one-line
model_typechange: this boxcarries both conventions under
model_type: qwen3_5— all seven 27Bbonsai2-27b-*dirs store plain gains(mean 0.9704, min 0.8711, no negatives), including the owner's live serve dir. A name-based flip would have
silently renormalised the production 27B.
Measured before/after
One prompt (
score_three_levels, 94 tokens), causal, bf16 container (weights byte-identical to the pack, soquantization is not a variable), host-GEMM under
npu-hold -l npufeat,amdxdnaerrors 0 before and after.The first op, computed from the pack's own tensors:
rmsnorm(embed, ·)xnxnw(before)1 + w(after)Layer 0 after the fix, against hooks on the pack's own runtime (d3
d3_runtime.py):xnqkv(pre-conv)co(conv + SiLU)a/bzcore(delta rule + gated RMSNorm)out(ssm_out_proj)Per-layer last-position residual, 24 layers, same prompt (
NPU_INFER_DUMP_LROWSdiagnostic, uncommitted):/v1/systemonevs the pack'sd3_runtime.py(causal, 9 cases): worst `The remaining 0.4140 is not this bug: layers 0-2 are now exact to 1e-3 and the divergence starts at the first
full-attention layer (partial rotary 64/256, q/k norm,
attn_output_gate) — that localisation is the next item onengine#796, and no accuracy claim is made here.
What lands
npu/forward/include/q4nx_semantics.h—rmsnorm_offset_probe(documented: onemodel_type, two conventions) andthe exported rule
q4nx_rmsnorm_offset_from_norm(const float*, int).npu/forward/src/q4nx_semantics.c— probe set forqwen3_5*; the rule: a negative value or a mean below 0.5 isthe zero-centered form (a delta trained from 0 vs a gain trained from 1).
npu/forward/src/q4nx_forward.cpp— resolves it once afterload_norms()and prints the evidence:tests/npu_rmsnorm_offset_test.c+ctest npu_rmsnorm_offset— both real shapes as constants (d3 delta →(1+w);27B gain, all-ones, null, empty → plain), registered through
add_dependencies(npu_sampling_test …)because thislane's token cannot write
.github/workflows/.docs/npu.md— the convention, both packs on this box, and why the GDN gated norm (linear_attn.norm,Qwen3_5RMSNormGated, ones-initialised, applied asw) is exempt.Every other
model_typeis untouched:gemma*keeps its(1+w), everything else keeps plainw, and the seven 27Bqwen3_5dirs resolve tooffset 0(checked against the containers themselves, not asserted).closes #901