Linear Attention and Kernel Feature Maps
Linear attention rewrites the softmax similarity in ordinary attention as a kernel function, then uses the associativity of matrix multiplication to reorder the computation so cost grows linearly, not quadratically, with sequence length.
Prerequisites: The Quadratic Cost of Attention, Scaled Dot-Product Attention and the √d Factor
Sparse attention (see Sparse Attention: Longformer and BigBird) escapes the cost of full attention by refusing to compute most pairs at all. Linear attention takes a different route: keep every token influencing every other token, in principle, but restructure the arithmetic so the full matrix never has to be built. The trick hinges on a piece of algebra from linear algebra, matrix multiplication is associative, so and give the same answer but can cost wildly different amounts of work, combined with replacing the softmax similarity function with something that factors cleanly.
The analogy: totalling a big multiplication table two different ways
Suppose you want for a fixed query against keys and values. One way: compute all similarity scores first, then weight and sum the values, this is what standard attention does, and doing it for every one of queries means building the whole table. But if the similarity factors as for some feature map , you can instead sum once, into one small matrix that doesn't depend on the query at all, then every query just does one small multiplication against that shared summary. Same total, computed in a cheaper order, the way and both equal 24 but can be organised differently.
Why softmax normally blocks this trick
Ordinary attention weights are , the exponential inside softmax does not factor as for any simple , so the reordering trick above does not apply directly, and the full matrix must be formed.
Linear attention replaces softmax similarity with a kernel function that does factor:
In words: instead of directly measuring how similar a query and key are, first map each one through a feature function (a fixed or learned transformation into a new space), and then just take a dot product there. Because this similarity is literally a dot product of transformed vectors, the sum over keys can be computed once, shared across all queries, exactly as in the analogy.
In words: each query's output is its transformed vector multiplied against a running sum built once from all keys and values (numerator), divided by a similarly shared normalising sum (denominator), no query ever needs the full set of pairwise scores, only these two small shared summaries.
Worked example 1: cost comparison by direct counting
With tokens and feature dimension for : standard attention forms an matrix, cost . Linear attention computes the shared sum once, an matrix built by summing outer products, cost , then applies it to each of the queries, cost again. Total: , linear in .
At , , : standard cost . Linear cost , roughly 125× fewer operations at this length, and the ratio keeps growing with .
Worked example 2: the recurrent-state view
Linear attention has an equivalent formulation as a running state updated one token at a time: autoregressive generation can maintain a small fixed-size state , updated incrementally as , output . Starting from (a matrix of zeros), after 3 tokens the state is the sum of 3 rank-one updates, a fixed 64×64 object regardless of whether 3 tokens or 3 million have been processed. This is why linear attention is described as "transformers behaving like RNNs": constant per-step memory and cost.
Set the exponent low above to trace roughly linear growth, that's how linear attention's cost scales with sequence length, against the steep exponent-2 curve of standard attention on the same axes.
Linear attention replaces softmax similarity with a factorable kernel , which lets the sum over keys be computed once and reused across every query, turning cost from to and giving attention an exact RNN-like recurrent form with constant per-step memory.
What this means in practice
Linear attention is genuinely sub-quadratic, unlike FlashAttention and IO-Aware Attention, which is fast but still in FLOPs, a real algorithmic change, not a scheduling one. The tradeoff is representational: choosing to approximate softmax well is an active research problem (random-feature approximations like Performer, or simpler maps like ELU+1), and linear attention variants often trail exact softmax attention on tasks needing very sharp, peaked attention, since a linear kernel cannot express an arbitrarily peaked softmax exactly.
"Linear attention" is often confused with "linear complexity from sparsity," but the mechanism is entirely different: sparse methods skip computing most pairs, while linear attention (via kernel feature maps) still lets every token influence every other token's output, just computed through a shared summary instead of explicit pairwise scores. Confusing the two leads to wrong claims about which pairs of tokens a given model can or cannot directly influence.
Practice
- If exactly (the identity feature map), what does linear attention reduce to, and why does that particular choice fail to approximate softmax attention well?
- Explain why the recurrent-state view means linear attention needs the same fixed amount of memory to generate token 10 as it does to generate token 10,000.
Discussion
💡 Discussion rules
- Ask and answer about this concept. Off-topic gets removed.
- No homework dumps. Show what you tried first.
- Corrections are welcome. Cite a source when you claim an error.
Loading discussion…
Related concepts
Practice in interviews
Further reading
- Katharopoulos et al., Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention (2020)
- Choromanski et al., Rethinking Attention with Performers (2020)