Skip to content

npu: prefill a GDN model in one chunk instead of one token at a time (#587) - #624

Merged
bong-water-water-bong merged 2 commits into
mainfrom
fix/587-gdn-batch
Oct 27, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
fix/587-gdn-batch

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

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.

  • 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.

bcloud and others added 2 commits October 27, 2026 18:33
)

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.
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.

1 participant