Efficient Streaming Language Models with Attention Sinks

Guangxuan Xiao, Yuandong Tian, Beidi Chen, Song Han, Mike Lewis

TL;DR

StreamingLLM is an efficient framework that enables Large Language Models (LLMs) to handle infinitely long text streams without fine-tuning. By identifying and preserving "attention sinks"—initial tokens that anchor the model's attention mechanism—the authors prevent the catastrophic performance collapse typically seen in window-based attention, achieving up to a 22.2x speedup over recomputation methods.

What Problem Does This Paper Address?

Standard LLMs are trained on fixed sequence lengths (e.g., 2,000 or 4,000 tokens). When deployed in "streaming" scenarios—like long-running chatbots or continuous document analysis—they face two major hurdles:

  1. Memory Bottlenecks: The Key-Value (KV) cache, which stores past token states to speed up generation, grows linearly with text length, eventually exhausting GPU memory.
  2. Generalization Failure: When the input length exceeds the training window, the model's performance collapses, and perplexity spikes to infinity.

A common "fix" is Window Attention, which only keeps the most recent tokens in the cache. However, the authors discovered a critical flaw: as soon as the very first token is evicted from the cache, the model breaks entirely. This creates a paradox where discarding old tokens is necessary to save memory, but discarding the oldest tokens destroys the model's ability to reason.

Dense Attention vs. Window Attention

Methods: The Attention Sink Discovery

To solve this, the researchers introduced StreamingLLM, a simple yet powerful modification to how the KV cache is managed during inference.

The core of this method relies on a phenomenon the authors term the Attention Sink. They found that LLMs allocate massive attention scores to the first few tokens of a sequence, regardless of their semantic meaning. Because the Softmax operation requires attention scores to sum to 1, the model effectively "dumps" unnecessary attention into these initial tokens.

Instead of a simple sliding window, StreamingLLM keeps two distinct sets of tokens in the cache:

  1. Attention Sinks: The first 1–4 tokens of the entire sequence, preserved indefinitely.
  2. Rolling Window: The most recent $N$ tokens.
StreamingLLM Architecture

To ensure the model stays within its "comfort zone," StreamingLLM assigns positional indices based on the cache's layout rather than the token's absolute position in the stream. Furthermore, the authors propose adding a specific placeholder token (a "sink token") during pre-training so the model doesn't have to hijack semantic tokens to serve as an attention anchor.

Key Insights and Ideas

  • Softmax is the "Culprit": The reason attention sinks exist is structural. Because autoregressive models see the first tokens in every single step of training, the Softmax layer learns to use them as a "trash bin" for attention scores that don't strongly match any other part of the context.
  • Position Over Semantics: In a fascinating experiment, the authors replaced the first four tokens with line breaks (\n). The model still functioned perfectly. This proves the model doesn't need the meaning of the first tokens; it just needs an anchor at the "start" position to stabilize the Softmax distribution.
  • No Fine-Tuning Required: This method works on existing, off-the-shelf models like Llama-2, Falcon, and MPT without any additional training.

Key Results

  • Infinite Length: StreamingLLM allowed models to perform stable language modeling for over 4 million tokens with constant memory usage.
  • Massive Speedup: Compared to the "Sliding Window with Recomputation" baseline (the only other way to maintain accuracy), StreamingLLM is up to 22.2x faster.
  • Stability: While standard window attention sees perplexity jump from ~10 to over 5,000 as soon as the first token is evicted, StreamingLLM matches the performance of "oracle" models that have access to the full history.

Limitations and Future Work

While StreamingLLM allows a model to run forever without crashing, it doesn't actually remember everything. It only has access to the "sinks" and the most recent window. If a user asks a question about something mentioned 10,000 tokens ago (and it's not in the current window), the model will not know the answer.

For true long-term memory, the authors suggest that StreamingLLM should be combined with Retrieval-Augmented Generation (RAG) or external databases. Finally, the paper strongly recommends that future LLMs be trained with a dedicated placeholder sink token to make streaming deployment even more robust natively.

Summary updated

BibTeX
@inproceedings{xiao2024efficient,
  author = {Guangxuan Xiao and Yuandong Tian and Beidi Chen and Song Han and Mike Lewis},
  title = {{Efficient Streaming Language Models with Attention Sinks}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=NG7sS51zVF},
}
Figure