↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

One replica per rank

Data parallelism trains one model on more data per step than one device can process by giving every device, or rank, a full copy of the model and a different slice of the batch. Each rank runs forward and backward on its own micro-batch; before the optimizer step the ranks average their gradients, so every copy takes the same step and the copies never drift apart. PyTorch's DistributedDataParallel (DDP) is the standard implementation, and this page runs it for real: four ranks, each a CPU process, training GPT-2 (124M parameters) through the gloo backend.

The averaging is exact because the loss is a mean. With ranks that each see the same number of tokens, the loss of the whole batch is the mean of the ranks' losses, and since the gradient is linear,

The 4 replicas panel shows the split: each step takes 1,024 tokens of the Apollo program article from SQuAD, eight sequences of 128 tokens, and gives each rank two of them. Each rank's loss on its own two sequences differs; their mean is the loss one process computes on all eight.

Buckets

Backward produces gradients from the last layer to the first. Waiting for all of them before communicating would leave the network idle during backward and the processors idle during communication, so DDP copies the gradients into flat buckets and starts a bucket's all-reduce as soon as its last gradient is ready, while backward carries on with the earlier layers. By default the first bucket closes at 1 MiB, so the first all-reduce can start early, and the rest at 25 MiB; larger messages use the network better, smaller ones overlap sooner.

DDP needs the order in which gradients arrive, and it learns it. In the first step it does not bucket at all: every gradient goes into one bucket that is all-reduced after backward. During that step it records the order the gradients became ready, and before the second step it rebuilds the buckets in that order. The gradient buckets panel paints each bucket on GPT-2's parameters, laid out in model order one block per row; pick a step to see the single first bucket and the rebuilt ones. , the token embedding, is also GPT-2's output layer, so its gradient is complete only when backward reaches the embedding at the very end.

The all-reduce

An all-reduce leaves every rank with the sum of all ranks' tensors; DDP's default communication hook divides each rank's bucket by the number of ranks first, so the sum is the average. gloo, like NCCL, does it with a ring: the bucket is cut into chunks, and in steps every rank passes one chunk to its neighbour, which adds its own (a reduce-scatter, after which each rank holds one fully summed chunk); in more steps the summed chunks travel round the ring (an all-gather). For a message of bytes each rank sends

bytes, which Patarasuk and Yuan showed is optimal in bandwidth. The Backward overlaps the all-reduces panel is the recorded timeline of one step on all four ranks, on one clock: the backward of each block, and each bucket's all-reduce from the moment DDP issued it to the moment gloo finished it on its own threads. backward() returns only once every bucket is done, so the last bucket's all-reduce is the one that is not hidden behind computation. The times are from this Mac's CPU; on GPUs everything is much faster, but the pattern, buckets filling in backward order and all-reduces overlapping the backward of earlier layers, is the same.

What every rank holds

Data parallelism is simple because nothing is split, and that is also its cost: every rank holds a whole copy of every model state. In float32, GPT-2's weights take 4 bytes per parameter, its gradients 4 more, DDP's buckets another 4 (DDP copies gradients into them unless gradient_as_bucket_view=True makes the gradients views of the buckets), and AdamW's two running moments 8 more. The Per rank, measured panel counts the bytes of the tensors each rank really holds, plus the activations autograd saved for backward on its micro-batch.

Adding ranks makes the batch bigger, not the model: a model whose states do not fit on one device does not fit with DDP either. Removing this redundancy is what the ZeRO and FSDP tabs do.

The same step as one process

The recorder also runs each step in a single process on the whole batch and compares. The strip The averaged gradient shows the first values of one bucket on each rank before the all-reduce, their average after it, and the single process's gradient at the same place; the table beside it gives the largest difference over all weights and gradients after each step, and between the four replicas, which stay bit-for-bit identical.

The gradients agree to float rounding: the sums are the same, added in another order. The weights differ more after AdamW, which divides each gradient by the square root of its running second moment: where a gradient is nearly zero, so is that root, and the rounding difference is scaled up with it. The table's rows marked one process, micro-batches in turn measure how much of that is the split: one process alone, adding the four micro-batches' gradients one after another instead of the whole batch at once, ends at least as far from itself. The gap is the order of float32 sums, not data parallelism.

Where it came from

Early large-scale training used parameter servers that collected gradients and sent back weights. Goyal et al. (2017) showed that synchronous data parallelism with large batches trains ImageNet models in an hour when the learning rate is scaled with the batch, and Horovod (Sergeev and Del Balso, 2018) built data parallelism on the ring all-reduce instead of a parameter server. PyTorch's DDP paper (Li et al., 2020) describes the bucketing, the overlap of communication with backward and the bucket rebuilding this page records. The ZeRO tab shards the optimizer state, the gradients and the weights that DDP replicates; the FSDP tab does the same with PyTorch's own fully sharded data parallelism.

Papers

  1. PyTorch Distributed: Experiences on Accelerating Data Parallel Training Shen Li et al. · VLDB 2020 The design of PyTorch's DistributedDataParallel: gradient buckets, all-reduces overlapped with backward, and buckets rebuilt in the order gradients arrive.
  2. Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour Priya Goyal et al. · arXiv 2017 Synchronous data parallelism at scale, and why averaging the ranks' gradients equals training on their combined batch.
  3. Horovod: fast and easy distributed deep learning in TensorFlow Alexander Sergeev, Mike Del Balso · arXiv 2018 Data parallelism built on the ring all-reduce instead of a parameter server, the pattern DDP and this page use.
  4. Bandwidth optimal all-reduce algorithms for clusters of workstations Pitch Patarasuk, Xin Yuan · Journal of Parallel and Distributed Computing 2009 The ring all-reduce and its bandwidth-optimal cost, $2(N-1)/N$ of the message sent per rank.
  5. Decoupled Weight Decay Regularization Ilya Loshchilov, Frank Hutter · ICLR 2017 AdamW, the optimizer every replica runs here, whose two moments double the memory of the weights.
  6. Language Models are Unsupervised Multitask Learners Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever · 2019 GPT-2, the model this page trains on four ranks.

See also