Quant Memo
Advanced

Performer and Random-Feature Attention

Performer approximates the exponential similarity function inside softmax attention with random feature maps, letting attention be computed as a product of smaller matrices in linear time instead of the usual quadratic full attention matrix.

Prerequisites: Scaled Dot-Product Attention and the √d Factor

Standard softmax attention needs the full n×nn \times n matrix of pairwise query-key similarities, which is why its cost scales quadratically with sequence length — every one of the nn queries must be compared against every one of the nn keys. Performer avoids ever forming that matrix by noting that softmax attention's exponential similarity function can be approximated as a dot product in a different, higher-dimensional feature space: each query and key vector is passed through a random nonlinear projection (a "random feature map"), chosen so that the dot product of two projected vectors approximates the exponential similarity of the two originals, in expectation.

Once queries and keys are replaced by these random features, attention can be rearranged using the associative property of matrix multiplication — computing (keys × values) first, then multiplying by queries — rather than (queries × keys) first, then by values. That reordering is the entire trick: it turns an n×nn \times n intermediate matrix into a much smaller fixed-size matrix, so the total cost becomes linear in sequence length instead of quadratic, at the price of the similarity scores being an unbiased random approximation rather than the exact softmax value.

Performer replaces the exact softmax similarity function with a random-feature approximation that can be written as a dot product, which lets attention be computed via (keys × values) first instead of (queries × keys) first — turning quadratic-cost attention into a linear-cost approximation.

Related concepts

Practice in interviews

Further reading

  • Choromanski et al., Rethinking Attention with Performers (2021, ICLR)
ShareTwitterLinkedIn