Redundancy in data parallelism
Data parallelism (the DDP tab) keeps a full copy of every model state on every rank: the weights, their gradients and the optimizer's state. With Adam the optimizer state is the largest part, two running moments per weight. In the mixed-precision training the ZeRO paper considers, a model with parameters needs bytes of 16-bit weights, of 16-bit gradients and of float32 master weights and moments, bytes in all, on every one of ranks.
ZeRO, the Zero Redundancy Optimizer, removes the copies in three stages, each splitting one more state among the ranks so that each holds and updates only its own part:
This page trains GPT-2 in float32 with DeepSpeed, the library ZeRO was introduced in, at stages 0 to 3 on four CPU ranks, the same four steps each time. The What each rank holds panel measures the bytes every rank's tensors take after backward, when the gradients are still there, for the chosen stage, with the other stages' rank 0 below for comparison.
Stage 1: the optimizer state
At stage 1 each rank keeps AdamW's moments only for its own part of the weights. DeepSpeed lays every tensor end to end in one flat buffer, pads it to a multiple of the ranks, and cuts it into equal parts; the How DeepSpeed split the weights panel paints each rank's part on GPT-2's parameters, one block per row, and the block card below it shows where the cuts fall inside one block's matrices.
After backward every rank still needs the averaged gradient of its own part, and after its optimizer step the other ranks need its updated weights back, so DeepSpeed all-gathers the parts into the full flat buffer. DeepSpeed also keeps a float32 copy of each rank's part, the copy AdamW updates; in mixed precision that is the master copy of the 16-bit weights, and in this float32 run it is an extra quarter of the weights per rank.
Stage 2: the gradients
A rank that updates only its part needs only its part of the gradient. Stage 2 therefore keeps only the averaged gradient of the rank's own values, a reduce-scatter in principle: each rank receives the sum of one part from everyone. DeepSpeed reduces the gradients bucket by bucket during backward, as DDP does, and frees each bucket once its values are in the right rank's part. The The collectives of one step table lists the messages DeepSpeed hands to gloo at each stage, and the timeline above it shows when.
In this run the two stages behave the same. With one micro-batch per step every backward ends an accumulation step, and DeepSpeed then reduces stage 1's gradients bucket by bucket too, keeping only the rank's own part; both stages all-reduce each bucket rather than reduce-scatter it (DeepSpeed's use_multi_rank_bucket_allreduce, on by default). Stage 2's saving appears with gradient accumulation: stage 1 keeps the whole gradient until the last micro-batch, stage 2 reduces after every one.
Stage 3: the weights
Stage 3 splits the weights too. Every tensor is cut into four on its own, so that any module's weights can be put back together when they are needed: before a module runs forward its weights are all-gathered, after it they are released, and before its backward they are gathered again; its gradients are reduce-scattered after backward. Gathering the next module's weights while the current one computes, prefetching, hides part of the cost. Tensors under DeepSpeed's persistence threshold of values, GPT-2's biases and LayerNorms, are kept whole on every rank (hatched on the map), since gathering them would cost more than keeping them.
Two settings of this run differ from DeepSpeed's defaults: a reuse distance of 0, so a gathered weight is released right after use, and a prefetch bucket of one GPT-2 block. In the first step DeepSpeed has not yet recorded the order the modules run in, and it does what the paper describes: each module's weights are gathered just before its forward, released, and gathered again before its backward. From the second step it prefetches from the recorded order, one bucket more at every module, until it reaches its live-parameter cap, stage3_max_live_parameters, whose default of values is more than all of GPT-2. So every weight's all-gather is issued within the first part of forward, and since a weight prefetched for a later use is not released in between, backward gathers nothing: the timeline of steps 2 to 4 shows it. Once the order is recorded it is that cap, not the prefetch bucket, that bounds how many weights are gathered at once. The memory panel counts the bytes after backward, when the gathered weights are gone again. With float32 weights, stage 3 updates its part of the weights in place, without a separate float32 copy.
What it costs
The ZeRO paper counts communication in units of . Data parallelism's all-reduce costs about per rank (a reduce-scatter and an all-gather); stages 1 and 2 cost the same, a reduce-scatter of the gradients and an all-gather of the weights; stage 3 adds a second all-gather of the weights for backward, in all. The table on the page counts what DeepSpeed really sent in this run, and the bytes each rank sends for it in gloo's ring algorithms. Activations are the same at every stage: ZeRO splits model states, not the activations each rank keeps for its own micro-batch.
Sharding changes who stores what, not the arithmetic. The table under the page compares every stage's weights after each step with one process training on the whole batch: they differ by float32 summation order, less than one process differs from itself when it adds its micro-batches one after another, as on the DDP tab.
Where it came from
Rajbhandari et al. introduced ZeRO in 2019 with DeepSpeed and used it to train Turing-NLG, a 17-billion-parameter model. ZeRO-Offload (2021) moved optimizer state and updates to the CPU, ZeRO-Infinity (2021) to NVMe, and ZeRO++ (2023) cut stage 3's communication with quantized and hierarchical collectives. PyTorch's fully sharded data parallelism implements the same idea as stage 3; the FSDP tab records it unit by unit.
Papers
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models The three stages of the Zero Redundancy Optimizer, their memory and their communication, introduced with DeepSpeed.
- ZeRO-Offload: Democratizing Billion-Scale Model Training ZeRO stage 2 with the optimizer state and its update moved to the CPU.
- ZeRO-Infinity: Breaking the GPU Memory Wall for Extreme Scale Deep Learning ZeRO stage 3 with model states offloaded to CPU memory and NVMe.
- ZeRO++: Extremely Efficient Collective Communication for Giant Model Training Quantized weights and gradients and a secondary partition within a node, to cut stage 3's all-gathers and reduce-scatters.
- Mixed Precision Training 16-bit weights and gradients with a float32 master copy, the setting of ZeRO's 16 bytes per parameter.
- Adam: A Method for Stochastic Optimization The two running moments per weight that make the optimizer state the largest model state.