Skip to content

5.3 Parallelism

ready≈ 14 hoursNeeds: 5.1
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Parallelism simulator)
  4. 4 Do
  5. 5 Check

Data parallelism. Every GPU holds the whole model and processes a slice of the batch, then the gradients are averaged with an all-reduce. In the ring algorithm each GPU sends about twice the gradient size, however many GPUs there are. Frameworks group gradients into buckets and start each bucket’s all-reduce as soon as its gradients are ready, overlapping communication with the rest of the backward pass (Li et al. 2020, §3).

Sharding the state. Data parallelism stores the full 16 bytes per parameter (5.1) on every GPU. ZeRO splits the optimizer state, then the gradients, then the weights across the data-parallel GPUs. Each GPU updates only its slice, and weights are gathered just before they are needed (Rajbhandari et al. 2020). PyTorch’s version is FSDP.

Tensor parallelism splits each weight matrix across GPUs. In the MLP, the first matrix is split by columns: each GPU computes its own block of hidden units and applies the nonlinearity locally. The second is split by rows, so the partial outputs only need to be summed, one all-reduce per block in each direction (Shoeybi et al. 2019, §3). It needs fast links, so it stays within one machine.

Pipeline parallelism gives each GPU a block of layers and streams microbatches through them. GPUs idle while the pipeline fills and drains: the bubble shrinks as the number of microbatches grows (Huang et al. 2019; Narayanan et al. 2021). Large runs combine all of these (How to Scale Your Model, part 5).

A tensor-parallel MLP needs one all-reducestep 1 of 5
  1. W1=[W1(1) ⋯ W1(p)],W2=[W2(1)⋮W2(p)]\mathbf{W}_1 = \big[\mathbf{W}_1^{(1)}\ \cdots\ \mathbf{W}_1^{(p)}\big],\qquad \mathbf{W}_2 = \begin{bmatrix}\mathbf{W}_2^{(1)}\\ \vdots\\ \mathbf{W}_2^{(p)}\end{bmatrix}

Parallelism simulator

Communication cost and pipeline bubbles

Static view on small screens. Open this page on a laptop to interact.

Start. Each worker holds its own gradient, split into one chunk per worker. Colours show whose data a chunk contains.

worker 1worker 2worker 3worker 4
elements sent by each worker so far 0 of 18 = 2(p − 1)n/p with n = 12 a naive scheme (everyone sends to worker 1, which sends the sum back) puts 72 elements through worker 1

Every worker sends and receives the same amount, about twice the gradient size however many workers there are, so the ring keeps every link busy and none becomes a bottleneck. After the reduce-scatter each worker owns one fully summed chunk; ZeRO stops there for the optimizer state it shards.

Three tabs. Ring all-reduce: a grid of workers by chunks, each chunk coloured by whose data it contains, stepped through the reduce-scatter and all-gather phases with the current sends outlined and a running count of elements sent. Data parallel + ZeRO: a bar of per-GPU memory (weights, gradients, optimizer state) against an 80 GB line, for a chosen model size, number of GPUs and ZeRO stage, with the communication per step. Pipeline: a GPipe schedule chart of forward and backward microbatches on each stage, with the idle bubble and its fraction. Keyboard: Tab to the tab buttons, then to the controls of each tab; press Enter to step.

First, simulate it in NumPy, with each worker an entry in a list: the ring all-reduce, data parallelism, DDP’s buckets, ZeRO’s memory, tensor parallelism and the pipeline bubble.

Ring all-reduce, DDP, ZeRO, tensor and pipeline parallelism stubs/m5_3_parallel.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m5_3_parallel.py and run mlc check 5.3. The tests are identical; import the results on the progress page.

Hints · ring_allreduce 0/3 revealed
Hints · tensor_parallel_mlp 0/3 revealed

Then run real DDP. Open notebooks/5.3-ddp.ipynb on Kaggle with GPU T4 x2. Write a naive DDP wrapper (broadcast the initial weights, all-reduce every gradient after the backward pass), then a bucketed one. The bucketed version uses your bucketize and gradient hooks to launch asynchronous all-reduces during the backward pass, as in CS336 Assignment 2. The script runs one process per GPU with torchrun and checks that both train exactly like one process on the full batch. Without GPUs it runs two CPU processes over the gloo backend instead.

1. In a ring all-reduce over p GPUs, how much does each GPU send?
2. ZeRO stage 2 shards…
3. Why does the Megatron MLP split W1 by columns and W2 by rows?
4. A 4-stage pipeline runs 4 microbatches per batch. What fraction of GPU time is idle (GPipe)?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.