Sparse Attention Patterns
Sub-quadratic attention through sparsity: strided patterns, block-sparse, BigBird (random+window+global), Routing Transformer, hash-based attention (Reformer), and learned sparsity — complexity proofs for each.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Fixed Sparse Patterns
- Strided Attention (Sparse Transformer)
- BigBird: Random + Window + Global
- Hash-Based Attention (Reformer)
- Routing Transformer
- Learned Sparsity (Adaptively Sparse)
- Theoretical Guarantees
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Classify sparse attention methods as fixed-pattern vs content-based.
- Derive the complexity of strided attention.
- Prove that BigBird's pattern can simulate any Turing machine (universal approximation).
- Explain LSH attention and its collision probability guarantee.
- Compare the expressiveness-efficiency tradeoffs of each method.
Notation
- — the set of positions token attends to
- — stride length
- — number of global tokens
- — number of random connections
- — number of hash buckets
Core Intuition
Full attention attends to all positions — but most attention weights are near-zero (softmax concentrates on few keys). Sparse attention pre-selects which positions to attend to, reducing from to computations. The challenge: choose to preserve the important connections while being efficient to compute.
Sparse Attention Patterns
Fixed Sparse Patterns
Definition: The attention connectivity is determined by position alone (independent of content).
Advantages: Simple, hardware-efficient, predictable memory. Disadvantages: May miss content-dependent important connections.
Strided Attention (Sparse Transformer)
OpenAI's Sparse Transformer (2019): Factorize attention into two patterns:
Pattern 1 (local): Attend to the previous positions:
Pattern 2 (strided): Attend to every -th position:
Alternating layers: Odd layers use pattern 1, even layers use pattern 2.
Complexity: Each pattern has connections per token. Total: per layer.
Effective full connectivity: After 2 layers (one local + one strided), information can flow between any pair of positions through an intermediate "stride" position.
BigBird: Random + Window + Global
BigBird (2020) combines three patterns:
- Window attention (local): Each token attends to neighbors.
- Global tokens: designated tokens attend to/from all positions.
- Random connections: Each token randomly attends to other positions.
Complexity: — linear in .
Theoretical result (Zaheer et al.): BigBird with global token is a universal approximator of sequence-to-sequence functions, and can simulate any Turing machine.
Proof sketch: The global tokens act as a "memory bus" — any position can write to and read from them, enabling arbitrary communication patterns through multiple layers.
Hash-Based Attention (Reformer)
Key idea: Similar queries and keys should attend to each other. Use Locality-Sensitive Hashing (LSH) to group similar vectors into the same bucket, then attend within buckets.
LSH with random projections: Hash function where is random.
Collision probability: — similar vectors hash together with high probability.
Algorithm:
- Hash all queries and keys into buckets.
- Sort by bucket (tokens in same bucket are adjacent).
- Apply attention within each bucket (size ).
Complexity: . With : . With multiple rounds of hashing: further improvement.
Issue: Approximate — may miss important key-query pairs that hash to different buckets. Multiple hashing rounds reduce miss probability.
Routing Transformer
Content-based sparse attention: Learn which tokens should attend to each other.
Algorithm:
- Cluster tokens using k-means on their representations: .
- Each token attends only to tokens in the same cluster.
Clustering: Online k-means updated during training. Cluster assignments based on current token representations.
Complexity: per layer. With : .
Advantage over LSH: Learned clustering adapts to data distribution; hash functions are data-independent.
Learned Sparsity (Adaptively Sparse)
Top-k attention: Compute full attention scores, keep only top-:
Problem: Still requires to compute all scores (then discard most). Useful only for reducing memory, not compute.
Entmax / -entmax: Replace softmax with a mapping that produces exact zeros:
where is the Tsallis -entropy. For , the solution has exact zeros — sparse attention without explicit top-k.
Theoretical Guarantees
Theorem (BigBird). Any function computable by a full-attention transformer with layers can be approximated by a BigBird sparse transformer with layers, window , and global tokens.
Theorem (Reformer, probabilistic). With rounds of LSH hashing, the probability of missing an important key (one with attention weight ) is at most , where depends on the cosine similarity threshold.
Information-theoretic lower bound: Any sparse attention pattern with per token cannot distinguish more than different attention configurations — suggesting a minimum sparsity level for given tasks.
Common Pitfalls
Pitfall 1. Using sparse attention for tasks requiring precise long-range retrieval. If the task requires finding a specific needle in a haystack, and the sparse pattern doesn't include that position, performance drops catastrophically.
Pitfall 2. Ignoring hardware efficiency. Strided access patterns are less hardware-friendly than dense or block-sparse patterns. Block-sparse attention (attending to contiguous blocks) is much faster in practice despite similar theoretical complexity.
Pitfall 3. Comparing sparse methods only on perplexity. Sparse attention often matches full attention on perplexity but fails on downstream tasks requiring long-range reasoning.
Summary
- Strided: , simple, effective for image/audio.
- BigBird: with universal approximation guarantee (window + global + random).
- Reformer (LSH): , content-aware, approximate.
- Routing: Learned clustering, content-adaptive, .
- Entmax: Exact zeros in attention weights, but still to compute scores.
- Modern trend: FlashAttention makes full attention fast enough for most lengths; sparse methods mainly for .
Exercises
Exercise 1. For a sequence of length with strided attention (): compute the total attention FLOPs and compare to full attention.
Exercise 2. Prove that BigBird with at least one global token can propagate information between any two positions in 2 layers.
Exercise 3. For Reformer with buckets and : compute the expected bucket size and attention complexity per token.
Exercise 4. Explain why block-sparse attention is faster on GPUs than element-wise sparse attention (same number of non-zero entries).
Exercise 5. Design a sparse attention pattern for a model that must process a 200K-token document but answer questions about specific sections.