Quant Memo
Advanced

Linformer Low-Rank Attention

Linformer speeds up self-attention by observing that the attention matrix is usually approximately low-rank, so projecting the keys and values down to a small fixed number of rows before computing attention gives near-identical results in linear time instead of quadratic.

Prerequisites: The Self-Attention Mechanism, The Quadratic Cost of Attention

Standard self-attention compares every token to every other token, so its cost grows with the square of sequence length nn — doubling the input length quadruples the work. Linformer's authors observed something empirical: in trained transformers, the n×nn \times n attention matrix is usually approximately low-rank, meaning most of its information could be reconstructed from a much smaller number of "directions" than nn. If that's true, you don't need to compute the full n×nn \times n matrix at all.

Linformer acts on this by projecting the keys and values — normally n×dn \times d matrices — down to a fixed, much smaller size k×dk \times d using two learned projection matrices, where kk is chosen once (e.g. 256) regardless of how long the sequence is. Attention is then computed between the full-length queries and these kk-row keys and values, producing an n×kn \times k attention matrix instead of n×nn \times n. Since kk is fixed, total cost scales linearly in nn, not quadratically.

Concretely, a sequence of n=4,096n = 4{,}096 tokens with standard attention costs on the order of n2=16,777,216n^2 = 16{,}777{,}216 score computations; with Linformer and k=256k = 256, it costs on the order of n×k1,048,576n \times k \approx 1{,}048{,}576 — roughly a sixteen-fold reduction, growing larger as nn increases further.

Linformer exploits the empirical low-rank structure of attention matrices to project keys and values down to a fixed small size kk, turning attention's cost from quadratic in sequence length to linear, at the price of an approximation that can lose accuracy on tasks needing genuinely full-rank, long-range attention.

Related concepts

Practice in interviews

Further reading

  • Wang et al., Linformer: Self-Attention with Linear Complexity (2020)
ShareTwitterLinkedIn