Scaling the context length of transformer-based large language models has historically collided with two fundamental walls: the computational and memory complexity of standard self-attention, and the high-bandwidth memory (HBM) capacity of individual accelerator devices. While IO-aware tiling algorithms such as FlashAttention eliminate intermediate quadratic activation storage by computing softmax within fast on-chip SRAM, the entire sequence key and value tensors must still reside within the memory pool accessible to a single device or tightly coupled tensor-parallel group.
Standard tensor parallelism (Megatron-LM TP) partitions attention heads across accelerators, but requires two costly All-Reduce operations per transformer layer and cannot scale beyond the number of query heads (often 32 or fewer in modern architectures). Furthermore, sequence parallelism approaches that gather the full sequence into each device recreate the memory bottleneck.
Ring Attention, introduced by Hao Liu, Matei Zaharia, and Pieter Abbeel, resolves this limitation by combining blockwise online softmax with a circular peer-to-peer communication topology. By distributing the sequence across devices and circulating key-value blocks in a ring while query blocks remain resident, Ring Attention eliminates the quadratic memory barrier per GPU, hides communication latency behind compute FLOPs, and achieves context scaling that is strictly linear in the number of participating accelerators.
The Memory and Communication Bottlenecks in Long Sequences
Standard scaled dot-product attention maps query tensor , key tensor , and value tensor to an output tensor :
For a sequence of length tokens with hidden dimension in 16-bit precision, storing the raw activation matrices requires . The intermediate attention matrix would require of storage per attention layer, making single-device materialization physically impossible on modern 80GB or 141GB GPUs.
While FlashAttention reduces the memory complexity to by streaming blocks through SRAM via online softmax, training a 1-million-token sequence still requires storing the input activations, key-value states, and optimizer parameters across all layers. Traditional distributed approaches fail under extreme sequence lengths:
- Tensor Parallelism (TP): Partitions weights and heads across devices. When is massive, activation memory per device remains proportional to , and intra-layer
All-Reduceover high-speed NVLink becomes a primary latency bottleneck. - All-Gather Sequence Parallelism (DeepSpeed Ulysses): Partitions sequence dimension across devices, then executes an
All-to-Allcollective to transform sequence-partitioned states into head-partitioned states for local full-context attention. While efficient within high-bandwidth clusters, Ulysses is bounded by the head count () and requires global all-to-all communication that degrades over inter-node InfiniBand networks.
Ring Attention eliminates both constraints by never collecting the full sequence on any single accelerator.
Blockwise Attention and Online Softmax Formulations
The algorithmic cornerstone of Ring Attention is the online normalizer calculation for softmax formulated by Milakov and Gimelshein, which allows an attention output to be computed incrementally over sequence blocks without storing intermediate attention weights.
Let a sequence of length be split into blocks of size . An accelerator holding query block receives a sequence of key blocks and value blocks for .
For each query token row, the attention block computation proceeds iteratively. Let . To maintain numerical stability without computing the global maximum beforehand, the algorithm tracks running statistics:
- Running Row Maximum: , tracking the maximum pre-softmax logit seen up to block step .
- Running Normalizer (Partition Function): , tracking the running sum of exponentiated logits.
- Unnormalized Running Output: .
When encountering the -th key-value block , the update rules are evaluated:
After iterating through all blocks (), the final exact attention output for query block is recovered by dividing by the accumulated normalizer:
This recurrence produces the identical mathematical result as standard full-sequence softmax attention, while requiring only active working memory on the accelerator.
The Ring Attention Protocol and Overlapped Communication
Ring Attention maps this blockwise recurrence onto a logical ring topology of devices. Each device is initialized with its local shard of input tokens:
- Query block: (stays resident on device across all steps)
- Key block: (rotates around the ring)
- Value block: (rotates around the ring)

The forward pass executes across discrete iterations:
- Step 0 (Local Initialization): Device computes blockwise attention between local and local , initializing running statistics . Simultaneously, device issues non-blocking asynchronous sends (
ncclSend) of to its downstream neighbor , while posting non-blocking asynchronous receives (ncclRecv) from upstream neighbor . - Step (): Device waits for the incoming key-value buffer from step . While computing the next blockwise attention update between resident and , the runtime simultaneously dispatches to device .
- Completion: After iterations, the rotating key-value buffers complete a full traversal of the ring and return to their origin device. Device applies final division by , yielding exact attention output .
Overlapping Arithmetic and Communication Ratios
The primary operational advantage of Ring Attention is the zero-bubble overlap between matrix multiplication and inter-device communication.
Consider a block of size tokens and hidden dimension on an accelerator with peak computation throughput (in FLOPs/s) and bidirectional ring interconnect bandwidth (in bytes/s):
- Computation Cost per Step: Computing takes FLOPs. Multiplying attention weights by takes another FLOPs. Total computation per step is FLOPs. Time required:
- Communication Volume per Step: Each device transfers one key block and one value block in FP16/BF16 (2 bytes per element). Total payload sent per step is bytes. Time required:
Communication is fully masked behind computation when :
The minimum block size required to achieve 100% communication overlap depends strictly on the arithmetic intensity ratio :
- On an NVIDIA H100 SXM (FP16 Tensor Core throughput , NVLink bidirectional ring bandwidth per direction):
For any per-device shard size tokens, the peer-to-peer transmission of key-value blocks is completely hidden behind the tensor contractions. The communication adds zero overhead to the model execution time.
Causal Masking Dynamics: Striped Attention and Zig-Zag Topologies
In non-causal encoder settings (such as bidirectional embeddings or vision transformers), every query block attends to every key-value block, ensuring uniform computational load across all steps on all devices.
In autoregressive (causal) language modeling, token can only attend to tokens where . If a sequence is partitioned sequentially (Device 0 holds tokens , Device 1 holds , etc.), a naive ring schedule leads to severe compute imbalance:
- Device 0 only attends to its own block; for the remaining steps, all incoming key-value blocks are fully masked out (), leaving Device 0 completely idle.
- Device must compute attention against all blocks.
- Overall cluster utilization drops by 50%, as half of the theoretical block interactions are masked.
Two distinct architectural solutions eliminate this causal imbalance:
1. Striped Attention (Interleaved Token Assignment)
Proposed by Brandon et al., Striped Attention permutes token assignment across devices in a round-robin interleaved fashion rather than contiguous chunks. Token index is assigned to device .
Under this permutation, every device holds a uniform distribution of prefix, middle, and suffix tokens. On every step of the ring exchange, each device processes an identical mix of valid and masked causal relations. Every device evaluates an upper-triangular causal mask within its local tiles, equalizing execution latency across all devices and recovering near-optimal compute efficiency without idle bubbles.
2. Zig-Zag Ring Attention (Block Reordering)
In Zig-Zag Ring Attention (adopted in modern sequence parallelism runtimes like DeepSpeed-Ulysses / USP), each device is assigned two half-blocks: one from the early sequence and one from the late sequence (for example, Device holds blocks and ).
By interleaving the forward rotation of the second half-block with the first, the total number of non-zero attention tiles per device across the ring traversal is strictly equalized to full block equivalents. Causal computation is balanced without requiring token-level scattering and gathering.
The Backward Pass and Gradient Exchange
During backpropagation, Ring Attention must compute gradients with respect to queries (), keys (), and values ().
Computing these gradients requires the attention probability matrix , which is recomputed on the fly using saved forward statistics via FlashAttention recomputation:
- Query Gradients (): Accumulate locally on device as incoming key-value blocks and output gradients circulate through the ring.
- Key and Value Gradients (): Gradients with respect to incoming blocks must either be accumulated locally into a temporary buffer and circulated in lockstep with , or output gradients can be circulated in reverse ring order while keys and values remain stationary.
In the standard reverse-ring implementation, and rotate in the same circular direction as during forward propagation, accumulating gradient contributions at each device before returning to their home device. Activation memory is bounded strictly to , allowing backpropagation through multi-million-token sequences without out-of-memory errors.
Ring Attention vs. Ulysses vs. Megatron Sequence Parallelism
Modern distributed long-context architectures select context parallelism paradigms based on cluster network hierarchy and model configurations:
- Megatron Sequence Parallelism (Megatron-SP): Splits non-attention operators (LayerNorm, MLP) along sequence length and executes
All-Gather/Reduce-Scatteraround attention. It is bounded by single-node tensor parallel groups and cannot extend context beyond memory limits of a single node. - DeepSpeed Ulysses: Uses
All-to-Allcollective communication across devices to convert sequence parallelism to head parallelism. Ulysses offers high kernel efficiency because each device executes a single local FlashAttention kernel on a subset of heads. However, Ulysses requires (preventing scaling to large cluster sizes when using Grouped-Query Attention with few KV heads) and requires high all-to-all cross-sectional network bandwidth. - Ring Attention: Uses peer-to-peer non-blocking point-to-point ring transfers. It scales independently of head count ( can exceed by arbitrary factors), operates efficiently over lower-tier ring topologies across distributed nodes, and requires only memory per device.
- Unified Sequence Parallelism (USP): Hybridizes Ulysses within high-speed intra-node NVLink domains and Ring Attention across inter-node InfiniBand networks, optimizing both collective bandwidth and global scaling.
Practical Implementation Patterns
In PyTorch with NCCL, a minimal non-blocking Ring Attention step leverages asynchronous communication handles to overlap computation:
import torch
import torch.distributed as dist
def ring_attention_forward(
q_local: torch.Tensor, # Shape: (batch, seq_block, num_heads, head_dim)
k_local: torch.Tensor,
v_local: torch.Tensor,
ring_group: dist.ProcessGroup,
) -> torch.Tensor:
rank = dist.get_rank(ring_group)
world_size = dist.get_world_size(ring_group)
next_rank = (rank + 1) % world_size
prev_rank = (rank - 1 + world_size) % world_size
# Allocate double buffers for async P2P communication
k_curr, v_curr = k_local.clone(), v_local.clone()
k_recv = torch.empty_like(k_local)
v_recv = torch.empty_like(v_local)
# Initialize online softmax statistics
out_accum = torch.zeros_like(q_local)
m_i = torch.full((q_local.shape[0], q_local.shape[2], q_local.shape[1]), -float('inf'), device=q_local.device)
l_i = torch.zeros((q_local.shape[0], q_local.shape[2], q_local.shape[1]), device=q_local.device)
for step in range(world_size):
# Asynchronously dispatch current KV to next rank and receive from prev rank
reqs = []
if step < world_size - 1:
reqs.append(dist.isend(k_curr, dst=next_rank, group=ring_group))
reqs.append(dist.isend(v_curr, dst=next_rank, group=ring_group))
reqs.append(dist.irecv(k_recv, src=prev_rank, group=ring_group))
reqs.append(dist.irecv(v_recv, src=prev_rank, group=ring_group))
# Compute blockwise attention between resident Q and current KV block
# (Executed concurrently while NCCL transfers execute in background streams)
scale = 1.0 / (q_local.shape[-1] ** 0.5)
scores = torch.einsum("bqhd,bkhd->bhqk", q_local, k_curr) * scale
m_block = scores.max(dim=-1).values
m_next = torch.maximum(m_i, m_block)
p_block = torch.exp(scores - m_next.unsqueeze(-1))
alpha = torch.exp(m_i - m_next)
l_next = alpha * l_i + p_block.sum(dim=-1)
# Update running output accumulator
out_accum = out_accum * alpha.permute(0, 2, 1).unsqueeze(-1) + torch.einsum("bhqk,bkhd->bqhd", p_block, v_curr)
m_i, l_i = m_next, l_next
# Await communication completion before next iteration
for req in reqs:
req.wait()
k_curr, k_recv = k_recv, k_curr
v_curr, v_recv = v_recv, v_curr
# Final normalizer division
final_output = out_accum / l_i.permute(0, 2, 1).unsqueeze(-1)
return final_outputArchitectural Trade-Offs and Scaling Limits
While Ring Attention enables theoretically infinite context scaling as additional GPU nodes are introduced, production deployments must account for several structural trade-offs:
- Kernel Launch Overhead on Fine Block Partitions: As the ring degree grows large for a fixed context length , block size decreases. When drops below the threshold required to saturate GPU Tensor Cores (), GEMM computation time drops below kernel launch latency, destroying the computation-communication overlap.
- Gradient Synchronization in Backpropagation: Backpropagation requires maintaining accurate causal masks and executing reverse rotations for gradient tensors. If network jitter occurs on any single node in the ring, the synchronous ring pipeline stalls across all devices.
- Serving KV Cache Fragmentation: In autoregressive decoding, new tokens arrive sequentially. Using Ring Attention for single-token prefill or step-by-step decoding introduces high inter-GPU coordination latency. Consequently, production serving engines generally utilize Ring Attention exclusively for chunked long-context prefill, transitioning to paged attention mechanisms during single-token decoding phases.
By reformulating attention as a distributed blockwise recurrence overlapped with circular communication, Ring Attention provides the foundational systems architecture enabling frontier language models to train and reason across million-token sequences.
Sources
- Ring Attention with Blockwise Transformers for Near-Infinite Context (Liu, Zaharia, Abbeel, 2023)
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Dao et al., 2022)
- Online Normalizer Calculation for Softmax (Milakov & Gimelshein, 2018)
- Striped Attention: Faster Ring Attention for Causal Transformers (Brandon et al., 2023)
- USP: A Unified Sequence Parallelism Approach for Long Context Generative AI (Fang et al., 2024)
- DeepSpeed Ulysses: System Optimizations for Enabling Training of Extreme Long Sequences (Jacob et al., 2023)



