Attention Mechanisms  · September 2020

Performer — Random Feature Softmax Approximation

advanced

efficiency

Approximate softmax attention with random features that decompose the kernel — letting attention scale linearly in sequence length while remaining an unbiased estimator of the original softmax operator.

§ 1 · Premise

Softmax attention is a kernel — approximate the kernel, decouple the matmul

Standard scaled-dot-product attention computes, for each query qt\mathbf{q}_t,

outt  =  sexp(qtks/dh)vssexp(qtks/dh).\mathrm{out}_t \;=\; \frac{\sum_{s} \exp(\mathbf{q}_t^\top \mathbf{k}_s / \sqrt{d_h}) \, \mathbf{v}_s}{\sum_{s} \exp(\mathbf{q}_t^\top \mathbf{k}_s / \sqrt{d_h})}.

The exponential exp(qk/dh)\exp(\mathbf{q}^\top \mathbf{k} / \sqrt{d_h}) is a positive-definite kernel K(q,k)K(\mathbf{q}, \mathbf{k}) on Rdh\mathbb{R}^{d_h}. By Mercer’s theorem (or its generalization to non-symmetric similarities), any positive-definite kernel admits a feature-map factorization K(x,y)=ϕ(x),ϕ(y)K(\mathbf{x}, \mathbf{y}) = \langle \phi(\mathbf{x}), \phi(\mathbf{y}) \rangle for some ϕ:RdhH\phi: \mathbb{R}^{d_h} \to \mathcal{H}.

If ϕ\phi is finite-dimensional with ϕ:RdhRr\phi: \mathbb{R}^{d_h} \to \mathbb{R}^{r}, then the attention numerator decomposes as

sK(qt,ks)vs  =  ϕ(qt)sϕ(ks)vs,\sum_s K(\mathbf{q}_t, \mathbf{k}_s) \mathbf{v}_s \;=\; \phi(\mathbf{q}_t)^\top \sum_s \phi(\mathbf{k}_s) \mathbf{v}_s^\top,

and the inner sum sϕ(ks)vsRr×dh\sum_s \phi(\mathbf{k}_s) \mathbf{v}_s^\top \in \mathbb{R}^{r \times d_h} no longer depends on qt\mathbf{q}_t. It can be computed once for all queries in Θ(Nrdh)\Theta(N r d_h) and reused — total FLOPs are Θ(Nrdh)\Theta(N r d_h) instead of Θ(N2dh)\Theta(N^2 d_h).

The catch: the exponential kernel does not admit an exact finite-dimensional feature map. Earlier work (Linear Attention, Katharopoulos et al. 2020, arXiv 2006.16236) worked around this by replacing softmax with a different kernel — typically ϕ(x)=elu(x)+1\phi(\mathbf{x}) = \mathrm{elu}(\mathbf{x}) + 1 — which is decomposable by construction but no longer approximates softmax. The resulting model has linear cost but inherits whatever inductive bias the substitute kernel implies, generally losing 1–3 perplexity points on language modeling vs. softmax (Katharopoulos et al. Table 2).

Performer’s bet: keep the softmax kernel and approximate ϕ\phi via random Fourier features. The random-feature literature (Rahimi & Recht 2007, NIPS 2007) gives an unbiased finite-dimensional estimator for any shift-invariant kernel. Choromanski et al. extend this to the (non-shift-invariant) exponential softmax kernel via positive random features — the ”+” in FAVOR+.

§ 2 · Derivation

FAVOR+: a positive unbiased random-feature softmax estimator

Step 1: factor the exponential kernel. Write the softmax kernel as

KSM(x,y)  =  exp(xy)  =  exp(12x2)exp(12y2)exp ⁣(12xy2).K_{\mathrm{SM}}(\mathbf{x}, \mathbf{y}) \;=\; \exp(\mathbf{x}^\top \mathbf{y}) \;=\; \exp(\tfrac{1}{2}\lVert \mathbf{x} \rVert^2) \exp(\tfrac{1}{2}\lVert \mathbf{y} \rVert^2) \exp\!\bigl(-\tfrac{1}{2}\lVert \mathbf{x} - \mathbf{y} \rVert^2\bigr).

(The standard identity xy=12(x2+y2xy2)\mathbf{x}^\top \mathbf{y} = \tfrac{1}{2}(\lVert\mathbf{x}\rVert^2 + \lVert\mathbf{y}\rVert^2 - \lVert\mathbf{x}-\mathbf{y}\rVert^2).) The first two factors are query-only and key-only normalizers. The third factor is the Gaussian kernel — which is shift-invariant, so Bochner’s theorem gives an unbiased random-feature estimator from its Fourier transform.

Step 2: trigonometric random features (FAVOR, the original). Rahimi & Recht’s classical construction:

ϕtrig(x)  =  1r[cos(ω1x),sin(ω1x),,cos(ωr/2x),sin(ωr/2x)],\phi^{\mathrm{trig}}(\mathbf{x}) \;=\; \frac{1}{\sqrt{r}}\bigl[\cos(\boldsymbol{\omega}_1^\top \mathbf{x}), \sin(\boldsymbol{\omega}_1^\top \mathbf{x}), \ldots, \cos(\boldsymbol{\omega}_{r/2}^\top \mathbf{x}), \sin(\boldsymbol{\omega}_{r/2}^\top \mathbf{x})\bigr],

with ωjN(0,Idh)\boldsymbol{\omega}_j \sim \mathcal{N}(0, I_{d_h}). Then Eϕtrig(x),ϕtrig(y)=exp(12xy2)\mathbb{E} \langle \phi^{\mathrm{trig}}(\mathbf{x}), \phi^{\mathrm{trig}}(\mathbf{y}) \rangle = \exp(-\tfrac{1}{2}\lVert \mathbf{x}-\mathbf{y}\rVert^2) exactly.

Why this fails for softmax attention. ϕtrig\phi^{\mathrm{trig}} takes both positive and negative values. When you plug it into the attention sum, the numerator ϕtrig(q)sϕtrig(ks)vs\phi^{\mathrm{trig}}(\mathbf{q})^\top \sum_s \phi^{\mathrm{trig}}(\mathbf{k}_s) \mathbf{v}_s^\top is a difference of partly-cancelling terms, and the same is true for the denominator ϕtrig(q)sϕtrig(ks)\phi^{\mathrm{trig}}(\mathbf{q})^\top \sum_s \phi^{\mathrm{trig}}(\mathbf{k}_s). The denominator can become tiny or negative, giving exploding or NaN attention weights. Choromanski et al. show empirically (their Figure 3) that trigonometric features cause training to diverge even at r=1,024r = 1{,}024.

Step 3: positive random features (FAVOR+, the contribution). Replace cosines/sines with exponentials:

ϕ+(x)  =  exp(12x2)r[exp(ω1x), , exp(ωrx)]    R>0r.\phi^{+}(\mathbf{x}) \;=\; \frac{\exp(-\tfrac{1}{2}\lVert \mathbf{x}\rVert^2)}{\sqrt{r}} \bigl[\exp(\boldsymbol{\omega}_1^\top \mathbf{x}),\ \ldots,\ \exp(\boldsymbol{\omega}_r^\top \mathbf{x})\bigr] \;\in\; \mathbb{R}^{r}_{>0}.

Every component is strictly positive. Choromanski et al. prove (Lemma 1) that

EωN(0,I)ϕ+(x),ϕ+(y)  =  exp(xy)  =  KSM(x,y),\mathbb{E}_{\boldsymbol{\omega} \sim \mathcal{N}(0, I)} \langle \phi^{+}(\mathbf{x}), \phi^{+}(\mathbf{y}) \rangle \;=\; \exp(\mathbf{x}^\top \mathbf{y}) \;=\; K_{\mathrm{SM}}(\mathbf{x}, \mathbf{y}),

so ϕ+\phi^{+} is an unbiased estimator of the softmax kernel. Variance is bounded by (Choromanski et al. Theorem 2)

Var[ϕ+(x),ϕ+(y)]  =  1rexp(x+y2)(exp(x+y2)exp(2xy)).\mathrm{Var}\bigl[\langle \phi^{+}(\mathbf{x}), \phi^{+}(\mathbf{y}) \rangle\bigr] \;=\; \frac{1}{r}\exp(\lVert \mathbf{x}+\mathbf{y}\rVert^2)\bigl(\exp(\lVert\mathbf{x}+\mathbf{y}\rVert^2) - \exp(2\mathbf{x}^\top\mathbf{y})\bigr).

Two consequences: (i) variance decays as Θ(1/r)\Theta(1/r), the standard random-feature rate; and (ii) variance is small when x\mathbf{x} and y\mathbf{y} have aligned (positively correlated) directions and large when they are anticorrelated — meaning the estimator is sharpest at the keys that matter most for attention. This is the second design win of FAVOR+ over trig features, which have uniform variance across the kernel.

Step 4: orthogonal projections reduce variance further. Sampling ωj\boldsymbol{\omega}_j i.i.d. is suboptimal — there is a redundancy when two random vectors happen to be nearly collinear. Choromanski et al. (§ 2.3) replace i.i.d. Gaussians with rows of a random orthogonal matrix (scaled to have Gaussian-distributed norms). This gives an unbiased estimator with strictly lower variance (Yu et al. 2016, “Orthogonal Random Features”, arXiv 1610.09072). In practice this halves the rr required for a fixed quality.

Step 5: assemble linear-time attention. With ΦQ=ϕ+(Q)RN×r\Phi_Q = \phi^+(Q) \in \mathbb{R}^{N \times r} and ΦK=ϕ+(K)RN×r\Phi_K = \phi^+(K) \in \mathbb{R}^{N \times r}:

out  =  diag(ΦQ(ΦK1))1  ΦQ(ΦKV).\mathrm{out} \;=\; \mathrm{diag}(\Phi_Q (\Phi_K^\top \mathbf{1}))^{-1} \; \Phi_Q (\Phi_K^\top V).

Read this right-to-left: ΦKVRr×dh\Phi_K^\top V \in \mathbb{R}^{r \times d_h} is a single matmul of size Θ(Nrdh)\Theta(N r d_h) FLOPs; multiplying on the left by ΦQ\Phi_Q is another Θ(Nrdh)\Theta(N r d_h); the normalizer ΦK1Rr\Phi_K^\top \mathbf{1} \in \mathbb{R}^{r} is Θ(Nr)\Theta(N r). Total: Θ(Nrdh)\Theta(N r d_h) — linear in NN.

For causal attention, the matmul becomes a prefix-sum recurrence (same as Linear Attention’s, with ϕ+\phi^+ in place of the identity-with-elu feature map):

St  =  St1+ϕ+(kt)vt,zt  =  zt1+ϕ+(kt),S_t \;=\; S_{t-1} + \phi^+(\mathbf{k}_t) \mathbf{v}_t^\top, \qquad z_t \;=\; z_{t-1} + \phi^+(\mathbf{k}_t), outt  =  ϕ+(qt)Stϕ+(qt)zt.\mathrm{out}_t \;=\; \frac{\phi^+(\mathbf{q}_t)^\top S_t}{\phi^+(\mathbf{q}_t)^\top z_t}.

This recurrence is well-defined for autoregressive decoders — the projection ϕ+\phi^+ acts on a single token at a time and does not mix across the time axis, unlike Linformer’s EKEK.

Parameter count and complexity. The random-feature matrix ωRr×dh\boldsymbol{\omega} \in \mathbb{R}^{r \times d_h} is not learned — it is sampled once and frozen (Choromanski et al. § 2.4 also describe a periodic resampling protocol that improves long-training stability). The Performer adds zero trainable parameters over the standard MHA baseline. FLOPs are Θ(Nrdh)\Theta(N r d_h) per head per layer; the recommended default is r=dhlogdh256r = d_h \log d_h \approx 256 for dh=64d_h = 64.

§ 3 · Reference implementation

Sketch

def performer_attention(Q, K, V, omega):
    # Q, K, V: [B, T, d_h]
    # omega:   [r, d_h], drawn from orthogonal Gaussian, frozen
    norm_Q = (Q * Q).sum(-1, keepdim=True) / 2       # [B, T, 1]
    norm_K = (K * K).sum(-1, keepdim=True) / 2
    phi_Q  = (Q @ omega.T - norm_Q).exp() / r**0.5   # [B, T, r]   positive features
    phi_K  = (K @ omega.T - norm_K).exp() / r**0.5
    # Bidirectional (encoder): reordered matmul
    KV     = phi_K.transpose(-2, -1) @ V             # [B, r, d_h]
    K_sum  = phi_K.sum(dim=-2, keepdim=True).transpose(-2, -1)   # [B, r, 1]
    num    = phi_Q @ KV                              # [B, T, d_h]
    den    = (phi_Q @ K_sum).clamp(min=1e-6)          # [B, T, 1]
    return num / den
    # Causal: replace the two matmuls with prefix-sum recurrences over t

The load-bearing change vs. softmax MHA is the application of ϕ+\phi^+ before any cross-token mixing, which makes QQ and KK enter the computation only through the projected matrices ΦQ,ΦK\Phi_Q, \Phi_K. Causality is supported by running the prefix-sum form over the time axis.

Performer approximates softmax(QK^T) as φ(Q) · φ(K)^T, then exploits matmul associativity to compute φ(K)^T V first (an m × d matrix), avoiding the N × N attention matrix entirely.Naive: forms the full N × N attention matrixQ (N×d)·K^T (d×N)=softmax(QK^T) (N×N)Performer: reorder via φ(Q)·(φ(K)^T·V), no N × N matrix ever materializedQ (N×d)φφ(Q) (N×m)·φ(K)^T V (m×d)=output (N×d)FLOP comparison (N = 256, d = 32, m = 64)Naive O(N² d): 4.19 M FLOPsPerformer O(N m d): 1.57 M FLOPsSpeedup: 2.7× at these settings
Performer's mechanism: replace softmax(qk^T/√d) with ⟨φ(q), φ(k)⟩ for a random feature map φ : ℝ^d → ℝ^m, then exploit associativity to compute φ(K)^T V (an m × d matrix) before multiplying by φ(Q) — never forming the N × N matrix. Cost drops from O(N²d) to O(Nmd), linear in sequence length. The φ map is chosen so the approximation is unbiased for the softmax kernel and works with causal masking (via a cumulative-sum recurrence).

§ 4 · Empirical evidence

Results

Approximation quality vs. rr (Choromanski et al. 2020, Figure 3). On a frozen ImageNet-trained ViT-Base, FAVOR+ at r=256r = 256 achieves MSE 104\sim 10^{-4} on attention output relative to the exact softmax; r=1,024r = 1{,}024 pushes MSE to 105\sim 10^{-5}. Trigonometric features (FAVOR without ”+”) have 10×\sim 10\times higher MSE at every rr and lead to training divergence at modest learning rates.

Protein language modeling (their Table 2). Performer-ReLU matches the Transformer baseline on TrEMBL protein modeling at L=8,192L = 8{,}192 within 0.02 nats/character at r=256r = 256, while running 2× faster. At L=32,768L = 32{,}768, where exact softmax does not fit on a TPU v3, Performer trains to 1.42 nats/char.

WMT’14 translation (Choromanski et al. Table 5). Performer at r=256r = 256 reaches BLEU 27.4 on WMT’14 En→De, vs. 27.8 for the dense Transformer baseline (matched parameter count) — a 0.4 BLEU gap. The wall-clock speedup at training length N=1,024N = 1{,}024 is modest (1.1×) and the extra approximation noise costs more BLEU than it saves training time. Performer’s advantage is in the long-sequence regime.

Long Range Arena (Tay et al. 2020, arXiv 2011.04006, Table 1). Independent LRA reproduction scores Performer at average 51.41 across the five LRA tasks (ListOps 18.0, Text 65.4, Retrieval 53.8, Image 42.8, Pathfinder 77.1) vs. 54.39 for dense softmax — a ~3-point gap. Performer matches or beats Linformer (51.36) and Reformer (50.67), trails BigBird (55.0). Pathfinder-X (sequence length 16K) tests at 77.1, which only Performer and BigBird complete among the linear variants.

Variance under sharp attention (Schlag et al. 2021, “Linear Transformers Are Secretly Fast Weight Programmers”, arXiv 2102.11174, § 4). When the underlying softmax distribution is highly peaked (one dominant key), the FAVOR+ estimator’s variance grows because the normalizer is dominated by a single feature-map entry. Schlag et al. report 20–40% perplexity degradation for Performer at r=256r = 256 on associative-recall tasks where attention is by design sharp. This is the load-bearing failure mode that has kept Performer out of frontier production decoders: the same softmax sharpening that makes attention expressive on retrieval-heavy tasks is exactly what makes the random-feature estimator high-variance.

Wall-clock comparisons in the FlashAttention era. Once FlashAttention (Dao et al. 2022, arXiv 2205.14135) landed, exact softmax attention became roughly compute-bound at moderate NN, so the constant-factor cost of rr random features became uncompetitive at N16,384N \le 16{,}384. Independent benchmarks (the flash-linear-attention project, github.com/fla-org/flash-linear-attention) report Performer crossing over FlashAttention at N32KN \approx 32K on A100 — comparable to vanilla Linear Attention but with a worse constant due to the exponential nonlinearity in ϕ+\phi^+.

Hyperparameter sensitivity. rr is the main knob. Below r=64r = 64, training diverges on most benchmarks (Choromanski et al. Figure 2). Above r=1,024r = 1{,}024, compute exceeds dense softmax and the technique loses its point. The recommended default is r=dhlogdhr = d_h \log d_h, which is the random-feature theory’s prescription.

No public production-LLM adoption. I do not know of any frontier decoder-only LLM that ships Performer. The technique appears in efficient-attention surveys (Tay et al. 2022, arXiv 2009.06732, § 3.3) as a reference linear-attention variant; its mathematical framing — random-feature approximation of softmax — is the load-bearing contribution rather than the specific architecture.

Lineage

Cite

BibTeX entry for the original paper
@article{arxiv2009_14794,
  title  = {Rethinking Attention with Performers},
  author = {Krzysztof Choromanski and others (Google Research)},
  year   = {2020},
  eprint = {2009.14794},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2009.14794}
}

Or cite the paper directly: arXiv:2009.14794.

Export

BibTeX
@article{arxiv_2009_14794,
  title         = {Rethinking Attention with Performers},
  author        = {Krzysztof Choromanski et al. (Google Research)},
  year          = {2020},
  eprint        = {2009.14794},
  archivePrefix = {arXiv},
  url           = {https://arxiv.org/abs/2009.14794}
}
CSL JSON
{
  "id": "arxiv_2009_14794",
  "type": "article-journal",
  "title": "Rethinking Attention with Performers",
  "author": [
    {
      "literal": "Krzysztof Choromanski et al. (Google Research)"
    }
  ],
  "issued": {
    "date-parts": [
      [
        2020
      ]
    ]
  },
  "URL": "https://arxiv.org/abs/2009.14794",
  "number": "2009.14794",
  "source": "arXiv"
}
RIS
TY  - JOUR
TI  - Rethinking Attention with Performers
AU  - Krzysztof Choromanski et al. (Google Research)
PY  - 2020
JO  - arXiv
AN  - arXiv:2009.14794
UR  - https://arxiv.org/abs/2009.14794
ER  -