Linear Attention
You’ll learn why standard attention’s cost makes long sequences expensive, and how the associativity trick can avoid the bottleneck. You’ll also see why softmax blocks that shortcut, how kernel functions repair it, and what linear attention trades for speed.
The Wall: Quadratic Attention
Standard self-attention computes:
The problem is the matrix , where is the sequence length. We showed that this costs in time and in memory.
For short sequences, this is fine. For long ones, it’s a wall. A 100K-token context needs a 100K × 100K attention matrix: 10 billion entries. Can we avoid ever building this matrix?
The Insight: Associativity
Step through the visualization below. It shows how the same computation can be reordered to avoid the bottleneck.
From Quadratic to Linear
Standard AttentionThe n×n attention matrix must be fully materialized.
Standard attention computes Q×Kᵀ first, producing an n×n matrix. Every query must score against every key before any output is produced.
What to look for: In standard attention, the first operation produces an matrix (expensive). In linear attention, the first operation produces a matrix (cheap and fixed). The trick is associativity of matrix multiplication: . Same result, different cost.
A Concrete Example
Consider 3 tokens with :
Standard order: produces a 3×3 matrix (9 entries), then multiply by .
Reversed order: produces a 2×2 matrix (4 entries), then multiply by .
With 3 tokens and , the savings are small. But with 10,000 tokens and , standard attention builds a 10,000 × 10,000 matrix (100 million entries), while the reversed order builds a 128 × 128 matrix (16,384 entries). That’s 6,100x fewer entries.
The Catch: Softmax Gets in the Way
If attention were just , we’d be done. Reorder to and enjoy linear scaling. But standard attention has softmax in the middle:
Softmax normalizes each row by the sum of all entries in that row. That means every entry in a row depends on every other entry in that row. You can’t compute the output for query without first computing all dot products . Softmax couples everything together and blocks the reordering.
The Fix: Kernel Functions
What if we replaced softmax with something that doesn’t couple the entries? A kernel function applied to Q and K independently:
If this holds, then the attention output for query becomes:
The numerator factors into . That inner sum is a matrix that doesn’t depend on . Compute it once, reuse it for every query. No matrix needed.
How Much Faster?
Click through different sequence lengths. Watch how the gap between standard () and linear () attention grows as sequences get longer.
Scaling: Standard vs. Linear Attention
At 1024 tokens, linear attention is modestly faster. The real gains come at longer sequences.
What to look for: At (128 tokens), both approaches cost the same. Below that, standard attention is actually cheaper. The crossover happens at , and beyond that, linear attention pulls away fast. At 16K tokens, linear is 128x cheaper.
This reveals an important detail: linear attention isn’t universally faster. It swaps for . When , the standard approach wins. Linear attention only pays off for sequences longer than the model dimension, which is exactly the regime where standard attention becomes painful.
The Trade-off: What Gets Lost
Linear attention isn’t a free upgrade. The kernel approximates softmax, and that approximation has consequences.
What softmax does well: It produces sharp, peaked attention patterns. When the model needs to focus hard on one or two tokens (like resolving a pronoun), softmax concentrates nearly all weight on the relevant entries and suppresses everything else.
What kernels struggle with: Most kernel functions produce smoother, more diffuse attention patterns. The model can still identify relevant tokens, but it can’t suppress irrelevant ones as aggressively. This hurts tasks that require precise, selective attention.
| Standard (softmax) | Linear (kernel) | |
|---|---|---|
| Time | ||
| Memory | ||
| Attention pattern | Sharp, selective | Smoother, more diffuse |
| Best for | Precision tasks, short/medium sequences | Long sequences, streaming |
| Causal support | Masking (still ) | Cumulative sum (true ) |
Real Architectures
Several production-grade architectures use linear or sub-quadratic attention:
Linear Transformers (Katharopoulos et al., 2020): The original linear attention paper. Uses as the kernel, achieving complexity. Simple to implement but loses some quality compared to softmax.
Performer (Choromanski et al., 2020): Uses random feature maps (FAVOR+) to approximate the softmax kernel more accurately. Better quality than simple kernels but adds a feature dimension that increases constant factors.
Mamba (Gu & Dao, 2023): Takes a different approach entirely. Instead of approximating attention with kernels, it uses a state-space model (SSM) with selective gating. Each token updates a fixed-size hidden state (like an RNN), giving time and memory per step. Mamba matches or beats transformer quality on many benchmarks while being much faster for long sequences.
Mamba-2 (Dao & Gu, 2024): Shows that the SSM approach and linear attention are actually connected. The structured state-space duality (SSD) framework unifies them, with the SSM update rule being equivalent to a specific form of linear attention with causal masking.
Quick Summary
| Step | What happens | Intuition |
|---|---|---|
| Standard attention | : build matrix first | Every query scores against every key |
| The reorder trick | : build summary first | Summarize all key-value pairs, then each query reads the summary |
| Softmax blocks this | Softmax couples entries within each row | Can’t summarize without seeing all scores first |
| Kernel replaces softmax | : independent per entry | Apply to each vector separately, then reorder freely |
| Linear attention | : | Summary is (small, fixed), linear in |
- I can explain why matrix associativity lets us reorder the attention computation
- I understand why softmax prevents this reordering
- I can describe what a kernel function does and why it enables linear scaling
- I know when linear attention is faster and when standard attention wins ( vs. )
- I can explain the quality trade-off (sharp vs. diffuse attention patterns)
- I’m aware of the connection between causal linear attention and RNNs
Now that you understand how attention can be made efficient, see how the KV Cache tackles the same problem from a different angle: caching rather than approximating.