Long sequences across devices
The activations of a transformer grow with the length of the sequence, and attention's work grows with its square, so a long enough context does not fit on one device even when the weights do. Context parallelism (CP) splits the sequence: every rank keeps the whole model and runs it on its own share of the tokens. The embeddings, LayerNorms, projections and MLP act on each token alone and need nothing from other ranks; only attention, where every query reads all earlier keys, does.
The page runs GPT-2 (124M) on its whole context, the first 1,024 GPT-2 tokens of SQuAD's Apollo program article, on four ranks of 256 tokens each, every rank a process on this Mac's CPU talking through gloo (the 1,024 tokens on 4 ranks panel). Its times come from that CPU run; the ring's pattern of transfers is what carries over to GPUs.
A block of attention and its statistics
Take a block of queries and a block of keys and values. For each query and key in them the score is , masked to when the key comes after the query. The block's statistics are its row maximum , the sum of the exponentials below it, and the unnormalised output:
Attention over the whole row is when the block holds every key. Subtracting the maximum keeps the exponentials from overflowing; it changes nothing in the result.
The online softmax
Two blocks' statistics can be merged without looking at the scores again. With ,
so after all blocks, in any order, and are the full row's maximum and sum and is exactly full attention. The The online softmax table follows one query of one head through the four steps: each block's and , the running values, and the factor that rescales what was summed before whenever a later block holds a larger score. GPT-2's heads put much of their weight on the first few tokens: in block 6, queries on ranks 1–3 give 55 % of their attention, averaged over the heads, to tokens 1–16. Those sit in rank 0's block, which holds the row's largest score for 84–86 % of these queries' heads, so the maximum jumps when it arrives, by a median of 2.2, and everything summed before is scaled by about . With the contiguous split that block is the last one they can see. The bars below split the softmax weight by block, and the strip adds each block's share into the output, which equals the unsplit model's.
The ring
Ring attention arranges the ranks in a ring. Each starts with its own keys and values; at every step it sends the block it holds to the next rank and receives one from the previous rank, while it computes attention from its queries to the block it holds. After four steps each rank has seen all four blocks and merged them. A hop carries one rank's keys and values for one layer, 2 × 256 × 768 floats, 1.5 MiB; each rank sends three per layer, 36 in GPT-2's forward pass. As long as attention on a block takes longer than moving the next one, the transfers are hidden behind compute; the bars on the Block 6 on 4 ranks time line show each transfer from when it was posted until it was waited for.
No rank ever holds the whole 1,024 × 1,024 score matrix, or more than two blocks of keys and values. The Head h's weights panel draws the unsplit model's attention weights for one head of block 6, averaged over 16 × 16 cells, with the blocks each rank computes at the selected step outlined.
The causal mask and the zigzag split
With the causal mask, a query block never needs keys from its future. If each rank holds a contiguous quarter, rank 0 needs only its own block and rank 3 needs all four: per head, rank 0 computes 65,536 query–key pairs and rank 3 262,144, and the ranks with little to do wait for the others. The zigzag split, used by Megatron's context parallelism and by Llama 3, cuts the sequence into eight chunks and gives rank chunks and , an early and a late one. Computing only the chunk pairs with a visible key, every rank then does 147,456 pairs per head, the same work. On this Mac's CPU, block attention over the 12 layers took 0.095 s on rank 0 and 0.264 s on rank 3 with the contiguous split, and 0.154–0.159 s on every rank with the zigzag split. The ranks with less to do waited for the others at the ring's transfers, so the forward pass took 0.40–0.41 s on every rank, against 0.31 s with zigzag. The Scores computed per rank bars and the time line show both splits as recorded.
Where it came from
The online softmax was introduced for a single pass over the scores (Milakov and Gimelshein, 2018) and is the core of FlashAttention (Dao et al., 2022) inside one GPU. Li et al. (2021) passed keys and values around a ring of devices for long sequences; Blockwise Parallel Transformer and Ring Attention (Liu et al., 2023) combined the ring with blockwise attention and overlapped the transfers with compute. DeepSpeed-Ulysses (Jacobs et al., 2023) splits the sequence with all-to-alls over heads instead, and Llama 3 (2024) trained its long-context stage with context parallelism. TP's sequence parallelism splits the sequence only for LayerNorm and dropout.
Papers
- Ring Attention with Blockwise Transformers for Near-Infinite Context Keys and values passed around a ring of devices while each computes blockwise attention, the transfers hidden behind compute.
- Online normalizer calculation for softmax Merging softmax maxima and sums block by block, the merge on this page.
- Blockwise Parallel Transformer for Large Context Models Attention and the MLP computed block by block, the single-device half of ring attention.
- Sequence Parallelism: Long Sequence Training from System Perspective Ring self-attention: the sequence split across devices, keys and values passed around a ring.
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness The online softmax over blocks inside one GPU, exact attention without the full score matrix.
- DeepSpeed Ulysses: System Optimizations for Enabling Training of Extreme Long Sequence Transformer Models The other way to split a sequence: all-to-alls that hand each device whole heads instead of a ring.
- The Llama 3 Herd of Models Context parallelism with the load-balanced (zigzag) split, used to train its long-context stage.