All courses

Sharded data parallelism (ZeRO / FSDP)

Shard optimizer states, gradients and parameters across data-parallel workers, gathering parameters just in time. Compute the resulting memory per GPU and the extra communication.

Builds on: Data parallelism, GPU memory for training

▶ Watch this lesson Start the whole course

What the animation says

Every GPU holds everything

Training with Adam in mixed precision costs about sixteen bytes for every parameter. Two for the bf16 weights, two for gradients, four for an fp32 master copy, and eight for Adam's two moments. A seven-billion-parameter model needs a hundred and twelve gigabytes. An eighty-gigabyte GPU can't hold it. Plain data parallelism gives every GPU its own identical copy of all of it. So go from eight GPUs to sixteen. What happens to the memory on each one?

Each GPU owns a slice

It stays at a hundred and twelve. More GPUs just means more identical copies. The fix: cut the parameters into slices, one per GPU. Each GPU keeps the master copy, moments and gradient only for its own slice. First, a reduce-scatter. Each GPU receives the summed gradient for its slice, and nothing else. Last, an all-gather shares the updated bf16 weights, so everyone has the full model again. One step is missing in between. Drag the Adam update to where it belongs.

Same math, sliced

Why is splitting safe? Adam is elementwise. Each number is updated using only its own gradient and moments. Here, four simulated GPUs each have their own gradients. This line does the plain update on the averaged whole. Now the sharded way. Each slice averages only its own columns. That's the reduce-scatter, then dividing by four. Each slice steps on its own, and the concatenate plays the role of the all-gather. The check prints True. Same result, just split across GPUs.

ZeRO-1 and ZeRO-2: a floor

Sharding comes in stages. ZeRO-1 shards the twelve bytes of master copy and moments. Four bytes per parameter stay whole, plus twelve split eight ways. Twenty-eight plus ten and a half: thirty-eight and a half gigabytes. ZeRO-2 also shards gradients. Two bytes stay whole, fourteen get split. Fourteen plus twelve and a quarter: twenty-six and a quarter. Notice the whole part never shrinks, however many GPUs you add. In ZeRO-2, tap the block every GPU still stores in full.

ZeRO-3 / FSDP: shard everything

Full sharding, called ZeRO-3, or FSDP in PyTorch, splits the weights too. All sixteen bytes get divided. Seven billion times sixteen bytes, over eight GPUs: fourteen gigabytes each. But a layer needs its full weights to run. So each GPU gathers a layer just before using it, then frees it. Activations and that one gathered layer come on top of the fourteen. Plain data parallelism sends about two model-sizes per step. What does ZeRO-3 send?

Paying in traffic

About three model-sizes: one and a half times plain data parallelism. An all-reduce is a reduce-scatter plus an all-gather: two model-sizes. DP, ZeRO-1 and ZeRO-2 all pay that, about twenty-eight gigabytes here. ZeRO-3 gathers weights for forward, gathers again for backward, then reduce-scatters gradients. Three, about forty-two gigabytes. Prefetching the next layer's weights hides much of this behind compute. ZeRO-1 and two: less memory, same traffic, but a floor. ZeRO-3: no floor, for one and a half times the traffic.

Why does every GPU store the same optimizer?

Plain data parallelism keeps all ~16 bytes/parameter on **every** GPU. A 7B model needs $16 \times 7\text{B} = 112$ GB per GPU before activations. That exceeds an 80 GB GPU, and adding more GPUs never helps, because each one holds an identical copy of the same Adam moments and fp32 master copy. Can we split those copies so each GPU stores only a slice, keep the math identical, and avoid drowning in communication?

Each GPU owns a slice of the parameters

Split the parameters into $G$ slices, one per GPU. Each GPU stores the fp32 master copy, the moments and the gradient **only for its slice**. The step works like this: 1. **Reduce-scatter** the gradients, so each GPU receives the summed gradient for its own slice. 2. Run Adam on that slice. Adam is elementwise, so the result is unchanged. 3. **All-gather** the updated bf16 weights. This is ZeRO (Zero Redundancy Optimizer). Full sharding (ZeRO-3, or PyTorch's FSDP) goes further and also drops the bf16 weights. It all-gathers each layer's weights just before using them, then frees them.

Memory and traffic per GPU, by stage

Let $P$ be the total parameter count and $G$ the number of GPUs. Mixed-precision Adam stores 16 bytes/param: - 2 for bf16 weights - 2 for bf16 gradients - 4 for the fp32 master copy - 8 for the two fp32 moments Model-state bytes per GPU: - Plain DP: $16P$ - ZeRO-1 (shard master copy and moments, 12 bytes): $4P + 12P/G$ - ZeRO-2 (also shard gradients): $2P + 14P/G$ - ZeRO-3 / FSDP (also shard weights): $16P/G$ Activations are added on top, plus the bf16 weights of whichever layers are currently gathered. Let $\Phi = 2P$ bytes (the bf16 model size). Traffic per GPU per step: - DP, ZeRO-1 and ZeRO-2: about $2\Phi$ (an all-reduce is a reduce-scatter plus an all-gather). - ZeRO-3: about $3\Phi$ (gather for forward, gather again for backward, then reduce-scatter). Each of these collectives moves $(G-1)/G$ of the tensor per GPU. That is close to 1, so $2\Phi$ and $3\Phi$ are slight overestimates.

A 7B model on 8 GPUs

Take $P = 7$B and $G = 8$. - DP: $16 \cdot 7 = 112$ GB. Does not fit. - ZeRO-1: $4 \cdot 7 + 12 \cdot 7/8 = 28 + 10.5 = 38.5$ GB - ZeRO-2: $2 \cdot 7 + 14 \cdot 7/8 = 14 + 12.25 = 26.25$ GB - ZeRO-3: $112/8 = 14$ GB Traffic, with $\Phi = 14$ GB: - DP, ZeRO-1 and ZeRO-2: about 28 GB sent per GPU per step (exactly $2 \cdot 14 \cdot 7/8 = 24.5$ GB). - ZeRO-3: about 42 GB (exactly $3 \cdot 14 \cdot 7/8 = 36.75$ GB). Prefetching the next layer's weights hides much of this traffic behind compute.

Sharded update equals the full update

This simulates 4 GPUs. Each GPU averages only its slice of the gradients: reduce-scatter sums them, and dividing by $G$ gives the mean. It then applies an elementwise, Adam-like update to that slice. Concatenating the slices plays the role of all-gather. The result matches the unsharded update exactly.
import numpy as np
G, P = 4, 8
w = np.random.randn(P)
grads = np.random.randn(G, P)          # each GPU's local gradient
upd = lambda w, g: w - 0.1 * g / (np.abs(g) + 1e-8)  # elementwise step
full = upd(w, grads.mean(0))           # plain DP: all-reduce then step
slices = np.split(np.arange(P), G)     # GPU i owns slice i
new = np.concatenate([upd(w[s], grads[:, s].mean(0))  # reduce-scatter + local step
                      for s in slices])            # concat = all-gather
print(np.allclose(full, new))          # True

Remember

Sharding spreads the per-parameter state across $G$ GPUs. - ZeRO-1 and ZeRO-2 cut memory at **no extra communication**, but they leave a floor that never shrinks: $4P$ or $2P$ bytes per GPU. - ZeRO-3/FSDP gets memory down to $16P/G$. It gathers each layer's weights just in time and pays about 1.5× the traffic of plain data parallelism.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.