Research: KVarN (variance-normalized KV-cache quantization)
Research Stage
- [x] Background Research (Let's try to avoid reinventing the wheel)
- [x] Hypothesis Formed (How do you think this will work and it's effect?)
- [ ] Strategy / Implementation Forming
- [ ] Analysis of results
- [x] Debrief / Documentation (So people in the future can learn from us)
Previous existing literature and research
I would like to document a recent KV-cache quantization paper that may be relevant to ongoing and future work in llama.cpp:
- Paper: KVarN: Variance-Normalized KV-Cache Quantization Mitigates Error Accumulation in Reasoning Tasks
- Reference implementation:
huawei-csl/KVarN
This issue is not a request for immediate implementation. The goal is simply to make the project aware of this work and record it as a potentially relevant research direction.
The paper focuses on an important distinction in KV-cache quantization evaluation:
- many methods are evaluated mainly in static or prefill-like settings;
- real autoregressive decoding repeatedly writes new KV states into a quantized cache;
- in that regime, quantization errors can accumulate over time and affect later generation disproportionately.
The paper argues that, in this decode-time setting, a significant part of the harmful error comes from incorrect per-token magnitude scaling, especially on the key side, rather than only from generic average reconstruction noise.
KVarN addresses this with a combination of:
- Hadamard rotation in the channel dimension;
- iterative variance normalization across both token and channel axes;
- low-bit round-to-nearest quantization.
The authors report that this reduces error accumulation and improves low-bit KV-cache quantization quality on long-form generative tasks, especially reasoning-heavy ones.
At a minimum, this seems worth documenting because it frames KV-cache quantization as a decode-time robustness problem, not only as a static tensor compression problem.
Hypothesis
This seems relevant to llama.cpp for several reasons:
- KV-cache memory pressure is already a major practical constraint for long-context inference.
llama.cppis used in environments where VRAM or unified memory is limited, so better KV-cache compression can have immediate practical value.- The project already has active work around KV-cache quantization, so this paper may be useful as background for evaluating future directions.
My main reason for opening this is not to claim that KVarN should necessarily be implemented as-is, but that its central observations may be useful for the project:
- decode-time error accumulation may be a more relevant benchmark target than static reconstruction quality alone;
- preserving token magnitude may matter more than one would expect from average error metrics;
- methods that look competitive in prefill-like evaluation may behave differently during long autoregressive generation.
If those observations also hold in llama.cpp workloads, they could influence how KV-cache quantization approaches are compared and prioritized.
Implementation
No implementation proposal in this issue. This is intentionally only a research-tracking and documentation issue.
This is the reference implementation: huawei-csl/KVarN
Analysis
Some points from the paper that appear especially relevant from a research perspective:
- the paper explicitly distinguishes between prefill-style evaluation and pseudo-decode evaluation;
- it argues that a small fraction of outlier quantization errors can dominate end-to-end degradation;
- it attributes many of those outliers to token-scale distortion, especially for keys;
- it reports strong results at very low KV-cache precision on reasoning and long-generation tasks.
That makes it potentially relevant not just as "another quantization method", but as a reminder that evaluation methodology matters a lot here.
Some open research-level questions that may be interesting for llama.cpp maintainers and contributors:
- Do the same error-accumulation effects show up clearly in
llama.cpplong-generation workloads? - Are current KV-cache quantization benchmarks in the project sufficiently decode-oriented?
- Would the paper's qualitative findings still hold across CPU, Metal, CUDA, Vulkan, HIP, and mixed-backend setups?
- Even if the full method is not attractive, are some of its ideas useful as evaluation guidance for future KV-cache quantization work?
I also checked for an existing issue specifically about KVarN and did not find one. There is clearly already active KV-cache quantization work in the repository, so this is meant as additional background and context rather than a duplicate feature request.
KVarN is now implemented in a publicly available BeeLlama.cpp v0.3.2 Preview (folk of llama.cpp), and you can try it yourself with --cache-type-k kvarn4 and --cache-type-v kvarn4 or whatever bits you want. If it works on your platform, because I only have RTX 3090 for testing. Qwen 3.6 27B and Gemma 4 31B are supported for sure, and their smaller versions probably are as well.
Now, should you try it? To answer this question, I booted up the good old KLD benchmark and started comparing KVarN to my collection of 50-something quant pairs. And it's not that bad. KVarN actually appears to be punching above it's weight even compared to rotation-enabled llama.cpp quants.
TL;DR is that it delivers q5 quality at 4-bit, and q4 quality at 3.5-bit. And that's on a very raw implementation. Probably can improved further. Especially speed. For speed I'm not claiming anything at all, it's really is just too raw to compare it. But the mature implementation in paper had it faster than usual quants.
Is it fp16 quality? No. Is it still better than like anything else in llama.cpp ecosystem? Look like yes.
KLD results on Qwen 3.6 27B Q5_K_S + 64k context
The rest of benchmark data and in-depth analysis are available in the article.
| Cache | Size | Mean KLD | Mean precision | 99.9% KLD | 99.9% precision | Tok/s |
|---|---|---|---|---|---|---|
| bf16 | 100.0% | 0.000375 | 100.00% | 0.023258 | 100.00% | 850.81 |
| q8_0 | 53.1% | 0.002328 | 99.80% | 0.078709 | 94.61% | 851.11 |
| q8_0-q5_1 | 45.3% | 0.002529 | 99.78% | 0.082880 | 94.21% | 828.63 |
| q8_0-q4_0 | 40.6% | 0.003316 | 99.71% | 0.104680 | 92.18% | 849.37 |
| q6_0 | 40.6% | 0.002614 | 99.78% | 0.090800 | 93.47% | 845.96 |
| q6_0-q5_0 | 37.5% | 0.002820 | 99.76% | 0.092682 | 93.29% | 846.86 |
| q5_1 | 37.5% | 0.002911 | 99.75% | 0.098354 | 92.77% | 841.65 |
| q5_0 | 34.4% | 0.003206 | 99.72% | 0.099073 | 92.70% | 849.79 |
| q5_0-q4_0 | 31.3% | 0.003581 | 99.68% | 0.113332 | 91.39% | 847.64 |
| q4_0 | 28.1% | 0.004711 | 99.57% | 0.130419 | 89.84% | 855.08 |
| kvarn4-kvarn4 | 27.9% | 0.002974 | 99.74% | 0.094819 | 93.09% | 760.88 |
| q5_0-turbo3_tcq | 27.3% | 0.005471 | 99.49% | 0.158514 | 87.35% | 815.80 |
| turbo4 | 25.8% | 0.004760 | 99.55% | 0.138370 | 89.13% | 705.32 |
| kvarn4-kvarn3 | 24.8% | 0.003824 | 99.66% | 0.135028 | 89.42% | 765.23 |
| q4_0-turbo3_tcq | 24.2% | 0.006269 | 99.41% | 0.186572 | 84.93% | 821.89 |
| kvarn4-kvarn2 | 21.7% | 0.010449 | 99.00% | 0.340392 | 72.82% | 765.57 |
| kvarn3-kvarn3 | 21.7% | 0.005349 | 99.50% | 0.168135 | 86.51% | 773.12 |
| turbo3_tcq | 20.3% | 0.007978 | 99.24% | 0.227104 | 81.56% | 795.20 |
| kvarn3-kvarn2 | 18.6% | 0.011122 | 98.93% | 0.345995 | 72.42% | 773.65 |
| kvarn2-kvarn2 | 15.4% | 0.021395 | 97.92% | 0.630208 | 54.50% | 776.81 |
| turbo2_tcq | 14.1% | 0.023073 | 97.76% | 0.632401 | 54.38% | 807.25 |
Added support of 5-bit, 6-bit and 8-bit KVarN to the latest BeeLlama.cpp v0.3.2 Preview.
It appears that the initial "punch one tier higher than its weight" principle holds up for 5-bit, 6-bit and 8-bit KVarN as well. This means you can match q8_0 while only paying for 6-bit memory, or even 5.5-bit by going for 6/5 combo with minimal losses. But there's also good quality at just 4-bit or asymmetrical 5/4-bit pairs.
Prompt processing is slower for now, but I'm not claiming it as inevitable yet. The implementation is very much raw and likely might be optimized further.
KLD results on Qwen 3.6 27B Q5_K_S + 64k context
The rest of benchmark data and in-depth analysis are available in the article.
| Cache | Size | Mean KLD | Mean precision | 99.9% KLD | 99.9% precision | Tok/s |
|---|---|---|---|---|---|---|
| bf16 | 100.0% | 0.000375 | 100.00% | 0.023258 | 100.00% | 850.81 |
| kvarn8-kvarn8 | 52.9% | 0.002361 | 99.80% | 0.076809 | 94.79% | 634.12 |
| q8_0 | 53.1% | 0.002328 | 99.80% | 0.078709 | 94.61% | 851.11 |
| kvarn8-kvarn6 | 46.7% | 0.002390 | 99.80% | 0.082415 | 94.26% | 643.46 |
| kvarn8-kvarn5 | 43.6% | 0.002266 | 99.81% | 0.084573 | 94.05% | 646.63 |
| kvarn6-kvarn6 | 40.4% | 0.002338 | 99.80% | 0.078797 | 94.60% | 689.31 |
| q8_0-q5_1 | 45.3% | 0.002529 | 99.78% | 0.082880 | 94.21% | 828.63 |
| kvarn8-kvarn4 | 40.4% | 0.002533 | 99.78% | 0.086218 | 93.90% | 645.67 |
| q8_0-q4_0 | 40.6% | 0.003316 | 99.71% | 0.104680 | 92.18% | 849.37 |
| q6_0 | 40.6% | 0.002614 | 99.78% | 0.090800 | 93.47% | 845.96 |
| kvarn6-kvarn5 | 37.3% | 0.002602 | 99.78% | 0.079818 | 94.50% | 692.77 |
| kvarn8-kvarn3 | 37.3% | 0.003529 | 99.69% | 0.121564 | 90.64% | 649.84 |
| kvarn5-kvarn5 | 34.2% | 0.002705 | 99.77% | 0.083457 | 94.16% | 699.80 |
| kvarn6-kvarn4 | 34.2% | 0.002831 | 99.75% | 0.091507 | 93.40% | 694.79 |
| kvarn8-kvarn2 | 34.2% | 0.009494 | 99.09% | 0.325652 | 73.90% | 651.45 |
| q6_0-q5_0 | 37.5% | 0.002820 | 99.76% | 0.092682 | 93.29% | 846.86 |
| q5_1 | 37.5% | 0.002911 | 99.75% | 0.098354 | 92.77% | 841.65 |
| q5_0 | 34.4% | 0.003206 | 99.72% | 0.099073 | 92.70% | 849.79 |
| kvarn5-kvarn4 | 31.1% | 0.002824 | 99.76% | 0.093313 | 93.23% | 700.73 |
| kvarn6-kvarn3 | 31.1% | 0.003533 | 99.68% | 0.123369 | 90.47% | 697.01 |
| q5_0-q4_0 | 31.3% | 0.003581 | 99.68% | 0.113332 | 91.39% | 847.64 |
| kvarn5-kvarn3 | 27.9% | 0.003515 | 99.69% | 0.118848 | 90.88% | 701.67 |
| kvarn6-kvarn2 | 27.9% | 0.009301 | 99.11% | 0.310819 | 75.01% | 697.56 |
| q4_0 | 28.1% | 0.004711 | 99.57% | 0.130419 | 89.84% | 855.08 |
| kvarn4-kvarn4 | 27.9% | 0.002974 | 99.74% | 0.094819 | 93.09% | 760.88 |
| kvarn5-kvarn2 | 24.8% | 0.009813 | 99.06% | 0.344122 | 72.55% | 705.26 |
| q5_0-turbo3_tcq | 27.3% | 0.005471 | 99.49% | 0.158514 | 87.35% | 815.80 |
| turbo4 | 25.8% | 0.004760 | 99.55% | 0.138370 | 89.13% | 705.32 |
| kvarn4-kvarn3 | 24.8% | 0.003824 | 99.66% | 0.135028 | 89.42% | 765.23 |
| kvarn3-kvarn4 | 24.8% | 0.004652 | 99.57% | 0.140358 | 88.95% | 770.52 |
| q4_0-turbo3_tcq | 24.2% | 0.006269 | 99.41% | 0.186572 | 84.93% | 821.89 |
| kvarn4-kvarn2 | 21.7% | 0.010449 | 99.00% | 0.340392 | 72.82% | 765.57 |
| kvarn3-kvarn3 | 21.7% | 0.005349 | 99.50% | 0.168135 | 86.51% | 773.12 |
| kvarn2-kvarn4 | 21.7% | 0.013639 | 98.68% | 0.418240 | 67.37% | 771.78 |
| turbo3_tcq | 20.3% | 0.007978 | 99.24% | 0.227104 | 81.56% | 795.20 |
| kvarn3-kvarn2 | 18.6% | 0.011122 | 98.93% | 0.345995 | 72.42% | 773.65 |
| kvarn2-kvarn3 | 18.6% | 0.014589 | 98.59% | 0.445014 | 65.59% | 773.83 |
| kvarn2-kvarn2 | 15.4% | 0.021395 | 97.92% | 0.630208 | 54.50% | 776.81 |
| turbo2_tcq | 14.1% | 0.023073 | 97.76% | 0.632401 | 54.38% | 807.25 |
kvarn4-kvarn4 looks almost too good to be true. kvarn5-kvarn5 looks like a solid choice.
I will conduct the full set of same benchmarks on a Gemma model to see how it holds up there.
Quick test of PPL with wikitext-2-raw Looks appealing, I'll use kvarn4-kvarn4.
Qwen3.6-27B-AutoRound-GGUF Q4_K_M -ctv f16 -ctk f16 PPL = 6.9746 +/- 0.04608 Qwen3.6-27B-AutoRound-GGUF Q4_K_M -ctv kvarn4 -ctk kvarn4 PPL = 6.9755 +/- 0.04608 Qwen3.6-27B-AutoRound-GGUF Q4_K_M -ctv q4_0 -ctk q4_0 PPL = 6.9875 +/- 0.04618
Qwen3.6-27B-Omnimerge IQ3_M MTP -ctv f16 -ctk f16 PPL = 7.0272 +/- 0.04502 Qwen3.6-27B-Omnimerge IQ3_M MTP -ctv kvarn4 -ctk kvarn4 PPL = 7.0289 +/- 0.04505 Qwen3.6-27B-Omnimerge IQ3_M MTP -ctv q4_0 -ctk q4_0 PPL = 7.0403 +/- 0.04511
qwen3.6-27b-IQ4_XS-pure-with-MTP-IQ4 -ctv f16 -ctk f16 PPL = 7.0547 +/- 0.04682 qwen3.6-27b-IQ4_XS-pure-with-MTP-IQ4 -ctv kvarn4 -ctk kvarn4 PPL = 7.0551 +/- 0.04682 qwen3.6-27b-IQ4_XS-pure-with-MTP-IQ4 -ctv q4_0 -ctk q4_0 PPL = 7.0672 +/- 0.04690
Datapoint on whether the tier rule generalizes: in our E8 lattice KV quant project (NexusQuant) the same 3-bit key budget that stays near +0.3% PPL on Mistral and Llama 3.1 blows up Qwen2.5-7B to four-digit PPL unless the first and last two layers stay fp16. A Qwen2.5 run next to the Gemma benchmarks would tell whether KVarN hits the same wall.
Ran a matched-bpe version of this on Llama-2-7B, wikitext-2-raw. Our E8 KV quant at 2.625 bpe (3-bit keys, 2-bit values) came out +0.42% PPL vs its own fp16; q4_0 in the same run sat at 4.5 bpe and +0.61% vs its own fp16. So lower bits and a smaller delta at that point. q5_1 at 6.0 bpe was cleaner (+0.09%) but at well over double the bit rate.
One caveat so this isn't misread: absolute PPL isn't comparable across the two stacks (different tokenizer and perplexity harness, our fp16 baseline is 5.04 vs llama.cpp f16 6.45), so only each stack's own %delta and the bpe axis line up. A KVarN point measured the same way (its own %delta vs f16 at a stated bpe) would drop straight onto that axis.