Search

Search papers and lessons

Efficient Streaming Language Models with Attention Sinks

Guangxuan Xiao, Yuandong Tian, Beidi Chen et al. · 2023

Xiao et al. (2023) found that LLMs park attention on their first few tokens, and keeping those four tokens plus a recent window lets models run on streams of millions of tokens without fine-tuning.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Architectures
  • Discrete math
  • Sign in to see your progress

TL;DR

A language model trained on 4K-token windows falls apart when you feed it a stream much longer than that. The paper shows why: the model dumps a large share of its attention onto the first few tokens, whatever they say. If you keep the KV states of those four starting tokens and slide a window over the rest, the model keeps working on streams of 4 million tokens or more, with no fine-tuning.

Where you'll see it: always-on chat assistants, and streaming serving stacks that cap the KV cache at a fixed size.

The problem

A chat assistant that runs all day receives an input that never ends. Two things stop a standard transformer from doing this.

The first is memory. During decoding, the model caches the keys and values (KV states) of every past token, so the cache grows with the stream. The paper's Figure 1 marks this as dense attention with O(T2)O(T^2) time and a cache that keeps growing.

The second is length. Models only work well up to the length they were pretrained on. The paper's example is 4K tokens for Llama-2. Past that point, perplexity rises. Position-encoding fixes such as RoPE variants and ALiBi extend the range somewhat, but the authors report that none of them reached infinite length.

The obvious fix is a fixed-size cache: keep only the most recent LL tokens, which is window attention. Memory is constant, and decoding is fast. The paper shows it fails. Once the cache is full, perplexity spikes. Evicting the key and value of the very first token is enough to break it. The other fix, recomputing the window for each new token, works, but its cost is O(TL2)O(TL^2) and it is too slow to serve.

So the open question was: can a model trained on a finite window run on an unbounded input, with constant memory and without fine-tuning?

Key idea

The authors' observation is that the first few tokens are not special in meaning, yet the model attends to them heavily in most layers and heads (every layer beyond the bottom two, in Llama-2-7B). They call these tokens attention sinks.

The paper's explanation is the softmax. Attention weights for a query must sum to one over all visible tokens. When the current query has no strong match among the earlier tokens, the model still has to put its weight somewhere. Early tokens are visible to almost every later token in an autoregressive model, so training teaches the model to park the surplus weight there. Its value vectors then contribute little.

Removing those tokens from the cache removes the sink. The softmax denominator loses most of its mass and the attention distribution shifts far from anything the model saw in training. Keeping a few initial tokens restores the distribution.

StreamingLLM is therefore a cache with two parts: the KV of four initial tokens (the sinks), and the KV of the most recent tokens in a sliding window. Everything else is evicted.

flowchart LR
  T[Incoming token stream] --> C{KV cache}
  C --> S[First 4 tokens: attention sinks, kept]
  C --> R[Most recent tokens: rolling window, kept]
  C -.-> X[Middle tokens: evicted]
  S --> A[Attention over sinks plus window]
  R --> A
  A --> N[Next token]

The method

Let the cache hold the sink tokens {1,…,s}\{1,\dots,s\} and a window of the most recent LL tokens. When a new token tt arrives, the model attends over this set, and the cache then drops the oldest non-sink entry.

Positions are counted inside the cache. This is the paper's most important implementation detail. Positions are assigned to the cached tokens, not to their positions in the original text. Take a cache holding original tokens [0,1,2,3,6,7,8][0,1,2,3,6,7,8] while decoding token 9. The cache positions are [0,1,2,3,4,5,6,7][0,1,2,3,4,5,6,7], not the original [0,1,2,3,6,7,8,9][0,1,2,3,6,7,8,9]. The relative distances the model sees therefore never exceed the cache size, which is the range it has seen before.

For rotary encodings this means caching the keys before the rotation is applied, and re-applying the rotation with cache positions at each step. For ALiBi, the bias stays contiguous across the cache instead of jumping over evicted tokens.

Sink tokens need no training. The method works on existing models, Llama-2, MPT, Falcon and Pythia, with no change to their weights.

A dedicated sink token can be trained in. The paper's second idea is to prepend one learnable token to every pretraining sequence. The model then has one committed place to put surplus attention, and that single token is enough for streaming. The authors suggest that future models be pretrained this way. Two variants were tested against this (Section 3.3, Table 3): a fixed zero-key-and-value token, which the paper calls Zero Sink, and standard softmax with no sink.

Results

Why four sinks. Table 1 ran Llama-2-13B on the first PG-19 book, 65K tokens. Window attention with a 1024-token cache has perplexity 5158.07. Keeping four initial tokens with 1020 recent tokens gives 5.40. Replacing those four with newline tokens gives 5.60. The sink is about position, not meaning. Table 2 varies the number of initial tokens. One or two sinks do not fully recover perplexity, and four is roughly the point of diminishing returns across Falcon-7B, MPT-7B, Pythia-12B and Llama-2-7B.

Pretraining with a sink token. The paper trained two 160M-parameter models from scratch on the deduplicated Pile, following the Pythia-160M recipe with batch size 256, for 143,000 steps. Table 3 compares streaming perplexity. With the vanilla model, the first column (cache 0 plus 1024) gives 27.87, and the streaming perplexity only settles at 18.05 once two or four initial tokens are kept. The learnable sink-token model reaches 18.01 with a single sink plus 1023 recent tokens. Table 4 reports zero-shot accuracy on seven benchmarks, with and without the sink token: the sink-token model scores slightly higher on each, for example 62.6 against 62.2 on PIQA.

Long streams. Figure 5 shows perplexity staying flat on texts of more than 4 million tokens for Llama-2 at 7B, 13B and 70B, Falcon 7B and 40B, Pythia, and MPT at 7B and 30B. Figure 3 shows StreamingLLM matching the sliding-window-with-recomputation oracle on 20K-token texts, where dense attention fails past its pretraining window.

Streaming question answering. On ARC-Easy and ARC-Challenge questions concatenated into one stream (Table 5), Llama-2-7B-Chat with StreamingLLM reaches 71.34 on Arc-E and 55.03 on Arc-C. The one-shot baseline is 71.25 and 53.16. Window attention drops to 3.58 and 1.39. Dense attention runs out of memory. The paper's StreamEval benchmark queries the model every 10 lines of new information, with answers 20 lines back. Accuracy holds up to inputs near 120K tokens.

Speed. Figure 10 measures per-token decoding on one A6000 with Llama-2-7B and 13B. The re-computation baseline's latency grows quadratically with cache size. StreamingLLM's grows linearly. The speedup reaches 22.2x per token, and memory stays close to the baseline's.

The limit. Table 7 gives the cutoff. In StreamEval, each line is 23 tokens. With a 2044-token cache, accuracy holds at line distances up to 80 (1840 tokens), then falls to zero at 100 lines (2300 tokens). The model uses only what sits in its cache. Table 6 also shows that a larger cache does not always lower perplexity, so the model does not make full use of the context it has.

Why it mattered

The paper separates two things that were coupled before: the window the model was pretrained on, and the length of text it can generate. StreamingLLM does not extend the context window. It makes the attention computation stable when the window slides. The authors say so directly: the method is for short-term memory, such as dialogue, and not for long-document question answering or summarisation (Appendix A).

The sink finding also reached beyond language models. The authors show the same behaviour in BERT-base (high attention on the [SEP] token, Appendix H) and suggest it likely extends to Vision Transformers, which they link to the "registers" of Darcet et al. (2023); they did not test ViTs themselves. Their pretraining recommendation, a dedicated sink token in every training sample, is a design the authors propose for future models; the paper reports no experiment on models trained at scale with it. The paper reports that the method was adopted by NVIDIA TensorRT-LLM, Intel Extension for Transformers, Hugging Face Transformers and MLC LLM.

Reading guide

Read the paper in this order. Each step names the prerequisites it needs.

  1. Section 1, Introduction. Needs Transformer and KV Cache. Notice that the two problems, memory and length, are separate.
  2. Figure 1 and Section 3.1. Needs Attention Mechanism and Softmax. This is where the sink argument is made, through the softmax denominator in Equation 1. Read Attention Sink first if the idea is new.
  3. Table 1 and Table 2. Needs Attention Sink. These show the sink effect in numbers.
  4. Section 3.2, Rolling KV Cache. Needs Sliding Window Attention and Rotary Position Embedding. The position-counting rule in the cache is the part that makes the method work, so read it slowly.
  5. Section 3.3 and Section 4.2, Sink-token pretraining. Needs Language Model and the Softmax variant in Equation 2. The Zero Sink idea is a softmax with an extra zero term.
  6. Section 4.5, Efficiency. Needs KV Cache and an informal sense of quadratic versus linear cost. The 22.2x result is in Figure 10.
  7. Appendices C and D. No new prerequisites. They give the limits of the method, and they are worth reading before you rely on the result.

Check yourself

QuestionWhy does the model put attention on the first token even when that token carries no meaning for the prediction?Show answer

The softmax forces the attention weights for each query to sum to one over every visible token. When no earlier token is a strong match, the surplus weight still has to go somewhere. The first token is visible to all later tokens in an autoregressive model, so training teaches the model to use it as a sink for the surplus. Its value vector contributes little, so the sink works as a dump rather than a source of information.

QuestionThe cache holds 4 sink tokens (original positions 0 to 3) and 1020 recent tokens, which are original tokens 4000 to 5019. The model is decoding original token 5020. What positions does the model assign to the sinks and to the first recent token?Show answer

Positions are counted within the cache, not in the original text. The sinks keep positions 0 to 3. The first recent token (original token 4000) takes position 4, and the largest cache position is 1023. Using original positions instead would put the recent tokens at 4000 to 5019, far outside the range of 0 to 1023 the model saw in pretraining.

QuestionTable 1 shows window attention with a 1024-token cache at perplexity 5158.07 on Llama-2-13B. Which change brings it down to 5.40, and what did the authors test to show the cause is the position of those tokens rather than their meaning?Show answer

Keeping the four initial tokens in the cache alongside the 1020 most recent tokens. Window attention alone, with the same 1024-token budget, has perplexity 5158.07; with the four initial tokens restored it is 5.40. To confirm that the sink is about position rather than content, the authors replaced the four initial tokens with newline tokens. Perplexity was 5.60, almost the same. The meaning of the first tokens does not matter; their absolute position does.

QuestionWhy does StreamingLLM not let a model read a 100K-token document and answer questions about its first page?Show answer

StreamingLLM does not extend the context window. It keeps only the sinks and a recent window in the cache, so tokens outside the window are evicted and can no longer be attended to. Table 7 shows the effect: accuracy falls to zero once the query-answer distance exceeds the cache size. The method is built for short-term memory such as dialogue, not for long-range retrieval, which the authors list as a limitation in Appendix A.

Sources

Read the full paper

Guangxuan Xiao, Yuandong Tian, Beidi Chen et al., 2023. arXiv:2309.17453