Data, Tensor & Pipeline Parallelism
Sharded optimizer states, tensor-parallel layers and pipeline bubbles.
Splitting a model across thousands of GPUs is a sequence of trade-offs that can be computed in advance: bytes on the wire against bytes in memory, idle time against stored activations, fast links inside a server against slow ones between servers. This lesson derives the costs of the standard collectives and of each kind of parallelism, so that a training configuration becomes arithmetic rather than folklore.
What you’ll learn
- Derive the ring all-reduce and its cost times the message size per device, and explain how gradient communication overlaps the backward pass.
- Derive ZeRO's memory per device at each stage and its communication volume, and relate stage 3 to fully sharded data parallelism.
- Compute the communication of tensor parallelism per token and explain why it stays inside a server; explain why sequence parallelism adds no communication.
- Derive the pipeline bubble for GPipe, 1F1B and interleaved schedules, describe zero-bubble scheduling, and compose data, tensor and pipeline parallelism.
Collectives and the ring all-reduce
In data parallelism each of GPUs holds a replica, processes its share of the batch and then needs the average gradient. That is an all-reduce: every device starts with a vector of size and ends with the elementwise sum of all vectors. Two simpler collectives build it: a reduce-scatter leaves each device with the sum of one slice, and an all-gather gives every device all the slices.
Arrange the devices in a ring and cut each vector into chunks of size . In each of steps, every device sends one chunk to its right-hand neighbor and adds the chunk it receives into its own copy. After steps, each device holds one chunk summed over all devices: a reduce-scatter.
In another steps the completed chunks travel around the ring: an all-gather. In total each device sends, and receives,
With latency per message and bandwidth bytes/s, the time is . The bandwidth term is nearly independent of and optimal: some device must send at least (Patarasuk & Yuan, 2009). The latency term grows with , which is why large clusters use hierarchical or tree-based variants.
The all-reduce need not wait for the backward pass to finish. Gradients of the last layers are ready first, so frameworks group them into buckets and start reducing each bucket while earlier layers are still being differentiated; with enough computation per byte, data parallelism's communication is almost entirely hidden. What it cannot hide is memory: every replica holds the full bytes of mixed-precision Adam state for parameters.
ZeRO and fully sharded data parallelism
Plain data parallelism replicates the 16-bit parameters (), 16-bit gradients () and optimizer state ( with for Adam: FP32 master weights, momentum, variance):
Each device updates only its share of the parameters, so it needs only that share of the optimizer state (stage 1, ) and of the reduced gradients (stage 2, ); stage 3 () shards the parameters too:
For and (Rajbhandari et al., 2020): 120 GB, 31.4 GB, 16.6 GB and 1.88 GB per device. Stages 1 and 2 level off at and as grows; only stage 3 keeps falling.
Communication. Plain data parallelism all-reduces gradients: about elements per device per step. Stages 1 and 2 replace the all-reduce by its two halves, a reduce-scatter of gradients () and an all-gather of the updated parameters (): the same . Stage 3 must all-gather each layer's parameters before its forward pass and again before its backward pass, then reduce-scatter the gradients: , 1.5 times as much, in exchange for memory that falls as .
PyTorch's fully sharded data parallel implements this stage-3 design: each layer's parameters are gathered just before use and freed after, with the gathers prefetched so that communication overlaps computation (Zhao et al., 2023). Sharding can be confined to a group of devices, typically one server, with replication across groups, trading memory for cheaper traffic. Variants offload optimizer state to CPU memory or NVMe when GPU memory is the binding constraint.
Which ZeRO stage communicates more data per step than plain data parallelism, and why?
- Stage 1, because the optimizer states must be gathered every step
- Stage 2, because gradients are no longer all-reduced
- None: all stages move exactly the same amount
- Stage 3, because each layer's parameters are gathered twice, for the forward and the backward pass, before the gradients are reduce-scattered: instead of
Show the answer
D. Stage 3, because each layer's parameters are gathered twice, for the forward and the backward pass, before the gradients are reduce-scattered: instead of
Stages 1 and 2 split the all-reduce into a reduce-scatter and an all-gather of parameters, which together cost the same . Only stage 3, which keeps no full copy of the parameters, must fetch them for both passes.
Tensor and sequence parallelism
Tensor parallelism over devices splits every weight matrix: the MLP's first matrix by columns and the second by rows, attention by heads, so each block needs one all-reduce of its output in the forward pass and one of its input gradient in the backward pass (Shoeybi et al., 2019). Unlike data parallelism, these all-reduces carry activations, sit on the critical path between matrix products, and recur for every micro-batch in every layer.
Per token and layer, four all-reduces (two forward, two backward) each move a 16-bit vector of size ; a ring all-reduce over devices sends times that from each device:
Each device does of the layer's training FLOPs per token, so the ratio of communication to computation is
For and that is bytes per FLOP. A GPU computing at FLOP/s then needs about 19 GB/s of all-reduce traffic that the computation cannot hide: a small fraction of the hundreds of GB/s inside a server, comparable to the tens of GB/s between servers. Hence the rule: tensor parallelism up to the number of GPUs in a server, and pipeline parallelism across servers (Narayanan et al., 2021). Wider models (larger ) tolerate it better.
Sequence parallelism. Tensor parallelism leaves layer normalization and dropout replicated: every device stores their full activations. Korthikanti et al. (2023) split those regions along the sequence instead. An all-reduce is a reduce-scatter followed by an all-gather, so replacing each all-reduce by a reduce-scatter into the sequence-split region and an all-gather out of it costs no extra communication, while the per-layer activation memory drops from to bytes. With selective recomputation of the attention scores it falls to .
Context parallelism splits the attention computation along the sequence for very long inputs: each device keeps a block of queries while blocks of keys and values circulate around a ring, overlapped with blockwise attention (Liu, Zaharia & Abbeel, 2024). Expert parallelism places a mixture-of-experts layer's experts on different devices and routes each token to its experts and back: two all-to-all exchanges per layer in the forward pass and two in the backward pass (Mixture of Experts).
Pipelines and their bubbles
Split the layers into stages and the batch into micro-batches, each taking forward and backward per stage. In GPipe (Huang et al., 2019) the last stage starts its first forward pass after , and the first stage finishes its last backward pass after the last stage does, so one step takes
The idle fraction of each device, the bubble, is therefore
GPipe stores the activations of all micro-batches on every stage before backpropagating. 1F1B (PipeDream-Flush) starts each micro-batch's backward pass as soon as possible, so stage holds at most micro-batches; the bubble is unchanged. Interleaving gives each device non-contiguous chunks of layers, so a micro-batch passes each device times in pieces times smaller; the bubble relative to the ideal time falls to , at the price of times more point-to-point communication (Narayanan et al., 2021).
A GPipe or 1F1B pipeline has stages and micro-batches. What fraction of the time is each GPU idle?
Show the answer
0.18
. Bigger batches split into more micro-batches shrink it, but the global batch is limited by optimization (the critical batch size) and each micro-batch must stay large enough to keep the GPU busy, so deep pipelines usually also need interleaving or a smarter schedule.
Zero-bubble schedules. The backward pass of a layer has two parts: the gradient with respect to its input, which the previous stage is waiting for, and the gradient with respect to its weights, which nobody waits for. Qi, Wan, Huang & Lin (2024) scheduled the two separately, using the weight-gradient work to fill the bubbles, and bypassed the synchronization at the optimizer step; their schedules beat 1F1B in throughput at similar memory while keeping synchronous training semantics.
Composing it all. Narayanan et al.'s trillion-parameter configuration used tensor parallelism 8 (one 8-GPU server), pipeline parallelism 64 across servers and data parallelism 6: 3,072 GPUs in all. Their guidance: tensor parallelism within a server, pipelines across servers until the model fits, and data parallelism (now usually sharded) for everything beyond.
Key takeaways
- A ring all-reduce is a reduce-scatter plus an all-gather; each device sends , which is bandwidth-optimal, and gradient buckets let it overlap the backward pass.
- ZeRO shards optimizer state, gradients and parameters in stages: bytes, with the same traffic as data parallelism for stages 1–2 and 1.5 times for stage 3 (FSDP).
- Tensor parallelism costs bytes per FLOP on the critical path, so it stays within a server; sequence parallelism shards the remaining activations at no extra communication.
- Pipelines idle a fraction ; 1F1B cuts stored activations, interleaving divides the bubble by , and zero-bubble schedules fill it with weight-gradient work.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Sixty-four GPUs all-reduce 2 GB of gradients with a ring. About how much does each GPU send?
- 2 GB
- About 3.9 GB
- 128 GB
- 31 MB
Show the answer
B. About 3.9 GB
GB: a reduce-scatter and an all-gather, each moving almost the whole message. The amount barely depends on , which is what makes the ring bandwidth-optimal.
With ZeRO stage 2 and mixed-precision Adam, what does the memory per device approach as the number of data-parallel devices grows?
- Zero
- bytes
- bytes
- bytes, the 16-bit parameters every device still keeps
Show the answer
D. bytes, the 16-bit parameters every device still keeps
Stage 2 memory is : the gradient and optimizer shards vanish as grows, but each device still holds all 16-bit parameters. Only stage 3 shards those too.
Why is tensor parallelism usually confined to the GPUs of one server?
- Its all-reduces carry activations at every layer and micro-batch, block the next matrix product, and need far more bandwidth than links between servers provide
- Because it needs the whole model on each GPU
- Because its communication volume grows with the number of parameters
- Because it cannot be combined with data parallelism
Show the answer
A. Its all-reduces carry activations at every layer and micro-batch, block the next matrix product, and need far more bandwidth than links between servers provide
Per token and layer it sends bytes for FLOPs, every micro-batch, on the critical path. At realistic throughput that needs tens of GB/s that cannot be hidden, cheap over in-server links and expensive between servers.
An interleaved schedule gives each of devices chunks of layers. What happens?
- The bubble is unchanged but memory falls by
- The bubble relative to the ideal time falls from to , and point-to-point communication grows -fold
- The bubble disappears entirely
- The number of micro-batches must be divided by
Show the answer
B. The bubble relative to the ideal time falls from to , and point-to-point communication grows -fold
Each chunk's forward and backward take as long, so the ramp-up and ramp-down shrink by ; but each micro-batch crosses device boundaries times as often.
Further reading
- S. Rajbhandari, J. Rasley, O. Ruwase & Y. He, "ZeRO: Memory optimizations toward training trillion parameter models," SC20 (2020).
- Y. Huang et al., "GPipe: Efficient training of giant neural networks using pipeline parallelism," NeurIPS (2019).
- D. Narayanan et al., "Efficient large-scale language model training on GPU clusters using Megatron-LM," SC21 (2021).
- V. Korthikanti et al., "Reducing activation recomputation in large transformer models," MLSys (2023).
- P. Qi, X. Wan, G. Huang & M. Lin, "Zero bubble (almost) pipeline parallelism," ICLR (2024).
- P. Patarasuk & X. Yuan, "Bandwidth optimal all-reduce algorithms for clusters of workstations," Journal of Parallel and Distributed Computing 69 (2009).