r/LocalLLaMA • • 14h ago • Bravo 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 VRAMKV in host RAM
Max context192K1,048,576
Max context with DFlash 2 drafter134K1,048,576
Decode at 32K118 tok/s115 tok/s (−3%)
Decode at 64K115 tok/s110 tok/s (−4%)
262K promptdoesn't fit272 s prefill, then 86 tok/s (DFlash 2: 128 tok/s)
983K promptdoesn't fit31.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.

133 Upvotes

23 comments sorted by

2

u/LippyBumblebutt 11h ago

What was your commandline for this?

1

u/TheActualStudy 7h ago
inference serve --format gguf -m ggml-org/Qwen4-35B-A3B-GGUF -f Qwen4-35B-A3B-BF16.gguf \
  --pa-cache-type q8 --pa-kv-on-host \
  --mtp-model RadixArk/Qwen4-35B-A3B-Dflash2 --mtp-n-predict 3

I haven't published quants, so I've made you a load-time recipe that will quantize what is needed from the BF16 GGUF. This won't have my GPTQ work so KLD won't be as low (the weights aren't ready to publish yet).

49

u/sisyphus-cycle 14h 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/Ren_Zekta llama.cpp 13h ago

kv unified in llama.cpp makes slots use cache dynamically within context window

2

u/Pentium95 8h ago

kvu in llama.cpp is quite terrible, It slows down a lot and if a request doesn't fit (because other requests are using a lot of context in the shared pool) It crashes

1

u/unjustifiably_angry 5h ago

If I'm not mistaken, kv unified in llama.cpp requires you to use rope/yarn/whateveritis applied to the entire unified kv-cache as opposed to multiple discrete unscaled kv-caches. Compounding with with people tending to use q8_0 kv very often, I worry about the accuracy. Better to have smaller discrete pools.

1

u/IAmBJ 3h ago

The problem with that is you are gambling on the sum of your sub agents never exceeding the unified cache size, which inevitably fails without tight controls on the parallel workers. 4 agents with 256k max individual context can ( and eventually will) unexpectedly overfill any unified context <1M tokens

13

u/txgsync 12h 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.

1

u/wren6991 8h ago

QFN has the same sparse attention mechanism, no?

4

u/codepoet82 7h ago

Yes, this same thing worked perfectly well in QFN and has been common practice in strata since September.

1

u/txgsync 8h ago

Qwen4 has fewer QSA layers. Call it 20KiB per context token at BF16, vs. 24 for Flash-Next. When you're stacking RAM advantages -- Qwen4-35B-A3B at BF16 is considerably smaller than Qwen3.8-Flash-Next at around 4.5bpw which is about as small as I can tolerate without it going off the rails, with similar performance characteristics -- that lets me run multiple parallel sessions.

But you're right, it's really more about the model size itself, and less about the KV impact. The two combined mean Qwen4-35B-a3B is simply better for my use case which is highly-parallel sessions re-using the primary sessions KV cache to evaluate outputs along several axes.

12

u/fragment_me 12h 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!

1

u/unjustifiably_angry 5h ago

How's performance scale as actual context length increases though? I would assume that even if performance is very similar at 8K it might be dogwater by 200K let alone 1M.

1

u/fragment_me 4h ago

I only tested up to 128k context and it was never slower. I can post my exllama3-slop fork here in a bit.

4

u/alineali 9h ago

I wonder, is it possible in llama.cpp?

2

u/danishaznita 8h ago

There is kvmem streaming fork of llamacpp

2

u/a_beautiful_rhind 11h 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/bbqfetus 11h ago

NInfer does, but they are locked to Nvidia cards because the engine is tuned around cuda kernels.

2

u/wren6991 8h 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

Specifically, to mitigate the HBM bottleneck across diverse hardware configurations, we design MiniMax Sparse Attention 2 (MSA2) alongside MFlashKV, enabling runtime KV caches to be offloaded directly to SSDs without compromising latency.

1

u/valarauca14 7h 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.

1

u/ElSrJuez 1h ago

Im reading the git repo, could not find Qwen4 mention?

0

u/[deleted] 12h ago

[deleted] — view deleted comment

4

u/TheActualStudy 12h ago

4 sticks, two channels, DDR4, 2666 MT/s