Sparse Attention

An attention variant that computes scores only for a subset of token pairs instead of every query-key combination.

Sparse attention is an attention variant that computes scores only for selected token pairs, trading full connectivity for lower cost on long contexts.

At a glance

Released

April 2019

Authors

Rewon Child, Scott Gray, Alec Radford, et al.

Optimizes

  • Attention Compute
  • Memory Bandwidth
  • Long Context Inference

What It Is

Sparse attention is an attention variant that restricts which query-key pairs participate in the softmax. Instead of a full n-by-n attention matrix, only entries allowed by a sparsity pattern receive non-zero weight.

Why It Exists

Sparse attention targets attention compute, memory bandwidth for score materialization, and the cost of serving long sequences when dense all-pairs attention would dominate runtime.

How It Works

Each query position attends only to keys permitted by a sparsity mask or fixed pattern. Disallowed pairs are zeroed before softmax normalization, so the model spends compute on selected connections rather than the full token grid.

Math Or Compute Schema

With sequence length n and sparsity mask M, sparse attention applies M to the score matrix before softmax. The formulas below contrast dense multi-head attention against masked sparse attention that limits which pairs receive weight.
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.
Sparse attention
Attention(Qi,Ki,Vi)=softmax ⁣(M⊙QiKi⊤dk)Vi\text{Attention}(Q_i, K_i, V_i) = \mathrm{softmax}\!\left(M \odot \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.
ss
Fraction of allowed query-key pairs in the sparsity pattern.
dkd_k
Key dimension per head.
ii
Query head index.
MM
Binary or weighted mask that zeroes disallowed attention pairs.

Compared To Nearby Modules

Compared with multi-head attention, sparse attention skips most token pairs rather than scoring all of them. Multi-query and grouped-query attention remain dense over allowed positions but reduce key-value head count. Sparse attention instead changes which positions connect at all.
Comparison dimensionSparse AttentionMulti-Head AttentionMulti-Query AttentionGrouped-Query Attention
Attention connectivityMask-limited subset of query-key pairsAll n-by-n query-key pairs per headAll n-by-n pairs with shared key-value headsAll n-by-n pairs with grouped key-value heads
Compute scaling with sequence lengthSubquadratic when sparsity fraction is fixedQuadratic O(n²) per headQuadratic O(n²) with fewer key-value projectionsQuadratic O(n²) with reduced key-value head count
Global token reachDepends on sparsity pattern; often local or block-structuredAny position can attend to any other positionAny position can attend to any other positionAny position can attend to any other position

Example Architectures

Sparse attention appears in long-context decoder models that combine local windows, block patterns, or learned sparsity with standard projection layers.

Limitations And Tradeoffs

Restricting connectivity can miss long-range dependencies that dense attention would capture. Pattern choice and mask quality strongly affect what information flows across the sequence.

Why It Still Matters

As sequence lengths grow, quadratic attention cost motivates sparsity patterns that preserve useful signal while avoiding a full n-by-n score matrix on every layer.

Tags

References

  1. Child, Rewon, et al. "Generating Long Sequences with Sparse Transformers." arXiv, 2019, https://arxiv.org/abs/1904.10509.