TL;DR
Standard attention compares every token with every other token, so the cost grows with the square of the sequence length. Linear attention changes the comparison to a product of two feature maps, . That product lets you sum the keys and values first and reuse the sum for every query, so the cost grows only linearly with length. The price is that the comparison is no longer the sharp softmax, and the model keeps less exact recall.
Where you'll see it: long-context language models and efficient transformer variants, and in the causal form, where a transformer is run token by token like a recurrent network with a fixed-size state.
Intuition
Filing cards instead of comparing every pair
Suppose you must answer questions about a long set of notes. The softmax way is to read every question against every note, one pair at a time. The linear way is to first build a single index card that adds up what all the notes say, weighted by their keys, and then answer each question by reading only that card. The second method does one pass over the notes plus one lookup per question, and never builds the full table of pairs.
The card has a fixed size, the same no matter how many notes you add. That is the point, and also the limit: the card can summarise the notes but cannot hold every detail separately.
Why the idea exists
For a sequence of tokens, softmax attention builds an matrix of scores. Doubling the length quadruples the work and the memory for that matrix. Models that read books or long code need many thousands of tokens, so the quadratic term becomes the bottleneck. Linear attention removes the matrix, so the cost depends on only once.
The math
Given queries, keys and values and , the output at position is
Here is the sequence length and the key dimension. The weights are positive and sum to 1 over .
Replace the exponential similarity with , where is a feature map that sends each -vector to a vector of positive numbers. Katharopoulos et al. use , which is always positive. Then
The two sums do not depend on . Define
Then . Computing and takes work once. Each output then takes . The total is linear in , because is fixed.
The causal case
A language model may only look at earlier tokens. Then the sums run up to , and they can be updated one token at a time:
This is the recurrent form. The state has fixed size plus a vector, whatever the length. It is why linear attention is related to the recurrent network in the paper's title.
Cost comparison
Softmax attention forms scores, each a dot product of length , so the score work is . Linear attention forms , which costs when . The ratio of the two is
So linear attention is cheaper once the sequence is longer than the key dimension.
Worked examples
Use scalars, so , and take the feature map . For positive this matches . Let the keys be , so . Let the values be . Let every query be , so for each . Because the feature of the query is a common factor in numerator and denominator, the outputs reduce to a weighted average of values.
- Output at uses only : .
- Output at uses : .
- Output at uses : .
Check with the recurrent form. Start with and . After token 1, and , so . After token 2, and , so . After token 3, and , so . The two methods agree.
Interpretation: each output is a running weighted average of the values seen so far. Later tokens with larger features pull the average toward their values.
Take tokens and key dimension .
- Softmax score work: multiply-adds.
- Linear attention work: multiply-adds.
- Ratio: .
Interpretation: at this length the linear form does about 64 times less of the main work. The gap grows as grows, and it is the same whatever the hardware, since it counts operations, not time. Real speedups also depend on memory traffic and on how well the code is written.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Very long input, memory limited | Softmax matrix does not fit | Linear attention, or a sparse or windowed method |
| Task needs exact recall of one earlier token | Fixed-size state blurs individual tokens | Softmax attention, or a hybrid with some softmax layers |
| Short sequences, not much larger than | Little or no speed gain | Softmax attention, which is also well optimised |
| Feature map gives a near-zero denominator | Division is unstable, outputs blow up | Choose a positive, bounded feature map; add a small constant |
| Autoregressive generation | Need a cache that grows with length | The recurrent form, whose state does not grow |
The main failure is quality, not speed. Softmax concentrates weight on a few tokens, while the feature map spreads it over many, so tasks that need sharp retrieval can suffer. How large that gap is depends on the task and the feature map; this lesson does not quote a figure for it.
The method is also sensitive to how the feature map interacts with position information. A position term added before the projections does not split cleanly into the product form, and the RoPE paper argues that this is why rotary embeddings suit linear attention better. See Rotary Position Embedding.
History
Attention with a weighted sum over a sequence came from machine translation work by Bahdanau, Cho and Bengio in 2014, and the softmax form became standard with the transformer of Vaswani and colleagues in 2017. The quadratic cost of that form was known from the start, and many variants followed to reduce it.
Linear attention was introduced by Angelos Katharopoulos, Apoorv Vyas, Nikolaos Pappas and François Fleuret, who published "Transformers are RNNs" at ICML in 2020. They showed the causal recurrent form and the speed gain on long sequences. In their experiments on image generation and speech recognition, the linear transformer reached quality close to the softmax transformer while generating images orders of magnitude faster (up to 4,000 times in their CIFAR-10 setup, which the paper attributes to the constant-size state).
Check yourself
QuestionWhy does the cost of softmax attention grow quadratically with sequence length, and what does linear attention change?Show answer
Softmax attention compares every query with every key, which forms an matrix. Linear attention replaces the similarity with , so the sums over keys and values can be computed once as and and reused for every query. The matrix is never formed, so the cost is linear in for fixed .
QuestionUsing the feature map with keys , values , and a query with , compute the output at the second position with causal masking.Show answer
Keys give . The output is . The query feature cancels from numerator and denominator, so the output is a weighted average of the values, using the key features as weights.
QuestionWhy is the state of the causal form useful for generation, and what does it cost?Show answer
Each new token updates and in constant size, so memory does not grow with the number of tokens generated. The cost is that the whole past is squeezed into a fixed matrix, so exact recall of one earlier token is weaker than with a cache of all keys and values.
QuestionA team has sequences of 128 tokens with key dimension 64 and wants to switch to linear attention for speed. Is this a good idea?Show answer
Not on speed grounds. The ratio of score work is , so the gain is at most about two times, and the implementation overhead can cancel it. A softmax baseline is likely to be faster in practice and better in quality at this length. Linear attention pays off when is many times .
Sources
- Katharopoulos, Vyas, Pappas, Fleuret, Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention: https://arxiv.org/abs/2006.16236
- Vaswani et al., Attention Is All You Need: https://arxiv.org/abs/1706.03762