Ring Attention with Blockwise Transformers: Mathematical Foundations, Circular KV Passing, Overlapped Communication-Computation, and Linear Context Scaling

Scaling the context length of transformer-based large language models has historically collided with two fundamental walls: the $O(S^2)$ 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 with

10 min
Ring Attention with Blockwise Transformers: Mathematical Foundations, Circular KV Passing, Overlapped Communication-Computation, and Linear Context Scaling

Scaling the context length of transformer-based large language models has historically collided with two fundamental walls: the O(S2)O(S^2) 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 NN 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 NN 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 QRS×dQ \in \mathbb{R}^{S \times d}, key tensor KRS×dK \in \mathbb{R}^{S \times d}, and value tensor VRS×dV \in \mathbb{R}^{S \times d} to an output tensor ORS×dO \in \mathbb{R}^{S \times d}:

O=softmax(QKTd)VO = \text{softmax}\left(\frac{QK^T}{\sqrt{d}}\right)V

For a sequence of length S=1,000,000S = 1,000,000 tokens with hidden dimension d=8,192d = 8,192 in 16-bit precision, storing the raw activation matrices Q,K,VQ, K, V requires 3×106×8192×2 bytes49.15 GB3 \times 10^6 \times 8192 \times 2 \text{ bytes} \approx 49.15\text{ GB}. The intermediate attention matrix A=QKTRS×SA = QK^T \in \mathbb{R}^{S \times S} would require 1012×2 bytes=2 TB10^{12} \times 2 \text{ bytes} = 2\text{ TB} of storage per attention layer, making single-device materialization physically impossible on modern 80GB or 141GB GPUs.

While FlashAttention reduces the memory complexity to O(Sd)O(S \cdot d) 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:

  1. Tensor Parallelism (TP): Partitions weights and heads across NN devices. When SS is massive, activation memory per device remains proportional to SS, and intra-layer All-Reduce over high-speed NVLink becomes a primary latency bottleneck.
  2. All-Gather Sequence Parallelism (DeepSpeed Ulysses): Partitions sequence dimension S/NS/N across devices, then executes an All-to-All collective 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 (NHN \le H) 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 SS be split into NN blocks of size B=S/NB = S / N. An accelerator holding query block QiRB×dQ_i \in \mathbb{R}^{B \times d} receives a sequence of key blocks KjRB×dK_j \in \mathbb{R}^{B \times d} and value blocks VjRB×dV_j \in \mathbb{R}^{B \times d} for j{0,1,,N1}j \in \{0, 1, \dots, N-1\}.

For each query token row, the attention block computation proceeds iteratively. Let Sij=QiKjTdRB×BS_{ij} = \frac{Q_i K_j^T}{\sqrt{d}} \in \mathbb{R}^{B \times B}. To maintain numerical stability without computing the global maximum beforehand, the algorithm tracks running statistics:

  1. Running Row Maximum: mi(k)RBm_i^{(k)} \in \mathbb{R}^B, tracking the maximum pre-softmax logit seen up to block step kk.
  2. Running Normalizer (Partition Function): li(k)RBl_i^{(k)} \in \mathbb{R}^B, tracking the running sum of exponentiated logits.
  3. Unnormalized Running Output: Oi(k)RB×dO_i^{(k)} \in \mathbb{R}^{B \times d}.

When encountering the kk-th key-value block (K(k),V(k))(K_{(k)}, V_{(k)}), the update rules are evaluated:

m~i(k)=rowmax(Si,(k))\tilde{m}_i^{(k)} = \text{rowmax}(S_{i,(k)})

mi(k)=max(mi(k1),m~i(k))m_i^{(k)} = \max(m_i^{(k-1)}, \tilde{m}_i^{(k)})

Pi,(k)=exp(Si,(k)mi(k))P_{i,(k)} = \exp(S_{i,(k)} - m_i^{(k)})

li(k)=exp(mi(k1)mi(k))li(k1)+rowsum(Pi,(k))l_i^{(k)} = \exp(m_i^{(k-1)} - m_i^{(k)}) \odot l_i^{(k-1)} + \text{rowsum}(P_{i,(k)})

Oi(k)=diag(exp(mi(k1)mi(k)))Oi(k1)+Pi,(k)V(k)O_i^{(k)} = \text{diag}\left(\exp(m_i^{(k-1)} - m_i^{(k)})\right) O_i^{(k-1)} + P_{i,(k)} V_{(k)}

After iterating through all NN blocks (k=0,,N1k = 0, \dots, N-1), the final exact attention output for query block QiQ_i is recovered by dividing by the accumulated normalizer:

Oi=diag(li(N1))1Oi(N1)O_i^* = \text{diag}\left(l_i^{(N-1)}\right)^{-1} O_i^{(N-1)}

This recurrence produces the identical mathematical result as standard full-sequence softmax attention, while requiring only O(Bd)O(B \cdot d) 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 NN devices. Each device p{0,1,,N1}p \in \{0, 1, \dots, N-1\} is initialized with its local shard of input tokens:

  • Query block: QpRB×dQ_p \in \mathbb{R}^{B \times d} (stays resident on device pp across all NN steps)
  • Key block: KpRB×dK_p \in \mathbb{R}^{B \times d} (rotates around the ring)
  • Value block: VpRB×dV_p \in \mathbb{R}^{B \times d} (rotates around the ring)
Ring Attention Systems Architecture

The forward pass executes across NN discrete iterations:

  1. Step 0 (Local Initialization): Device pp computes blockwise attention between local QpQ_p and local (Kp,Vp)(K_p, V_p), initializing running statistics (mp(0),lp(0),Op(0))(m_p^{(0)}, l_p^{(0)}, O_p^{(0)}). Simultaneously, device pp issues non-blocking asynchronous sends (ncclSend) of (Kp,Vp)(K_p, V_p) to its downstream neighbor (p+1)(modN)(p + 1) \pmod N, while posting non-blocking asynchronous receives (ncclRecv) from upstream neighbor (p1)(modN)(p - 1) \pmod N.
  2. Step kk (k=1,,N1k = 1, \dots, N-1): Device pp waits for the incoming key-value buffer (Krecv,Vrecv)(K_{recv}, V_{recv}) from step k1k-1. While computing the next blockwise attention update between resident QpQ_p and (Krecv,Vrecv)(K_{recv}, V_{recv}), the runtime simultaneously dispatches (Krecv,Vrecv)(K_{recv}, V_{recv}) to device (p+1)(modN)(p + 1) \pmod N.
  3. Completion: After NN iterations, the rotating key-value buffers complete a full 360360^\circ traversal of the ring and return to their origin device. Device pp applies final division by lp(N1)l_p^{(N-1)}, yielding exact attention output OpO_p.

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 BB tokens and hidden dimension dd on an accelerator with peak computation throughput FF (in FLOPs/s) and bidirectional ring interconnect bandwidth WW (in bytes/s):

  • Computation Cost per Step: Computing QiKjTQ_i K_j^T takes 2B2d2 B^2 d FLOPs. Multiplying attention weights by VjV_j takes another 2B2d2 B^2 d FLOPs. Total computation per step is 4B2d4 B^2 d FLOPs. Time required:

Tcomp=4B2dFT_{\text{comp}} = \frac{4 B^2 d}{F}

  • Communication Volume per Step: Each device transfers one key block KRB×dK \in \mathbb{R}^{B \times d} and one value block VRB×dV \in \mathbb{R}^{B \times d} in FP16/BF16 (2 bytes per element). Total payload sent per step is 2×(2Bd)=4Bd2 \times (2 B d) = 4 B d bytes. Time required:

Tcomm=4BdWT_{\text{comm}} = \frac{4 B d}{W}

Communication is fully masked behind computation when TcompTcommT_{\text{comp}} \ge T_{\text{comm}}:

4B2dF4BdW    BFW\frac{4 B^2 d}{F} \ge \frac{4 B d}{W} \implies B \ge \frac{F}{W}

The minimum block size BminB_{\text{min}} required to achieve 100% communication overlap depends strictly on the arithmetic intensity ratio F/WF/W:

  • On an NVIDIA H100 SXM (FP16 Tensor Core throughput F989 TFLOPsF \approx 989\text{ TFLOPs}, NVLink bidirectional ring bandwidth W450 GB/sW \approx 450\text{ GB/s} per direction):

Bmin989×1012450×1092,197 tokensB_{\text{min}} \approx \frac{989 \times 10^{12}}{450 \times 10^9} \approx 2,197\text{ tokens}

For any per-device shard size B2,048B \ge 2,048 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 NN steps on all devices.

In autoregressive (causal) language modeling, token tit_i can only attend to tokens tjt_j where jij \le i. If a sequence is partitioned sequentially (Device 0 holds tokens [0,B1][0, B-1], Device 1 holds [B,2B1][B, 2B-1], etc.), a naive ring schedule leads to severe compute imbalance:

  • Device 0 only attends to its own block; for the remaining N1N-1 steps, all incoming key-value blocks are fully masked out (j>ij > i), leaving Device 0 completely idle.
  • Device N1N-1 must compute attention against all NN 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 tt is assigned to device p=t(modN)p = t \pmod N.

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 pp holds blocks pp and 2N1p2N - 1 - p).

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 N/2N/2 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 (Q\nabla Q), keys (K\nabla K), and values (V\nabla V).

Computing these gradients requires the attention probability matrix PijP_{ij}, which is recomputed on the fly using saved forward statistics (mi,li)(m_i, l_i) via FlashAttention recomputation:

  1. Query Gradients (Qi\nabla Q_i): Accumulate locally on device pp as incoming key-value blocks (Kj,Vj)(K_j, V_j) and output gradients Oi\nabla O_i circulate through the ring.
  2. Key and Value Gradients (Kj,Vj\nabla K_j, \nabla V_j): Gradients with respect to incoming blocks must either be accumulated locally into a temporary buffer and circulated in lockstep with (Kj,Vj)(K_j, V_j), or output gradients Oi\nabla O_i can be circulated in reverse ring order while keys and values remain stationary.

In the standard reverse-ring implementation, K\nabla K and V\nabla V 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 O(Bd)O(B \cdot d), 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-Scatter around 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-All collective communication across NN 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 NHheadsN \le H_{\text{heads}} (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 (NN can exceed HheadsH_{\text{heads}} by arbitrary factors), operates efficiently over lower-tier ring topologies across distributed nodes, and requires only O(S/N)O(S/N) 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_output

Architectural 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:

  1. Kernel Launch Overhead on Fine Block Partitions: As the ring degree NN grows large for a fixed context length SS, block size B=S/NB = S/N decreases. When BB drops below the threshold required to saturate GPU Tensor Cores (B<1,024B < 1,024), GEMM computation time drops below kernel launch latency, destroying the computation-communication overlap.
  2. 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 NN devices.
  3. 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

Written by

More to read