Linear Attention

What You’ll Learn

You’ll learn why standard attention’s O(n2)O(n^2) cost makes long sequences expensive, and how the associativity trick can avoid the n×nn \times n 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:

Attention(Q,K,V)=softmax(QK⊤)V\text{Attention}(Q, K, V) = \text{softmax}(QK^\top)V

The problem is the n×nn \times n matrix QK⊤QK^\top, where nn is the sequence length. We showed that this costs O(n2d)O(n^2 d) in time and O(n2)O(n^2) 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 n×nn \times n bottleneck.

From Quadratic to Linear

Standard Attention
Q × Kᵀ n×n O(n²d)
softmax n×n O(n²)
× V n×d O(n²d)
Time O(n²d)
Memory O(n²)

The 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 n×nn \times n matrix (expensive). In linear attention, the first operation produces a d×dd \times d matrix (cheap and fixed). The trick is associativity of matrix multiplication: (QK⊤)V=Q(K⊤V)(QK^\top)V = Q(K^\top V). Same result, different cost.

A Concrete Example

Consider 3 tokens with d=2d = 2:

Q=[0.20.80.50.30.90.1],K=[0.10.70.60.40.30.9],V=[1.00.50.20.80.70.3]Q = \begin{bmatrix} 0.2 & 0.8 \\ 0.5 & 0.3 \\ 0.9 & 0.1 \end{bmatrix}, \quad K = \begin{bmatrix} 0.1 & 0.7 \\ 0.6 & 0.4 \\ 0.3 & 0.9 \end{bmatrix}, \quad V = \begin{bmatrix} 1.0 & 0.5 \\ 0.2 & 0.8 \\ 0.7 & 0.3 \end{bmatrix}

Standard order: QK⊤QK^\top produces a 3×3 matrix (9 entries), then multiply by VV.

Reversed order: K⊤VK^\top V produces a 2×2 matrix (4 entries), then multiply by QQ.

With 3 tokens and d=2d = 2, the savings are small. But with 10,000 tokens and d=128d = 128, 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 (QK⊤)V(QK^\top)V, we’d be done. Reorder to Q(K⊤V)Q(K^\top V) and enjoy linear scaling. But standard attention has softmax in the middle:

softmax(QK⊤)⋅V\text{softmax}(QK^\top) \cdot V

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 ii without first computing all nn dot products qi⋅kjq_i \cdot k_j. 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 ϕ\phi applied to Q and K independently:

softmax(qi⊤kj)≈ϕ(qi)⊤ϕ(kj)\text{softmax}(q_i^\top k_j) \approx \phi(q_i)^\top \phi(k_j)

If this holds, then the attention output for query ii becomes:

outputi=∑jϕ(qi)⊤ϕ(kj)⋅vj∑jϕ(qi)⊤ϕ(kj)\text{output}_i = \frac{\sum_j \phi(q_i)^\top \phi(k_j) \cdot v_j}{\sum_j \phi(q_i)^\top \phi(k_j)}

The numerator factors into ϕ(qi)⊤(∑jϕ(kj)vj⊤)\phi(q_i)^\top \left(\sum_j \phi(k_j) v_j^\top \right). That inner sum ∑jϕ(kj)vj⊤\sum_j \phi(k_j) v_j^\top is a d′×dd' \times d matrix that doesn’t depend on ii. Compute it once, reuse it for every query. No n×nn \times n matrix needed.

Quick Check
Why can't we just reorder standard attention from (QK⊤)V to Q(K⊤V) and call it linear?
Your turn
Choose an answer to check your understanding.

How Much Faster?

Click through different sequence lengths. Watch how the gap between standard (O(n2d)O(n^2 d)) and linear (O(nd2)O(nd^2)) attention grows as sequences get longer.

Scaling: Standard vs. Linear Attention

d = 128 (typical head dimension)
Standard
O(n²d)
134.2M
Linear
O(nd²)
16.8M
Linear is 8.0x cheaper

At 1024 tokens, linear attention is modestly faster. The real gains come at longer sequences.

What to look for: At n=dn = d (128 tokens), both approaches cost the same. Below that, standard attention is actually cheaper. The crossover happens at n=dn = d, 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 n2dn^2 d for nd2nd^2. When n<dn < d, 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 ϕ\phi 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)
TimeO(n2d)O(n^2 d)O(nd2)O(nd^2)
MemoryO(n2)O(n^2)O(nd+d2)O(nd + d^2)
Attention patternSharp, selectiveSmoother, more diffuse
Best forPrecision tasks, short/medium sequencesLong sequences, streaming
Causal supportMasking (still O(n2)O(n^2))Cumulative sum (true O(n)O(n))
Fill in the Blanks
Linear attention replaces with a kernel function, enabling the computation to be reordered from O(n²d) to O( ). The trade-off is that attention patterns become less .
Fill in every blank, then check your answer.

Real Architectures

Several production-grade architectures use linear or sub-quadratic attention:

Linear Transformers (Katharopoulos et al., 2020): The original linear attention paper. Uses ϕ(x)=elu(x)+1\phi(x) = \text{elu}(x) + 1 as the kernel, achieving O(nd2)O(nd^2) 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 d′d' 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 O(n)O(n) time and O(1)O(1) 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

StepWhat happensIntuition
Standard attention(QK⊤)V(QK^\top)V: build n×nn \times n matrix firstEvery query scores against every key
The reorder trickQ(K⊤V)Q(K^\top V): build d×dd \times d summary firstSummarize all key-value pairs, then each query reads the summary
Softmax blocks thisSoftmax couples entries within each rowCan’t summarize without seeing all scores first
Kernel replaces softmaxϕ(Q)ϕ(K)⊤V\phi(Q)\phi(K)^\top V: independent per entryApply ϕ\phi to each vector separately, then reorder freely
Linear attentionϕ(Q)⋅(ϕ(K)⊤V)\phi(Q) \cdot (\phi(K)^\top V): O(nd2)O(nd^2)Summary is d×dd \times d (small, fixed), linear in nn
Learning Checklist
  • 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 (nn vs. dd)
  • 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.