What sparse attention is
Full attention compares every query with every key, so a sequence of tokens costs scores per head and layer, and long documents quickly become too expensive. Sparse attention fixes in advance a small set of keys for each query and computes the softmax over those alone. If does not grow with , the cost grows linearly.
This page records the sparse pattern of BigBird in BigBird-RoBERTa base, an encoder (so attention is bidirectional), on a 1,024-token passage from Alice's Adventures in Wonderland or A Scandal in Bohemia, in all 12 layers. The tokens come in 16 blocks of 64. One token per block is drawn at its real position, so the 16 rows show the whole pattern.
Global, window and random blocks
BigBird works on blocks rather than single tokens. Each 64-token block of queries reads:
- 2 global blocks, the first and the last, which every block reads and which themselves read every block;
- 3 window blocks, itself and its two neighbours;
- 3 random blocks, drawn once per layer and head.
That is at most 8 of the 16 blocks, 512 of the 1,024 keys. The windows of the second and second-to-last blocks already contain a global block, and a random block can land on the last block, which is global too, so in this recording the middle blocks read 6, 7 or 8. The Block pattern panel colours each query-block × key-block cell by why it is read and shades it by the attention it receives; the title above it counts the blocks the selected row reads. The random blocks differ by layer and head, and the first and last rows are full.
Weight per key block
Within the chosen keys the head computes ordinary scaled dot-product attention. With 1,024 keys a row is too long to draw, so the head card shows each drawn query's weight summed over the 64 keys of every block:
Each row of sums to 1, and blocks the query never reads are drawn as masked. The row popup lists the weight on every block and how many keys the row reads in all.
Why these three parts
Each part answers a different need. The window keeps what matters most in text, the nearby tokens. The global blocks act as hubs: any token can reach any other in two steps, through a global token. The random blocks add shortcuts, as in a random graph, where a few random edges make every node close to every other. Zaheer et al. proved that this pattern, with its global tokens, keeps two properties of full attention: it is a universal approximator of sequence functions and it is Turing complete.
Blocks are a concession to hardware. Accelerators are fast at dense matrix products and slow at scattered reads, so BigBird gathers whole 64-key blocks and multiplies dense tiles instead of picking single keys.
Two details of the recording
In evaluation mode the transformers library skips the random draw and fills every random slot with block 0. To show the pattern BigBird was trained with, the recorder runs the block-sparse modules with their training flag set, which draws the random blocks as the original code does, with the layer index as the random seed; those modules have no dropout, and the rest of the model stays in evaluation mode.
A random block can also be the last block, which is already global. Its keys then enter the softmax twice, and the weights count both copies; the Block pattern panel marks such cells. In both passages this happens in 435 of the 2,304 block rows (12 layers × 12 heads × 16 blocks).
Where it came from
Child et al. (2019) introduced fixed sparse patterns, local and strided, for long images, audio and text. Longformer (2020) combined a sliding window with a few task-specific global tokens, and ETC (2020) a global-local split for structured inputs; BigBird (2020) added random blocks and the theory above, and was warm-started from RoBERTa's weights for the model recorded here. Tay et al. survey this family of efficient Transformers. The Window tab shows the window alone in a modern decoder; the NSA and DSA tabs show sparse attention whose keys are chosen by the model itself rather than fixed in advance.
Papers
- Big Bird: Transformers for Longer Sequences Global, window and random blocks, with proofs that the pattern keeps full attention's expressive power; the model this page records.
- Generating Long Sequences with Sparse Transformers The first fixed sparse attention patterns, local and strided, for long sequences.
- Longformer: The Long-Document Transformer Sliding-window attention plus global tokens, BigBird's closest relative.
- ETC: Encoding Long and Structured Inputs in Transformers Global-local attention for long and structured inputs, which BigBird builds on.
- RoBERTa: A Robustly Optimized BERT Pretraining Approach The encoder whose weights BigBird-RoBERTa starts from.
- Efficient Transformers: A Survey A survey of sparse, low-rank, kernel and other efficient attention methods.