Repository navigation
npu-pack: de-interleave a gated q_proj's rows, so the forward attends over q and gates with gate - #904
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.
… over q and gates with gate (closes #903) HF builds a gated attention layer's query as view(*, -1, 2*head_dim) + chunk(2, -1), i.e. [q_h | gate_h] PER HEAD, while the forward's fused QKV is [q block | gate block | k | v] -- the layout the FLM/OGA packs already use (their attn_q and attn_gate are separate tensors that get concatenated). The converter was a pure rename, so a d3 container kept the per-head order and the forward attended over half-gate rows and gated with half-query ones. Measured on the real pack (engine#796, layer 3 = first full attention, last position, bf16 container): engine q slice vs HF q per-head cos 0.0774, vs the block reading cos 0.999992; gate 0.5989 vs 0.999994; v 0.999964. After the reorder the whole per-layer chain agrees: first layer below cos 0.999 goes from 3 (cos 0.452) to none, and /v1/systemone vs the pack's own d3_runtime.py moves from worst |dp| 0.4140 to 0.0061 with 0/5 choice argmax flips. The transform needs the pack's own head geometry, so it runs only when config.json declares attn_output_gate, refuses when num_attention_heads/head_dim are missing, and verify() applies the same reorder (it compares container against source, so it would otherwise call the fix a loss). ctest safetensors_to_q4nx covers both, with the row order as a known answer.
PR Reviewer Guide 🔍(Review updated until commit 33eaccc)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 a034d3216. But it is stacked on #902: the first commit here is 7d0ecee27 ("npu: resolve qwen3_5's RMSNorm convention…"), and #902 is still open with my READY. So the order is #902 first, then #904 rebased — merging #904 on today's base carries #902's commit in its squash.
I verified the permutation against the real weights, which is stronger than the body's own evidence. The d3 source is on this box, so I compared the container on disk with the source rows, both directions, rounding the source to bf16 so the comparison is like-for-like:
layer 3 source [4096, 1024] container [4096, 1024]
container vs source AS-IS (per-head [q_h | gate_h]) cos=0.138608
container vs DE-INTERLEAVED (this PR's transform) cos=1.000000
layer 7 as-is cos=0.186560 de-interleaved cos=1.000000
layer 11 as-is cos=0.158402 de-interleaved cos=1.000000
Bit-exact agreement with the de-interleaving and none with the source order, on three layers of a real 4096x1024 fused q_proj. That also confirms the container built from this branch is the one the reference agrees with.
The consumer's layout is what the converter now produces. Both attention paths read the fused QKV as [q | gate | k | v] by block: host :3289-3296 and device :4283-4290 both set gate_ptr = qkv + nhd and copy k/v from 2 * nhd. So the rows this transform emits are exactly the ones the forward slices.
tests/safetensors_to_q4nx_test.py passes at this head (safetensors_to_q4nx: ok), including the new cases — rows are [q block | gate block], rows are not the source order, the converter prints that it reordered, and a config with attn_output_gate but no head_dim is refused with attn_output_gate in the message.
Checked around the edges too:
gate_geometry()reads undertext_configfirst, which matters — I confirmed both the d3 source and the 27B family keepnum_attention_heads/head_dim/attn_output_gatenested there, so the hoisted read is not accidental luck.verify()applying the same transform is necessary (it compares container to source, so it would otherwise call the fix a regression) — with the caveat below.- Qwen3.5's gated attention has no
q_proj.bias(the d3 source has only weights), and a suffix the converter does not know is skipped and counted (skipped N tensor(s)), not silently stored under a reshaped name.
One risk worth naming, one note:
verify()shares the transform, so a systematically wrong permutation would pack and verify green — the guard against that is exactly the measurement above (a real pack against its source), notverify. Fine as long as that is the habit; worth remembering when the next converter fix lands, becauseverifycannot be the witness for it.- All the 27B packs also declare
attn_output_gate=1(nh 24, hd 256). Nothing on disk changes today — their containers were not produced by this converter and no 27B source is on the box — but the converter decides purely from the config's claim that the source is HF-layout. A source whoseq_projis already[q block | gate block](e.g. assembled from separateattn_q/attn_gate) would now be de-interleaved a second time, silently, withverifystill green. One sentence in the newdocs/npu.mdrow ("only for an HF source with a single fusedq_proj; a pack whose q_proj is already block-laid must not be passed through this") would close it cheaply.
I ran no device work: the cos residual table across all 24 layers and the |Δp| 0.4140 → 0.0061 on /v1/systemone are the lane's npu-hold runs, and the owner's serve holds the NPU lock.
|
Persistent review updated to latest commit dd0082e |
|
Persistent review updated to latest commit 2244d03 |
|
Persistent review updated to latest commit 33eaccc |
Root cause
Qwen3.5's gated attention builds its query as
so a gated
q_proj's rows are[q_h | gate_h]per head. The forward assembles the fused QKV as[q block | gate block | k | v]— the layout the FLM/OGA packs store, whereattn_qandattn_gateare separatetensors that get concatenated.
tools/safetensors_to_q4nx.pywas a pure rename, so avllm-sr/d3-litecontainer keptthe per-head order and the forward attended over half-gate rows and gated with half-query ones.
The two layouts are unrelated (below, cos 0.077), so this survives every statistical check and only shows up as
"the first full-attention layer is 0.45 off" — which is exactly where engine#796's per-layer instrument put it.
Measured (real d3 pack, layer 3 = first full attention, last prompt position, bf16 container)
The engine's own dump of the slices it attends over, against the pack's runtime:
Before/after on the whole forward
Per-layer last-position residual over all 24 layers (one 94-token prompt, causal, host-GEMM,
npu-hold -l npufeat,amdxdnaerrors 0):/v1/systemonevs the pack'sd3_runtime.py, 9 cases, worst |Δp|(The other half of the 0.6367 → 0.4140 step was engine#901's norm convention, already merged.)
What lands
tools/safetensors_to_q4nx.py—gate_geometry()readsattn_output_gate+num_attention_heads+head_dimfrom the pack's own
config.json;qwen35_transform()de-interleavesself_attn.q_proj.weightrows into[q block | gate block];verify()applies the same transform, because it compares container against source andwould otherwise report the fix as a loss.
tests/safetensors_to_q4nx_test.py— the fixture declares the gated geometry, the packed rows are checked againstthe de-interleaved source (and asserted not to be the source order), the converter says it reordered them, and the
missing-geometry refusal is a case.
docs/npu.md— the transform row in the converter table, with the measured cosines.Nothing else changes:
attn_output_gatefalse (and every non-qwen3_5arch) takes the identity path, and the FLM/OGA27B containers are already block-wise.
closes #903