TL;DR
Ring Attention is a way to run a transformer on a sequence too long for one device's memory. The sequence is cut into blocks, one per device. The devices form a ring, and while each device computes attention for its own query block, it sends its key and value blocks to the next device and receives blocks from the previous one. If the computation is slower than the transfer, the communication costs nothing extra, so the usable context grows with the number of devices.
Builds on: Transformer, FlashAttention: Fast and Memory-Efficient Exact Attention, Model Parallelism · Leads to: KV Cache
Why it mattered
Long context is a product need. The paper names books, high-resolution images, long videos and codebases as tasks that need large contexts, and cites models such as GPT-3.5-turbo-16K and Claude-2-100K as context-length references. Ring Attention is one of the methods that let a training run go much further than one device allows. The paper reports training 7B-scale models with more than a million tokens of context on 32 A100 GPUs, and claims training sequences above 100 million tokens, without approximating attention.
The problem
Attention compares every token with every other token. For a sequence of s tokens, the score matrix has s × s entries, and the outputs of one layer must be kept for the next layer to attend to them. The paper's example: processing 100 million tokens with batch size 1 needs over 1000 GB for a model with hidden size 1024. A current GPU or TPU has less than 100 GB of high-bandwidth memory. Memory-efficient methods had already reduced the cost of the s × s matrix and the feed-forward activations, but the stored output of each layer still grows with s on one device. So the bottleneck is the per-device memory that holds the sequence, not the attention arithmetic itself.
How it works
Here is the method on a toy case: 6 tokens, 3 devices, 2 tokens per device. Device i holds query block Q_i and key-value block KV_i. Devices are numbered 1, 2, 3 and form a ring, so device 1 receives from device 3, device 2 receives from device 1, and device 3 receives from device 2.
- Split the sequence. Device 1 gets tokens 1 and 2, device 2 gets tokens 3 and 4, device 3 gets tokens 5 and 6. Each device computes Q, K and V for its own tokens. Check: each device holds 2 of the 6 tokens.
- Step 0: attend to your own block. Each device computes attention between its query block and its own KV block, and keeps running statistics for the rows it has seen so far. At the same time it sends its KV block to the next device.
- Step 1: attend to the block you received. Device 1 now holds KV_3, which came from device 3. It attends to KV_3 and updates its running statistics. It sends KV_3 onward and receives KV_2 from device 3, which had received it from device 2.
- Step 2: attend to the last block. Device 1 attends to KV_2 and now has seen all three key-value blocks.
- Finish the layer. Each device normalises its running output and runs the feed-forward network on its own block. Each device needed only the block it was using and the one in transit. Check: after N = 3 steps every device has seen N blocks, which is the whole sequence.
The order of the blocks does not change the answer, because attention with the right running rescaling is the same whichever block comes first. That is why the devices can process blocks in whatever order they arrive.
The communication is hidden when each step's compute takes longer than its transfer. The paper gives the condition in the math section, and it translates into a minimum block size, about 1000 tokens for the hardware it lists with a fast link.
flowchart LR D1[Device 1: Q_1, KV_1] -->|send KV| D2[Device 2: Q_2, KV_2] D2 -->|send KV| D3[Device 3: Q_3, KV_3] D3 -->|send KV| D1 D1 -.->|compute attention with the block just received| D1
The math
Attention for one head is
where Q, K and V are s × d matrices, s is the sequence length and d is the head dimension. The softmax is taken row by row.
Step 2 in symbols: blocks can be combined in any order
For a query block, the paper relies on the fact that the attention over a group of key-value blocks can be accumulated one block at a time, as long as each block's statistics are combined correctly when the softmax is rescaled. In general form, for a query row, if the scores against block j are s_j, a running maximum m, a running normaliser l and a running output o are updated as each block arrives. This is the rescaling technique described in Online Softmax, and it gives the same result for any order of blocks. The paper uses it so that a device can process whichever block arrives next.
Step 3 in symbols: the overlap condition
Let F be the device's FLOPS, B the bandwidth between neighbouring devices, and c the block size in tokens. Per step, a device computes the two products of blockwise attention, about 4dc² FLOPs, and sends the key and value blocks, about 4cd bytes. The transfer is hidden when computation takes at least as long as communication:
So the minimum block size is the device's FLOPS divided by its link bandwidth. The paper leaves out the projections and feed-forward compute in this count, which makes the condition stricter, not weaker.
Step 4 in symbols: memory per device
A device stores six blocks of size c at hidden size h: one for its query block, two for its current key and value blocks, two for the blocks it is receiving, and one for its output. With bf16 values this is 6bch bytes for batch size b, independent of the full sequence length s. The paper's analysis also gives a minimum sequence length of s = 6c, six times the minimum block size.
Results
The paper compares maximum context length against the vanilla Transformer and two memory-efficient baselines. The table below copies the rows for the A100 and TPU configurations that the paper reports for the 7B model, with the context in thousands of tokens.
| Hardware (7B model) | Vanilla | Memory-efficient attention | Memory-efficient attention and feed-forward | Ring Attention | Ratio to best baseline |
|---|---|---|---|---|---|
| 8 A100, NVLink | 2K | 16K | 32K | 256K | 8× |
| 32 A100, InfiniBand | 4K | 64K | 128K | 4096K | 32× |
| TPUv3-512 | 1K | 4K | 8K | 2048K | 256× |
| TPUv4-1024 | 4K | 8K | 16K | 8192K | 512× |
The ratio column is the paper's "Ours vs SOTA". Each ratio is the Ring Attention figure divided by the best baseline in its row, and the paper's claim is that the gain scales with the number of devices.
Language model fine-tuning. The paper fine-tunes a 13B LLaMA-based model to a 512K context on 32 A100 GPUs, and reports high accuracy on a long-document line-retrieval test. The fine-tuning length was limited by the authors' compute budget, not by the method.
Reinforcement learning. In a trajectory-conditioned RL experiment across six ExoRL tasks, the model conditioned on 128 trajectories with Ring Attention reaches a total average return of 113.66. The same model with blockwise attention (conditioned on 32 trajectories, since 128 did not fit) reached 111.13, and the 128-trajectory version did not fit in memory with the earlier methods.
Limits. The maximum-context numbers are the longest sequences that fit under FSDP in the paper's end-to-end setup (section 5.1), not measured speed, and the experiments summarised here report no wall-clock speedups over the baselines. The model-flops-utilisation table is reported as negligible overhead for very long contexts, but this lesson does not quote its values. The text and the tables also disagree in places: the prose mentions TPUv4-512 while the table reports TPUv4-1024, and the prose gives a minimum sequence length of 6K to 10K per device while the table lists 149.5K for InfiniBand-connected A100s. The lesson uses the table values.
Reading guide
- Section 1 (Introduction): needs Transformer and Attention Mechanism. Read it for the motivation and the table of context windows of earlier systems.
- Section 2 (Large context memory constraint): needs Feedforward Neural Network for the feed-forward memory and GPU Memory Hierarchy for why high-bandwidth memory is the limit. Table 1 compares activation sizes.
- Section 3 (Ring attention): needs Online Softmax for the rescaling that makes block order irrelevant, Model Parallelism and Data Parallelism for the parallel setting, and Softmax for the attention formula. Figure 2 and Algorithm 1 are the walkthrough above; the overlap condition is in the Arithmetic Intensity paragraph.
- Section 4 (Setting) and Section 5 (Results): need KV Cache for the inference case in Appendix C. Table 1 counts memory in bfloat16, two bytes per value.
- Section 6 (Related work): needs FlashAttention: Fast and Memory-Efficient Exact Attention, which the paper cites as the blockwise attention background.
Check yourself
QuestionWhy can the devices process key-value blocks in any order and still get the correct attention output?Show answer
Attention for a query row is a softmax-weighted sum over all keys. The sum can be accumulated one block at a time if each block's contribution is rescaled by the running statistics. Once all blocks have been folded in, the result is the same whichever order they came in. The ring only decides the order in which each device sees the blocks, not what the final output is.
QuestionWith a block size of c = 1000 tokens, how much memory does one device need for the six blocks, in bytes, at batch size 1 and hidden size 1024, using the paper's 6bch formula?Show answer
The formula gives 6 × b × c × h bytes, with b = 1, c = 1000 and h = 1024. That is 6 × 1000 × 1024 = 6,144,000 bytes, about 6.1 MB. Because the formula does not depend on the full sequence length, this stays the same for a 1 million token sequence split over many devices.
QuestionThe paper gives a minimum block size c = F / B. For a device with F = 312 TFLOPS and a link of B = 300 GB/s, what is c, and does it match the table?Show answer
F / B = 312 × 10^12 / (300 × 10^9) = 1040, about 1.0 × 10^3 tokens. This matches the table's minimal block size of 1.0 × 10^3 for the A100 NVLink row.
QuestionUsing Table 3, how many times longer is the 7B context for 32 A100 GPUs with Ring Attention than with the best memory-efficient baseline on the same hardware?Show answer
Ring Attention gives 4096K and the best baseline gives 128K, so the ratio is 4096 / 128 = 32. This is the 32× figure in the table.
QuestionA team wants a longer context on a fixed number of devices and is considering a slower network. What does the overlap condition tell them to check first?Show answer
The condition is that compute per step must be at least as long as the transfer, which needs block size c of at least F / B. A slower link (smaller B) raises the minimum block size, so the team must either use larger blocks, which raises per-device memory in proportion to c, or accept that communication will no longer be hidden. The first check is the ratio of the device's FLOPS to the link bandwidth.
Sources
- Liu, Zaharia and Abbeel, Ring Attention with Blockwise Transformers for Near-Infinite Context, arXiv 2310.01889: https://arxiv.org/abs/2310.01889
- DOI for arXiv 2310.01889: https://doi.org/10.48550/arXiv.2310.01889