Block-Sparse Attention
An attention variant that divides the attention map into token blocks and computes scores only for selected block regions instead of every token pair.
Block-sparse attention is a structured sparse-attention variant that groups tokens into blocks and keeps only selected block-to-block connections, lowering long-context attention work while staying easier to implement than arbitrary sparse pair lists.
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
Block-sparse attention is an attention variant that divides the attention matrix into square regions of tokens and computes scores only for selected regions. Instead of deciding pair by pair, it keeps or skips whole blocks of query-key interactions at once.Why It Exists
Dense attention becomes expensive on long sequences because every query token can compare with every key token. Block-sparse attention lowers that cost by removing large unused regions, while the regular block layout gives kernels and memory access a more predictable shape than fully irregular sparse attention.How It Works
The sequence is split into blocks of B tokens. Each query block can attend only to key blocks chosen by a mask, such as nearby diagonal blocks plus a few farther anchor blocks. Inside an allowed block, attention is still dense, but skipped blocks are never scored. That makes block-sparse attention narrower than full attention, more structured than general sparse attention, and less purely local than sliding-window attention when the mask keeps some distant blocks.q blockKV block 0KV block 1KV block 2KV block 3KV block 4KV block 5q block to KV block 0q block to KV block 3q block to KV block 5
Math Or Compute Schema
With sequence length n, block size B, and a block mask M_B, block-sparse attention applies the block mask before softmax so only selected query-block to key-block regions contribute weight. The formulas below contrast dense multi-head attention against a block-masked variant.Compared To Nearby Modules
Regular attention scores every token pair. Sparse attention is the broader family that allows many different masks, including irregular ones. Local attention limits each query to a nearby neighborhood; sliding-window attention is one common fixed-band version of that idea. Block-sparse attention sits between them: it is sparse because it skips many pairs, but it keeps those skips aligned to block boundaries so implementations can batch work cleanly. Compared with purely local patterns, block-sparse attention can still keep selected distant blocks when the mask allows.| Comparison dimension | Block-Sparse Attention | Multi-Head Attention | Sparse Attention | Local Attention | Sliding-Window Attention |
|---|---|---|---|---|---|
| Connectivity pattern | Selected block-to-block regions; dense work inside each allowed block | All n-by-n query-key pairs per head | General sparse pair mask with no required block regularity | Nearby-only neighborhoods around each query token | Fixed local band around each query token |
| Compute scaling with sequence length | Subquadratic when the number of kept blocks per query block stays bounded | Quadratic O(n²) per head | Subquadratic when sparsity fraction is fixed | Often near-linear O(n·r) when the local radius stays fixed | Linear O(n·W) per head when W is fixed |
| Distant token reach | Possible when the mask keeps far blocks, but only at block-level granularity | Any position can attend to any other position | Depends on the chosen sparse pattern | Usually indirect; far information moves through layers or companion links | Usually no direct distant reach inside one layer |