Full text not available for this paper

Summary

  • KV-SURGEON introduces a compensation-aware offline KV-cache compaction method that jointly optimizes which cache entries to delete and how the remaining entries should be adjusted, rather than treating selection and refitting as separate sequential steps.
  • The method derives a quadratic surrogate of the attention-output error that admits closed-form expressions for greedy deletion saliencies and compensating updates, inspired by Optimal Brain Surgeon (OBS) pruning techniques.
  • KV-SURGEON substantially advances the cost–accuracy Pareto frontier across QuALITY, LongHealth, and Long Code Arena benchmarks, with models from 4B to 30B parameters and compaction ratios from 5× to 100×.
  • At 50× compaction, KV-SURGEON improves mean question-answering accuracy by 3.0 percentage points over the prior state of the art (attention matching), while its fast variant (KV-SURGEON-fast) exceeds all prior methods at only two-fifths of the cost.
  • A controlled ablation demonstrates that the performance gain comes specifically from compensation during selection (updating surviving entries after each deletion), not merely from the surrogate objective.

Introduction and Theoretical Foundation

Background and Motivation

Large language model (LLM) applications frequently query a fixed context (documents, patient records, codebases). The key-value cache (KV-cache) grows linearly with context length and dominates memory usage during long-context inference. Offline compaction processes a context once, compresses its cache, and reuses the compacted cache for all subsequent requests—amortizing a one-time cost over many queries.

The Core Problem: Softmax Coupling

The central challenge is deciding which cache entries to delete. For one attention head with TT cache entries, each containing a key kj∈Rdkk_j \in \mathbb{R}^{d_k} and value vj∈Rdvv_j \in \mathbb{R}^{d_v}, a query q∈Rdkq \in \mathbb{R}^{d_k} produces:

P≜softmax(q⊤kℓdk)ℓ=1T∈RT,o≜∑j=1TPjvj∈RdvP \triangleq \text{softmax}\left(\frac{q^\top k_\ell}{\sqrt{d_k}}\right)_{\ell=1}^T \in \mathbb{R}^T, \quad o \triangleq \sum_{j=1}^T P_j v_j \in \mathbb{R}^{d_v}

The key insight is that softmax renormalization couples deletion decisions nonlinearly: removing one entry changes the attention weights of all remaining entries. This makes selection a combinatorial problem rather than a simple ranking problem.

Limitations of Existing Approaches

Existing methods suffer from a critical restriction: they evaluate each candidate deletion with the rest of the cache held fixed. Specifically:

  • Attention matching (Zweiger et al., 2026) refits the retained cache after selection, but selection itself ignores compensation potential.
  • Output-aware methods (CAOTE, OBCache, ReST-KV) estimate single-deletion error but evaluate deletions in isolation.
  • Eviction-by-proxy methods (H2O, StreamingLLM) rank entries by scores that ignore the rest of the cache entirely.

What is missing is compensation-aware selection: each candidate deletion should be scored by the error remaining after surviving entries have been adjusted to compensate for it.

Methodology

Problem Formulation

For one attention head with TT cache entries and NN calibration queries q1,…,qNq_1, \ldots, q_N, the attention weights and outputs are:

Pij≜eZij∑ℓ=1TeZiℓ,oi≜∑j=1TPijvjP_{ij} \triangleq \frac{e^{Z_{ij}}}{\sum_{\ell=1}^T e^{Z_{i\ell}}}, \quad o_i \triangleq \sum_{j=1}^T P_{ij} v_j

where Zij≜qi⊤kj/dkZ_{ij} \triangleq q_i^\top k_j / \sqrt{d_k}.

Given a budget t<Tt < T, the goal is to retain a support set SS with ∣S∣=t|S| = t, assigning each retained entry j∈Sj \in S an adapted value v^j\hat{v}_j and a positive attention multiplier wj>0w_j > 0. The compacted cache produces:

P^ij≜wjPijμi,o^i≜1μi∑j∈SPijwjv^j\hat{P}_{ij} \triangleq \frac{w_j P_{ij}}{\mu_i}, \quad \hat{o}_i \triangleq \frac{1}{\mu_i} \sum_{j \in S} P_{ij} w_j \hat{v}_j

where μi≜∑j∈SPijwj\mu_i \triangleq \sum_{j \in S} P_{ij} w_j is the ratio between compacted and original normalization terms.

The Quadratic Surrogate

The key technical contribution is deriving a tractable surrogate. By introducing the variable U≜diag(w)V^U \triangleq \text{diag}(w)\hat{V}, the output can be written as a ratio of two linear terms:

o^i=niμi,ni≜∑j∈SPijuj,μi=∑j∈SPijwj\hat{o}_i = \frac{n_i}{\mu_i}, \quad n_i \triangleq \sum_{j \in S} P_{ij} u_j, \quad \mu_i = \sum_{j \in S} P_{ij} w_j

Instead of matching the ratio directly, the method asks the compacted cache to reproduce numerator and denominator separately. The resulting quadratic surrogate is:

∥O−PSU∥F2+γ2∥1−PSw∥22=∥[O  γ1]−PS[U  γw]∥F2\|O - P_S U\|_F^2 + \gamma^2 \|1 - P_S w\|_2^2 = \|[O \; \gamma 1] - P_S [U \; \gamma w]\|_F^2

The right-hand side forms a single linear least-squares problem with dv+1d_v + 1 target columns. With Y≜[O  γ1]∈RN×(dv+1)Y \triangleq [O \; \gamma 1] \in \mathbb{R}^{N \times (d_v+1)} and a ridge penalty λ>0\lambda > 0, the support score is:

F(S)≜min⁡B∥Y−PSB∥F2+λ∥B∥F2F(S) \triangleq \min_B \|Y - P_S B\|_F^2 + \lambda \|B\|_F^2

Greedy Elimination with Closed-Form Compensation

For a fixed support SS, the optimal coefficients and shared inverse are:

B=GPS⊤Y∈R∣S∣×(dv+1),G≜(PS⊤PS+λI)−1∈R∣S∣×∣S∣B = G P_S^\top Y \in \mathbb{R}^{|S| \times (d_v+1)}, \quad G \triangleq (P_S^\top P_S + \lambda I)^{-1} \in \mathbb{R}^{|S| \times |S|}

The saliency of entry jj (the increase in optimal surrogate objective when removed) is:

sj≜F(S∖{j})−F(S)=∥Bj,:∥22Gjjs_j \triangleq F(S \setminus \{j\}) - F(S) = \frac{\|B_{j,:}\|_2^2}{G_{jj}}

After removing entry jj, the updates are:

B←B−j,:−G−j,jBj,:Gjj,G←G−j,−j−G−j,jGj,−jGjjB \leftarrow B_{-j,:} - \frac{G_{-j,j} B_{j,:}}{G_{jj}}, \quad G \leftarrow G_{-j,-j} - \frac{G_{-j,j} G_{j,-j}}{G_{jj}}

These give the exact minimizer of the surrogate on S∖{j}S \setminus \{j\} without solving another regression. The algorithm repeatedly removes the entry with smallest saliency, updating BB and GG until ∣S∣=t|S| = t.

Implementation Details

Key practical additions include:

  • Candidate pool: Pre-filter to m=min⁡{T,⌈ct⌉}m = \min\{T, \lceil ct\rceil\} entries using a leave-one-out score (c=4c = 4)
  • Block deletion: Delete nb=32n_b = 32 entries per step using block-update formulas
  • Final refit: Optimize multipliers ww (box-constrained) then values V^\hat{V} (least-squares)
  • Mild-compaction fallback: For heads retaining >30%> 30\% of entries, skip greedy elimination

Empirical Validation / Results

Main Results at 50× Compaction

Table 1: Accuracy (%) at 50× compaction across model–benchmark pairs

MethodTime (s)Qwen3-4B QuAL.Qwen3-4B LongH.Llama-3.1-8B QuAL.Llama-3.1-8B LongH.Gemma-3-12B QuAL.Gemma-3-12B LongH.Qwen3-30B QuAL.Qwen3-30B LongH.Mean
KV-SURGEON-fast5465.966.961.061.568.152.171.464.163.9
OBCache (adapted)5862.462.859.852.466.650.267.362.860.5
AM-HAK-fast5458.153.558.753.562.245.665.859.857.1
KV-SURGEON14566.867.862.362.167.556.472.565.265.1
LOO13965.664.261.259.165.149.669.662.362.1
AM-HAK13864.165.060.458.265.152.769.561.462.0
AM-OMP-CS26665.765.960.655.867.250.969.763.162.4
AM-OMP76366.465.561.357.367.754.569.263.963.2

Key findings:

  • KV-SURGEON reaches 65.1% mean accuracy in 145 seconds, 3.0 points above AM-HAK at comparable cost
  • KV-SURGEON-fast reaches 63.9% in 54 seconds, exceeding AM-OMP (63.2% in 763 s) with a 14.1× reduction in pipeline time
  • Gains are consistent across all 8 model–benchmark pairs and all compaction ratios (5× to 100×)

Source of Performance Gain

Table 2: Impact of compensation during selection (Qwen3-4B, QuALITY, 50×)

MethodSelection ruleCompensationAccuracy (%)
KV-SURGEONEquation (11)✓66.9
KV-SURGEON w/o compensationEquation (11)✗63.9
LOOEquation (13)✗64.7

Disabling only the compensating update (keeping the surrogate, pool, and refit fixed) reduces accuracy by 3.0 points, demonstrating that compensation during selection—not the surrogate itself—drives the improvement.

Robustness to Calibration

Replacing expensive self-study calibration with a single repeat pass costs KV-SURGEON only 1.2 points of mean accuracy (from 65.1% to 63.9%), while the same change costs AM-HAK 4.9 points. This robustness persists across all compaction ratios.

Theoretical and Practical Implications

Theoretical Contributions

  1. Quadratic surrogate derivation: The surrogate in Equation (8) is shown to be the second-order Taylor expansion of the exact loss around the original cache (Appendix A.1), providing a principled approximation.

  2. Closed-form compensation: The saliency formula sj=∥Bj,:∥22/Gjjs_j = \|B_{j,:}\|_2^2 / G_{jj} has a natural interpretation: it equals the squared coefficient magnitude of entry jj times the ridge-regularized residual of regressing its attention column on the other retained columns. An entry with a large coefficient is cheap to delete when its attention column lies close to the span of others.

  3. Block deletion exactness: The block updates in Equation (14) give the exact minimizer on S∖JS \setminus J for any selected block JJ; only the block selection (by individual saliencies) is approximate.

Practical Implications

  • Cost–accuracy frontier: KV-SURGEON establishes a new Pareto frontier for offline KV-cache compaction, making aggressive compaction (50×–100×) practical for production long-context applications.
  • Calibration flexibility: The method's insensitivity to calibration quality means practitioners can use cheap repeat-pass calibration without significant accuracy loss.
  • Model generality: Gains hold across dense and mixture-of-experts architectures from 4B to 30B parameters, suggesting broad applicability.
  • Compute efficiency: The O(m3m^3) complexity (bounded by the pool size ctct, not cache size TT) makes the method scalable to very long contexts.

Conclusion

KV-SURGEON demonstrates that deletion and adaptation can be coupled efficiently in offline KV-cache compaction, contrary to prior methods that treated them sequentially. The key insight is that a quadratic surrogate of the attention-output error enables closed-form compensation-aware selection, where each candidate deletion is scored after surviving entries have been adjusted to compensate.

The method improves the cost–accuracy frontier at every evaluated compaction ratio (5×–100×) across three benchmarks and five models, with the largest gains at aggressive compaction levels (50× and 100×). Its fast variant exceeds all prior methods at two-fifths of the cost of the strongest baseline.

Future directions suggested by this work include:

  • Exploring whether compensation-aware selection extends to other attention variants (e.g., sliding-window, linear attention)
  • Investigating joint optimization across heads rather than independent per-head compaction
  • Applying the OBS-inspired framework to other cache-compression axes (quantization, low-rank projection)

Related papers