InfLLM: Training-Free Long-Context Extrapolation for LLMs with an Efficient Context Memory

Chaojun Xiao, Pengle Zhang, Xu Han, Guangxuan Xiao, Yankai Lin, Zhengyan Zhang, Zhiyuan Liu, Maosong Sun

TL;DR

InfLLM is a training-free method that enables Large Language Models (LLMs) to process extremely long sequences—up to 1 million tokens—without the need for expensive fine-tuning. By utilizing an efficient, block-level context memory and smart CPU offloading, it allows models trained on just a few thousand tokens to match the performance of specialized long-context models while significantly reducing GPU memory overhead.

What Problem Does This Paper Address?

Modern LLMs are typically pre-trained on sequences of a fixed maximum length (e.g., 4K or 8K tokens). When these models encounter "streaming" inputs or massive documents that exceed this window, they run into two major walls:

  1. Out-of-Domain Issues: The positional encodings used during training don't generalize to much larger indices, causing the model to "break."
  2. Attention Distraction: As the context grows, the attention mechanism gets bogged down by "noise"—irrelevant tokens that distract the model from the few pieces of information that actually matter.

Existing solutions usually involve "continual pre-training" on long sequences, which is computationally expensive, requires massive high-quality datasets, and can actually degrade the model's performance on shorter, standard tasks.

Methods

The authors introduce InfLLM, which treats long-context processing as a memory retrieval problem rather than a sequence length problem.

  • Sliding Window + External Memory: InfLLM uses a standard sliding window for local tokens but doesn't just discard the "evicted" tokens. Instead, it stores them in an external memory.
  • Block-Level Organization: To avoid the overhead of searching through every single past token, InfLLM organizes the KV (key-value) cache into blocks.
  • Representative Tokens: For each block, the system identifies a few "representative tokens"—those that received the highest attention scores during the initial encoding. These act as a semantic summary for the entire block during lookup.
  • Efficient Lookup & Offloading: At each step, the model looks up only the most relevant memory blocks to include in the current attention calculation. To save precious GPU VRAM, the bulk of these blocks are offloaded to CPU memory and swapped into the GPU only when needed using a Least Recently Used (LRU) cache strategy.
Figure 1: The illustration of InfLLM. Here, the current tokens refer to tokens that need to be encoded in the current computation step. The past key-value vectors can be divided into the initial tokens, evicted tokens, and local tokens, arranged the furthest to the nearest relative to the current tokens. For each computation step, the context window consists of the initial tokens, relevant memory units, and local tokens.

Datasets

The researchers evaluated InfLLM using two primary benchmarks:

  • $\infty$-Bench: A suite of tasks (QA, summarization, retrieval) where the average sequence length exceeds 100K tokens.
  • LongBench: A comprehensive bilingual benchmark for long-context understanding with sequences up to ~32K tokens.
  • Retrieve.PassKey: A synthetic task used specifically to test extreme scaling, requiring the model to find a 5-digit code hidden deep within a massive "haystack" of noise.

Key Insights and Ideas

  • Intrinsic Capacity: The paper reveals that LLMs already have the capacity to understand long contexts; they just need a better way to filter out the noise.
  • Blocks > Tokens: Searching for relevant information at the block level is not just more efficient; it's more effective. Coherent semantic blocks provide a clearer signal for retrieval than individual, noisy tokens.
  • Training-Free is Viable: You don't need to train a separate "retriever" or fine-tune the LLM. The model's own internal attention scores are sufficient to identify which parts of the past are important.
  • Fixed Positional Encodings: To solve the "out-of-domain" position issue, InfLLM simply assigns all retrieved distant memory units the same relative distance (the edge of the local window), which prevents the model from encountering unseen position indices.

Key Results

InfLLM shows that you can get "long-context" performance for free.

  • Superior Performance: On $\infty$-Bench, InfLLM (based on Mistral-7B) achieved an average score of 57.7, far outperforming standard sliding window methods (21.5) and even specialized interpolation techniques like NTK-aware scaling (44.3).
  • Efficiency Gains: Compared to a Llama-3 model fine-tuned for 1M context, InfLLM achieved comparable results while using 66% less GPU memory and running 34% faster.
  • Extreme Scaling: InfLLM maintained 100% accuracy on the PassKey retrieval task even when the context was scaled to 1,024,000 tokens on a single GPU.
Table 1: The results of InfLLM and baseline models on ∞-Bench. The 95% quantile for text lengths in ∞-Bench is 214K. The context window size for sliding window models refers to the local window size, and for InfLLM refers to “local window size + selected memory size”.
Scaling results showing InfLLM maintains perfect accuracy up to 1M tokens, while standard sliding windows fail quickly.

Limitations and Future Work

While impressive, the authors note a few areas for improvement:

  • CPU Memory Bottleneck: Storing the full KV cache for millions of tokens requires significant CPU RAM. Future work could integrate KV cache quantization to shrink this footprint.
  • Inference Speed: While faster than full-attention models, there is still overhead from memory lookup and swapping. Integration with optimized kernels (like vLLM or llama.cpp) is a logical next step.
  • Dynamic Segmentation: The current version uses fixed-size blocks. Exploring dynamic segmentation based on semantic boundaries could further improve retrieval accuracy.

Summary updated

BibTeX
@inproceedings{xiao2024infllm,
  author = {Chaojun Xiao and Pengle Zhang and Xu Han and Guangxuan Xiao and Yankai Lin and Zhengyan Zhang and Zhiyuan Liu and Maosong Sun},
  title = {{InfLLM: Training-Free Long-Context Extrapolation for LLMs with an Efficient Context Memory}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=bTHFrqhASY},
}
Figure