r/LocalLLaMA • u/TheActualStudy • 2h ago
Showcase Offloading KV Cache to Host RAM with QSA costs only ~5% speed: ~1M context on 1x3090
Qwen4-35B-A3B uses QSA, a trained sparse attention. Past a 2K-token budget, each query attends only to the top 512 four-token blocks its small indexer picks, plus the most recent tokens. Decode therefore never reads most of the KV cache, so the cache can live in pinned host RAM and the GPU reads it over PCIe.
How it works in my engine (inference.rs, Rust):
- The full K/V lives in host RAM. The GPU keeps only a small cache of recently selected blocks. Consecutive tokens pick mostly the same blocks, so about 98% of reads hit VRAM.
- The indexer stays in VRAM, compacted to one pooled key per block (64 B/token).
- Long prefill attends only to what each query selects, so VRAM use stays flat however long the prompt gets.
- Outputs are bit-identical to the in-VRAM path.
Setup: one RTX 3090 (24 GB), 128 GB RAM, a ~4.5-bit GGUF, q8 KV cache.
| - | KV in VRAM | KV in host RAM |
|---|---|---|
| Max context | 192K | 1,048,576 |
| Max context with DFlash 2 drafter | 134K | 1,048,576 |
| Decode at 32K | 118 tok/s | 115 tok/s (−3%) |
| Decode at 64K | 115 tok/s | 110 tok/s (−4%) |
| 262K prompt | doesn't fit | 272 s prefill, then 86 tok/s (DFlash 2: 128 tok/s) |
| 983K prompt | doesn't fit | 31.6 min prefill, then 55 tok/s |
It's a buffer more than a regular workflow: agents should compact long before this. But 983K tokens on one consumer GPU does run.
3
u/haherar830 30m ago
QSA is is kind of designed for this but I'm curious how many sticks of RAM that is (i.e. how many channels), DDR4/5, and what the clock speed is?
14
u/sisyphus-cycle 1h ago
Yeah this is huge, QSA is super clutch, being able to actually use —parallel with 4 slots at 250k context at f16 kv would have been unthinkable for me a week ago
3
u/txgsync 24m ago
Yeah this feature is exactly why I am considering replacing QFN with Qwen4. It lets me run more agents in parallel that reuse the prefix cache which I value more than a slight improvement in quality. I can improve quality by using multiple rounds of adversarial reviews, often using other fast/cheap models or a few bucks with a cloud provider.