Sparse Attention
The self-attention mechanism is the foundation of Transformer models, but its default design assumes that every token in a sequence can relate to every other token. For a sequence of (n) tokens, the attention operation involves an (n \times n) score matrix. Its time and memory complexity are therefore quadratic, or (O(n^2)). That design works well for many tasks, yet it becomes fragile when inputs grow long. Long-form text, high-resolution images, and genomic sequences can produce sequence lengths that make dense attention impractical.
Sparse attention is an important family of techniques that addresses this limitation by restricting which token pairs are allowed to interact. Instead of computing attention across the full matrix, a model computes it only for selected connections. The central idea is simple: not all tokens need to attend to all tokens.
In a standard attention layer, the operation can be written as:
[
\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d}}\right)V
]
Sparse attention modifies the score matrix, often called (S = QK^T), so that each query (q_i) only attends to a subset of positions. This subset might be nearby tokens, strided tokens, global tokens, or randomly selected positions. If each query looks at about (w) keys on average, the complexity can fall from (O(n^2)) to (O(n \cdot w)). When (w) is much smaller than (n), the savings are substantial.
The approach is not purely speculative. Studies of trained Transformers suggest that attention patterns are often already close to sparse. Many weights become very small or close to zero. Sparse attention can therefore be seen as formalizing and enforcing a useful structure.
Common Sparse Patterns
Several patterns are widely used in practice.
- Sliding window: each token attends to nearby tokens, often with a small window on each side. This captures local context. Mistral and Longformer use local attention patterns.
- Strided attention: tokens attend to positions at fixed intervals. This allows some distant context while reducing density. OpenAI's Sparse Transformer explored this direction.
- Global tokens: a few special tokens connect to all positions and act as global context providers.
- Random connections: each token receives a small number of random connections. BigBird includes this as a key component.
- Block sparse attention: sequences are split into blocks, and connections are determined at the block level. This can be more efficient on GPU hardware.
Longformer and BigBird often combine several patterns, such as local, global, and random attention, to balance efficiency and expressiveness.
Why Sparse Attention Matters
Dense attention creates a computational bottleneck as sequence length increases. Sparse attention introduces structure that can reduce cost dramatically. It can also make the model more interpretable, because the connections that are used can be more easily described.
The main benefit is that compute and memory usage can grow in a more near-linear fashion rather than quadratically. This makes it easier to process long documents, images, or sequences. It also allows researchers to control how much context a token can see.
Benefits and Tradeoffs
Sparse attention can reduce computation and memory costs, sometimes allowing longer sequences than dense attention permits. It can also make patterns more explicit and interpretable.
However, fixed sparse patterns may miss important long-range dependencies. The best structure can vary by task. Sparse operator implementations can also be complex and may require custom hardware-aware kernels.
Typical Uses
Sparse attention appears in long-document models, long-sequence generation, image transformers, and modern language models. BigBird, Longformer, Sparse Transformer, Mistral, and Gemma are often mentioned in this space.
Conclusion
Sparse attention offers a practical path beyond dense self-attention. By limiting connections, it lowers complexity and helps scale to longer inputs. Understanding sliding window, strided, global, random, and block patterns is valuable for building efficient Transformer systems.