Repository navigation
Description
Measured on the shipped Bonsai-2-27B NPU serve (~/wt/serve-bin/t2s2-20261103/1bit, model
~/models/npu/bonsai2-27b-t2s2-serve, :18097) on 2026-11-04, and reproducible from the code alone.
Symptom
The prefix cache (#650) never hands a state back. Every request that repeats a prompt logs
[prefix] slot=-1 cached=0 of 849 reason=slot has no usable state
and re-prefills from 0 — 5 of 5 long requests across three windows, including a byte-identical repeat of an
849-token prompt on the same serve, which paid its full prefill twice (104.0 s total against an 81.9 s TTFT).
That line is app/forward_serve.cpp:253, i.e. restore_state() returned false.
Root cause
snapshot_state() (npu/forward/src/q4nx_forward.cpp:262-278) cuts all four planes with one count,
want = tokens * NKV * HD:
auto copy = [&](const std::vector<std::vector<float>>& src, std::vector<std::vector<float>>& dst) {
...
const size_t n = std::min(want, src[l].size());
};
copy(kcache_, out.k); // per position: correct
copy(vcache_, out.v); // per position: correct
copy(conv_state_, out.conv); // one fixed block per layer: WRONG
copy(rec_state_, out.rec); // one fixed block per layer: WRONGThe conv window and the recurrent state are one block per layer that each position updates, not appends to. On
Bonsai-2-27B the plane sizes are NKV*HD = 1024 floats per position, conv CD*(CK-1) = 30,720, recurrent
48*128*128 = 786,432.
- Prompts longer than 30 tokens:
want > 30,720, so the saved conv plane is 30,720 floats and
restore_state's cover test (src[l].size() < want) fails for the conv plane — henceslot has no usable stateon every real prompt. - Prompts shorter than 768 tokens:
want < 786,432, so the snapshot keeps onlywantfloats of the recurrent
plane (23 % of it at 180 tokens) and the install would shorten the plane the GDN step reads.
The saved byte counts are the fingerprint. [prefix] saved slot=... bytes= on one serve:
| prompt tokens | slot bytes | 393,216*tokens + 7,864,320 |
|---|---|---|
| 180 | 78,643,200 | 78,643,200 |
| 252 | 106,954,752 | 106,954,752 |
| 584 | 237,502,464 | 237,502,464 |
| 704 | 284,688,384 | 284,688,384 |
| 849 | 320,471,040 | 341,704,704 (the planes cap at 786,432 floats here) |
384 KiB per token, six times the 64 KiB/token of KV the geometry actually has: the fixed planes were growing as if
they were per-position. At 849 tokens the measured slot is 21,233,664 bytes below the line — exactly
64 layers * 82,944 floats — which is the recurrent planes hitting their real size.
Fix
Carry the per-position planes with the per-position cut and the GDN planes whole; refuse a truncated whole plane
rather than installing it short (so the fallback is a full prefill, not a wrong state). The plane copy is in
npu/forward/include/q4nx_state_planes.h, device-free, with tests/q4nx_state_planes_test.cpp reproducing the
miss on the old arithmetic and gating the new rule. PR follows.
Activity
bong-water-water-bong commentedon Nov 4, 2026
Fixed on the box and in review as #985 (tip
aa2f5c1, CI green including the newq4nx_state_planesctest). Measured with onenpu-holdticket, one serve per binary, the same 180-token prompt twice: shippedt2s2-20261103->slot has no usable stateand a full re-prefill (19.26 -> 22.17 s); this branch ->slot=0 cached=179 of 180 hit, TTFT 22.90 -> 7.89 s (0.34x), greedy text byte-identical to the shipped binary's.The device run also found a second bug in the same path, which the PR now fixes too: the snapshot was saved after the last prompt token and restored at
cached - 1positions, so with the planes carried whole the last token's in-place GDN update landed twice — the planes-only arm hit the cache (cached=179, 15.63 -> 5.32 s) and answered the same prompt with different, fragmented text.forward-servenow snapshots one token short (splitting the final prefill chunk) and resumes throughnpu::resume_point().