Simple Algorithm
Deep Learning · Emergence · 28 min

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 2(p−1)/p2(p-1)/p 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 pp 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 MM and ends with the elementwise sum of all pp vectors. Two simpler collectives build it: a reduce-scatter leaves each device with the sum of one 1/p1/p slice, and an all-gather gives every device all the slices.

DerivationRing all-reduce
  1. reduce-scatter: (p−1)⋅Mp sent per device\text{reduce-scatter: }(p-1)\cdot\frac Mp\ \text{sent per device}

    Arrange the devices in a ring and cut each vector into pp chunks of size M/pM/p. In each of p−1p-1 steps, every device sends one chunk to its right-hand neighbor and adds the chunk it receives into its own copy. After p−1p-1 steps, each device holds one chunk summed over all devices: a reduce-scatter.

  2. 2 p−1p M → p→∞  2M2\,\frac{p-1}{p}\,M\ \xrightarrow{\ p\to\infty\ }\ 2M

    In another p−1p-1 steps the completed chunks travel around the ring: an all-gather. In total each device sends, and receives,

  3. Tring≈2(p−1) α+2MβT_{\text{ring}}\approx2(p-1)\,\alpha+\frac{2M}{\beta}

    With latency α\alpha per message and bandwidth β\beta bytes/s, the time is 2(p−1)(α+Mpβ)2(p-1)\big(\alpha+\tfrac{M}{p\beta}\big). The bandwidth term is nearly independent of pp and optimal: some device must send at least 2M(p−1)/p2M(p-1)/p (Patarasuk & Yuan, 2009). The latency term grows with pp, 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 16Ψ16\Psi bytes of mixed-precision Adam state for Ψ\Psi parameters.

ZeRO and fully sharded data parallelism

DerivationMemory per device with $N_d$-way data parallelism
  1. stage 0: (2+2+K) Ψ=16Ψ\text{stage 0: }(2+2+K)\,\Psi=16\Psi

    Plain data parallelism replicates the 16-bit parameters (2Ψ2\Psi), 16-bit gradients (2Ψ2\Psi) and optimizer state (KΨK\Psi with K=12K=12 for Adam: FP32 master weights, momentum, variance):

  2. stage 1: 4Ψ+KΨNd,stage 2: 2Ψ+(2+K)ΨNd,stage 3: (2+2+K)ΨNd\text{stage 1: }4\Psi+\frac{K\Psi}{N_d},\qquad\text{stage 2: }2\Psi+\frac{(2+K)\Psi}{N_d},\qquad\text{stage 3: }\frac{(2+2+K)\Psi}{N_d}

    Each device updates only its 1/Nd1/N_d share of the parameters, so it needs only that share of the optimizer state (stage 1, PosP_{os}) and of the reduced gradients (stage 2, Pos+gP_{os+g}); stage 3 (Pos+g+pP_{os+g+p}) shards the parameters too:

  3. For Ψ=7.5×109\Psi=7.5\times10^9 and Nd=64N_d=64 (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 4Ψ4\Psi and 2Ψ2\Psi as NdN_d grows; only stage 3 keeps falling.

Communication. Plain data parallelism all-reduces gradients: about 2Ψ2\Psi elements per device per step. Stages 1 and 2 replace the all-reduce by its two halves, a reduce-scatter of gradients (Ψ\Psi) and an all-gather of the updated parameters (Ψ\Psi): the same 2Ψ2\Psi. Stage 3 must all-gather each layer's parameters before its forward pass and again before its backward pass, then reduce-scatter the gradients: 3Ψ3\Psi, 1.5 times as much, in exchange for memory that falls as 1/Nd1/N_d.

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.

Check your understanding

Which ZeRO stage communicates more data per step than plain data parallelism, and why?

  1. Stage 1, because the optimizer states must be gathered every step
  2. Stage 2, because gradients are no longer all-reduced
  3. None: all stages move exactly the same amount
  4. Stage 3, because each layer's parameters are gathered twice, for the forward and the backward pass, before the gradients are reduce-scattered: 3Ψ3\Psi instead of 2Ψ2\Psi
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: 3Ψ3\Psi instead of 2Ψ2\Psi

Stages 1 and 2 split the all-reduce into a reduce-scatter and an all-gather of parameters, which together cost the same 2Ψ2\Psi. Only stage 3, which keeps no full copy of the parameters, must fetch them for both passes.

Tensor and sequence parallelism

Tensor parallelism over tt 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.

DerivationBytes per FLOP of tensor parallelism
  1. bytes sent per token per layer=4⋅2(t−1)t⋅2h=16h(t−1)t\text{bytes sent per token per layer}=4\cdot\frac{2(t-1)}{t}\cdot2h=\frac{16h(t-1)}{t}

    Per token and layer, four all-reduces (two forward, two backward) each move a 16-bit vector of size hh; a ring all-reduce over tt devices sends 2(t−1)/t2(t-1)/t times that from each device:

  2. 16h(t−1)/t72h2/t=2(t−1)9h  bytes per FLOP\frac{16h(t-1)/t}{72h^2/t}=\frac{2(t-1)}{9h}\ \ \text{bytes per FLOP}

    Each device does 1/t1/t of the layer's 6×12h26\times12h^2 training FLOPs per token, so the ratio of communication to computation is

  3. For h=12,288h=12{,}288 and t=8t=8 that is 1.3×10−41.3\times10^{-4} bytes per FLOP. A GPU computing at 1.5×10141.5\times10^{14} 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 hh) 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 sbh(10+24/t+5as/(ht))sbh(10+24/t+5as/(ht)) to sbh(34+5as/h)/tsbh(34+5as/h)/t bytes. With selective recomputation of the attention scores it falls to 34sbh/t34sbh/t.

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

DerivationThe bubble of a synchronous pipeline
  1. T=(m+p−1)(tf+tb),busy time per stage=m (tf+tb)T=(m+p-1)(t_f+t_b),\qquad\text{busy time per stage}=m\,(t_f+t_b)

    Split the layers into pp stages and the batch into mm micro-batches, each taking tft_f forward and tbt_b backward per stage. In GPipe (Huang et al., 2019) the last stage starts its first forward pass after (p−1)tf(p-1)t_f, and the first stage finishes its last backward pass (p−1)tb(p-1)t_b after the last stage does, so one step takes

  2. T−m(tf+tb)T=p−1m+p−1(or p−1m relative to the ideal time)\frac{T-m(t_f+t_b)}{T}=\frac{p-1}{m+p-1}\qquad\Big(\text{or }\frac{p-1}{m}\text{ relative to the ideal time}\Big)

    The idle fraction of each device, the bubble, is therefore

  3. interleaved: 1v⋅p−1m\text{interleaved: }\frac{1}{v}\cdot\frac{p-1}{m}

    GPipe stores the activations of all mm micro-batches on every stage before backpropagating. 1F1B (PipeDream-Flush) starts each micro-batch's backward pass as soon as possible, so stage kk holds at most p−k+1p-k+1 micro-batches; the bubble is unchanged. Interleaving gives each device vv non-contiguous chunks of layers, so a micro-batch passes each device vv times in pieces vv times smaller; the bubble relative to the ideal time falls to (p−1)/(vm)(p-1)/(vm), at the price of vv times more point-to-point communication (Narayanan et al., 2021).

Predict before you look

A GPipe or 1F1B pipeline has p=8p=8 stages and m=32m=32 micro-batches. What fraction of the time is each GPU idle?

Show the answer

0.18

(p−1)/(m+p−1)=7/39≈0.18(p-1)/(m+p-1)=7/39\approx0.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 2(p−1)M/p2(p-1)M/p, which is bandwidth-optimal, and gradient buckets let it overlap the backward pass.
  • ZeRO shards optimizer state, gradients and parameters in stages: 16Ψ→4Ψ+12Ψ/Nd→2Ψ+14Ψ/Nd→16Ψ/Nd16\Psi\to4\Psi+12\Psi/N_d\to2\Psi+14\Psi/N_d\to16\Psi/N_d bytes, with the same traffic as data parallelism for stages 1–2 and 1.5 times for stage 3 (FSDP).
  • Tensor parallelism costs 2(t−1)/(9h)2(t-1)/(9h) 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 (p−1)/(m+p−1)(p-1)/(m+p-1); 1F1B cuts stored activations, interleaving divides the bubble by vv, 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.

Question 1

Sixty-four GPUs all-reduce 2 GB of gradients with a ring. About how much does each GPU send?

  1. 2 GB
  2. About 3.9 GB
  3. 128 GB
  4. 31 MB
Show the answer

B. About 3.9 GB

2(p−1)/p×M=2×6364×2 GB≈3.942(p-1)/p\times M=2\times\tfrac{63}{64}\times2\ \text{GB}\approx3.94 GB: a reduce-scatter and an all-gather, each moving almost the whole message. The amount barely depends on pp, which is what makes the ring bandwidth-optimal.

Question 2

With ZeRO stage 2 and mixed-precision Adam, what does the memory per device approach as the number of data-parallel devices grows?

  1. Zero
  2. 4Ψ4\Psi bytes
  3. 16Ψ16\Psi bytes
  4. 2Ψ2\Psi bytes, the 16-bit parameters every device still keeps
Show the answer

D. 2Ψ2\Psi bytes, the 16-bit parameters every device still keeps

Stage 2 memory is 2Ψ+14Ψ/Nd2\Psi+14\Psi/N_d: the gradient and optimizer shards vanish as NdN_d grows, but each device still holds all 16-bit parameters. Only stage 3 shards those too.

Question 3

Why is tensor parallelism usually confined to the GPUs of one server?

  1. 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
  2. Because it needs the whole model on each GPU
  3. Because its communication volume grows with the number of parameters
  4. 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 16h(t−1)/t16h(t-1)/t bytes for 72h2/t72h^2/t 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.

Question 4

An interleaved schedule gives each of pp devices vv chunks of layers. What happens?

  1. The bubble is unchanged but memory falls by vv
  2. The bubble relative to the ideal time falls from (p−1)/m(p-1)/m to (p−1)/(vm)(p-1)/(vm), and point-to-point communication grows vv-fold
  3. The bubble disappears entirely
  4. The number of micro-batches must be divided by vv
Show the answer

B. The bubble relative to the ideal time falls from (p−1)/m(p-1)/m to (p−1)/(vm)(p-1)/(vm), and point-to-point communication grows vv-fold

Each chunk's forward and backward take 1/v1/v as long, so the ramp-up and ramp-down shrink by vv; but each micro-batch crosses device boundaries vv 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).