Attention Sinks › Mathematical Analysis
Language AI HandbookAll chapters Attention Sinks › Mathematical AnalysisOne section · Sections in this chapter5 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To understand why attention sinks emerge and why they're essential for streaming inference, we need to examine the mathematics of attention itself. This walkthrough from the basic attention formula to the StreamingLLM solution will show a core tension in transformer design: the softmax function that makes attention work also creates the problem that sinks must solve.
The analysis proceeds in three stages. First, we examine the standard attention formula to identify exactly where the softmax constraint forces excess attention to accumulate somewhere. Second, we trace the training dynamics that direct that excess toward the first positions. Third, we follow the mathematics through a sink removal event to see precisely why and how the model breaks when sinks are absent. Each stage builds directly on the previous one, so by the end you will have a complete mechanistic account of the phenomenon from first principles.
One thing to keep in mind as you read through the formulas: the key tension is not unique to transformers. Any mechanism that must produce a normalized probability distribution over a set of candidates faces this pressure. The attention mechanism simply makes the problem particularly visible because the number of candidates (sequence positions) can be thousands deep, making the "spare" probability mass substantial even when the model is making perfectly good predictions about the relevant positions.
The Attention Mechanism: A Probability Distribution Problem
Self-attention is a weighted averaging mechanism. For each position in a sequence, the model asks: "Which previous positions should I draw information from, and how much from each?" The answer comes as a set of weights that sum to 1, forming a probability distribution over the context.
In a standard autoregressive transformer, the attention computation at position produces:
where:
- : the query vector at position , representing what information this position is looking for
- : the key matrix containing key vectors from positions 1 through , where each key represents what information that position offers
- : the value matrix containing value vectors from positions 1 through , holding the actual content to be aggregated
- : the dimension of the key vectors, used to scale the dot products and prevent them from growing too large
The softmax function is the critical piece. It takes the raw attention scores (dot products between queries and keys) and converts them into a probability distribution:
where each individual weight is:
where:
- : the attention weight from query position to key position , indicating how much position attends to position
- : the dot product between query and key , measuring their compatibility
- : the exponential function, which ensures all values are positive
- The denominator sums over all positions, normalizing the weights to form a valid probability distribution
This is where the tension arises. The exponential function has a mathematical property that creates the sink phenomenon: for all finite . No matter how irrelevant a position is, the model cannot assign it exactly zero attention. The softmax constraint forces the model to distribute some probability mass everywhere.
When the current token needs information from only a handful of positions, what happens to the attention weight that must go to the other 995 tokens in a 1000-token sequence? The model needs somewhere to put this "excess" attention, somewhere that won't disrupt the computation. During training, the first tokens become that destination.
How Sink Tokens Emerge During Training
The emergence of sink tokens is not designed but learned. During pretraining on billions of tokens, the model discovers that attending to early positions is "safe" for several reasons:
- They're always present: Position 0 exists in every training example, so the model can reliably use it
- Their representations stabilize early: By the time later positions attend to them, their hidden states have passed through many layers
- Attending to them causes minimal harm: Mixing in a small amount of irrelevant but stable information is better than spreading that attention across many varying positions
Let denote the key vector at position 0 after training. This vector develops a distinctive property: for most query vectors , the dot product is relatively high compared to random positions. The model has learned to make a universal "attractor" in key space.
To see why this specialization emerges, consider the gradient dynamics during training. Whenever the model generates a token that benefits from "dumping" excess attention, the gradient updates reinforce key-query alignment at position 0. Because this pattern occurs consistently across diverse training examples (position 0 is always present; the content at other positions varies wildly), the key vector experiences a stronger and more consistent gradient pressure than any other position. Over millions of training steps, this consistently pushes toward a direction in key space that maximizes dot products with a wide variety of queries.
Notice that this is a self-reinforcing dynamic. Once starts absorbing excess attention during training, the gradient updates make it even better at absorbing excess attention, which causes it to absorb even more, and so on. The model converges to a configuration where position 0 is a reliable sink precisely because the training process rewards that configuration. It is an emergent coordination mechanism: there is no explicit objective that says "make position 0 a sink," but the model discovers that arrangement independently as the globally stable solution to the softmax pressure problem.
The same mechanism applies, to a lesser extent, to positions 1 and 2. Each subsequent early position sees a consistent gradient pressure from all later positions, diminishing as we move further from the start. This explains the characteristic shape of the attention spike: highest at position 0, decaying quickly across the next few positions, then dropping to near-uniform levels for middle positions.

The heatmap shows that sink behavior is not an artifact of any single layer. Across all transformer layers, the first few positions consistently attract high attention weights. This cross-layer consistency suggests that sink tokens are part of how the model processes information, rather than a quirk of one particular layer's learned weights.
We can write the attention weight on position 0 using simplified notation:
where:
- : the attention weight from query position to position 0 (the first token)
- : the scaled attention score between query and key
- : the numerator, which grows exponentially with how well query matches key 0
- : the normalization constant summing over all previous positions
For to be consistently high across different queries and contexts, must be consistently large. The key vector evolves during training to project strongly onto the directions that queries typically occupy, regardless of the actual content at position 0.
The Mathematics of Failure: What Happens When Sinks Disappear
Understanding why removal of sink tokens breaks the model requires following the math carefully. When you remove position 0 from the key-value cache, the attention computation changes in a specific and damaging way.
The new attention weights become:
where:
- : the modified attention weight after removing position 0
- The denominator now sums only over positions , excluding the removed sink
The critical change is in the denominator. By removing from the sum, we've made the denominator smaller. Let's trace through what this means:
- Numerators stay the same: For any remaining position , its numerator hasn't changed
- Denominator shrinks: The sum that divides the numerator is now missing the term
- All weights increase: Since numerator/smaller-denominator > numerator/larger-denominator, every remaining position receives more attention
The probability mass that previously went to position 0 must redistribute across the remaining positions. If the sink absorbed 30% of attention (), that 30% now spreads across positions that weren't trained to receive it.
This redistribution causes two compounding problems:
-
Distribution mismatch: The new attention distribution doesn't match what the model saw during training. Each attention head has learned to produce useful representations given a specific expected distribution of weights. When that distribution changes, the representations become unreliable, leading to out-of-distribution hidden states that propagate through subsequent layers.
-
Cascade of new sinks: The model may try to use other early positions as sinks, but they lack the specialized key representation of position 0. As generations continue, the model keeps shifting which positions absorb excess attention, never stabilizing into a pattern it can use effectively.
The StreamingLLM Solution: Preserving the Distribution
StreamingLLM's insight is that you don't need the full history to maintain the attention distribution the model expects. You need exactly two things: the sink tokens to absorb excess attention, and recent context for actual language modeling. Everything in between can be discarded.
Formally, we maintain positions as sinks plus a sliding window for recent context. The attention weight for any position in this combined set becomes:
where:
- : the attention weight from query position to position
- : the attention score between query and key
- : the set of preserved sink token positions
- : the sliding window of the most recent positions
- The denominator sums over both sets. This makes the attention weights form a valid probability distribution
The key insight is that the denominator structure matches what the model saw during training. The sink tokens contribute their terms to the normalization, absorbing their usual share of attention weight. The window tokens receive the context-relevant attention for actual language modeling. Because the sink tokens are present, the model operates in its training distribution rather than an out-of-distribution state.
This is why StreamingLLM works: it doesn't try to eliminate the sink phenomenon or work around it. Instead, it embraces the sink as a necessary component of how the model has learned to distribute attention, and simply ensures that component is always present.