What is linear attention?
Linear attention is a family of attention methods that replace the softmax in attention with kernel feature transformations and reorder the multiplications, never building the full attention matrix, so compute and memory grow linearly with sequence length.
Unlike sparse attention, which skips some token pairs, linear attention uses all tokens but restructures the computation. Since it can run recurrently with a fixed-size state in generation, it takes on a structure close to state space models.
- Topic
- Machine learning, models and training
- Also known as
- Kernelised attention
- Origin
- 2020, Katharopoulos et al.
How linear attention works
- Query and key vectors pass through a kernel feature function.
- Products of keys and values are summed first, then multiplied by the query, so no matrix the size of token count squared is formed.
- In text generation this sum can be kept as a fixed-size state updated with each new token.
- Newer variants add forgetting or gating to this state, learning how much old information to keep.
In more detail
The mechanism and where the gain begins
Mathematically, linear attention replaces the exponential similarity with a dot product of query and key feature maps, then uses associativity to sum key-value products first and divide by a normalising term. Cost then grows linearly with sequence length but with the square of the head dimension, so on sequences shorter than the head dimension linear attention can be slower than FlashAttention; it only wins as sequences grow long.
Chunkwise parallel training and hardware efficiency
Purely recurrent linear attention trains slowly on modern GPUs because sequential step-by-step updates underutilise Tensor Cores. Modern variants such as Gated Linear Attention (GLA) and Mamba-2 use a chunkwise parallel algorithm that processes tokens within each chunk via quadratic matrix multiplications on Tensor Cores while passing fixed-size states recurrently between chunks.
State capacity limits, the delta rule and hybrid models
Because the recurrent state is a fixed-size matrix, storing too many key-value associations causes interference and degrades exact recall or in-context copying. Delta-rule variants (such as DeltaNet and Gated DeltaNet) address this by selectively overwriting old memory entries before adding new ones; some recent production models also interleave linear-attention layers with full softmax layers to preserve sharp retrieval.
Commonly confused terms
| Term | How it differs |
|---|---|
| State space model | State Space Models (like Mamba) originate from continuous-time linear dynamical systems discretised into recurrences, whereas linear attention derives from kernelising Transformer attention, though modern architectures (e.g., Mamba-2's SSD) mathematically unify both under structured linear recurrences. |
| FlashAttention | FlashAttention computes exact softmax attention with quadratic FLOPs while eliminating HBM memory bottlenecks via IO-aware tiling, whereas linear attention changes the mathematical formulation itself to achieve linear time and constant decoding state memory. |
Why linear attention matters for on-premises AI
Linear attention can keep compute and generation memory constant or linear on long sequences, making long contexts economical on limited in-house hardware. The cost is losing the sharp selectivity of softmax attention: performance can drop on tasks such as recalling an exact detail from a long text. Many models therefore mix linear attention with classic attention layers. Before choosing such a model in-house, long-document and recall tests are run on the organisation's own data.
For an in-house model summarising long-running machine logs, a manufacturer tries a hybrid model with linear attention layers, measuring memory use alongside how accurately it recalls specific fault codes from the logs.
Common questions
Is linear attention as good as classic attention?
Not on every task. It is far more efficient on long sequences but can trail softmax attention on tasks such as recalling specific details.
How does linear attention differ from sparse attention?
Sparse attention skips some token pairs. Linear attention uses all tokens but reorders the computation to cut cost.
Related terms
- Sparse attention
- State space model
- Self-attention
- Attention mechanism
- Transformer
- Selective state space model
Sources
In the glossary: Glossary, D to L