Linear Attention

An attention variant that replaces explicit softmax dot-product attention with kernel or feature-map formulations that scale near-linearly with sequence length.

Linear attention is an attention variant that replaces full all-pairs softmax scoring with feature-map or recurrent formulations, trading exact dense attention for better sequence-length scaling on long inputs.

At a glance

Released

June 2020

Authors

Katharopoulos, Angelos, Vyas, Apoorv, Pappas, Nikolaos, et al.

Optimizes

  • Sequence Scaling
  • Attention Compute
  • Memory Bandwidth

What It Is

Linear attention is an attention variant that reformulates attention without building the full softmax score matrix. Feature maps on queries and keys let the model accumulate key-value statistics in linear time rather than scoring every query-key pair explicitly.

Why It Exists

Linear attention targets sequence-length scaling, attention compute cost, and memory bandwidth when dense quadratic attention would dominate runtime on long contexts.

How It Works

Queries and keys pass through a feature map φ that makes their product associative. The model accumulates φ(K)ᵀV across positions and combines the result with φ(Q) at each step, avoiding explicit materialization of the full n-by-n attention matrix.

Math Or Compute Schema

With feature map φ applied to queries and keys, linear attention computes output through associative products rather than softmax over all pairs. The formulas below contrast dense multi-head attention against linear attention that routes through φ and a normalization term.
Multi-head attention (MHA)
Attention(Qi,Ki,Vi)=softmax ⁣(QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(\frac{Q_i K_i^{\top}}{\sqrt{d_k}}\right) V_i
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
dkd_k
Key dimension per head.
ii
Query head index.
Linear attention
Attention(Qi,Ki,Vi)=ϕ(Qi)(ϕ(Ki)⊤Vi)ϕ(Qi)(∑jϕ(Ki,j))\text{Attention}(Q_i, K_i, V_i) = \frac{\phi(Q_i)\big(\phi(K_i)^{\top} V_i\big)}{\phi(Q_i)\big(\sum_j \phi(K_{i,j})\big)}
QQ
Query vectors for head i.
KK
Key vectors for head i.
VV
Value vectors for head i.
HH
Number of query heads.
φφ
Feature map applied to queries and keys to enable associative accumulation.
dkd_k
Key dimension per head.
ii
Query head index.
ZZ
Normalization denominator from summed feature-mapped keys.

Compared To Nearby Modules

Compared with multi-head attention, linear attention changes the compute mechanism rather than only sharing key-value heads. Multi-query and grouped-query attention remain dense over allowed positions but reduce key-value head count. Linear attention instead pursues subquadratic sequence scaling through kernel or recurrence formulations.
Comparison dimensionLinear AttentionMulti-Head AttentionMulti-Query AttentionGrouped-Query Attention
Sequence scalingNear-linear O(n) per head with fixed feature-map dimensionQuadratic O(n²) per headQuadratic O(n²) with fewer key-value projectionsQuadratic O(n²) with reduced key-value head count
Compute mechanismFeature-map φ on queries and keys; associative key-value accumulationExplicit softmax over all query-key dot productsDense softmax with shared key-value headsDense softmax with grouped key-value heads
ExpressivenessApproximates softmax dot-product attention via chosen kernelFull pairwise softmax attentionFull pairwise softmax attention with shared key-value headsFull pairwise softmax attention with grouped key-value heads

Example Architectures

Linear attention appears in long-context models and research architectures that combine subquadratic attention layers with standard dense blocks to balance efficiency and expressiveness.

Limitations And Tradeoffs

Feature-map linear attention is not identical to softmax dot-product attention. Approximation quality depends on the chosen kernel, and some formulations sacrifice the exact pairwise scoring that dense attention provides.

Why It Still Matters

As context lengths grow, subquadratic attention options remain a practical design axis for models that must serve long sequences without quadratic attention cost on every layer.

Tags

References

  1. Katharopoulos, Angelos, et al. "Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention." arXiv, 2020, https://arxiv.org/abs/2006.16236.