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
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.
Each GPU owns a slice of the parameters
Memory and traffic per GPU, by stage
A 7B model on 8 GPUs
Sharded update equals the full update
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
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.
Bytewise
All courses