Repository navigation
npu: prefill a GDN model in one chunk instead of one token at a time (#587) - #624
Merged
Merged
Conversation
) forward_serve prefilled one token at a time because step_batch refused every hybrid model: simple_dense required lin_kd_ == 0, so each prompt token cost a full M=1 sweep of 368 GEMMs at ~26 ms of launch/device wait each. - gdn_layer_batch(): qkv/z as M-row projections, the conv window, alpha/beta, q/k norm, gated delta rule and gated norm per row, out-projection as an M-row GEMM. Same arithmetic as gdn_layer(), one weight read per chunk. - reset() cleared only the KV cache. conv_state_ and rec_state_ survived, so a second request on the same process (and the NPU_BATCH_CHECK oracle, which replays from position 0) started from the previous stream's recurrent state. - step_batch skipped load_layer() whenever the weight BOs were cached. That is right for a dense model, whose host floats only feed assemble_B, but a GDN layer's conv window, alpha/beta, ssm_a/dt and gated norm are per-layer host buffers outside the cache -- layer 0 was computing with layer 63's parameters. - Batched attention derived the head width as g->q_rows / NH. With attn_output_gate the fused QKV is [q | gate | k | v] and q_rows covers q AND the gate, so the 27B got 512 instead of 256 and the context width 12288 instead of 6144 -- the out-projection was handed a K that does not exist. No dense model sets attn_output_gate, which is why this never fired. Measured on strixhalo, Qwen3.8-27B-NPU2, prompt "The capital of France is", greedy, 4 threads pinned to cores 0-3: before: 2298 submits, 145.7 s wall after: 350 submits, 58.1 s wall Correctness: NPU_BATCH_CHECK replays the request through step() first and compares argmax -- 11751 vs 11751 MATCH -- and the per-layer hidden-state dump ([L] vs [LB]) agrees on all 64 layers (no layer exceeding 1e-3 relative difference), with the same " Paris" text from a fresh process. The debug dumps ([GB] per GDN tensor, [LB] per layer, [TB] top-5 logits) are gated behind the existing NPU_INFER_DUMP_LAYERS variable and follow that convention; they are what localised the last two bugs.
bong-water-water-bong
enabled auto-merge (squash)
October 27, 2026 21:45
This was referenced Oct 27, 2026
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.
npu: batch prefill through GDN layers and fix the gated head width (#587)
forward_serve prefilled one token at a time because step_batch refused every
hybrid model: simple_dense required lin_kd_ == 0, so each prompt token cost a
full M=1 sweep of 368 GEMMs at ~26 ms of launch/device wait each.
q/k norm, gated delta rule and gated norm per row, out-projection as an M-row
GEMM. Same arithmetic as gdn_layer(), one weight read per chunk.
second request on the same process (and the NPU_BATCH_CHECK oracle, which
replays from position 0) started from the previous stream's recurrent state.
right for a dense model, whose host floats only feed assemble_B, but a GDN
layer's conv window, alpha/beta, ssm_a/dt and gated norm are per-layer host
buffers outside the cache -- layer 0 was computing with layer 63's parameters.
attn_output_gate the fused QKV is [q | gate | k | v] and q_rows covers q AND
the gate, so the 27B got 512 instead of 256 and the context width 12288
instead of 6144 -- the out-projection was handed a K that does not exist.
No dense model sets attn_output_gate, which is why this never fired.
Measured on strixhalo, Qwen3.8-27B-NPU2, prompt "The capital of France is",
greedy, 4 threads pinned to cores 0-3:
before: 2298 submits, 145.7 s wall after: 350 submits, 58.1 s wall
Correctness: NPU_BATCH_CHECK replays the request through step() first and
compares argmax -- 11751 vs 11751 MATCH -- and the per-layer hidden-state dump
([L] vs [LB]) agrees on all 64 layers (no layer exceeding 1e-3 relative
difference), with the same " Paris" text from a fresh process.
The debug dumps ([GB] per GDN tensor, [LB] per layer, [TB] top-5 logits) are
gated behind the existing NPU_INFER_DUMP_LAYERS variable and follow that
convention; they are what localised the last two bugs.