Chip Foundry Services Request Assessment

Sparse Attention

Sparse Attention is the family of attention mechanism variants that restrict the full $N\times N$ attention matrix to a sparse pattern — reducing the quadratic $O(N^2)$ time and memory complexity of standard self-attention to $O(N\sqrt{N}), O(N \log N)$, or $O(N)$, enabling transformer models to process much longer sequences than would be feasible with dense attention while retaining most of the representational power.

Why Sparse Attention?

- Standard attention: Every token attends to every other token $\to O(N^2)$ compute and memory.
$- N = 4096: \sim 17M$ attention entries per head per layer. Manageable.
$- N = 100K: \sim 10B$ entries. Expensive but doable with FlashAttention.
$- N = 1M: \sim 1T$ entries. Impossible with dense attention → sparse patterns essential.

Sparse Attention Patterns

PatternWhat Tokens Attend ToComplexityExample
Sliding Window$W$ nearest neighbors$O(N\times W)$Mistral, Longformer (local)
DilatedEvery k-th token within window$O(N\times W/k)$Longformer (dilated)
Global + LocalSome tokens attend globally, rest locally$O(N\times (W+G))$Longformer, BigBird
StridedFixed stride pattern (blockwise)$O(N\sqrt{N})$Sparse Transformer (Strided)
RandomRandomly selected tokens$O(N\times R)$BigBird (random component)
Block SparseDense attention within blocks$O(N\times B)$Block-sparse attention

Sliding Window Attention (Mistral/Mistral-style)

- Each token attends to only the $W$ previous tokens (e.g., $W=4096$).
- Effectively: Local context window that slides with the sequence.
$- With L$ stacked layers: Effective receptive field $= L \times W$ tokens.
- Mistral: 32 layers × 4096 window $= 131K$ effective context.
- KV cache bounded: Only need to cache $W$ tokens → constant memory regardless of sequence length.

Longformer (Beltagy et al., 2020)

- Combines three patterns:
1. Local (sliding window): Every token attends to $W$ neighbors.
2. Dilated: Attend to tokens spaced $k$ apart → larger receptive field.
3. Global: Designated tokens (e.g., [CLS]) attend to all tokens.
$- Complexity: O(N \times W)$ instead of $O(N^2) \to $ handles documents up to 16K+ tokens.

BigBird (Zaheer et al., 2020)

- Random + Local + Global attention:
- Random: Each token attends to $R$ random tokens → captures long-range dependencies.
- Local: Sliding window of $W$ neighbors → local context.
$- Global: G$ special tokens attend to all → aggregate global information.
- Theoretically: Random attention makes the graph an expander → provably approximates full attention.

Linear Attention

$- Replace \operatorname{softmax}(\mathrm{QK}ᵀ)V$ with $\phi (Q)\phi (K)ᵀV \to $ kernelized attention.
$- Rearrange: \phi (Q)(\phi (K)ᵀV) \to $ compute KᵀV first ($d\times d$ matrix) $\to O(Nd^2)$ instead of $O(N^2d)$.
$- If d \ll N \to $ linear in $N$.
- Challenge: Quality gap vs. softmax attention — linear approximation loses sharpness.

Modern Hybrid Approaches

  • Mistral/Mixtral: Sliding window + GQA → efficient long-context.
  • Gemini: Hybrid with full attention at certain layers, sparse at others.
  • Ring Attention: Distribute sequence across devices, overlap communication with attention compute.

Sparse attention is the enabling architecture for long-context transformers — by intelligently selecting which token pairs to compute attention for, these methods extend the practical reach of transformers from thousands to millions of tokens while preserving the ability to capture the long-range dependencies that make attention powerful.