Summary (Overview)

  • CompKV is the first compensation-aware sparse attention framework for long-context LLM inference that explicitly optimizes KV block selection for the downstream compensation mechanism, rather than treating selection and compensation as separate stages.
  • The paper provides a theoretical analysis showing that the post-compensation residual under block-mean compensation is jointly governed by a block's attention mass (pg,bp_{g,b}) and within-block logit variance (σg,b2\sigma_{g,b}^2), leading to a selection score of pg,b⋅σg,b2p_{g,b} \cdot \sigma_{g,b}^2.
  • CompKV estimates these quantities from compact block statistics (mean keys, grouped variances, mean values), requiring only 2d+r+12d + r + 1 scalars per block, and uses an efficient asynchronous CPU-offload implementation.
  • On RULER and LongBench-Pro benchmarks across Llama-3.1-8B-Instruct, Qwen3-8B, and Qwen3-32B, CompKV achieves the highest average scores among all evaluated sparse baselines, with accuracy drops as low as 2.7 points from full attention (vs. 18.1 points for the best baseline on Qwen3-32B).
  • The asynchronous implementation delivers up to 6.85× self-attention speedup over full attention, with the lowest mean decoding-step latency across all nine combinations of context lengths (32K–128K) and token budgets (512–2,048).

Introduction and Theoretical Foundation

Background and Motivation

During autoregressive decoding, the KV cache grows with context length, and dense attention reads the entire cache at every step. This makes long-context inference increasingly bottlenecked by KV cache memory traffic. Query-aware sparse attention reduces this traffic by reading only a small set of tokens selected for the current query.

Recent methods divide tokens into blocks and compensate for omitted blocks' attention contributions using compact summaries (e.g., mean values). However, selection and reconstruction remain largely decoupled: blocks are ranked by predicted relevance or attention mass before reconstruction is considered.

Key Insight: A high-mass block may be easy to reconstruct, whereas a lower-mass block with greater within-block logit variation may leave a larger error. Therefore, selection should prioritize blocks that would leave the largest reconstruction error if omitted.

Theoretical Foundation

The paper defines Mean compensation as replacing every logit in an unselected block by its block mean. The compensated logit is:

z^g,j={zg,j,b∈S,zˉg,b,b∉S,(1)\hat{z}_{g,j} = \begin{cases} z_{g,j}, & b \in \mathcal{S}, \\ \bar{z}_{g,b}, & b \notin \mathcal{S}, \end{cases} \tag{1}

where zˉg,b=∣b∣−1∑j∈bzg,j\bar{z}_{g,b} = |b|^{-1} \sum_{j \in b} z_{g,j} is the block-mean logit.

The selection objective minimizes the KL divergence between the compensated and full attention distributions:

min⁡S⊆B,∣S∣=KL(S)=∑g=1GDKL(P^g(S)∥Pg)(2)\min_{\mathcal{S} \subseteq \mathcal{B}, |\mathcal{S}| = K} \mathcal{L}(\mathcal{S}) = \sum_{g=1}^{G} D_{\mathrm{KL}}\left(\widehat{P}_g(\mathcal{S}) \lVert P_g\right) \tag{2}

Methodology

3.1 Deriving the Compensation-Aware Selection Criterion

For each query head gg, define the attention mass and logit variance of block bb:

pg,b=Zg,b∑c∈BZg,c,σg,b2=1∣b∣∑j∈b(zg,j−zˉg,b)2(3)p_{g,b} = \frac{Z_{g,b}}{\sum_{c \in \mathcal{B}} Z_{g,c}}, \quad \sigma_{g,b}^2 = \frac{1}{|b|} \sum_{j \in b} (z_{g,j} - \bar{z}_{g,b})^2 \tag{3}

The KL divergence expansion yields:

L(S)=−∑g=1Glog⁡[1−∑b∉Spg,b(1−∣b∣ezˉg,bZg,b)](4)\mathcal{L}(\mathcal{S}) = -\sum_{g=1}^{G} \log\left[1 - \sum_{b \notin \mathcal{S}} p_{g,b}\left(1 - \frac{|b| e^{\bar{z}_{g,b}}}{Z_{g,b}}\right)\right] \tag{4}

A second-order Taylor expansion around the block-mean logit gives:

pg,b(1−∣b∣ezˉg,bZg,b)=pg,b(12σg,b2+O(σg,b3))(5)p_{g,b}\left(1 - \frac{|b| e^{\bar{z}_{g,b}}}{Z_{g,b}}\right) = p_{g,b}\left(\frac{1}{2}\sigma_{g,b}^2 + O(\sigma_{g,b}^3)\right) \tag{5}

This leads to the key result: the optimal selection criterion is to maximize

max⁡S⊆B,∣S∣=K∑b∈S∑g=1Gpg,bσg,b2(7)\max_{\mathcal{S} \subseteq \mathcal{B}, |\mathcal{S}| = K} \sum_{b \in \mathcal{S}} \sum_{g=1}^{G} p_{g,b} \sigma_{g,b}^2 \tag{7}

4.1 Block Scoring from Compact Statistics

CompKV estimates the score using:

  1. Grouped key-variance estimation — partition key coordinates into rr groups, storing average variance per group:
σ^g,b2=1d∑t=0r−1(∑i∈Gtqg,i2)(Dˉb)t(8)\widehat{\sigma}_{g,b}^2 = \frac{1}{d} \sum_{t=0}^{r-1} \left(\sum_{i \in \mathcal{G}_t} q_{g,i}^2\right)(\bar{\mathbf{D}}_b)_t \tag{8}
  1. Attention-mass estimation using the log-partition expansion:
p^g,b=∣b∣exp⁡(zˉg,b+12σ^g,b2)∑c∈B∣c∣exp⁡(zˉg,c+12σ^g,c2)(9)\widehat{p}_{g,b} = \frac{|b| \exp\left(\bar{z}_{g,b} + \frac{1}{2}\widehat{\sigma}_{g,b}^2\right)}{\sum_{c \in \mathcal{B}} |c| \exp\left(\bar{z}_{g,c} + \frac{1}{2}\widehat{\sigma}_{g,c}^2\right)} \tag{9}
  1. Compensation-aware block scoring:
Sb=∑g=1Gp^g,bσ^g,b2(10)S_b = \sum_{g=1}^{G} \widehat{p}_{g,b} \widehat{\sigma}_{g,b}^2 \tag{10}

4.2 Block Selection and Compensated Decoding

The selected set includes mandatory blocks (sink + local window) plus Top-K by score:

S=F∪TopK⁡K−∣F∣{Sb:b∈B∖F}(11)\mathcal{S} = \mathcal{F} \cup \operatorname{TopK}_{K-|\mathcal{F}|}\{S_b : b \in \mathcal{B} \setminus \mathcal{F}\} \tag{11}

Output is computed via joint normalization:

o^g=∑b∈S∑j∈bezg,jvj+∑b∉S∣b∣ezˉg,bvˉb∑b∈SZg,b+∑b∉S∣b∣ezˉg,b(12)\widehat{\mathbf{o}}_g = \frac{\sum_{b \in \mathcal{S}} \sum_{j \in b} e^{z_{g,j}} \boldsymbol{v}_j + \sum_{b \notin \mathcal{S}} |b| e^{\bar{z}_{g,b}} \bar{\boldsymbol{v}}_b}{\sum_{b \in \mathcal{S}} Z_{g,b} + \sum_{b \notin \mathcal{S}} |b| e^{\bar{z}_{g,b}}} \tag{12}

4.3 Asynchronous Implementation

The implementation keeps KV cache in pinned CPU memory, using:

  • Main CUDA stream: transfers selection region, runs block statistics + Top-K scoring
  • Auxiliary stream: computes Mean compensation, overlapping with CPU gathering and exact attention
  • The summary buffer stores {∣b∣,kˉb,Dˉb,vˉb}\{|b|, \bar{k}_b, \bar{\mathbf{D}}_b, \bar{v}_b\} — 2d+r+12d + r + 1 scalars per block

Empirical Validation / Results

RULER Accuracy (32K context, 512-token budget)

Table 1 highlights (best sparse result per model):

ModelFullQuestInfLLMQuest+RESACompKV
Llama-3.1-8B87.077.0 (↓10.0)73.1 (↓13.9)78.2 (↓8.8)83.2 (↓3.8)
Qwen3-8B91.576.5 (↓15.0)76.6 (↓14.9)75.8 (↓15.7)86.0 (↓5.5)
Qwen3-32B93.775.7 (↓18.0)82.5 (↓11.2)75.6 (↓18.1)91.0 (↓2.7)

CompKV shows particularly large gains on MK3 (multi-key retrieval), outperforming baselines by 35–90 points on this difficult task.

LongBench-Pro Scores

ModelFullQuestInfLLMQuest+RESACompKV
Llama-3.1-8B25.2624.28 (↓0.98)23.60 (↓1.66)24.30 (↓0.96)24.44 (↓0.82)
Qwen3-8B34.2330.58 (↓3.65)31.75 (↓2.48)30.60 (↓3.63)32.74 (↓1.49)
Qwen3-32B42.0037.62 (↓4.38)39.62 (↓2.38)36.16 (↓5.84)40.30 (↓1.70)

Efficiency Results

  • CompKV (r=4r=4) achieves the lowest mean latency in all nine settings (3 context lengths × 3 budgets)
  • Up to 6.85× speedup over full attention
  • At 512-token budget: CompKV outperforms Quest and InfLLM across 32K, 64K, and 128K contexts

Ablation Studies

Table 3: Factor ablations on RULER (13-task AVG %)

VariantLlama 256Llama 512Qwen 256Qwen 512
CompKV (full)78.683.280.986.0
p^(2)\widehat{p}^{(2)} only (no Mean)75.681.879.283.5
p^(2)+Mean\widehat{p}^{(2)} + \text{Mean} (no outer var)78.182.879.785.1
p^(1)σ^2+Mean\widehat{p}^{(1)}\widehat{\sigma}^2 + \text{Mean} (first-order mass)76.181.277.583.8

Key findings:

  • Mean compensation adds 0.48–2.47 points
  • Outer variance factor adds 0.35–1.19 points
  • Second-order mass correction adds 1.96–3.39 points
  • Finer variance groups (r=128r=128) improve accuracy but at 32× metadata cost; r=4r=4 is the practical default

Theoretical and Practical Implications

Theoretical Contributions

  1. First formalization of compensation-aware selection: The paper identifies and formalizes the mismatch between block selection and downstream compensation, showing that optimal selection must consider reconstruction error, not just attention mass.

  2. Analytically tractable residual analysis: By using Mean compensation, the KL divergence reduces to a log-partition gap, enabling closed-form analysis. The key theoretical result—that residual scales as pg,b⋅σg,b2p_{g,b} \cdot \sigma_{g,b}^2—provides a principled, deployable selection criterion.

  3. Unified framework: The paper shows that removing Mean compensation from the KL objective recovers standard Top-K attention-mass selection, demonstrating that CompKV is a strict generalization.

Practical Implications

  • Training-free: CompKV requires no model fine-tuning, making it immediately deployable with existing LLMs.
  • Compact metadata: Only 2d+r+12d + r + 1 scalars per block (e.g., ~1 KB per 16-token block for d=128d=128, r=4r=4), enabling efficient CPU offloading.
  • Generalizable: Works across model families (Llama, Qwen) and scales (8B–32B), with consistent improvements over baselines.
  • Efficient: The asynchronous CPU-offload design overlaps compensation with KV retrieval, achieving sub-millisecond attention latencies at 128K context.

Conclusion

CompKV addresses a fundamental limitation of existing sparse attention methods by making block selection explicitly aware of the downstream compensation error. The paper's key contributions are:

  1. Theoretical: Proves that post-compensation residual is jointly governed by attention mass and within-block logit variance, yielding the selection score pg,b⋅σg,b2p_{g,b} \cdot \sigma_{g,b}^2.

  2. Methodological: Introduces a practical, training-free framework using compact block statistics (mean keys, grouped variances) to estimate this score without reading all keys.

  3. Empirical: Achieves state-of-the-art accuracy among sparse baselines on RULER and LongBench-Pro across three models, while delivering up to 6.85× speedup over full attention.

Future directions suggested by this work include:

  • Extending compensation-aware selection to other compensation mechanisms beyond Mean (e.g., low-rank reconstructions)
  • Exploring finer-grained variance statistics for accuracy-critical applications
  • Investigating adaptive block sizes and variance group allocations based on observed attention patterns

Related papers