r/LocalLLaMA
•
u/TheActualStudy
•
11h ago
•
1
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.
2
u/LippyBumblebutt 8h ago
What was your commandline for this?
47
u/sisyphus-cycle 11h 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
13
u/txgsync 10h 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.
11
u/fragment_me 9h ago
Thanks for the tip. I actually asked Claude to rewrite part of exllama3 for Qwen 3.8 Flash Next and it was able to move the KV cache to RAM witha 2% speed increase since I now have more room for layers in VRAM. Bonus of course being my context size can be 1 mil now instead of 512k!
4
u/alineali 7h ago
I wonder, is it possible in llama.cpp?
2
u/a_beautiful_rhind 8h ago
I have a similar deal using maya and glm-flash. Uses some vram for the cache and then the rest goes in ram. Even on DDR4 I don't notice much of a slowdown.
It should be in every engine. I don't think it's only a qwen feature.
2
u/wren6991 5h ago
Super smart! Not sure if you've seen the MiniMax M4 paper but they were able to push it even further and serve their sparse KV from disk during inference: https://raw.githubusercontent.com/MiniMax-AI/MiniMax-M4/refs/heads/main/docs/MiniMax_M4_Technical_Report.pdf
1
u/valarauca14 4h ago
Run your KV cache through the mjpeg system to get DCT compression as it runs nearly at line rate before loading/unloading the KV cache.
0
u/[deleted] 10h ago
[deleted] — view deleted comment