RefineKV: Query-Adaptive KV Cache Compression via Attention-Error Bounds
Organizations: National University of Singapore
Abstract
Repeatedly accessing the full key--value (KV) cache is a major bottleneck in long-context language model decoding. We find that small query-specific subsets can accurately approximate full attention, yet their union covers nearly the entire cache, motivating query-adaptive access for the KV cache. A natural approach is to split the KV cache into clusters, using compact representatives for clusters that incur negligible error while expanding the rest to their original KV entries. We derive a selection criterion from an upper bound on attention-output error, which reveals two complementary factors governing the error: importance measures the impact of a cluster, while distortion measures how faithfully the cluster can be represented. Based on this criterion, we propose RefineKV, a decoding-time query-adaptive KV compression method. Experiments on two models and three benchmarks demonstrate that RefineKV retains near-full-attention quality while achieving up to 7.6x speedup in the attention phase and 1.66x faster decoding, establishing a new quality-latency Pareto frontier.
Figures & tables
| Method | LongBench | RULER | HELMET | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Doc QA | Summ. | Few-shot | Synth. | Code | NIAH | A&T | QA | RAG | LC-QA | ICL | Rec./Rank. | ||
| Llama | Full | ||||||||||||
| Snap | |||||||||||||
| Snap | |||||||||||||
| Str. | |||||||||||||
| Str. | |||||||||||||
| Score | Acc. | Acc. | ||
| Ours ( , box) | 70.27 | — | 62.58 | — |
| , ellipsoid | 68.90 | 57.29 | ||
| Quest-style | 66.38 | 58.87 | ||
| 58.78 | 41.38 | |||
| 59.35 | 42.87 | |||
| Ref. | Threshold | Cluster size | GQA | Summary | Anchors | |||||
|---|---|---|---|---|---|---|---|---|---|---|
| Metric | Full | Ours | mean | ellipsoid | ||||||
| Acc. | 73.37 | 70.19 | 73.63 | 62.58 | 34.67 | 70.36 | 70.34 | 68.82 | 68.90 | 72.99 |
| TPOT (ms) | 24.43 | 15.68 | 19.08 | 14.62 | 14.42 | 15.86 | 16.20 | 15.06 | 15.14 | 17.14 |
| Speedup | 1.00 | 1.56 | 1.28 | 1.67 | 1.69 | 1.54 | 1.51 | 1.62 | 1.61 | 1.43 |
Appendix figures & tables13 assets
Supplementary material from the paper’s appendix.
Appendix
| Metric | Ref. | Number of query anchors | |||
|---|---|---|---|---|---|
| Full | (default) | ||||
| Acc. | |||||
| Steady TPOT (ms) | |||||
| Speedup | |||||
| Score | Raw | Partial |
|---|---|---|
| .975 | – | |
| .975 | .06 | |
| Quest-style bound | .864 | .32 |
| (box ) | .975 | .49 |
| (exact , oracle) | .990 | .81 |
| Sequence length | Latency (s) | Fraction (%) | |||
|---|---|---|---|---|---|
| Prefill | Decode | Prefill | Decode | Prefill | Decode |
| 16K | 512 | 1.01 | 9.20 | 9.9 | 90.1 |
| 32K | 512 | 2.42 | 11.15 | 17.8 | 82.2 |
| 32K | 1024 | 2.41 | 22.37 | 9.7 | 90.3 |
| TPOT (ms/token) | |||
|---|---|---|---|
| Benchmark | 128 tokens | 256 tokens | 512 tokens |
| LongBench | 14.76 | 14.61 ( ) | 14.55 ( ) |
| RULER | 16.35 | 16.20 ( ) | 16.14 ( ) |
| HELMET | 17.20 | 17.06 ( ) | 17.01 ( ) |
| Method | S-Doc QA | M-Doc QA | Summ. | Few-shot | Synth. | Code | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| NarrativeQA | Qasper | MultiFieldQA | HotpotQA | 2WikiMQA | MuSiQue | GovReport | QMSum | MultiNews | TREC | TriviaQA | SAMSum | Count | Retrieval | LCC | RepoBench-P | ||
| Llama | Full | ||||||||||||||||
| Snap | |||||||||||||||||
| Snap | |||||||||||||||||
| Str. | |||||||||||||||||
| Str. | |||||||||||||||||
| Method | S-NIAH | MK-NIAH | MV/MQ-NIAH | A&T | QA | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| NIAH-S1 | NIAH-S2 | NIAH-S3 | NIAH-MK1 | NIAH-MK2 | NIAH-MK3 | NIAH-MV | NIAH-MQ | CWE | FWE | VT | QA-1 | QA-2 | ||
| Llama | Full | |||||||||||||
| Snap | ||||||||||||||
| Snap | ||||||||||||||
| Str. | ||||||||||||||
| Str. | ||||||||||||||
| Method | RAG | LC-QA | ICL | Recall | Rerank. | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| KILT-Hotpot | KILT-NQ | KILT-PopQA | KILT-Trivia | InfB-Choice | InfB-QA | ICL-Bank77 | ICL-CLINC | ICL-NLU | ICL-TREC-C | ICL-TREC-F | JSON-KV | NIAH-MK2 | NIAH-MK3 | NIAH-MV | MSMARCO | ||
| Llama | Full | ||||||||||||||||
| Snap | |||||||||||||||||
| Snap | |||||||||||||||||
| Str. | |||||||||||||||||
| Str. | |||||||||||||||||
| Same set (%) | Cluster Jaccard | Eq. 4 (%) | |||||
|---|---|---|---|---|---|---|---|
| Deviation | |||||||
| Box | 96.4 | 97.7 | 0.9999 | 0.9991 | 99.5 | 99.7 | |
| Diag. ellipsoid | 96.4 | 97.7 | 0.9998 | 0.9982 | 99.7 | 99.8 | |
| Exact (oracle) | 0 2.2 | 38.4 | 0.918 | 0.945 | 0 1.7 | 0 5.0 | |
| original | box | 70.27 | 62.58 |
|---|---|---|---|
| simplified | box | 70.19 | 62.67 |
| original | oracle | 72.00 | 69.06 |
| simplified | oracle | 72.13 | 68.01 |
| Full Attention | 73.37 | ||
| Qwen2.5-14B | Qwen2.5-7B | ||||||
|---|---|---|---|---|---|---|---|
| Method | Acc. | [95% CI] | TPOT (ms) | Speedup | Acc. | Speedup | |
| Full Attention | 72.96 | — | 40.98 | 1.00 | 65.88 | — | 1.00 |
| RefineKV ( ) | 71.97 | [ , ] | 29.56 | 1.39 | 65.28 | 1.19 | |
| RefineKV ( ) | 69.19 | [ , ] | 26.49 | 1.55 | 60.35 | 1.27 | |
| SnapKV (25%) | 68.44 | [ , ] | 28.33 | 1.45 | 51.05 | 1.27 | |
| Llama-3.1-8B | Qwen2.5-7B | ||||||
|---|---|---|---|---|---|---|---|
| RULER | HELMET | LongBench | RULER | HELMET | LongBench | ||
| Full Attention | Quality | 73.37 | 69.10 | 48.86 | 65.88 | 54.57 | 49.34 |
| RefineKV, | Quality | 70.19 | 68.43 | 48.87 | 65.28 | 54.48 | 49.23 |
| Speedup, CUDA | 1.56 | 1.43 | 1.55 | 1.19 | 1.13 | 1.18 | |
| Speedup, eager | 1.49 | 1.37 | 1.49 | 1.15 | 1.10 | 1.15 | |
| RefineKV, | Quality | 62.58 | 56.80 | 48.95 | 60.35 | 52.83 | 49.13 |
| Full Attention | RefineKV ( ) | RefineKV ( ) | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Length | Acc. | TPOT | Acc. | Exp. | Speedup | Acc. | Exp. | Speedup | ||
| 16K | 89.03 | 14.46 | 89.22 | 52.8% | 1.01 | 85.34 | 10.9% | 1.05 | ||
| 32K | 83.41 | 15.86 | 83.42 | 32.8% | 1.07 | 75.12 | 6.7% | 1.14 | ||
| 64K | 82.80 | 18.71 | 81.94 | 18.7% | 1.24 | 74.74 | 4.6% | 1.33 | ||
| 128K | 73.35 | 24.43 | 70.27 | 11.2% | 1.56 | 62.58 | 4.0% | 1.67 | ||
| Model | Benchmark | Full | Acc. | [95% CI] | Acc. | [95% CI] |
|---|---|---|---|---|---|---|
| Llama-3.1-8B | RULER | 73.37 | 70.19 | [ , ] | 62.58 | [ , ] |
| HELMET | 69.10 | 68.43 | [ , ] | 56.80 | [ , ] | |
| LongBench | 48.86 | 48.87 | [ , ] | 48.95 | [ , ] | |
| Qwen2.5-7B | RULER | 65.88 | 65.28 | [ , ] | 60.35 | [ , ] |
| HELMET | 54.57 | 54.48 | [ , ] | 52.83 | [ , ] | |
KV-Cache Compression
Key-value cache compression reduces the memory and bandwidth needed to retain attention states during long-context inference.
See all 300 papers