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 — doubling the input length quadruples the work. Linformer's authors observed something empirical: in trained transformers, the attention matrix is usually approximately low-rank, meaning most of its information could be reconstructed from a much smaller number of "directions" than . If that's true, you don't need to compute the full matrix at all.
Linformer acts on this by projecting the keys and values — normally matrices — down to a fixed, much smaller size using two learned projection matrices, where is chosen once (e.g. 256) regardless of how long the sequence is. Attention is then computed between the full-length queries and these -row keys and values, producing an attention matrix instead of . Since is fixed, total cost scales linearly in , not quadratically.
Concretely, a sequence of tokens with standard attention costs on the order of score computations; with Linformer and , it costs on the order of — roughly a sixteen-fold reduction, growing larger as increases further.
Linformer exploits the empirical low-rank structure of attention matrices to project keys and values down to a fixed small size , 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)