Adapting Language Models to Compress Contexts

Alexis Chevalier, Alexander Wettig, Anirudh Ajith, Danqi Chen · OpenReview

Abstract

Transformer-based language models (LMs) are powerful and widely-applicable tools, but their usefulness is constrained by a finite context window and the expensive computational cost of processing long text documents. We propose to adapt pre-trained LMs into AutoCompressors. These models are capable of compressing long contexts into compact summary vectors, which are then accessible to the model as soft prompts. Summary vectors are trained with an unsupervised objective, whereby long documents are processed in segments and summary vectors from all previous segments are used in language modeling. We fine-tune OPT models on sequences of up to 30,720 tokens and show that AutoCompressors can utilize long contexts to improve perplexity. We evaluate AutoCompressors on in-context learning by compressing task demonstrations. We find that summary vectors are good substitutes for plain-text demonstrations, increasing accuracy while reducing inference cost. Finally, we explore the benefits of pre-computing summary vectors for large corpora by applying summary vectors to retrieval-augmented language modeling. Overall, AutoCompressors emerge as a simple and inexpensive solution for extending the context window of LMs while speeding up inference over long contexts.

Introduction

Standard Transformer-based language models are victims of their own success. While the self-attention mechanism allows for sophisticated modeling of dependencies within a sequence, its computational cost grows quadratically with the length of the input. This creates a significant "context window" bottleneck. When tasks require analyzing extensive documents, such as entire books or massive codebases, researchers are often forced to choose between astronomical GPU memory requirements or truncating the text, which inevitably leads to the loss of vital information.

Previous attempts to mitigate this—such as sparse attention or recurrent architectures—often require training models from scratch or struggle to maintain fine-grained details over long sequences. AutoCompressors offer a different path: teaching existing, high-performance models (like OPT or Llama-2) to compress their own history into a compact, usable format without sacrificing the reasoning capabilities gained during large-scale pre-training.

Methodology

AutoCompressors build upon the Recurrent Memory Transformer (RMT) architecture but introduce several critical innovations to improve information retention and training stability.

Summary Vectors and Accumulation

The core of the approach involves adding special <Sum> tokens to the model's vocabulary. As the model processes a segment of text, it generates "summary vectors" at these token positions. These are not discrete words but high-dimensional "soft prompts" that encapsulate the semantic essence of the segment. Unlike previous methods that might only pass the most recent summary forward, AutoCompressors concatenate summaries from all previous segments. This creates a direct, persistent information pathway from the beginning of a document to the current processing point.

Training Objective

The models are fine-tuned using an unsupervised objective: the model must predict the next tokens in a future segment using only the summary vectors derived from previous segments. To ensure robustness, the authors employed randomized segmenting during training. By varying the lengths of text segments, the model is forced to learn how to compress contexts of arbitrary lengths rather than relying on fixed-window patterns.

To manage the computational overhead of training on long sequences, the researchers utilized "stop-gradients," preventing backpropagation through the entire historical chain. This allowed for effective fine-tuning on a single NVIDIA A100 GPU.

Experimental Setup

The researchers evaluated AutoCompressors across several high-quality corpora and tasks:

  • Language Modeling: Fine-tuning on subdomains of The Pile (Books3, FreeLaw, GitHub, Wikipedia) and RedPajama for scaling experiments.
  • In-Context Learning (ICL): Testing on 11 classification and QA tasks from SuperGLUE and other benchmarks (e.g., SST-2, BoolQ, AG News).
  • Retrieval-Augmented Generation (RAG): Using Natural Questions (NQ) to evaluate passage re-ranking and the efficiency of pre-computed summary vectors.

Key Results

Context Extension

AutoCompressors successfully extended the effective context of OPT and Llama-2 models to 30,720 tokens. The experiments demonstrated consistent improvements in perplexity as more context was provided, proving that the model can successfully extract and utilize information from the compressed summary vectors.

Efficiency in In-Context Learning

One of the most striking findings involved task demonstrations. Using just 150 summary vectors (representing approximately 2,250 raw tokens) outperformed standard ICL using 750 raw tokens on 8 out of 11 tasks. This indicates that summary vectors can represent abstract concepts more densely than raw text, allowing for better performance with a smaller "active" context window.

Throughput and RAG

In retrieval-augmented settings, the use of "fused summaries"—where summary vectors are pre-computed for a library of documents—achieved a 1.7x increase in throughput compared to processing full-text passages. Furthermore, this approach actually improved the model's accuracy (perplexity), creating a new Pareto front for the trade-off between accuracy and inference speed.

Discussion and Limitations

While AutoCompressors represent a significant step forward, certain challenges remain. There is an "initial loss" observed at the beginning of sequences; full-attention models still outperform AutoCompressors in the very early stages of a document before the model has "settled" into the compressed context. Additionally, while summary accumulation is far more efficient than quadratic attention, the memory footprint still grows linearly. Future work may need to explore more sophisticated methods for merging or pruning these vectors for truly infinite contexts.

Furthermore, while the 7B parameter Llama-2 model showed success, the scaling behavior for 70B+ parameter models remains an open question. Finally, ensuring the training signal is strong enough to force the model to rely on long-range summaries rather than local patterns is a continuing area of refinement.

Conclusion

AutoCompressors provide a simple and inexpensive solution for extending the context window of existing LMs. By transforming long contexts into pre-computable soft prompts, they significantly speed up inference and improve performance on long-range reasoning tasks, offering a practical path toward more efficient large-scale document processing.

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