docs(kv): KV storage and streaming port, selection, proofs and the long-context job

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-16 10:50:59 +02:00
co-authored by Claude Opus 5.5
parent 70f057578f
commit 6bfb44fab9
3 changed files with 82 additions and 1 deletions
+73
View File
@@ -0,0 +1,73 @@
# QSA KV storage and streaming (`--kv`, `--kv-resident`)
Port of the Original's `src/kernels/cuda/{kv_q8,kv_stream}.cu` (`include/strata/kernels/{kv_q8,kv_stream}.hpp`,
`layer.cpp::kv_plan`) for the production profile `--max-context 262144 --kv int8 --kv-resident 32768`.
No performance or quality claim is made here.
## Where each Original unit lives
| Original | Rust / CUDA | Notes |
| --- | --- | --- |
| `kv_append_q8_kernel` (decode / verify window) | `mtp_original_kv.cu` `mtp_original_append` | literal; host copy always, slot only if resident |
| `prefill/kernels.cu` `kv_append_kernel` (INT8, with host copy and stage) | `qsa_prompt_kv.cu` `leleka_qsa_kv_append` | literal batched form |
| `kv_gather_q8_kernel` | not needed | the sealed decode attention (`mtp_original_full_attention.cu`, `qsa_decode_attn.cu` mode 1) reads INT8 through the page table, as the Original's native path does |
| `kv_stream.cu` resolve / copy / reset | `qsa_prompt_resolver.cu` `leleka_qsa_resolve`, `leleka_qsa_miss_copy`, `leleka_qsa_stream_reset` | literal; `n_slots` (resident pages) is a kernel argument since `claude/kv-streaming` (was baked at 8192) |
| `kv_stage_from_host` (prompt path) | `OriginalQsaResidentTransport::copy_prefix` | DMA of the host prefix into the identity staging pool |
| `kv_stream_counters` | `Resolver::counters`, `OriginalQsaMappedJournal::stream_counters` | |
| `kv_plan` (mode 1 slots), `qsa_kv_resident_min()` | `leleka_context::geometry::QsaGeometry::resident_pages_for` | cross-checked against `experimental-ref::kv_stream::kv_plan` (480 golden cases there) |
| `qsa_set_kv_resident` | `leleka_cuda_driver::configure_kv_resident` (process-wide, set once before any QSA owner) | |
| per-layer state | `QsaPromptKv` (resident runs, page table, resolver, pinned mapped host copy) | every owner keeps its own slot count; the verify window binds it (`OriginalVerifyQsaKvBinding::slots`) |
Layout (unchanged, the Original's): codes `[page][kv_head][4][256]`, scales `[page][kv_head][4][4]` fp16,
1056 B per cell and layer; host copy in identity layout over the whole serving context; VRAM holds `slots`
pages and `page_table[block]` is the slot or -1.
## Behaviour and defaults
* The Rust stack always streams (the Original's `kv_mode == 1`). `--kv-resident N`: `ceil(max(N, 20480) / 4)`
pages per QSA layer; `0` = every page of the serving context (the Original's fully resident mode: the
streaming machinery stays on and never evicts, outputs are identical). Default 32768 cells, i.e. the 8192-page
pool every earlier build allocated, so the default behaviour is unchanged.
* Footprint at the production profile (`KvStreamFootprint`, printed by `OriginalServeStack::open` as
`leleka-serve kv int8 context=... resident=...`): 12 x 35,029,056 B VRAM, 277,086,212 B prompt staging pool,
12 x 276,825,088 B pinned host. A 20480-cell window frees 12 x 3072 x 4244 B (~156 MB) of VRAM.
* `--kv fp16|q4_0|k8v4` (and keyless) are **excluded**: no sealed append/attention owner on the serve path;
`StackConfig::validate` refuses them (`Unsupported(Session)`). CPU references and unsealed-path CUDA ports of
Q4_0/K8V4/rotated INT8 exist under `experimental-ref` and `experimental_kv_{q4,q8}.cu` (experimental, opt-in,
not consumed by the forward pass). Adding one needs a sealed append, a decode/prompt attention mode and the
window planner variant; not cheap, so not done.
* The MTP drafter's ring (20480 cells, `mtp_original_kv.cu` `mtp_original_ring_table`) is unchanged.
## Selection
`leleka-inference ... --backend-mode original [--kv int8] [--kv-resident CELLS]`; in code
`StackConfig { kv_format, kv_resident, .. }`.
## Proofs
* `leleka-context`: `resident_window_follows_kv_plan_and_reproduces_the_frozen_pool_at_the_default`,
`production_profile_footprint`; `leleka-cuda-driver`: `resident_pages_are_the_originals_kv_plan_slots`,
`resolve_launches_with_the_owners_resident_window_and_refuses_one_too_small`; `ptx_abi` requires the new
resolver parameter lists.
* GPU, model-free (`cargo test -p leleka-cuda-driver --features cuda-driver --lib -- --ignored kv_stream_gpu`;
needs `libcublas.so.12` on the library path for the workspace):
* `int8_kv_append_kernels_match_the_original_host_reference_bit_exactly`: both append kernels vs
`experimental-ref::kv_q8` (Rust twin of the Original host reference), 13.3 MB compared byte for byte
(ties, zero, subnormal and near-overflow scale groups).
* `streamed_attention_equals_fully_resident_attention_with_a_20480_cell_window`: 40960 cells, 5120 slots,
64 resolve calls (~31.5K misses, i.e. heavy eviction), decode attention bit-equal on the streamed pool, a
fully resident identity pool and the staged copy.
* Result 2026-10-16 on the RTX 5090 (driver 595.99.02, sealed sm_120 PTX, nvcc 13.2.86): both pass; also
passed on an RTX 3080 Laptop (sm_86 builds of the same sources, logic check only).
* Real model (operator job, not run by the porting agent): `original_serve_long_context_streamed_kv_smoke`,
env in `backend_original_serve_smoke.rs`: `LELEKA_ORIGINAL_SERVE_LONG_SMOKE=yes`,
`LELEKA_ORIGINAL_SERVE_LONG_TOKENS=40000`, `LELEKA_ORIGINAL_SERVE_KV_RESIDENT=20480` (one process per window),
`LELEKA_ORIGINAL_SERVE_MAX_CONTEXT=65536`, plus the usual model inputs. Compare the `long-smoke output ids` line
of a 20480 run with a 65536 run (no eviction): they must be identical.
## Open
* The real-model long-context run and its A/B against the non-evicting window.
* The freed VRAM is not yet handed to GPU expert slots (expert residency is ported separately); the prefill
chunk does not follow the Original's slot-derived ladder.
* Counters are not printed at the end of a request (the Original's `kv_stream` summary line).
+8 -1
View File
@@ -20,7 +20,10 @@ accepted-prefix commit. The preview path (the Rust-native decoder chain) is unch
## Selecting it
`leleka-inference ... [--backend-mode auto|original|preview] [--original-prefill-capacity N] [--mtp-rt-dir PATH] [--spec-min-p F]`
`leleka-inference ... [--backend-mode auto|original|preview] [--original-prefill-capacity N] [--mtp-rt-dir PATH] [--spec-min-p F] [--kv int8] [--kv-resident CELLS]`
`--kv` / `--kv-resident` select the QSA KV storage and the resident window (default INT8, 32768 cells per layer
in VRAM, the rest streamed from pinned host memory); see `docs/kv-streaming.md`.
* `auto` (default): the Original path when every compiled asset and every verifier window asset/producer it
needs is present in the build (`leleka_cuda_driver::unsealed_original_serve_assets()`: native head,
@@ -70,6 +73,10 @@ LELEKA_STRA169_MTP_PACK=… LELEKA_STRA169_MTP_STAGING=… \
cargo test -p leleka-runtime --features cuda-driver -- --ignored original_serve_smoke
```
The long-context variant (`original_serve_long_context_streamed_kv_smoke`, `LELEKA_ORIGINAL_SERVE_LONG_SMOKE=yes`,
`LELEKA_ORIGINAL_SERVE_LONG_TOKENS`, `LELEKA_ORIGINAL_SERVE_KV_RESIDENT`, `LELEKA_ORIGINAL_SERVE_MAX_CONTEXT`) is
described in `docs/kv-streaming.md`.
It opens the stack, serves a greedy request twice (the second must reproduce the first across a reset),
a sampled request with penalties, a request cancelled after its first committed round, and a final request
that must reproduce the first. While `UNBOUND_PRODUCERS` is non-empty the stack refuses to open with the
+1
View File
@@ -122,6 +122,7 @@ A `.cu` file may sit at **S** while its Rust host side sits at **L** or **O**; b
| Q4_0, K8V4, rotated-INT8 KV, FWHT-256 | `crates/experimental-ref/src/{fwht,q4_0,kv_q8,layout,cache}.rs`, `experimental_kv_{q4,q8}.cu` | references full; forward pass does not consume the formats | O (goldens from the Original's host C++, Separate and Fused FMA models); the literal `.cu` is run by a CPU thread emulator and compared bit for bit |
| Keyless V-only, query routing | `experimental-ref/src/{keyless,attn}.rs`, `experimental_{keyless,qsa_attn}.cu` | references full; quality gate previously failed, opt-in only | O (emulator), S |
| KV streaming planner and CLOCK resolver | `experimental-ref/src/kv_stream.rs`, `experimental_kv_stream.cu` | references full | O (480 plan cases + resolver) |
| `kv_q8.cu` INT8 append, `kv_stream.cu` resolve/copy/reset/stage, `--kv-resident` (serve path) | `qsa_prompt_kv.cu`, `mtp_original_kv.cu`, `qsa_prompt_resolver.{cu,rs}`, `qsa_resident_transport.rs`, `serving_context::configure_kv_resident`, `StackConfig::{kv_format, kv_resident}` | full for INT8; fp16/q4_0/k8v4 excluded on the serve path (`docs/kv-streaming.md`) | G: `kv_stream_gpu` model-free proofs on the RTX 5090 (2026-10-16); real-model long-context run pending |
| Experimental driver facade, opt-in, pins | `cuda-driver/src/experimental{,_gpu,_gpu_smoke}.rs`, `leleka-runtime/src/experimental.rs` | full; every mode refuses at Load until the consumer is wired (`FORWARD_CONSUMER_WIRED`) | L; entry signatures cross-checked against the `.cu` text (T) |
| `experimental_routed.hpp`, `experimental_experts.hpp` prefill patches | none | absent | not applicable |