feat(kvflash): mode 4's exact GPU scorer uses a fused chunk-mass kernel over a device archive of evicted K (stacked on #963)
Stacked on #963. Base: kvflash/prefill-recall at 2a01709b0 (#963's head: the lean #963 with main 1d949fd88 merged). Review range: 2a01709b0..643e03c09, 15 commits, 14 files, +1639 / -42. Restacked 2026-11-19 from 934a9451a: its 9 commits plus 43fe79c39 (the fused scorer workspace is released before the GPU scorer refuses on free VRAM) replay onto the lean #963 with one conflict, in kvflash_pager.h: #963 no longer has the device victim tier, so the chunk versions stay and the victim fields go. The last five commits answer the review findings (0fa2db286, eb8df077b, 3503fa2e2, ed10245dc, 643e03c09). check_commit_messages.py --base 2a01709b0 --head 643e03c09: all 15 OK.
Gate on this head (2026-11-19, section "Restack gate" below): mode 4 against #963's head on 3 recorded sessions, 165 of 165 requests with identical output and picks, session prefill 677 vs 651 tok/s, wall 608 vs 628 s, decode 30.1 tok/s on both; HIP unit tests 380/380 on gfx1201 and gfx1151, the two new device tests run (not skipped) on both. The 14-session landing gate and the tests further below ran on an earlier form of this range.
The tests and the gate below ran on aec6a3e5c, the same 7 commits on 50020e4c5 (the head G2's gate ran on). 4be2caad4 is that tree plus #963's last three commits (2a6e64c53, 9fc7a36a4, d98512ff0): git diff aec6a3e5c 4be2caad4 is 3 files, +9 / -3 (ENVIRONMENT.md, a comment in kvflash_query_capture.h, one unit-test fixture). No compiled server code differs. Build and test_server_unit on 4be2caad4: pass, 0 failures (rebase record).
The landing gate passes: mode 4 with this PR against K1 on the 14 recorded sessions, one binary, session prefill 0.896x (bar 0.85x; G2's gate was 0.860x), P1 31/40 vs 0/40, P2 26/40 vs 24/40. Against the G2 scorer on the same binary (LUCE_KVFLASH_GPU_SCORE_FUSED=0, 3 sessions) the pick lists are identical on 137 of 137 requests.
Why
On G2's gate mode 4 costs 0.860x of K1's session prefill. After G2's reuse path, what is left of that cost is the exact scorer on the turns that still score: every call uploads the K of every evicted chunk from host again and runs a logits graph over it. On a 3-session screen (lucebox6 R9700, 151 requests, 36 scorer calls) that was 28.0 s of scorer time: "h2d 10.4 s graph 8.7 s total 28.0 s; uploaded 31701 of 31701 evicted chunks".
The evicted K does not change between calls unless the pager rewrites the chunk, and the score needs one number per chunk and query plane, not the logits.
What it does
- Device archive. The scorer keeps the evicted K it uploads in one device tensor per layer,
[D, cap * chunk_tokens, heads]. Slots are keyed byKvFlashPager::chunk_version(c), so a chunk is uploaded again only after the pager rewrote it.kvflash_gpu_archive_plan(pure) decides hit, upload and evict per call. The archive grows with device-to-device carry-over and is taken only under the scorer's existing free-memory guard (about 1.06 MiB of VRAM per evicted chunk at q8_0). - Fused chunk-mass kernel (
kvflash_fused_mass.h,kvflash_fused_mass_cuda.cu). One kernel reads the pool K and the archive in place (q8_0 blocks), multiplies by the queries quantized exactly as MMQ quantizes them for a q8_0 operand, and reduces to per-chunk partials (local max and sum-exp), which a second pass rescales into the attention mass. No logits tensor exists. int8 WMMA on gfx12, dp4a on CUDA and other HIP targets.kvflash_fused_mass.hholds a CPU reference of the same numerics; the unit tests compare device, reference and the logits-graph scorer. - Default and fallback. Mode 4's exact GPU scorer uses the kernel when
KvFlashGpuMass::fused_available(k_type, head_dim, n_layers)holds: K is q8_0, head dim is a multiple of 32 and at most 256, at most 64 layers, and the build has the kernel. Otherwise, on a kernel error, or withLUCE_KVFLASH_GPU_SCORE_FUSED=0, the logits graph scores as on G2 and the archive stays off (the graph spans every slot held, so an archive under it costs more than it saves: graph 8.7 s to 13.3 s on the screen). A kernel error logs once:[kvflash] fused scorer unavailable: <why>; using the logits graph.
Nothing else changes: the selector, the picks budget, the reuse / prescore / two-pass paths and the rewind are G2's. With --kvflash off none of this code runs.
Which device gets the fused path with the default q8_0 K:
| Device | Path | Evidence on this head |
|---|---|---|
| RTX 3090 (sm_86, CUDA) | fused, dp4a | unit: device vs reference worst 1.22e-06 |
| R9700 (gfx1201, HIP) | fused, int8 WMMA | unit: 1.2e-06; engine: gate, 173 of 173 scorer calls fused=1, 0 fused scorer unavailable lines |
| Strix Halo (gfx1151, HIP) | fused, dp4a | unit: 1.13e-06 |
Any device with another K type (f16, q4_0, ...) or head dim takes the logits graph.
One new knob, in server/docs/ENVIRONMENT.md: LUCE_KVFLASH_GPU_SCORE_FUSED (default 1; 0 = logits graph, no archive).
Commits
| Commit | Subject |
|---|---|
d1f6c8ce9 | feat(kvflash): exact GPU scorer can keep the evicted K it uploads in a device archive, keyed by pager chunk versions |
52701ab4f | test(kvflash): CPU reference of the fused exact chunk-mass math (per-chunk local max and sum-exp, rescaled reduce) with the 217K roofline; matches the exact scorer to 3e-7 on mean and plane masses |
8453eeb4c | feat(kvflash): fused exact chunk-mass kernel (CUDA/HIP, q8_0 K x q8_1 q int8 dp4a, per-chunk partials, rescaled reduce, query tiles) with a CPU reference of the MMQ numerics; device matches it to 1.4e-6 |
0389b2444 | perf(kvflash): fused kernel keeps the q block and loops in registers (compile-time D and chunk tokens, online max/sum-exp); 3090 kernel 140 ms per 217K call extrapolated, was local-memory bound |
a47087d92 | fix(kvflash): fused kernel quantizes q exactly as MMQ does for q8_0 K (d_inv = 127 / amax, float block scale, no fp16 rounding); device scores now match the logits graph to 4.4e-7 |
2e74c75a0 | perf(kvflash): fused scorer uses int8 WMMA on gfx12 (16 planes x 16 tokens per wave, MMQ scale order kept); gfx1201 kernel 302 ms -> 82 ms at 3382 chunks, dp4a stays for CUDA and as impl=1 |
767dbc734 | feat(kvflash): mode 4's exact GPU scorer defaults to the fused chunk-mass kernel over the pool K and the device archive |
4736fa393 | test(kvflash): fused-mass device tests skip without a GPU and the chunk-version test uses pageable staging, so check-cpu passes on the stub-driver runner |
c20464905 | fix(kvflash): fused scorer takes the WMMA path only on gfx1200/gfx1201, not any gfx12* |
43fe79c39 | fix(kvflash): release the fused scorer workspace before the GPU scorer refuses on free VRAM |
0fa2db286 | fix(kvflash): a failed fused run clears the CUDA sticky error, so the logits-graph fallback does not abort on it |
eb8df077b | fix(kvflash): the first failed fused run latches the kernel off for the scorer, so it is not retried on every call |
3503fa2e2 | fix(kvflash): a pager reset frees the GPU scorer's device archive instead of holding it until backend teardown |
ed10245dc | test(kvflash): the GPU CI jobs run the fused-mass and GPU-scorer fixtures, and a device case covers D=256 ct=64 |
643e03c09 | fix(kvflash): a pooled snapshot restore drops the GPU scorer's device archive, since deserialize() resets the pager inside |
Each of the 15 commits builds alone with every target (CUDA sm_86 build, luce_server, test_server_unit, test_model_smoke and the kernel object; 2026-11-19, rc=0 for all 15). The HIP builds are the two heads in the gate below.
Evidence
Restack gate (2026-11-19)
lucebox6 R9700 hip:0 (gfx1201, ROCm 7.2.4), HIP builds gfx1151;gfx1201 of #963's head 2a01709b0 (base, md5 dfd56422) and of 643e03c09 (md5 0500e482), Qwen3.8-27B UD-IQ4_XS, q8_0 K/V, --max-ctx 139264 --kvflash 16384 --prefix-cache-slots 8 --prefix-cache-max-mib 40960 --disk-prefix-cache off --no-kv-cache, mode 4 (recall profile: mode 4 (exact scorer) on every arm), 3 recorded sessions, 165 requests per arm, temperature 0, seed 1, page cache dropped before each arm, arms base / this PR / G3 on this PR / base.
| Arm | Output vs base | Picks vs base | Session prefill | Decode med / mean / best / min (decode > 2 s, n 10) | Wall |
|---|---|---|---|---|---|
| base (1st) | - | - | 652 tok/s | 30.10 / 30.08 / 30.30 / 29.90 | 627.4 s |
| this PR | 165/165 identical | identical (387 lines) | 677 tok/s (1.04x) | 30.10 / 30.12 / 30.30 / 30.00 | 608.2 s (0.97x) |
| base (2nd) | 165/165 identical | identical | 650 tok/s | 30.10 / 30.09 / 30.30 / 30.00 | 628.3 s |
Prompt and cached tokens are equal on every request. The fused kernel scored all 48 scorer calls (fused=1), with no fused scorer unavailable line. The same replay on 43fe79c39 (this range without the five review commits) gave the same 165 outputs and picks, so the review commits do not change the numerics.
HIP unit tests (ctest -R '[Kk]vflash|[Ll]aunch|[Pp]rops|[Ff]eature.?[Gg]ate|[Ss]pec|[Dd]raft|[Rr]ollback'): 380 passed / 0 failed on gfx1201 and on gfx1151 (base: 357 / 0). KvflashFusedMassFixture.device_kernel_matches_the_cpu_reference_at_d256_ct64 and device_failed_run_leaves_no_cuda_error_behind run and pass on both GPUs (no no CUDA/HIP device skip).
Tests (earlier form)
Head aec6a3e5c. test_server_unit <group>.
| Check | RTX 3090 (sm_86, WSL) | R9700 (gfx1201, lucebox6 hip:0) | Strix (gfx1151, lucebox6 hip:1) |
|---|---|---|---|
KvflashFusedMass | 7 passed, 0 failed | 7 passed, 0 failed | 7 passed, 0 failed |
| device vs CPU reference, worst (1 tile / 5 tiles) | 1.22e-06 / 1.22e-06 | 1.2e-06 / 1.2e-06 | 1.13e-06 / 1.13e-06 |
KvflashGpuMass (20 cases) | 20 passed | 20 passed (fused over archive vs graph worst 4.02e-07) | 20 passed (fused over archive vs graph worst 4.72e-07) |
KvflashPagerRecall | 4 passed | in the full suite | in the full suite |
full test_server_unit | 1401 passed, 0 failed, 7 skipped | 1382 passed, 0 failed, 5 skipped (private TMPDIR) | 1382 passed, 0 failed, 5 skipped (private TMPDIR) |
On lucebox6 the first full run with the shared /tmp failed one case on both cards, test_disk_cache_longest_prefix_and_prune_ancestors (test_server_unit.cpp:14732, cache.save); the log shows /tmp/luce_test_*.gguf files owned by another user that the suite could not open for writing. With TMPDIR set to a private directory the same binary passes all 1382 on both cards.
Selection and fallback cases in test_kvflash_gpu_mass.cpp: fused_kernel_is_selected_only_for_the_k_it_can_score, fused_request_on_other_k_types_scores_with_the_logits_graph, fused_kernel_over_the_archive_matches_the_logits_graph (5 residency steps, tolerance 1e-3).
Screen before the gate
lucebox6 R9700 hip:0 (gfx1201), 2026-11-11, luce_server md5 4346a634 built from 7cd7b260e on the screens branch (the same kernel, archive and selection code as this head, plus the experiment switches that are not in this PR), Qwen3.8-27B UD-IQ4_XS, 3 recorded sessions, 151 requests / 330,710 new tokens per arm, one binary and one session for all arms. Per-session values: median / mean / best / min.
| Arm | Session prefill (all) | Per session | Decode median | Wall | Scorer (36 calls) | P1 | P2 |
|---|---|---|---|---|---|---|---|
| K1 (lru + pin) | 800 tok/s (414 s) | 805 / 803 / 840 / 764 | 28.0 tok/s | 764 s | - | 0/9 | 5/9 |
Mode 4, G2 scorer (FUSED=0) | 679 tok/s (487 s), 0.849x | 687 / 681 / 698 / 657 | 27.1 tok/s | 851 s (1.115x) | 28.0 s, 31701 of 31701 chunks uploaded | 9/9 | 4/9 |
| Mode 4, this PR | 747 tok/s (443 s), 0.934x | 751 / 748 / 764 / 729 | 28.2 tok/s | 753 s (0.986x) | 4.0 s, 5985 of 31701 uploaded | 9/9 | 4/9 |
Picks, this PR against the G2 scorer: 137 pick lists compared, 137 identical.
Gate on the earlier form (aec6a3e5c)
lucebox4 R9700 hip:0 (gfx1201, PCIe 3.0 x2, the card of G2's gate), 2026-11-11, one luce_server (md5 1daf5b10, built from aec6a3e5c; libggml-hip.so.0 md5 9dc3e9b8) for every arm, Qwen3.8-27B UD-IQ4_XS, the same 14 sessions as G2's gate (882 requests, 40 P1 and 40 P2 probes per arm). The arms ran with G2's arm script m4cost_arm.sh (md5 4b1e2b8b) and were judged by m4cost_eval.py gate, both unchanged, with the bars fixed before the run. There is no m4cost_gate.sh file; G2's gate is that pair, called from its chain script. Server flags on every arm: --max-ctx 139264 --prefix-cache-max-mib 40960 --prefix-cache-slots 8 --disk-prefix-cache off --no-kv-cache --cache-type-k q8_0 --cache-type-v q8_0 --kvflash 16384. Runner: thinking on, effort medium, T 1.0, top_p 0.95, top_k 20, probe max_tokens 16384. Arms differ only in LUCE_KVFLASH_POLICY=lru on K1; arm identity is read from the server logs (K1 policy=lru, M4 recall profile: mode 4 and policy=qk, 851 preamble-pin lines each). Caching state: prompt tokens 62,012,284 on both arms, cached prefix 60,542,275 (K1) vs 60,542,272 (M4), prefix_len > 0 on 881 of 882 requests in both. No power limit set or recorded.
| Arm | P1 | P2 | Session prefill (all) | Per-session prefill med / mean / best / min | Per-session decode med / mean / best / min | Wall (per session med / mean / min / max) |
|---|---|---|---|---|---|---|
| K1 (lru + pin) | 0/40 | 24/40 | 812 tok/s (1811 s) | 822 / 814 / 882 / 717 | 29.2 / 29.2 / 29.2 / 29.1 | 3346 s (236 / 239 / 151 / 334) |
| Mode 4, this PR | 31/40 | 26/40 | 728 tok/s (2020 s), 0.896x | 750 / 733 / 818 / 619 | 29.0 / 29.0 / 29.1 / 28.8 | 3631 s, 1.085x (257 / 259 / 174 / 391) |
Decode over all requests with out >= 64: median 29.2 tok/s (n 80) on K1, 29.0 (n 71) on mode 4, 0.993x.
Gate lines: PASS prefill >= 0.85 x K1 (0.896), PASS P1 >= K1+3 (31 vs 0), PASS P2 >= K1-2 (26 vs 24), GATES M4 vs K1: PASS. Mode-4 paths: reused 637, prescore 80, full 93, skip-fits 21, skip-pool 20, none 31 (the same counts as G2's gate); re-prefill 63,332 tokens; paging 27.8 s. Scorer: 173 calls, all fused=1, 16.1 s total (h2d 8.3 s, kernel 4.7 s in the log field graph), 26,344 of 148,018 evicted chunks uploaded.
Against G2's gate (same box, card and sessions, other binary and another day's run, so not a same-binary comparison): scorer 106.3 s to 16.1 s, session prefill 697 to 728 tok/s, 0.860x to 0.896x of K1 (K1 811 and 812 tok/s), wall 1.11x to 1.085x, P1 and P2 unchanged at 31/40 and 26/40.
Picks control, same binary. A third arm ran 3 of the sessions (151 requests) with LUCE_KVFLASH_GPU_SCORE_FUSED=0 (the G2 scorer; started with a copy of the arm script that adds this arm and limits its idle check to card 0). Compared with the same 3 sessions of the gate's mode-4 arm:
| Arm (3 sessions) | Session prefill | Per session med / mean / best / min | Decode median | Wall | Scorer | P1 | P2 |
|---|---|---|---|---|---|---|---|
FUSED=0 (logits graph) | 736 tok/s (449 s) | 742 / 738 / 752 / 719 | 29.1 tok/s | 742 s | 36 calls, 23.5 s, 31701 of 31701 chunks uploaded | 9/9 | 4/9 |
| This PR (from the gate arm) | 770 tok/s (428 s) | 775 / 771 / 785 / 754 | 29.1 tok/s | 722 s | 3.6 s | 9/9 | 4/9 |
picks, M4 fused vs M4G graph: compared 137, identical 137, mean overlap 1.000. The two rows are not a clean speed pair: the control ran on a fresh server and the gate arm inside its 14-session run, and one request differs in cached prefix (new tokens 330,710 vs 329,645; prefix_len > 0 on 150 vs 151 of 151). The speed claim rests on the gate table; this arm is for the pick identity.
Limits
Not run:
- E20, EXECODE, LongMemEval-S and K0 on this head.
- The 14-session gate and its P1/P2 probes on this head; the restack gate above is 3 sessions without probes.
- Any engine run on CUDA or on Strix; their evidence is unit tests. The engine numbers are R9700 (HIP).
- The fallback path in an engine run on a non-q8_0 K (unit test only). The
FUSED=0control arm exercises the logits graph on q8_0. - Tensor parallelism (recall is off there, as on G2).
- Full
ctest; the suite run istest_server_unit. - A power limit is not recorded for any engine run.