Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

Performer

The Performer (Choromanski and colleagues, ICLR 2021) approximates softmax attention in linear time with FAVOR+: random features φ such that φ(q)·φ(k) estimates exp(q·k) without bias. Attention is then computed as φ(Q)(φ(K)ᵀV), never forming the n × n matrix. Its features are positive, avoiding the unstable negative weights of trigonometric random features, and orthogonal, which lowers variance. This page covers kernelised attention, positive and orthogonal random features, causal prefix sums and limits. In the exercises you check that positive features estimate exp(x·y) without bias and implement linear attention. You then measure approximation error against attention sharpness. For mild attention, orthogonal positive features with 1024 features reach 9% error. As attention sharpens, trigonometric features explode (error 29 with 64 features) while positive ones stay bounded (0.81), and every variant needs many more features.

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.

Transformer lab
Full attention stores 16,777,216 scores per head; a linear-time method with 64 features stores 262,144, 64 times fewer.
Why engineers care

Linear-time attention lets models read very long sequences (proteins, long recordings) on modest hardware, if the approximation is accurate enough for the task.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The Performer approximates attention with…
  2. 2. Its cost grows with length…
  3. 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).
📝 Quick check (Level 1)
0/3 answered
  1. 1. φ(x) in FAVOR+:
  2. 2. Work ratio n/(2m) at n = 65,536, m = 256:
  3. 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))
🐍 Exercise 1: positive random features
Implement FAVOR+ features, linear attention and its cost; check unbiasedness.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A common number of features per head:
  2. 2. The 1/√d scale is folded in by dividing q and k by…
  3. 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).

📝 Quick check (Level 3)
0/3 answered
  1. 1. Trigonometric estimates can be…
  2. 2. Random-feature variance grows with…
  3. 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.
🐍 Exercise 2 (research): when are random features accurate?
Approximate softmax attention with trigonometric, positive and orthogonal positive features at several sharpness levels and feature counts.

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

  1. K. Choromanski et al. (2021). “Rethinking attention with Performers.” Proc. ICLR.
  2. A. Rahimi, B. Recht (2007). “Random features for large-scale kernel machines.” Proc. NeurIPS.
  3. A. Katharopoulos, A. Vyas, N. Pappas, F. Fleuret (2020). “Transformers are RNNs: fast autoregressive Transformers with linear attention.” Proc. ICML.