LKV: End-to-End Learning of Head-wise Budgets and Token Selection for LLM KV Cache Eviction
Summary (Overview)
- Core contribution: LKV (Learned KV Eviction) reformulates KV cache compression as an end-to-end differentiable optimization problem, replacing heuristic-based approaches with learned policies for both budget allocation and token selection.
- Two key components: LKV-H learns global budget allocation across all attention heads via a flattened 1D competition, while LKV-T performs matrix-free token selection using lightweight MLPs that predict token importance from intrinsic KV features.
- State-of-the-art results: Achieves 46.73 average score on LongBench with Llama-3.1-8B-Instruct at 15% KV retention, recovering 98.4% of Full Cache performance (47.48) and outperforming the strongest baseline (Ada-SnapKV) by 7.67 points.
- Key insight: Learned budgeting (LKV-H) is identified as the dominant driver of compression quality, contributing significantly more to performance than token selection improvements.
- Practical efficiency: LKV scales to 262k+ context length (vs. 225k for Full Cache), achieves 6.6× KV storage reduction, and incurs only ~7.7% prefill latency overhead.
Introduction and Theoretical Foundation
Problem Context
Long-context inference in LLMs faces a memory bottleneck due to the linear growth of KV cache. The KV cache stores key-value states for all tokens across all layers and heads, consuming memory proportional to sequence length. This limits maximum context length and reduces inference throughput.
Limitations of Existing Approaches
The paper identifies three fundamental limitations of current KV eviction methods:
-
Fragmented budget allocation: Methods like H2O and SnapKV use uniform budgets across layers/heads, ignoring heterogeneity in attention patterns. PyramidKV enforces rigid layer-wise decay; Ada-KV only adjusts within-layer head budgets.
-
Costly token selection: Methods like H2O and SnapKV require computing query-key attention scores to evaluate token importance, creating a "chicken-and-egg" problem—the attention matrix computation is exactly what compression aims to avoid.
-
Heuristic proxies: Many methods optimize proxy metrics (attention concentration, attention weights) rather than the actual task objective, and rely on manual safeguards like minimum budget thresholds or attention sinks.
Theoretical Formulation
The paper formulates KV compression as minimizing the discrepancy between full-cache and compressed models:
subject to the budget constraint , where is a binary retention mask operating at the KV-head level.
The Differentiable Soft-TopK Operator
A critical enabler is the Soft-TopK operator , which provides:
- Normalization: Output sums exactly to , enforcing budget constraints
- Order preservation: Higher scores yield higher mask values
- Translation invariance: , decoupling selection from absolute magnitude
The operator has a closed-form solution for the threshold:
where and .
Methodology
LKV-H: Learning Global Budget Allocation
Mechanism: Each KV head has a learnable embedding , mapped to importance scores via a shared MLP:
Global 1D Competition: The score tensor is flattened into a 1D vector, enabling cross-layer budget transfer. The Soft-TopK operator generates retention ratios:
Zero Inference Overhead: Ratios depend only on static embeddings, pre-computed once and frozen. At inference, the discrete budget for head is simply .
LKV-T: Matrix-Free Intrinsic Token Selection
Head-Specific Scoring: Each head has a unique lightweight MLP that predicts token importance from concatenated key-value features:
This is query-agnostic—importance derives solely from KV states, avoiding dependency on the current query.
Gumbel-Based Training: During training, Gumbel noise is injected with temperature annealing:
Temperature decays from high (exploration) to near-zero (deterministic). At inference, hard Top-K selection is used.
Training Objective
Self-distillation with a frozen teacher (full cache) and student (compressed):
A custom FlashAttention kernel uses multiplicative masking for numerical stability under BF16 precision.
Empirical Validation / Results
Experimental Setup
- Models: Llama-3.1-8B-Instruct, Qwen3-8B (both GQA)
- Benchmarks: LongBench (16 subtasks), RULER (synthetic)
- Training: ~23k samples, <2 hours on 8×A100 GPUs, ~0.1% trainable parameters
- Baselines: SnapKV, PyramidKV, DuoAttention, Ada-SnapKV, ExpAttn
Main Results (LongBench, 15% KV Budget)
| Method | Avg. | Single-Doc QA | Multi-Doc QA | Summarization | Few-Shot | Synthetic | Code |
|---|---|---|---|---|---|---|---|
| Full Cache | 47.48 | 43.57 | 45.41 | 28.89 | 65.43 | 53.48 | 51.42 |
| SnapKV | 38.05 | 27.27 | 34.08 | 22.93 | 58.11 | 40.73 | 50.05 |
| PyramidKV | 32.12 | 25.17 | 26.75 | 22.57 | 49.29 | 24.99 | 46.29 |
| DuoAttention | 31.03 | 21.76 | 25.94 | 21.66 | 50.71 | 17.75 | 50.40 |
| Ada-SnapKV | 39.06 | 27.38 | 35.46 | 23.29 | 59.28 | 43.77 | 50.56 |
| LKV (Ours) | 46.73 | 41.66 | 42.79 | 28.76 | 66.30 | 53.38 | 51.25 |
Ablation Study: Budgeting vs. Selection
| Selection | Budgeting | Avg. | ΔBase | Multi-Doc QA | Synthetic |
|---|---|---|---|---|---|
| SnapKV | Uniform | 38.05 | - | 34.08 | 40.73 |
| SnapKV | AdaKV | 39.06 | +1.01 | 35.46 | 43.77 |
| SnapKV | LKV-H | 44.06 | +6.01 | 41.75 | 52.87 |
| ExpAttn | Uniform | 37.12 | - | 37.39 | 17.77 |
| ExpAttn | AdaKV | 38.16 | +1.04 | 37.33 | 23.26 |
| ExpAttn | LKV-H | 45.72 | +8.60 | 43.27 | 45.00 |
| LKV-T | Uniform | 39.74 | - | 40.27 | 22.41 |
| LKV-T | AdaKV | 43.45 | +3.71 | 38.83 | 47.76 |
| LKV-T | LKV-H | 46.73 | +6.99 | 42.79 | 53.38 |
Robustness Results
- Length scalability: Stable performance from 4k to 32k contexts (trained on 16k), demonstrating policy transferability
- Extreme compression: At R=0.1, LKV maintains high performance while baselines degrade significantly
- Rigid priors fail: PyramidKV and DuoAttention drop to 24.99 and 17.75 on Synthetic tasks due to aggressive pruning of critical deep layers/heads
Efficiency Analysis
| Method | Complexity | Time (s) | Overhead |
|---|---|---|---|
| Full Cache | - | 82.40 | - |
| DuoAttention | O(1) | 87.21 | +5.8% |
| Ada-SnapKV | O(W·T) | 88.37 | +7.2% |
| LKV (Ours) | O(T) | 88.76 | +7.7% |
- Memory: LKV scales to 262k+ context (vs. 225k OOM for Full Cache)
- Storage: 6.6× reduction (25.0 GB → 3.75 GB at 200k length)
Theoretical and Practical Implications
Theoretical Contributions
- Unified framework: Bridges existing selection mechanisms—the Soft-TopK operator approximates Softmax when k=1 and converges to Hard Top-K as τ→0⁺
- End-to-end differentiability: Enables joint optimization of global budgets and token selection through gradient propagation
- Cross-layer resource transfer: Flattened competition allows critical deep-layer heads to claim capacity from redundant shallow-layer heads
Practical Implications
- Near-lossless compression: 98.4% of Full Cache performance at 15% retention enables extended contexts on commodity hardware
- Query-agnostic efficiency: Matrix-free selection avoids O(t²) attention computation during eviction
- Zero runtime overhead: Pre-computed budgets eliminate online allocation costs
- Architecture compatibility: Fully compatible with GQA and orthogonal to other efficiency paradigms (quantization, sparse attention, prompt compression)
Key Insight
The ablation study reveals that learned budgeting is the dominant factor in compression quality. LKV-H provides +6.01 to +8.60 average gains across selectors, while LKV-T contributes an additional +2.67 over SnapKV under uniform budgets. This suggests that resource allocation—not token selection—is the primary bottleneck in KV cache compression.
Conclusion and Future Directions
LKV establishes a data-driven framework for KV cache compression, achieving state-of-the-art performance through:
- Global learned budgeting (LKV-H) that bypasses rigid structural priors
- Matrix-free token selection (LKV-T) that avoids attention computation
- End-to-end differentiable optimization via Soft-TopK with closed-form thresholds
Limitations and future work:
- Requires a lightweight training phase (though only ~2 hours on 8×A100)
- Needs specialized kernels (flattened FlashAttention) for maximum sparse access efficiency
- Future directions may include extending to other model architectures, integrating with quantization methods, and exploring even more aggressive compression regimes
Related papers
- A Jagged Frontier: Evaluating Robustness of Code Agents to Semantics-Preserving Transformations
Repository-level code agents lose up to 6.7 points in resolution rate under semantics-preserving code perturbations, and robustness is a jagged frontier—jointly determined by model, scaffold, and workload, not the model alone.
- How Do Agents Fail on AutoResearch: End-to-End Diagnostic Evaluation on 100 Real-World Frontier Research Tasks
Autonomous research agents fail across all models because they lack a metacognitive loop: they cannot check outputs against evidence, act on identified flaws, or question their methods.
- The Empire, Long Divided, Must Unite: Architectural Convergence in Three LLM Agent Harnesses
Three agent harnesses with opposing philosophies converged on five shared architectural elements, leaving external verifiability as the sole unconverged dimension and next competitive frontier.