Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Reorder the arithmetic
Attention computes a score for every pair of words, then averages. The Performer rewrites each word as a list of random features chosen so that multiplying two lists approximates the attention score. Then the order of multiplication can change: first summarise all keys and values into a small table, then let each query read from it. The cost becomes linear in length. The approximation is good when attention is spread out, and needs more features when attention is sharp, as the research exercise shows. The demo compares costs.
Linear-time attention lets models read very long sequences (proteins, long recordings) on modest hardware, if the approximation is accurate enough for the task.
- 1. The Performer approximates attention with…
- 2. Its cost grows with length…
- 3. FAVOR+ features are always…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Kernel view:
- softmax attention is Aᵢⱼ ∝ exp(qᵢ·kⱼ), with the 1/√d scale folded into q and k;
- if φ(q)·φ(k) ≈ exp(q·k), then attention ≈ φ(Q)(φ(K)ᵀV) / φ(Q)(φ(K)ᵀ1);
- computing φ(K)ᵀV first (m × d) costs O(nmd).
- Positive features (FAVOR+):
- φ(x) = exp(Wx − |x|²/2)/√m with Gaussian W;
- E[φ(x)·φ(y)] = exp(x·y) exactly, and every feature is positive, so normalisers never go negative.
- Trigonometric features (Rahimi and Recht): φ(x) = exp(|x|²/2)·[sin Wx, cos Wx]/√m is also unbiased, but its estimates can be negative and have huge variance for small kernel values.
- Orthogonal features: draw W in orthogonal blocks with Gaussian-distributed norms, which lowers variance.
- Cost:
- about 4nmd against 2n²d, a ratio of n/(2m);
- at n = 65,536 and m = 256, 128 times less work;
- at n = 512 and m = 256, no saving.
- Causal attention: keep running sums of φ(kⱼ)vⱼᵀ and φ(kⱼ) as j increases (prefix sums).
- 1. φ(x) in FAVOR+:
- 2. Work ratio n/(2m) at n = 65,536, m = 256:
- 3. Causal Performers use…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Performers in code
Exercise 1.
- With 200,000 features, positive features estimated exp(x·y) within 1%, and all features were positive.
- Linear attention equalled normalised φ(Q)φ(K)ᵀV exactly.
Practice.
- Use m = 256 features per head as a common default, and redraw W periodically during training to avoid overfitting to one draw.
- Fold the 1/√d scaling into Q and K by dividing each by d^(1/4).
- Linear attention variants: Katharopoulos et al. (2020) used φ(x) = elu(x) + 1, cheap but not a softmax approximation.
- On GPUs, exact attention with FlashAttention (E12-20) is often faster for lengths up to tens of thousands. Random-feature methods also lose accuracy on tasks needing sharp attention.
def favor_features(x, W): # x: (..., n, d), W: (m, d)
return torch.exp(x @ W.T - (x ** 2).sum(-1, keepdim=True) / 2) / W.shape[0] ** 0.5
q, k = q / d ** 0.25, k / d ** 0.25
fq, fk = favor_features(q, W), favor_features(k, W)
out = (fq @ (fk.transpose(-2, -1) @ v)) / (fq @ fk.sum(-2, keepdim=True).transpose(-2, -1))- 1. A common number of features per head:
- 2. The 1/√d scale is folded in by dividing q and k by…
- 3. elu(x) + 1 features come from…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why positive features.
- Trigonometric features estimate exp(q·k) as a product of an exp(|x|²/2)-sized factor with oscillating terms.
- For small true values, which most attention weights are, the estimate can be negative or wildly large, and normalisers can approach zero.
- Positive features have relative variance that shrinks for small kernel values and never produce negative weights, the key insight of FAVOR+.
Why accuracy falls for sharp attention.
- The variance of a random-feature estimate grows roughly like exp(|q|² + |k|²).
- Sharp attention needs large dot products, so large norms and exponentially growing variance.
- Mild attention (small norms) is approximated well with a few hundred features; sharp attention needs many more.
- Exercise 2 shows the error rising steeply as attention sharpens.
Linear attention as an RNN. With causal prefix sums, a linear-attention model carries a state of size m × d updated per token, an RNN view that inspired later linear recurrent models (E17).
- 1. Trigonometric estimates can be…
- 2. Random-feature variance grows with…
- 3. Causal linear attention can be run as…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.When are random features accurate?
History.
- A. Rahimi and B. Recht introduced random Fourier features (NeurIPS 2007).
- A. Katharopoulos et al., “Transformers are RNNs” (ICML 2020), proposed linear attention.
- K. Choromanski et al., “Rethinking attention with Performers” (ICLR 2021), introduced FAVOR+.
Exercise 2.
- Inputs: queries and keys of dimension 16, 256 tokens, with component standard deviation s·d^(−1/4), so q·k has standard deviation s². Larger s means sharper attention.
- Approximations: the softmax attention matrix approximated with trigonometric features, positive features, and orthogonal positive features, with m = 64 or 1024.
- Measurement: median relative Frobenius error of the normalised attention matrix over 3 draws.
| sharpness s | trig, 64 | trig, 1024 | positive, 64 | positive, 1024 | orthogonal positive, 64 | orthogonal positive, 1024 |
|---|---|---|---|---|---|---|
| 0.5 | 0.271 | 0.071 | 0.341 | 0.091 | 0.234 | 0.088 |
| 0.8 | 29.4 | 0.485 | 0.811 | 0.616 | 0.786 | 0.479 |
| 1.0 | 43.7 | 4.53 | 1.32 | 1.13 | 1.18 | 1.07 |
- Mild attention is approximated well by every method. Orthogonal positive features are best with few features, and trigonometric features slightly better with many.
- As attention sharpens, trigonometric features explode (error 29 with 64 features), while positive features stay bounded. This is the stability FAVOR+ was designed for.
- But all methods degrade steeply with sharpness. At s = 1 even 1024 positive features give errors above 1. In a separate check, 20,000 features reduced positive-feature error to 0.70 there and to 0.02 at s = 0.5.
- Random-feature attention suits diffuse attention. Sharp heads (copying, exact matching) need exact attention.
Performers in AI, robotics and aerospace
- Biology: Performers modelled long protein sequences in the original paper.
- Streaming: causal linear attention runs as a recurrent state update, attractive for on-board streaming inference.
- Check sharpness: before swapping in linear attention, inspect whether the needed attention is diffuse enough to approximate.
References
- K. Choromanski et al. (2021). “Rethinking attention with Performers.” Proc. ICLR.
- A. Rahimi, B. Recht (2007). “Random features for large-scale kernel machines.” Proc. NeurIPS.
- A. Katharopoulos, A. Vyas, N. Pappas, F. Fleuret (2020). “Transformers are RNNs: fast autoregressive Transformers with linear attention.” Proc. ICML.