Files
leleka/docs/kv-streaming.md
T

6.6 KiB

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.

  • Real-model result 2026-10-16 (job 312, RTX 5090, branch claude/long-context-fix 791a4fa, budget 24 GiB, capacity 2048, 40000-token prompt, greedy, 32 new tokens): both windows pass and print identical output ids (20480 cells: 5120 pages/layer, 0.246 GiB VRAM; 65536 cells: no eviction, 0.780 GiB VRAM); 12 rounds, ~133 s per request after load. needle_found=false only because 32 tokens end inside the model's <think> preamble (the text is already quoting the prompt's opening line); this is a correctness smoke, not a quality or speed result.

  • Jobs 310/311 had failed before that fix (Failed(IdentityMismatch)) at the publication of the SECOND batched prefill chunk of any prompt longer than one chunk: OriginalDraftSession::prepare_publication (session_original_draft.rs) checked the driver's per-chunk first_materialized (the chunk's start) against end - MTP_ORIGINAL_WINDOW. Unrelated to KV streaming; fixed in 791a4fa with unit tests.

Open

  • A longer generation (or a no-think prompt) to read the passcode answer; parity against the Original on the same ids.
  • 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).