Linear Attention & Kernel Methods
Breaking the quadratic barrier: kernel decomposition of softmax, feature maps (elu, random Fourier), causal linear attention, connection to RNNs, RetNet, RWKV, and the expressiveness-efficiency tradeoff.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- From Softmax to Kernels
- The Linear Attention Trick
- Feature Map Choices
- Causal Linear Attention as RNN
- RetNet: Retention Mechanism
- RWKV: Linear Attention Language Model
- Expressiveness Analysis
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive linear attention from the kernel view of softmax.
- Prove the complexity of linear attention.
- Show the equivalence between causal linear attention and an RNN.
- Explain why linear attention struggles with precise retrieval.
- Compare RetNet and RWKV architectures.
Notation
- — feature map
- — accumulated state matrix
- — normalizer state
Core Intuition
Standard attention computes pairwise similarities between all pairs, then normalizes. But what if the similarity function can be decomposed into a product of features? Then we can change the order of computation: instead of "for each query, compare to all keys," we compute "aggregate all key-value pairs into a summary, then query the summary." This changes to .
Linear Attention
From Softmax to Kernels
Standard softmax attention for a single query:
View the exponential as a kernel: .
Kernel trick: If for some feature map , then:
The Linear Attention Trick
Key insight: The sums and don't depend on the query — compute them once.
Define:
Then for any query:
Complexity:
- Build : .
- Query each of positions: .
- Total: . If : — linear in .
Feature Map Choices
The softmax kernel has an infinite-dimensional exact feature map. Practical choices approximate it:
1. ELU + 1 (Katharopoulos et al., 2020):
Simple, non-negative, but doesn't approximate softmax well.
2. Random Fourier Features (Performers):
Unbiased estimator of the Gaussian kernel: .
3. Positive random features (FAVOR+): Ensure non-negativity:
Unbiased estimator of softmax kernel.
Causal Linear Attention as RNN
For autoregressive (causal) models, the state updates incrementally:
This is an RNN with state :
- Fixed-size state: regardless of sequence length.
- Constant-time per-step update: .
- No KV-cache needed.
Implication: Linear attention converts a transformer into an RNN at inference time (linear generation), while allowing parallel training (using the cumulative sum formulation).
RetNet: Retention Mechanism
RetNet adds exponential decay to linear attention:
where is a decay factor.
Effect: Old information is gradually forgotten (exponential decay). This acts as a learned forgetting mechanism and helps with:
- Reducing the influence of distant irrelevant tokens.
- Providing implicit positional encoding (closer tokens have higher weight).
Three computation modes:
- Parallel (training): using matrix form.
- Recurrent (inference): per step using state update.
- Chunk-wise (hybrid): Process chunks in parallel, connect chunks recurrently.
RWKV: Linear Attention Language Model
RWKV combines ideas from RNNs and transformers:
Time-mixing (attention analog):
where is a learned decay and is a bonus for the current token.
Properties:
- Linear complexity: per layer.
- Can be computed as an RNN (constant memory per step).
- Competitive with transformers up to ~14B parameters.
Expressiveness Analysis
Theorem (Limitation). Linear attention cannot implement "sharp" attention (near-one-hot) patterns.
Argument: In standard attention, softmax can produce distributions arbitrarily close to one-hot (by scaling up logits). In linear attention, the output is:
This is always a smooth average of values weighted by — cannot perfectly select a single key.
Consequence: Tasks requiring precise retrieval from memory (e.g., copying a specific token, looking up a fact at a specific position) are harder for linear attention.
Common Pitfalls
Pitfall 1. Assuming linear attention always beats quadratic for long sequences. At small , the overhead of feature computation and the larger constant in (vs with ) can make linear attention slower.
Pitfall 2. Using a feature map that produces negative values. Negative attention weights are semantically meaningless and cause instability in the normalization.
Pitfall 3. Expecting linear attention to handle in-context learning well. Tasks requiring precise token recall (common in ICL) are fundamentally limited by the fixed-size state.
Summary
- Linear attention replaces with using kernel decomposition.
- Causal linear attention = RNN with state .
- No KV-cache needed; constant memory for generation.
- Feature maps: ELU+1, random Fourier features, FAVOR+.
- RetNet adds exponential decay; RWKV adds position-dependent decay.
- Tradeoff: linear complexity at the cost of reduced expressiveness (no sharp retrieval).
Exercises
Exercise 1. Derive the parallel form of causal linear attention using cumulative sums.
Exercise 2. Prove that with (identity), linear attention reduces to a linear layer.
Exercise 3. Compute the state size for a model with and compare to the KV-cache size at .
Exercise 4. Show that RetNet with reduces to standard linear attention.
Exercise 5. Design an experiment to demonstrate the retrieval limitation of linear attention vs softmax attention.