Efficient Streaming Language Models with Attention Sinks
Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024))
Subscriber deck — free during early access
14 cards
1 Introduction
- What two challenges arise when running a language model on an endless input stream such as a day-long chat?The KV cache of all previous tokens keeps growing, using more memory and slowing decoding; and models degrade on sequences longer than their pre-training attention window.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §1
- Why are window attention and sliding-window recomputation unsatisfactory for streaming?Window attention (cache only the last \(L\) tokens’ keys and values) is efficient but collapses as soon as the first tokens are evicted. Recomputing the KV states of the recent window for each new token works well but costs \(O(TL^2)\), far too slow.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §1
- What is an attention sink?Initial tokens that receive a surprisingly large share of attention in most layers and heads regardless of their meaning, because softmax forces attention weights to sum to one: when a query matches nothing, the excess attention has to go somewhere.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §1
- Why do the first tokens, in particular, become attention sinks?In autoregressive models the first tokens are visible to every later token, while later tokens are visible only to those after them, so the initial ones are the easiest to train as a place to dump unneeded attention.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §1
3.1 The Failure of Window Attention and Attention Sinks
- Why does window attention collapse when the initial tokens are evicted?Their keys took a large part of the softmax denominator in most layers; removing them shifts the attention distribution far from what the model saw in training, and perplexity spikes.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.1
- How did Xiao et al. show that the initial tokens matter for their position rather than their meaning?Replacing the first four tokens with line-break tokens and keeping them alongside the recent window restored perplexity almost as well as the original tokens: the model attends to them because of where they are.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.1
3.2 Rolling KV Cache with Attention Sinks
- How does StreamingLLM manage the KV cache?It keeps the keys and values of a few initial tokens (four suffice) as attention sinks, plus a rolling window of the most recent tokens, evicting everything in between. No fine-tuning is needed.
A few initial tokens plus recent ones: constant memory, stable perplexity - Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.2
- How does StreamingLLM assign positions to cached tokens?By position within the cache, not in the original text: with cached tokens 0–3 and 6–8 while decoding token 9, positions are 0–7. For RoPE, keys are cached before rotation and rotated by their cache position at each step; ALiBi gets a contiguous bias.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.2
- Why are four initial tokens needed rather than one?Models like Llama-2 weren’t pre-trained with a consistent first token in every sample (the start token is added before text is chunked), so they learned to spread sink attention over several initial tokens; one or two don’t fully restore perplexity.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.2
3.3 Pre-training LLMs with Attention Sinks
- What did Xiao et al. propose for future pre-training?Prepend a learnable sink token to every training sample. In 160M-parameter models it became a dedicated attention sink: the sink token plus the recent window gave stable streaming perplexity, with no loss in convergence or benchmark accuracy.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.3
- How did the “Zero Sink” (softmax-off-by-one) compare with a learnable sink token?Letting attention sum to less than one (equivalent to a zero key and value token) helped only somewhat; the model still relied on other initial tokens. A learnable sink token worked much better.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §3.3
4.1 Language Modeling on Long Texts Across LLM Families and Scales
- How far could StreamingLLM go?Llama-2, MPT, Falcon and Pythia models (2.8B–70B) kept stable perplexity over 4 million tokens of PG19 books, matching the sliding-window-with-recomputation oracle, while dense attention failed past the pre-training length and window attention past the cache size.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §4.1
4.4 Ablation Studies
- Does a larger cache always improve StreamingLLM’s perplexity?No: increasing the cache size didn’t consistently lower perplexity, suggesting the models don’t fully use the context they get. StreamingLLM also doesn’t extend the context window: tokens evicted from the cache are forgotten.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §4.4
4.5 Efficiency Results
- How much faster is StreamingLLM than sliding-window recomputation?Its per-token decoding latency grows linearly with cache size versus quadratically for recomputation, giving up to 22.2× speedup per token with a similar memory footprint.
- Xiao et al., 2024, Efficient Streaming Language Models with Attention Sinks (The Twelfth International Conference on Learning Representations (ICLR 2024)), §4.5
Source
- Guangxuan Xiao, Yuandong Tian, Beidi Chen, Song Han and Mike Lewis. 2024. Efficient Streaming Language Models with Attention Sinks. In The Twelfth International Conference on Learning Representations (ICLR 2024). https://arxiv.org/abs/2309.17453