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 allows Large Language Models (LLMs) to handle extremely long sequences—up to 1,024K tokens—by using an efficient, block-based external memory. By intelligently retrieving only the most relevant distant contexts and offloading the rest to CPU memory, InfLLM achieves performance comparable to models explicitly fine-tuned for long contexts, but at a fraction of the computational cost.

What Problem Does This Paper Address?

Most LLMs are pre-trained on a fixed budget of tokens (e.g., 4K or 8K). When you try to feed them longer sequences, two major issues arise:

  1. Out-of-Domain Issues: The model encounters positional embeddings it has never seen before, causing its internal logic to break down.
  2. Attention Distraction: As the number of tokens grows, the "signal-to-noise" ratio drops. The model gets distracted by irrelevant distant tokens, leading to a sharp decline in reasoning and retrieval accuracy.

While "continual pre-training" on long sequences is a common fix, it is incredibly expensive and can actually degrade the model's performance on shorter, standard tasks. The authors of InfLLM ask a critical question: Can we unlock the model's intrinsic ability to handle long contexts without changing its weights?

Methods

InfLLM introduces a training-free, memory-based approach that combines a standard sliding window for local context with an efficient lookup mechanism for distant context.

InfLLM Architecture
  • Block-Level Context Memory: Instead of treating every past token as equally important, InfLLM organizes the KV (Key-Value) cache into distinct blocks or "memory units."
  • Representative Tokens: For each block, the system identifies a few "representative tokens"—those that received the highest attention scores within their local window. These act as a semantic "index" for the block.
  • Efficient Lookup: During inference, the model only attends to the current local window plus a small number of highly relevant blocks retrieved from memory based on the representative tokens.
  • Memory Offloading: To keep GPU memory (VRAM) usage low, InfLLM offloads the bulk of the context memory to the CPU. It uses a Least Recently Used (LRU) cache strategy to keep only the most "active" blocks on the GPU.
  • Positional Encoding Hack: To avoid the out-of-domain issue, all tokens in the distant memory are assigned a fixed distance (equal to the local window size) from the current token, ensuring the model stays within its trained positional range.

Key Insights and Ideas

  • LLMs are "Long-Context Ready" by Default: The most striking insight is that models like Llama-3 and Mistral already know how to process long contexts; they just need a better way to filter out the noise. InfLLM proves that fine-tuning isn't always necessary for context extension.
  • Sparsity is a Feature: Attention matrices are naturally sparse. By only looking at the most relevant blocks, InfLLM mimics the way humans might flip back to a specific page in a book rather than trying to remember every single word simultaneously.
  • Block-Level > Token-Level: Retrieving entire blocks of text is more semantically coherent and computationally efficient than trying to retrieve individual tokens. It reduces the overhead of memory loading and provides the model with enough local context to make sense of the retrieved information.

Key Results

InfLLM was evaluated using challenging long-context benchmarks, including $\infty$-Bench, LongBench, and a synthetic Retrieve.PassKey test.

Performance Comparison on Long-Context Tasks
  • The 1M Token Milestone: On the PassKey retrieval task, InfLLM maintained 100% accuracy even as the context length was scaled to 1,024,000 tokens. In contrast, baseline models failed almost immediately after exceeding their training length.
  • Competitive with Fine-Tuning: On $\infty$-Bench, InfLLM (which requires zero training) achieved performance comparable to models that underwent extensive continual pre-training, significantly outperforming other training-free baselines like NTK and Streaming.
  • Efficiency Gains: InfLLM processed 100K token sequences using only 26GB of VRAM. Compared to full-attention models, it achieved a 34% reduction in time consumption and used only 34% of the GPU memory required by standard long-context implementations.

Limitations and Future Work

  • CPU Overhead: While InfLLM saves GPU memory, it stores a massive amount of data in CPU RAM. For extremely long streaming applications, this could still lead to memory bottlenecks.
  • Fixed Block Sizes: The current implementation uses fixed-size blocks. The authors suggest that "dynamic segmentation"—letting the model decide where a semantic block starts and ends—could further improve retrieval accuracy.
  • Inference Speed: While efficient, the lookup mechanism still adds some latency. Integrating InfLLM with high-performance inference frameworks like vLLM or llama.cpp is a noted direction for future engineering.
  • KV Cache Compression: Combining InfLLM with KV cache quantization (reducing the precision of stored vectors) could further shrink the memory footprint.

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