BasinKV: Long Context Without the KV Cache Tax
- Published
- Authors
- Zellige
- Reading time
- 8 min
- #inference
- #kv-cache
- #long-context
- #systems
- #open-source
Drop-in sparse KV offloading to RAM and NVMe for llama.cpp, SGLang, and vLLM. No retraining, no new weights, and huge concurrency headroom with unmodified attention kernels inside CUDA graphs.
Contents6
Published by Zellige (2026-11-30). BasinKV is our open-source implementation and extension of the OasisKV lookahead-prefetch architecture [Xiao et al., arXiv:2608.08097]. For all 19 benchmark tables, McNemar significance tests, and roofline derivations, read the full technical paper.
Long-context serving hits the same memory wall on both local workstations and datacenter GPUs: KV cache grows linearly with sequence length, forcing stock engines to OOM or serialize batches while reading gigabytes of cold history on every decoded token.
BasinKV keeps a fixed-size working set of KV blocks per full-attention head in GPU memory (2,048 tokens by default), offloads the full KV history to pinned host RAM or NVMe (INT8 by default, uncompressed BF16 when RAM is abundant, or rotated 4-bit WHT-INT4 for maximum compression), scores evicted blocks on GPU (Shrunk-Box Quest INT8 with alpha = 0.25 by default, or 136-byte SphereIndex for tightest VRAM), and prefetches newly needed blocks asynchronously over PCIe UVA behind layer compute. Because the resident GPU buffer has a static shape, FlashAttention, FlashInfer, and full-model CUDA graphs run unmodified.
Flat decode throughput and bounded VRAM
On bare-metal A100-80GB PCIe in llama.cpp, BasinKV keeps decode throughput nearly flat through 128K and 256K while stock slows down as the KV cache grows. WHT-INT4 halves host RAM and NVMe footprint relative to INT8 with less than 0.5% difference in decode speed:
That fixed GPU footprint carries directly to tighter cards. On a 24 GB NVIDIA L4 (23,034 MiB total / 22,614 MiB ECC-usable), where Qwen3.6-35B-A3B-UD-Q4_K_M weights alone take 20.60 GiB, stock llama.cpp hits CUDA OOM starting at 64K. BasinKV keeps the entire 256K GPU KV cache plus index in 84.75 MiB (1.29 GiB in pinned host RAM), decodes flat from 69.86 tok/s (d = 0) to 67.12 tok/s (256K) with 255/256 CUDA graphs reused, and passes live llama-server -c 262144 passkey retrieval at 64K (59.5 tok/s, 21,327 MiB peak) and 203K (60.4 tok/s, 21,885 MiB peak).
(open full-size image in a new tab)Results across llama.cpp, SGLang, and vLLM V1
| Engine & workload | Hardware | Stock baseline | BasinKV | Quality check |
|---|---|---|---|---|
llama.cpp Qwen3.6-35B-A3B Q4_K_M @ 256K | NVIDIA L4 (24 GB) | CUDA OOM (from 64K) | 67.12 tok/s, 84.8 MiB GPU KV+idx (21.4 GiB peak) | Live 64K & 203K passkeys: PASS |
llama.cpp Qwen3.6-35B-A3B Q4_K_M @ 256K | A100-80GB PCIe | 95.01 tok/s (31.5 GiB) | 141.12 tok/s (1.49x), 22.4 GiB (-8.91 GiB) | 18/18 iterative coding; 96.2% retrieval @ 240K |
llama.cpp Qwen3-8B Q4_K_M @ 128K | A100-80GB PCIe | 54.28 tok/s | 124.29 tok/s (2.29x) | 16/18 coding (W = 4,096) |
SGLang Qwen3.8-27B BF16, B = 16 x 64K (1.02M tok) | 1x A100-80GB | OOM w/ MTP (~97 no-MTP cap) | 581.4 tok/s w/ native MTP, 18.13 GB free | 16/16 unique passkeys |
SGLang Qwen3.8-27B-FP8, B = 16 x 64K (1.02M tok) | 1x H100-80GB | 358.0 tok/s FP8 KV (264.8 s) / 33.9 BF16 KV | 470.4 tok/s (1.31x), 130.3 s wall (2.03x vs FP8 KV) | 16/16 passkeys; 5/5 64K RULER |
| SGLang cold block-sparse prefill @ 240K | 1x H100-80GB | 43.88 s dense streaming TTFT | 33.38 s (-10.50 s) via fused Triton + UVA gather | 40/40 RULER fields (64K to 240K) |
vLLM V1 zero-fork OKVP1 prefix cache (Qwen3-8B) | A100-80GB PCIe | 961.4 ms repeated request | 456.5 ms RAM / 702.4 ms NVMe (728.4 ms post-restart) | 1.94x smaller on disk vs INT8 |
In SGLang on H100 with FP8 weights, Qwen3.8-27B-FP8 at B = 16 x 64K hits 470.4 tok/s and cuts batch wall time in half against stock FP8 KV (130.3 s vs 264.8 s), while keeping resident KV near 2 GiB on Qwen3.6-35B-A3B-FP8 (where stock's full 20 GiB KV still fits inside H100 HBM at B = 16).
(open full-size image in a new tab)In vLLM V1 (0.19.1), our zero-fork KVConnectorBase_V1 plugin drops directly into stock vllm serve to persist rotated 4-bit prefix caches to RAM or NVMe (OKVP1) across process restarts, cutting on-disk snapshots by 1.94x and speeding up NVMe hits by 1.36x over INT8:
(open full-size image in a new tab)Try it
(Repository paths and CLI flags in v0.1 retain the oasiskv namespace.)
Clone BasinKV, then run the steps below from inside its folder:
git clone https://github.com/zellige-ai/basinkv && cd basinkv
1. llama.cpp
git clone https://github.com/ggml-org/llama.cpp
git -C llama.cpp checkout ca123ba76311290ec6ab041dd2058fed8d9f03d2
python3 integrations/llamacpp/apply_llamacpp_wiring.py llama.cpp
cmake -S llama.cpp -B llama.cpp/build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="86;89"
cmake --build llama.cpp/build --target llama-server llama-bench -j
./llama.cpp/build/bin/llama-server -m Qwen3.6-35B-A3B-UD-Q4_K_M.gguf \
-ngl 99 -c 262144 -b 1024 -ub 1024 -fa on -np 1 \
--oasiskv --oasiskv-resident 2048 --oasiskv-cap 8 \
--oasiskv-index quest --oasiskv-alpha 0.25 --oasiskv-kv-quant int8
2. SGLang
pip install -e .
git clone https://github.com/sgl-project/sglang.git /tmp/sglang
git -C /tmp/sglang checkout a3f0bff5babfe18d3862e134449cd8fd11fcb84a
pip install -e /tmp/sglang/python
python3 integrations/sglang/apply_wiring.py /tmp/sglang integrations/sglang
python3 -m sglang.launch_server \
--model-path Qwen/Qwen3.8-27B-FP8 --context-length 262144 \
--enable-oasiskv --attention-backend oasiskv --disable-radix-cache --page-size 16 \
--oasiskv-config '{"resident_tokens":2048,"sink_blocks":4,"local_blocks":8,"transfer_cap_c":16,"pcie_quant":"int8","index_quant":"int8","quest_alpha":0.25,"successor_cofetch":true,"lazy_host_slabs":true,"streaming_prefill":true,"draft_ring_window":4096,"draft_ring_sink":64}' \
--speculative-algorithm NEXTN --speculative-num-steps 3 \
--speculative-eagle-topk 1 --speculative-num-draft-tokens 4 \
--enable-linear-replayssm-spec
3. vLLM V1 (zero-fork prefix cache)
vllm serve Qwen/Qwen3-8B --kv-transfer-config '{
"kv_connector": "OasisKVConnector",
"kv_connector_module_path": "integrations.vllm.oasiskv_connector",
"kv_role": "kv_both",
"kv_connector_extra_config": {
"cache_mode": "mixed",
"pcie_quant": "int8",
"prefix_cache_dir": "/tmp/basinkv_cache",
"prefix_cache_ram_gb": 4.0,
"prefix_cache_nvme_gb": 32.0
}
}'
(The zero-fork vLLM V1 plugin persists host KV formats only (none, int8, or wht_int4), with no Quest or SphereIndex block index. For tight host RAM, NVMe budgets, or maximum multi-user concurrency, switch to the high-compression tier: --oasiskv-index sphere --oasiskv-beta 0.10 --oasiskv-kv-quant wht_int4 in llama.cpp, "pcie_quant": "wht_int4", "index_quant": "sphere_int4", "sphere_beta": 0.10 in SGLang, or "pcie_quant": "wht_int4" in vLLM. In SGLang and vLLM with abundant host RAM, "pcie_quant": "none" keeps uncompressed BF16.)
Five technical pieces behind the numbers
- Partial-RoPE radius weight (
alpha = 0.25). Qwen3.x hybrids put 64 rotary and 192 non-rotary dimensions in each full-attention head. On native BF16Qwen/Qwen3.8-27B, the non-rotary box radius averages2.70xthe midpoint magnitude (1.898vs0.704), drowning dot-product signal under unweighted Quest (alpha = 1.0,29.9%Top-8 recall,0/5RULER at 128K). Dropping the radius weight toalpha = 0.25raises per-step Top-8 recall from82.5%(OasisKV Eq. 3alpha = 0.50sum) to95.5%(93.2%under GQA max) and restores5/5RULER with48/48greedy prefix parity. SphereIndex(136 bytes/block,7.53xsmaller than BF16 min/max). Instead of storing per-block BF16 min/max bounds (1,024 B/blkatD = 256), SphereIndex rotates the block centroid with a fixed randomized Walsh-Hadamard transform, quantizes it to symmetric INT4, and appends two FP16 subspace radii (r_rope,r_nope). AtD = 256, Top-8 salient-event recall improves from61.8%to70.2%while cutting index memory7.53x.WHT-INT4rotated host KV (1.97xsmaller than INT8). Each offloaded K and V vector is Hadamard-rotated and quantized to symmetric 4-bit (D/2 + 4bytes/vector), cutting synthetic outlier L2 error3.9xvs naive INT4 and doubling host concurrency and NVMe cache density at equal decode speed.- Engine plumbing that respects CUDA graphs. Static fixed-shape GPU slabs, lazy host slab allocation guarded by
/proc/meminfo, a ring-mapped FP8 draft KV pool for hybridNEXTNMTP, direct-to-host streaming prefill, an optional fused Triton block-sparse prefill kernel (-10.50 sat 240K), and atomic CRC32-checkedOKVP1/OKVS1prefix snapshots (including recurrent states for hybrids). - Best-effort 1-hop successor (
b + 1) co-fetch for induction heads. Running BasinKV live in coding agents exposed a block-sparse failure mode: when a multi-token cue (<tool_call>\n<function=) straddles a 16-token block boundary, fetching blockbwithoutb + 1flips induction-head logits by +30 to +40 nats (30%to60%failure rate at offsets 14 or 15 vs0/43aligned). Propagating each eligible block's score one hop forward (nextafter(score[b], -inf)) before top-k selection promotes boundary-straddling pairs at zero VRAM cost (0/30observed live boundary failures on H100).
Quality and practical notes
- Quality parity on hybrids:
100%retrieval/QA on 64K to 128K RULER,18/18on 128K to 192K 3-turn iterative coding,7/10vs7/10on 128K/224K RepoPatch-LC,45.0%vs42.5%stock on an 80-item stratified LongBench v2 subset (paired 95% CI-3.8to+8.8pts), and flat throughput across non-Qwen Mamba2 hybrids (IBM Granite-4.0-H-Small and NVIDIA Nemotron-Nano-9B-v2). - Choosing host KV and index precision: A controlled 6-arm H100 logit ablation on 66K to 199K live coding transcripts (
Qwen3.8-27B-FP8) showed whyint8+ QuestINT8(alpha = 0.25) is the cleanest default:int8matches uncompressed BF16 runner-up logit margins on rare exact identifiers (+12.75to+13.00vs+12.50to+13.12nats), whereaswht_int4compresses tail margins by ~5 nats (+7.75to+7.88nats). Under 4-tokenNEXTNMTP at 199K, QuestINT8(alpha = 0.25) also avoids occasionalSphereIndexcentroid-dilution spikes (-0.06mean /+1.50worst nat delta vs+2.44/+4.87underwht_int4+sphere_int4, and+6.12worst even under BF16 +sphere_int4). Reach forwht_int4+sphere_int4when host RAM, NVMe footprint, or peak concurrency is the binding constraint (as in our24 GBL4 and1.02M-token SGLang throughput runs above). - When full KV already fits in HBM: Stock full-HBM serving is still faster when there is no memory pressure. On H100
Qwen3.6-35B-A3B-FP8atB = 16 x 64K(where all 20 GiB of BF16 KV fits comfortably in HBM), stock decodes20.1%faster (1,141.2vs950.2 tok/s), while BasinKV holds resident KV near2 GiBso concurrency and context can keep scaling past the HBM wall (details in §5.8 of the technical paper). - Tuning pure-attention models: Deep 36-layer pure-attention models (
Qwen3-8B) compound per-layer misses more steeply than interleaved hybrids; useW = 4,096instead of2,048. - Global counting at sub-1% budgets: Unkeyed global counting tasks like 240K RULER
cwe(finding the 10 most common words scattered across ~300 blocks) drop from50%to0%under a0.83%resident window (W = 2,048of 245K); see §6.1 of the technical paper for the full depth breakdown and sampling analysis.
Read more
- Full technical paper (19 tables, methodology, roofline analysis): BasinKV Technical Paper
- Practical tuning recipes and flag reference: Tinkering & Tuning Guide
- Raw measurements and commands: Benchmark Log