Pidoku

StreamingLLM and Attention Sinks (2023)

Module 30 Lesson 30.p 30 min Difficulty 2/5

Prerequisites 12.e, 12.h, 10.g


Before you start: words you need#

WordPlain meaning
Window attentionKeeping only the KV cache of the most recent L tokens and evicting older ones, so memory stays fixed.
Sliding window with re-computationFor every new token, recompute the KV cache of the last L tokens from scratch. Accurate but O(L²) work per token.
Attention sinkA token (usually the very first ones) that receives a large share of attention from almost every query, even though it carries little meaning.
PerplexityHow surprised a model is by text; lower is better. A sudden jump to the thousands means the model has broken.
Rolling KV cacheA fixed-size cache where the newest token’s KV replaces the oldest (excluding the sink tokens).
RoPERotary position embedding: positions are encoded by rotating Q and K by an angle that depends on position (10.i).
Pre-training context lengthThe longest sequence the model saw in training (4,096 for Llama-2).

1. The paper#

  • Title: “Efficient Streaming Language Models with Attention Sinks”
  • Authors: Guangxuan Xiao, Yuandong Tian, Beidi Chen, Song Han, Mike Lewis (MIT, Meta AI, Carnegie Mellon, NVIDIA)
  • Venue: ICLR 2024 (arXiv September 2023).
  • Link: arXiv:2309.17453 · code: mit-han-lab/streaming-llm

2. The problem#

A chat that runs for hours, or an agent that never stops, keeps growing its KV cache. Two things go wrong:

  1. Memory and time grow with every token (12.h).
  2. Quality collapses past the training length. Feed Llama-2 more than 4k tokens and perplexity explodes.

The obvious fix, window attention (keep only the last L tokens), fails too — and surprisingly badly. On Llama-2-13B with a 1,024-token window, perplexity on a book was 5,158 — the model was producing nonsense — as soon as the first tokens left the window. The working alternative, recomputing the window for every token, is far too slow.

Why would losing the first few tokens of a long book matter so much?


3. The idea#

The observation: in every model they looked at (Llama-2, MPT, Falcon, Pythia), most layers and heads send a large share of their attention to the first few tokens, whatever those tokens are.

The explanation: softmax forces attention weights to sum to 1. When a query has nothing useful to attend to, the weight has to go somewhere. Because the first tokens are visible to every later token during training, the model learns to dump excess attention there. They act as a sink. Evict them and the softmax denominators change, every attention output shifts, and the model breaks.

Evidence: replacing the first four tokens with four line breaks ("\n") restored almost all the quality (perplexity 5.60 vs 5.40). Their position, not their content, makes them sinks.

The fix (StreamingLLM): keep the KV of the first 4 tokens forever, plus a rolling window of recent tokens. No fine-tuning.

flowchart LR
  S["4 sink tokens (kept forever)"] --- E["evicted middle tokens (gone)"] --- R["rolling window of recent tokens"] --- N["new token"]
  class S memory
  class E warn
  class R compute
  class N io

For new models: train with one dedicated, learnable sink token at the start, and a single sink is enough.


4. How it works#

  1. Cache = 4 sink tokens + the most recent L − 4 tokens. When a new token arrives, evict the oldest non-sink token.
  2. Positions are assigned inside the cache, not in the original text. If the cache holds tokens 0–3 and 96–99, they get positions 0–7 for RoPE. Otherwise the model would see positions far beyond its training range. With RoPE, the keys are cached before rotation and rotated at each step.
  3. Attention is computed only over the cached tokens, so cost per token is constant.

What it is not: StreamingLLM does not extend the model’s memory. Evicted tokens are gone. A question about something said 100,000 tokens ago cannot be answered. It keeps the model fluent indefinitely; it does not make it remember indefinitely.


5. Results#

MeasureResult
Llama-2-13B, PG19 first book, 1,024-token cache: window only (0 + 1024)perplexity 5,158.07
Same, 4 sinks + 1,020 recent5.40
Same, 4 line-break tokens + 1,020 recent5.60
Llama-2-7B, 4,096-token cache: 0 sinks / 1 / 2 / 4 / 83,359.95 / 11.88 / 10.51 / 9.59 / 9.54
Longest stable stream tested4 million tokens (Llama-2, MPT, Falcon, Pythia)
Speed vs sliding window with re-computationup to 22.2× faster per token

Four sinks were enough; one or two helped a lot but not fully (Table 2). Models pre-trained with one dedicated sink token needed only that one token to stream stably.


6. What held up and what did not#

Held up

  • Attention sinks are real and everywhere. They became a standard idea in interpretability and in inference engineering.
  • Never evict or crush the first tokens. KV-cache compression and eviction methods (H2O, SnapKV and later; 13.i, 24.c), KV quantization schemes that keep sink tokens in higher precision, and sparse attention patterns (30.y) all keep the initial tokens.
  • Sinks built into the architecture. OpenAI’s gpt-oss models (2025) alternate 128-token banded (sliding-window) layers with dense layers and give every head a learned sink term in the softmax denominator, citing this paper — which serving engines must implement in their attention kernels.
  • Sliding-window layers with a fixed-size cache are common in 2026 hybrid models (Gemma, gpt-oss), and engines size their caches per layer type (17.i).

Did not, or changed

  • “Infinite length” was oversold by some readers. It is infinite streaming, not infinite context. Long-context work moved to genuinely using long contexts (RoPE scaling, sparse attention, context parallelism; 14.i, 24.g).
  • Sinks can be removed by design. Gated attention (Qwen team, 2025) adds a sigmoid gate after attention and largely eliminates sinks and the huge activations that come with them, which also helps quantization.
  • Few production chat systems run pure StreamingLLM; they summarise, truncate or retrieve old context instead.

7. Where it lives in 2026#

  • Taught in depth: context length and its costs in 12.h, KV cache optimisation and eviction in 13.i, KV cache research in 24.c, long-context inference in 14.i.
  • vLLM and SGLang support learned sinks (needed for gpt-oss) in their attention backends and sliding-window caches for hybrid models.
  • Hugging Face Transformers once shipped a SinkCache class for this; it has since been removed from the main library, a sign that pure StreamingLLM is now a research technique, not a product feature.

Checked on 13 November 2026.


8. Questions#

  1. Why does window attention collapse once the first tokens are evicted?
  2. What is an attention sink, and why does softmax create one?
  3. Replacing the first four tokens with line breaks barely hurt quality. What does that show?
  4. Why must StreamingLLM assign positions within the cache rather than in the original text?
  5. Can a StreamingLLM chatbot answer a question about something said 1 million tokens ago? Why not?
  6. How do attention sinks affect KV-cache eviction and KV quantization?
  7. gpt-oss has a learned sink per head. What must an attention kernel do differently to support it?

9. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in