Sparse Attention
Definition
Sparse attention breaks the wall by changing which query–key pairs may interact — masking most entries of the score matrix before softmax — while keeping the attention math itself unchanged. The motivation is empirical: in trained models most attention mass is local, so the dense matrix pays in full for a nearly-sparse object. Structured patterns (window + global + random) recover cost, provably without losing expressive power in the limit.
Key math / How it works
One masked op with binary mask :
Core cost , not . Pattern families:
- Sliding window — local band, , .
- Longformer (Beltagy et al., 2020; arXiv:2004.05150): window + dilation (gaps of widen reach per layer, like dilated convs) + global tokens (a few positions, e.g.
[CLS], attend to and are attended by all — one-hop hubs). Cost , . Scaled bidirectional BERT-style attention to thousands of tokens. - BigBird (Zaheer et al., 2020; arXiv:2007.14062): window + global + random ( random keys per query). Viewing as a graph, random edges make it an expander with small diameter, so any two tokens connect via a short path. Proven to be a universal approximator and Turing-complete at cost.
Entries and FLOPs:
| Pattern | Allowed entries | Scaling |
|---|---|---|
| Dense | quadratic | |
| Window () | linear | |
| Window + global () | linear | |
| BigBird ( random) | linear |
Why it matters
Sparse patterns pay off precisely in the long-context regime () where the term dominates the projections. Practically, window + global does most of the work: BigBird’s universality is an existence result, and random access is hardware-unfriendly (gather-scatter, poor coalescing), so deployed stacks lean on window + global + a few dense layers rather than random edges.
Taught in
- 06-sparse-and-sliding-window — §3 the pattern taxonomy and universality argument.
See also
- sliding-window-attention — the local base pattern
- attention-sinks — the streaming/eviction cousin
- linear-attention — dissolving the matrix vs. thinning it
- attention-glossary · attention-mechanisms