Search

Search papers and lessons

ZeRO: Memory optimizations Toward Training Trillion Parameter Models

Samyam Rajbhandari, Jeff Rasley, Olatunji Ruwase et al. · 2019

Rajbhandari and colleagues (2019) split the training state of a large model across data-parallel devices instead of copying it, so each GPU holds only a slice and can train models far bigger than its memory.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Calculus
  • Optimizers
  • Linear algebra
  • Probability
  • ReadReady to readNot yet

TL;DR

Training a large neural network needs far more memory than the network's weights alone. Most of that memory holds the optimizer's running averages, the gradients and a full copy of the weights, and standard data parallelism stores all of it again on every GPU. ZeRO (Zero Redundancy Optimizer), introduced by Samyam Rajbhandari and colleagues in 2019, cuts that copying by giving each GPU only a slice of the training state, fetching the rest when it is needed. The result is models of 100 billion parameters trained at high speed on 400 GPUs, and models of up to 13 billion parameters trained without splitting the network across devices.

Builds on: Model Parallelism, Activation Checkpointing, Adam Optimizer, Mixed-Precision Training · Leads to: the sharded training stack that later large-model systems use to scale data parallelism

Why it mattered

Many large-model systems that followed used the same idea of sharding the training state instead of copying it, including PyTorch's FSDP, which was inspired by ZeRO, and the DeepSpeed library that ships ZeRO itself. The paper's headline claim was a model about 8 times larger, and training about 10 times faster at the same size, than the best published system of its time, Megatron-LM. Microsoft used the same techniques to train Turing-NLG, a 17-billion-parameter language model that was, at the time, the largest published one.

History

By 2019 the largest models were roughly 10 billion parameters, trained by splitting layers across the GPUs of one server. Going beyond one node meant slow cross-node communication. Earlier work had compressed activations or offloaded state to CPU memory, but each trade cost speed or accuracy.

The authors, working at Microsoft, posted the paper to arXiv in October 2019 and built it into the open source DeepSpeed library. The paper reported Turing-NLG, a 17-billion-parameter language model, trained with these techniques. The paper stresses usability: data scientists could train models of up to 13 billion parameters without refactoring their models for model parallelism.

The problem

Before ZeRO, a team that wanted to train a model larger than one GPU's memory had two choices, and both were poor.

Data parallelism (see Data Parallelism) copies the whole model onto every GPU, gives each copy a different slice of the batch and averages the gradients. It is simple and communicates little, but every GPU holds a full copy of the model state, so it runs out of memory at around 1.4 billion parameters on 32 GB GPUs, as the paper measured for PyTorch's standard data parallelism.

Model parallelism cuts each layer across GPUs. It saves memory, but each layer now needs frequent communication between devices. The paper measured a 40-billion-parameter model across two nodes of 16 GPUs each and found about 5 teraflops per V100 GPU, less than 5 percent of the hardware's peak. The communication is cheap inside one node, where GPUs share a fast link, and expensive across nodes.

The paper's diagnosis is that the memory is spent in two places. Model states (the weights, the gradients and the optimizer's own numbers) take the most, and residual states (activations, temporary buffers and memory lost to fragmentation) take the rest. ZeRO has one set of techniques for each.

How it works

Take a tiny model with Ψ=1\Psi = 1 billion parameters, trained with Adam in mixed precision on Nd=4N_d = 4 GPUs. Each step below adds one idea to the last.

  1. Start with the standard cost. Each GPU holds the full training state: 2 bytes per parameter for the 16-bit weights, 2 bytes for the 16-bit gradients, and 12 bytes for the optimizer's 32-bit master weights, momentum and variance. That is 16 bytes per parameter, so 16 GB on every GPU. Adding GPUs does not reduce it.

  2. Shard the optimizer states (stage 1, PosP_{os}). Split the 12-byte optimizer part into 4 equal pieces, one per GPU. Each GPU now keeps only one quarter of the optimizer state and updates only the matching quarter of the weights. After each step the GPUs exchange the updated weights so everyone has the full set again. Memory per GPU drops to 4 GB for the weights and gradients plus 3 GB for its quarter of the optimizer, 7 GB in total.

  3. Shard the gradients too (stage 2, Pos+gP_{os+g}). A GPU only needs the averaged gradients for the weights it owns. So instead of averaging every gradient on every GPU, the gradients are sent to their owner as soon as each layer's backward pass produces them, and then freed. The gradient memory falls from 2 GB to 0.5 GB. Total: 2 GB of weights plus 3.5 GB for gradients and optimizer, 5.5 GB.

  4. Shard the parameters as well (stage 3, Pos+g+pP_{os+g+p}). Each GPU now stores only its quarter of the weights. Before a layer runs, the GPUs that own it broadcast its weights to the others, and the copies are dropped after use. Total: 16 bytes divided by 4, which is 4 GB per GPU.

  5. Check the communication cost. Standard data parallelism moves about 2 times the model size per step (a reduce followed by a gather). Stages 1 and 2 move the same amount. Stage 3 adds one extra gather, so it moves about 1.5 times as much.

flowchart LR
    A["Standard data parallel: 16 bytes per parameter on every GPU"] --> B["Stage 1: shard optimizer states"]
    B --> C["Stage 2: also shard gradients"]
    C --> D["Stage 3: also shard parameters"]
    D --> E["Memory per GPU falls toward 16 divided by N_d bytes per parameter"]

The paper also has a second set of techniques, ZeRO-R, for the residual memory. Activation checkpointing (Activation Checkpointing) stores only some activations and recomputes the rest. ZeRO-R partitions those stored checkpoints across model-parallel GPUs, can move them to CPU memory for very large models, uses fixed-size communication buffers instead of buffers that grow with the model, and reorganises allocations so that short-lived and long-lived tensors do not interleave and fragment memory.

The math

Definition. Let Ψ\Psi be the number of parameters, NdN_d the number of data-parallel GPUs, and KK the number of bytes of optimizer state per parameter. For mixed-precision Adam, K=12K = 12. The memory each GPU needs for its model states, in bytes, is a sum of three parts: the 16-bit weights, the 16-bit gradients and the optimizer states.

Step 1 in symbols: the standard cost

Standard data parallelism stores everything on every GPU:

MDP=2Ψ+2Ψ+KΨ=16ΨbytesM_{\text{DP}} = 2\Psi + 2\Psi + K\Psi = 16\Psi \quad \text{bytes}

This does not depend on NdN_d. For Ψ=7.5×109\Psi = 7.5 \times 10^9 this is 120120 GB.

Step 2 in symbols: stage 1 shards the optimizer

The optimizer part shrinks from KΨK\Psi to KΨ/NdK\Psi / N_d; the other two parts are unchanged:

MPos=4Ψ+KΨNdM_{P_{os}} = 4\Psi + \frac{K\Psi}{N_d}

When NdN_d is large the last term vanishes, so the model-state memory approaches 4Ψ4\Psi bytes, a fourfold reduction. For Ψ=7.5×109\Psi = 7.5 \times 10^9, Nd=64N_d = 64 and K=12K = 12, this is 30+1.41≈31.430 + 1.41 \approx 31.4 GB.

Step 3 in symbols: stage 2 shards the gradients

The gradient part, 2Ψ2\Psi, also shrinks to 2Ψ/Nd2\Psi / N_d:

MPos+g=2Ψ+(2+K)ΨNdM_{P_{os+g}} = 2\Psi + \frac{(2+K)\Psi}{N_d}

With K=12K = 12 the sharded part is 14Ψ/Nd14\Psi / N_d. For the same model, 2Ψ=152\Psi = 15 GB and 14Ψ/64≈1.6414\Psi / 64 \approx 1.64 GB give 15+1.64≈16.615 + 1.64 \approx 16.6 GB.

Step 4 in symbols: stage 3 shards the parameters

Now every part is divided by NdN_d:

MPos+g+p=16ΨNdM_{P_{os+g+p}} = \frac{16\Psi}{N_d}

For the same model, 120/64=1.875120 / 64 = 1.875, about 1.9 GB. The reduction is linear in the number of GPUs, which is why the paper can say that ZeRO fits models of any size once there are enough devices.

Step 5 in symbols: communication

Let VV be the number of values sent per GPU per step. Standard data parallelism sends 2Ψ2\Psi (reduce-scatter plus all-gather). Stages 1 and 2 also send 2Ψ2\Psi. Stage 3 sends Ψ\Psi for the forward gather, Ψ\Psi for the backward gather and Ψ\Psi for the gradient reduce:

VPos+g+p=3Ψ=1.5×VDPV_{P_{os+g+p}} = 3\Psi = 1.5 \times V_{\text{DP}}

Worked examples

Example 1 (tiny, by hand). A model has Ψ=109\Psi = 10^9 parameters, Nd=4N_d = 4, K=12K = 12.

  1. Standard data parallel: 16×10916 \times 10^9 bytes, which is 16 GB per GPU.
  2. Stage 1: 4Ψ+KΨ/Nd=4+12/4=74\Psi + K\Psi / N_d = 4 + 12 / 4 = 7 GB.
  3. Stage 2: 2Ψ+14Ψ/Nd=2+14/4=5.52\Psi + 14\Psi / N_d = 2 + 14 / 4 = 5.5 GB.
  4. Stage 3: 16Ψ/Nd=16/4=416\Psi / N_d = 16 / 4 = 4 GB.
  5. Each stage gives less memory than the one before, and the last one is exactly one quarter of the first.

Example 2 (the paper's standard case). A 7.5-billion-parameter model on 64 GPUs.

  1. Standard: 16×7.5=12016 \times 7.5 = 120 GB.
  2. Stage 1: 4×7.5+12×7.5/64=30+1.41=31.44 \times 7.5 + 12 \times 7.5 / 64 = 30 + 1.41 = 31.4 GB.
  3. Stage 2: 2×7.5+14×7.5/64=15+1.64=16.62 \times 7.5 + 14 \times 7.5 / 64 = 15 + 1.64 = 16.6 GB.
  4. Stage 3: 16×7.5/64=1.87516 \times 7.5 / 64 = 1.875 GB, which the paper rounds to 1.88.
  5. The three stages reduce the memory by about 3.8, 7.2 and 64 times, respectively. Only the last one is limited by the number of GPUs.

Example 3 (realistic: a trillion-parameter model). A model with Ψ=1012\Psi = 10^{12} parameters on Nd=1024N_d = 1024 GPUs, mixed precision with Adam.

  1. The full state is 16×101216 \times 10^{12} bytes, which is 16 TB.
  2. Standard data parallelism needs 16 TB on each GPU, which no GPU can hold.
  3. Stage 3 needs 16×1012/1024≈15.616 \times 10^{12} / 1024 \approx 15.6 GB per GPU, which fits in a 32 GB V100.
  4. Activations and buffers still need memory, which is why the paper pairs this with ZeRO-R.
  5. The point is not speed. Stage 3 makes the model fit, and the 1.5 times communication cost is the price.

Try it. A 13-billion-parameter model is trained on 64 GPUs with stage 3. How much model-state memory does each GPU hold? Answer: 16×13/64=3.2516 \times 13 / 64 = 3.25 GB, a little over 3 GB. For comparison, stage 2 gives 2×13+14×13/64≈28.82 \times 13 + 14 \times 13 / 64 \approx 28.8 GB, which fits on a 32 GB GPU only with little room left for activations. That is the arithmetic behind the paper's 13-billion-parameter result without model parallelism.

Results

The paper reports measurements for the first two stages (ZeRO-100B, which is stage 2 plus ZeRO-R) and analysis for the third. The measured numbers are:

  • Size. With model parallelism, ZeRO-100B trains models of 170 billion parameters. Without any model parallelism it trains models of up to 13 billion parameters, more than the 11-billion-parameter T5 that was then the largest published model.
  • Speed. On a cluster of 400 V100 GPUs, a 100-billion-parameter model ran at more than 38 teraflops per GPU and more than 15 petaflops in total, about 10 times the speed of the best baseline at the same size.
  • Scaling. Between 64 and 400 GPUs the paper observed superlinear speedup, meaning doubling the GPUs more than doubled the throughput. The reason is that more shards free memory for larger per-GPU batches, which use the hardware better. The limits matter. Stage 3, the one that makes the trillion-parameter claim, was not run at that scale in the paper, so the trillion-parameter figure is an extrapolation from the memory model. The speed results are for one hardware generation and one framework. Speedup depends on batch size and network bandwidth, and the paper does not claim ZeRO makes any single GPU faster.

Common mistakes and tradeoffs

It is tempting to think ZeRO is a form of model parallelism. In fact it is still data parallelism: every GPU runs the whole model on its own slice of the batch. What changes is where the state lives between steps, so the model is never split by layer and the communication keeps the simple pattern of data parallelism.

It is tempting to think ZeRO saves memory by moving the state to CPU memory. In fact the core stages keep everything on the GPUs. In the paper, CPU offload applies only to activation checkpoints in ZeRO-R.

It is tempting to think more GPUs always mean faster training. In fact ZeRO saves memory, not time. Freed memory can allow bigger per-GPU batches, which is where the superlinear speedup comes from, but the gain depends on the batch and the network.

Use stage 1 or 2 when the model fits with room to spare, because they add no communication. Use stage 3 when memory is the binding limit, and accept the 1.5 times communication cost. Use model parallelism inside a node when activations, not model states, are the problem, and combine it with ZeRO across nodes.

Reading guide

Read the paper in this order, with the lessons it leans on:

  • Introduction. The problem and the two-part memory split. Needs Data Parallelism and Model Parallelism, and the idea of a Transformer language model.
  • Related work. Pipeline parallelism, activation memory and CPU offload. Skim it on a first read; it needs Activation Checkpointing.
  • Where the memory goes. The core accounting. Needs Mixed-Precision Training, Floating-Point Numbers and Adam Optimizer. Work the 16-bytes-per-parameter count yourself.
  • Insights and overview. The insights behind the optimizer-state sharding (ZeRO-DP) and the residual-memory techniques (ZeRO-R). Read it twice; the rest of the paper follows from it.
  • The three stages of ZeRO-DP. Needs Stochastic Gradient Descent for what the gradients are for, and Backpropagation for when each gradient becomes available.
  • ZeRO-R. Partitioned activation checkpoints, constant-size buffers and defragmentation. Needs GPU Memory Hierarchy for why moving data is costly.
  • Communication analysis. The 2 times and 1.5 times volumes. Needs no new lesson beyond the collectives named in the text (reduce-scatter and all-gather).
  • Results and outlook. The measured numbers and the open compute gap.

Recap

  • Training memory is dominated by model states: 16 bytes per parameter for mixed-precision Adam, which is 120 GB for a 7.5-billion-parameter model.
  • Data parallelism stores that state on every GPU. Model parallelism splits layers and pays in cross-device communication.
  • ZeRO shards optimizer states, then gradients, then parameters across the data-parallel GPUs, so per-GPU memory falls toward 16Ψ/Nd16\Psi / N_d.
  • Stages 1 and 2 cost no extra communication. Stage 3 costs 1.5 times as much.
  • ZeRO-R handles activations, buffers and fragmentation, the residual memory.

Check yourself

QuestionA model has Ψ=2×109\Psi = 2 \times 10^9 parameters, is trained with mixed-precision Adam and Nd=8N_d = 8. How much model-state memory does each GPU hold under stage 2, in GB?Show answer

Stage 2 gives 2Ψ+14Ψ/Nd=4+28/8=4+3.5=7.52\Psi + 14\Psi / N_d = 4 + 28 / 8 = 4 + 3.5 = 7.5 GB. The 2 GB of 16-bit weights stay on every GPU, and the 14 bytes per parameter of gradients and optimizer states are split eight ways.

QuestionWhy does stage 3 cost about 1.5 times the communication of standard data parallelism, while stage 1 costs the same?Show answer

Standard data parallelism sends about 2Ψ2\Psi per step, a reduce plus a gather. Stage 1 and stage 2 keep exactly that pattern: the gradient reduce becomes a scatter of size Ψ\Psi, and the weight gather is size Ψ\Psi. Stage 3 must also gather the weights before the forward pass and again before the backward pass, which adds one extra Ψ\Psi, for 3Ψ3\Psi in total.

QuestionA team wants to train a model that fits on one GPU with the standard setup but leaves no room for activations. Which ZeRO stage would you pick first, and why not stage 3 right away?Show answer

Stage 1 or 2 is the better first choice, because stages 1 and 2 remove most of the redundant optimizer and gradient memory with no extra communication. Stage 3 saves the most memory but adds 1.5 times the communication, which is only worth paying when the lighter stages are not enough.

QuestionWhy is a plain ZeRO stage 3 run on 1024 GPUs not enough, by itself, to train a trillion-parameter model in practice?Show answer

Stage 3 brings the model states to about 16 GB per GPU, but activations, temporary buffers and fragmentation still need memory and can run out in practice. The paper therefore pairs stage 3 with ZeRO-R's partitioned activation checkpoints, buffer sizing and defragmentation. Also, the paper's measured results cover stage 2 with ZeRO-R, so stage 3 at a trillion parameters is an analysis, not an experiment.

Sources

Read the full paper

Samyam Rajbhandari, Jeff Rasley, Olatunji Ruwase et al., 2019. arXiv:1910.02054