Folding Attention Biases into Keys Makes Compacted KV Caches Run on Standard Kernels

Utkarsh KanwatarXiv 2026

TL;DR

Folding a per-key logit bias directly into the key vector itself allows compacted KV caches to run on standard, high-performance kernels like FlashAttention without any code modifications. This technique bridges the "deployment gap" for KV cache compaction, enabling massive throughput gains and memory savings in production environments like vLLM while maintaining near-original accuracy.

What Problem Does This Paper Address?

As Large Language Models (LLMs) handle increasingly long contexts, the Key-Value (KV) cache becomes a massive memory bottleneck. Compaction methods like Attention Matching (AM) solve this by keeping only a small subset of tokens and adding a scalar bias ($\beta_j$) to each kept key’s logit to compensate for the dropped information.

However, there is a major deployment hurdle: high-performance attention kernels (like FlashAttention) and serving engines (like vLLM) are highly optimized for "plain" KV tensors. They simply have no input slot for a per-key bias. Researchers previously had to choose between using slow, custom kernels or dropping the bias entirely, which significantly degrades model accuracy.

Methods

The authors propose a "fold" operation that mathematically moves the bias into the keys.

  1. The Fold: Instead of calculating attention as $q \cdot k_j / \sqrt{d} + \beta_j$, they modify the key to $k'_j = k_j + \beta_j \sqrt{d} u$, where $u$ is a shared "direction" vector for the entire attention head.
  2. Finding the Direction ($u$): The trick is finding a vector $u$ such that for most queries $q$, the projection $q \cdot u \approx 1$. If this holds, then $q \cdot k'_j / \sqrt{d} \approx q \cdot k_j / \sqrt{d} + \beta_j$. The authors derive a closed-form solution for $u$ based on the queries produced during the initial context processing.
  3. Value Refit: After folding the keys, they perform a final least-squares optimization on the Values ($V$) to further minimize the output error relative to the full, uncompacted cache.
The fold moves each retained key along a shared direction $u$ so that queries near the $q \cdot u = 1$ hyperplane see the intended logit shift.

Datasets

The method was evaluated across several rigorous long-context benchmarks:

  • QuALITY: Multiple-choice QA over long documents (2.8k to 8.1k tokens).
  • LongHealth: QA based on clinical documents (up to 13.2k tokens).
  • LongBench: A suite of multi-task long-context challenges (averaging 8k+ tokens).
  • Pride and Prejudice: Used for head-by-head error analysis over very long sequences (32k tokens).

Models tested included the Qwen3 family (1.7B, 4B, 8B, 14B, and 32B) and Llama-3.1-8B.

Key Insights and Ideas

  • Hyperplane Alignment: The core "aha" moment is that queries within a single attention head often cluster such that they can be approximated by a single hyperplane. This allows a static bias to be "simulated" using the existing query-key dot product mechanism.
  • Structural Generality: The folding technique isn't limited to AM. It applies to any static per-entry logit term, including log-count merging and other recent compaction successors.
  • Error Bounding: The authors provide a formal bound showing that the error is proportional to $|q \cdot u - 1|$. This gives a clear metric for when the fold is likely to succeed or fail based on "query drift."

Key Results

  • Accuracy Parity: In 13 out of 14 experimental settings with equal per-head budgets, the folded cache was within the noise floor of the original biased AM method's accuracy.
  • Massive Throughput Gains: On a Qwen3-4B model served via unmodified vLLM on a Tesla T4 GPU, compaction (at 10% size) increased request capacity from 4 to 31 and boosted peak decoding speed from 29 to 200 tokens/second.
  • Superior to Dropping Bias: In almost all cases, folding the bias resulted in significantly lower KL divergence to the full cache compared to the common practice of simply discarding the bias.

Limitations and Future Work

  • Query Drift: Because $u$ is fitted on context queries, the approximation may degrade if future queries in a long conversation drift significantly away from the initial distribution.
  • Small Model Sensitivity: The smallest model tested (Qwen3-1.7B) showed more sensitivity to the fold than larger models, occasionally losing accuracy where larger models remained robust.
  • Multi-turn Dynamics: The current study focuses on single-turn QA. Future work is needed to see how query drift accumulates over many turns of a conversation.
  • Ragged Budgets: While the fold works for plain caches, serving engines still struggle with "ragged" (variable length) caches per head, which is often required for optimal compaction schedules.

Summary updated

Abstract

Attention Matching (AM) compacts a KV cache by keeping some keys per head, refitting their values and adding a bias to each kept key's logit. FlashAttention and vLLM's paged caches have no input for it. We fold the bias into the keys along one closed-form direction per head, fitted on context queries, giving a plain cache with AM's size and head dimension and the model's own query path; the output error is bounded by how far each query's projection on that direction is from 1. With equal per-head budgets, unmodified vLLM serves folded caches through a KV connector, matching Hugging Face's answers on all 51 Qwen3-4B questions and its greedy output on 180 of 200 Qwen3-14B LongBench samples. With equal budgets on QuALITY (Qwen3-1.7B, 4B, 8B, Llama-3.1-8B) and LongHealth (three Qwen3 models), the fold is within noise of AM's accuracy in 13 of 14 settings, with lower KL to the full cache than dropping the bias in all 12 where both ran. Under the published per-head budgets, on Qwen3-4B, its Instruct-2507 variant and Llama-3.1-8B, it is within noise of AM in 9 of 10, no longer beats dropping the bias in KL, and the two each win once in accuracy.
BibTeX
@misc{kanwat2026folding,
  author = {Utkarsh Kanwat},
  title = {{Folding Attention Biases into Keys Makes Compacted KV Caches Run on Standard Kernels}},
  year = {2026},
  eprint = {2610.36520},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2610.36520},
}
Figure