Skip to content
King Jin

29 July 2026

ZeRO: Splitting Training Memory Across GPUs

When I started looking at how big models are trained across many GPUs, one name kept coming up: ZeRO (Zero Redundancy Optimizer), from DeepSpeed. In one sentence: store things split up, gather them only when you need them, and let every GPU update only its own piece. It took me a while to see what exactly gets split and when, so today I will go through it step by step.

ZeRO-3 on one GPU: it permanently stores only shard A (with its master weights, m and v), gathers B, C, D from the other GPUs to compute the current layer, receives only its own gradient gA through Reduce-Scatter, updates A, and frees the borrowed shards.

The problem with plain data parallel

In standard DDP (Distributed Data Parallel), every GPU (each one is a rank):

  1. keeps a full, identical copy of the model state
  2. processes a different mini-batch
  3. runs forward and backward on its own
  4. averages gradients with every other GPU using All-Reduce
  5. applies the same optimizer update

With NN GPUs and local gradients g(r)g^{(r)}, the synced gradient is:

gˉ=1N∑r=0N−1g(r)\bar g=\frac{1}{N}\sum_{r=0}^{N-1}g^{(r)}

The catch is that the memory per GPU does not go down as you add GPUs. More GPUs give you more throughput, but a model that does not fit on one card still does not fit.

Why one parameter costs 16 bytes

The ZeRO paper uses the classic FP16 mixed precision + Adam setup. For every trainable parameter we keep:

StatePrecisionBytes
Working parametersFP162
GradientsFP162
Master weightsFP324
Adam first moment mmFP324
Adam second moment vvFP324
Total16

So with PP parameters, DDP needs 16P16P bytes on every GPU, and 12P12P of that is optimizer-related. The forward pass only uses 2 bytes per parameter, but stable updates need the FP32 master weights plus Adam's mm and vv. That is where most of the memory goes.

These are only model states. Activations, temporary buffers, communication buffers and memory fragmentation come on top.

The three stages

ZeRO removes the duplicates one type at a time. Each stage keeps everything from the stage before and splits one more thing:

ModeParametersGradientsOptimizer statesMemory per GPU
DDPcopiedcopiedcopied16P16P
ZeRO-1copiedcopiedsplit4P+12PN4P+\frac{12P}{N}
ZeRO-2copiedsplitsplit2P+14PN2P+\frac{14P}{N}
ZeRO-3splitsplitsplit16PN\frac{16P}{N}

(The paper also has ZeRO-R for the other memory: activation checkpointing, fixed-size buffers and defragmentation. ZeRO-1/2/3 usually only means the first part.)

ZeRO-1: split the optimizer states

Every GPU still has full FP16 parameters and gradients, but rank rr only keeps the FP32 master weights, mm and vv for its own slice θr\theta_r. After backward, each rank updates only its slice, then an All-Gather sends the new FP16 slices to everyone.

Think of it as: everyone still has a copy of the model and the gradients, but the biggest items are divided up.

ZeRO-2: also split the gradients

As soon as a group of gradients is ready during backward, a Reduce-Scatter averages them across GPUs and hands each rank only the slice it owns:

gˉr=1N∑i=0N−1gr(i)\bar g_r=\frac{1}{N}\sum_{i=0}^{N-1}g_r^{(i)}

The other gradient slices are freed. Each GPU still keeps the full FP16 model, so the model itself must still fit on one card.

ZeRO-3: split the parameters too

This is the stage I misunderstood the most. Now every GPU only keeps its own slice of everything. The full parameters of a layer only exist temporarily, while that layer is being computed. The picture in my head: normally each GPU keeps its own piece; when a layer is needed, everyone puts that layer together; when it is done, it is taken apart again.

The full loop for one step:

  1. Forward: before each layer, All-Gather that layer's parameters. Every GPU computes its own batch, then frees the borrowed slices.
  2. Loss: each GPU computes its own local loss.
  3. Backward: gather the layer's parameters again, compute local gradients, then Reduce-Scatter so each rank keeps only its averaged gradient slice.
  4. Update: each rank updates only its own slice with its own master weights, mm and vv. Nothing is collected on one central GPU.

All-Gather has no central rank. Every GPU sends its slice and receives everyone else's at the same time.

You trade bandwidth for memory

ZeRO does not make the model states disappear. It uses communication to bring slices in when needed and drop them afterwards.

  • Standard DDP moves about 2P2P elements per step (All-Reduce = Reduce-Scatter + All-Gather).
  • ZeRO-1 and ZeRO-2 reorder this into gradient Reduce-Scatter + parameter All-Gather, so they also move about 2P2P.
  • ZeRO-3 needs a gather in forward (PP), a gather in backward (PP) and a gradient Reduce-Scatter (PP), so about 3P3P, roughly 1.5× DDP.

So ZeRO-3 is not free memory. It replaces stored copies with more parameter gathers.

How the update works with FP16

With FP16 training, small gradients can underflow during backward, so we multiply the loss by a scale SS first, then divide it back out before the update:

g32=cast⁡FP32(gscaled16)Sg^{32}=\frac{\operatorname{cast}_{FP32}\left(g_{\text{scaled}}^{16}\right)}{S}

Adam then runs in FP32 on the master weights, and the result is cast back to FP16 for the next forward. Casting the FP16 gradient to FP32 does not bring back lost precision. The real point of the FP32 master weights is that tiny updates can add up there instead of being rounded away in FP16. (BF16 has FP32's range, so it usually does not need loss scaling. I wrote more about that in Floating-Point Precision in LLMs.)

A worked example: 2B parameters on 4 GPUs

ModeParametersGradientsOptimizer statesTotal per GPU
DDP4 GB4 GB24 GB32 GB
ZeRO-14 GB4 GB6 GB14 GB
ZeRO-24 GB1 GB6 GB11 GB
ZeRO-31 GB1 GB6 GB8 GB

These are model states only. They tell you how much is stored long-term, not whether training will run out of memory. For that you still have to add activations, temporary unsplit buffers and communication buffers.

References

OlderDecoding Strategies and Exposure Bias →