Training Transformers for KV-Cache Compressibility

Yoav Gelberg, Yam Eitan, Michael M. Bronstein, Yarin Gal, Haggai MaronNeurIPS 2026

TL;DR

When Large Language Models read massive documents, their short-term memory—the Key-Value (KV) cache—balloons in size, becoming a major bottleneck. Most remedies try to squeeze this memory after the model is already trained, assuming redundancy is just a feature of the text itself. This paper proves that compressibility actually depends on how the model chooses to organize its internal thoughts, and introduces a training method (KV-CAT) that teaches Transformers to pack their memories neatly so existing compression tools work dramatically better.

What Problem Does This Paper Address?

As we ask autoregressive Transformers to digest entire codebases or books, we hit a wall: the Key-Value (KV) cache. To avoid recomputing past context at every step, the model stores key and value vectors for every single preceding token across every layer. For long sequences, storing and reading this cache dominates both memory capacity and generation speed.

Recent "post-hoc" compression algorithms try to shrink this cache at inference time—either by evicting "unimportant" tokens (like H2O or SnapKV) or using gradient optimization to distill long caches into compact ones. However, these methods treat the pretrained Transformer as a frozen black box.

The authors ask a fundamental question: what if two Transformers produce the exact same outputs on full contexts, yet one builds messy, fragile internal states that shatter when compressed, while the other builds neat, compactable ones? If compressibility is a property of learned representations rather than just the input prompt, post-hoc compressors are fighting with one hand tied behind their backs.

Methods

To nudge models toward compressible representations without altering their final architecture, the authors propose KV-Compression Aware Training (KV-CAT), a continued pretraining procedure.

Figure 1: KV-Compression Aware Training (KV-CAT). A context a goes through both masked (left) and dense (right) forward passes. In the masked forward pass, routers (orange) compute masks for groups of consecutive layers, marking KV slots as active (green) or inactive (muted green). In the dense forward pass (blue), all KV slots are kept. The output of the masked forward pass is used to compute Lmask, router distributions are used to compute Lbudget, and the outputs of the dense forward pass are used to compute Lanchor. These are jointly used to update the parameters.

During KV-CAT, lightweight, learnable routers (built as linear attention modules) are temporarily inserted between groups of Transformer layers. Each training batch goes through two parallel passes:

  1. A Dense Pass: Standard unmasked processing, trained with standard next-token prediction ($\mathcal{L}_{\text{anchor}}$) so the model doesn't forget how to be a good LLM.
  2. A Masked Pass: Routers score each token's importance and mask out KV slots falling below a threshold. A self-distillation loss ($\mathcal{L}_{\text{mask}}$) forces this sparse pass to match the probability distribution of the dense pass, while a budget loss ($\mathcal{L}_{\text{budget}}$) keeps the routers honest about hitting a target retention rate.

Crucially, at inference time, the routers are thrown away. You are left with a completely standard Transformer whose weights have been reorganized to expect—and survive—downstream KV cache compression.

Datasets

  • Continued Pretraining: FineWeb-Edu (5.24 billion tokens).
  • Uncompressed QA Baselines: HellaSwag, WinoGrande, PIQA, OpenBookQA, ARC-Easy, and ARC-Challenge (via LightEval).
  • Compressed-Context Evaluation: Held-out prefix–suffix pairs from FineWeb-Edu (for perplexity), a synthetic needle-in-a-haystack passkey retrieval task, and seven long-context QA tasks from LongBench v2. No new benchmarks were introduced.

Key Insights and Ideas

  • Compressibility is a representation choice, not just a task property: The authors mathematically prove (Theorem 3.1) that for almost any sequence-to-vector function, you can construct two Transformers of the exact same architecture that both solve the task—where one can have its prefix compressed down to a single KV pair, and the other incurs constant error under any compression at all.
  • The Histogram Intuition: Imagine computing a character histogram by uniformly averaging token embeddings. If you drop half the prefix tokens, your suffix tokens suddenly carry way too much weight in the average, ruining the math—unless a token secretly preserved the original prefix length via positional encodings so a later layer can re-normalize the sum! Standard training has no reason to preserve redundant metadata like prefix length for a position-invariant task; compression-aware training creates the evolutionary pressure to keep it.
  • Long-context generalization for free: Even though KV-CAT was only trained on short 1K-token sequences, its compressibility benefits transferred seamlessly to 32K-token contexts on LongBench v2.

Key Results

Evaluated on Qwen2.5-0.5B and Qwen2.5-1.5B against control baselines (continually pretrained on the exact same data without masking):

  • No Uncompressed Tax: On standard dense benchmarks, KV-CAT matches the base models (-0.02 points on 0.5B; +0.29 points on 1.5B).
  • Universal Compressor Boost: Across six diverse compressors (Attention Matching, H2O, SnapKV, KVzip, StreamingLLM, KeyDiff), KV-CAT improves suffix perplexity retention in every single configuration, averaging 1.32x better retention and reaching up to 2.58x.
  • Faster Cache Optimization: When using gradient-based KV compaction, KV-CAT reaches target perplexity gaps with up to 5x fewer optimization steps.
Figure 2: KV-CAT speeds up gradient-based KV cache compression. We plot the gap in suffix perplexity under full/compressed-prefix inference throughout gradient-based KV cache optimization. Each panel fixes a different KV keep ratio.
  • Downstream Retrieval & QA: Under moderate cache budgets (30–50% keep), needle-in-a-haystack exact match jumps by up to 17.1 percentage points (a 51% relative boost on 0.5B). On LongBench v2, KV-CAT beats the base model across all retention budgets by up to 19% relatively.

Limitations and Future Work

  • Training Overhead: Running dual forward passes (masked and dense) plus router updates increases the computational cost of the continued pretraining phase compared to standard training, and directly embedding heavy optimization-based compressors inside the training loop remains computationally prohibitive.
  • Theoretical Scope: While Theorem 3.1 proves incompressible implementations exist, it is an existence proof rather than a guarantee that standard gradient descent always converges to them—though the empirical gains strongly show real-world LMs leave plenty of compressibility on the table.
  • Scale & Context: Experiments in the main text focus on sub-2B parameter models (0.5B and 1.5B) trained at 1K sequence lengths without explicit long-context training adaptation.

Summary updated

Abstract

Long-context language modeling is increasingly constrained by the Key–Value (KV) cache, whose memory and decode-time access costs scale linearly with the prefix length. This bottleneck has motivated a range of context-compression methods, from token-level summarization to recent optimization-based KV compression methods. These post-hoc methods operate on the KV cache of a fixed pretrained model, so their effectiveness is fundamentally limited by how well the model's internal representations can be compressed. In this work, we formalize the notion of KV compressibility and show that it is a property of the *learned representations*, rather than of the context alone. We prove that almost any sequence-to-vector function admits both highly compressible and inherently non-compressible transformer implementations, highlighting the need to guide transformers toward compressible representations during training. Motivated by this, we propose **KV**-**C**ompression **A**ware **T**raining (KV-CAT), a continued pretraining procedure that incentivizes the emergence of compressible representations. We introduce a train-time KV sparsification policy that masks KV slots during training. This forces the model to use fewer KV slots and encourages it to learn representations amenable to post-hoc compression. Empirically, we show that KV-CAT improves the quality–budget tradeoff of downstream compression methods across retrieval, long-context question answering, and perplexity-based evaluation of compressed-prefix continuation.
BibTeX
@inproceedings{gelberg2026training,
  author = {Yoav Gelberg and Yam Eitan and Michael M. Bronstein and Yarin Gal and Haggai Maron},
  title = {{Training Transformers for KV-Cache Compressibility}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=igMG5UgMAj},
}
Figure