Adapting Language Models to Compress Contexts

Alexis Chevalier, Alexander Wettig, Anirudh Ajith, Danqi Chen

Abstract

Large Language Models (LLMs) are limited by finite context windows and the quadratic computational cost of long-range attention. We present AutoCompressors, a method to fine-tune pre-trained LMs to recursively compress long contexts into compact "summary vectors." These vectors serve as soft prompts, enabling models to process tens of thousands of tokens with significantly improved efficiency. Our results demonstrate that AutoCompressors can extend effective context windows while speeding up inference by up to 1.7x and maintaining high performance on long-range modeling and in-context learning tasks.

Introduction

Standard Transformer architectures face a fundamental bottleneck: the quadratic scaling of attention with respect to context length. While recent models have expanded context windows to 128k tokens and beyond, the memory and compute requirements remain prohibitive for many real-time applications. Previous attempts to mitigate this, such as the Recurrent Memory Transformer (RMT), introduced fixed-size memory bottlenecks between segments but often struggled with long-term information retention.

Researchers from Princeton University address these challenges with AutoCompressors. By teaching an LM to generate its own "summary vectors" from segments of text, they create a mechanism to "cache" the essence of a document, allowing the model to "remember" more without the linear increase in KV-cache size or the quadratic increase in computation.

Methods

The AutoCompressor framework modifies pre-trained LMs (such as OPT and Llama-2) to generate summary vectors from text segments. The process involves several key innovations:

  • Summary Vectors: Special <Sum> tokens are added to the model's vocabulary. When the model encounters these tokens, it is trained to output hidden states that act as compressed representations of the preceding text.
  • Summary Accumulation: Unlike traditional recursive methods that only pass the last summary forward, AutoCompressors concatenate summary vectors from all previous segments. This creates a "direct information pathway" from every part of the document to the current prediction point.
  • Randomized Segmenting: During the fine-tuning phase, the model processes segments of varying lengths. This prevents over-fitting to a specific segment size and ensures the model remains robust for diverse real-world document structures.
  • Efficient Training: To make training feasible on standard hardware (e.g., a single NVIDIA A100 80GB), the authors utilize Backpropagation Through Time (BPTT) with stop-gradients after two compression steps, drastically reducing memory overhead.
AutoCompressor architecture showing recursive compression of segments into summary vectors.

Results and Performance

Evaluation across datasets like The Pile, RedPajama, and Gutenberg shows that AutoCompressors effectively utilize contexts of up to 30,720 tokens. As illustrated in the figure below, perplexity (PPL) consistently decreases as the number of context tokens increases, outperforming RMT and other ablation variants.

Perplexity (PPL) vs. context tokens for AutoCompressor and its variants compared to RMT.

Key findings include:

  • In-Context Learning (ICL): Compressing 750 tokens of task demonstrations into just 50 summary vectors outperformed standard few-shot ICL on 8 out of 11 SuperGLUE tasks. This indicates that summary vectors can capture task-specific logic more efficiently than raw tokens.
  • RAG Efficiency: In Retrieval-Augmented Generation settings, using "Fused Summaries" achieved 1.5x the perplexity gains of plain-text passages while providing a 1.7x throughput increase.
  • Passage Re-ranking: AutoCompressors established a new Pareto front for re-ranking tasks, offering a significantly better trade-off between accuracy (Recall@20) and throughput compared to standard OPT models.

Discussion and Limitations

AutoCompressors represent a significant step toward efficient long-context modeling by treating summary vectors as "semantic zip files." However, several limitations remain. Compression is inherently lossy, and the authors note that summary vectors generalize slightly less well to out-of-domain data than raw attention. Additionally, while the growth of summary vectors is much slower than raw tokens, it still grows linearly with document length, meaning the attention cost eventually becomes quadratic again at extreme scales.

Future work may focus on scaling these capabilities to 70B+ parameter models and refining the training signal to better prioritize long-term information storage over immediate token prediction.

Summary updated

BibTeX
@inproceedings{chevalier2023adapting,
  author = {Alexis Chevalier and Alexander Wettig and Anirudh Ajith and Danqi Chen},
  title = {{Adapting Language Models to Compress Contexts}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=TZ7eFOqVeB},
}
Figure