Data parallelism
Replicate the model on every GPU, give each a different shard of the batch, and all-reduce the gradients before the step. Explain why this keeps training mathematically identical to one large batch.
Builds on: The LLM training loop, Collective ops: all-reduce, all-gather, reduce-scatter
What the animation says
One run, eight GPUs
Your model fits on one GPU. But each training step should use a batch of 1024 sequences, and one GPU is too slow. You have eight GPUs. The goal is strict: the exact step one huge GPU would take. Same gradient, same parameters, after every step. Not just something close. So give every GPU a full copy of the model, and a different slice of the batch: 128 sequences each. Each GPU runs forward and backward on its own slice, all at the same time.
Four tokens, two GPUs
Shrink it so we can follow the numbers. Four tokens, with per-token gradients two, four, minus one, and three. One big GPU averages all four: eight over four, which is two. That's the target. Now split them. GPU one gets two and four; its own mean is three. GPU two gets minus one and three; its mean is one. An all-reduce adds up the GPUs' results and hands every GPU the sum: four. But the target is two. What turns this four into two?
Why it's exact
Divide by the number of GPUs: four over two is two. Exactly the full-batch gradient. The reason: with equal shards, the batch loss is just the average of the shard losses. And the gradient of an average is the average of the gradients. Each GPU gets its piece by ordinary backprop. Every copy starts with the same weights and applies the same averaged gradient. So the copies stay identical, up to tiny rounding. Only one kind of thing crosses between the GPUs each step. Tap it.
The trap: unequal shards
Now a trap. Same four tokens, but split unevenly: three on GPU one, one on GPU two. In real runs this happens with padding. Shards end up with different numbers of real tokens. GPU one's mean is five thirds. GPU two's mean is three. If we average those two means, do we still get the full-batch two?
The fix: divide by tokens
Averaging the means gives seven thirds, not two. The single token on GPU two weighs as much as three. The fix: each GPU sends the sum of its per-token gradients, plus its token count. All-reduce both. Five plus three is eight. Three plus one is four tokens. Eight over four: two again. With equal shards, this is the same as averaging the means and dividing by the number of GPUs.
Check it in code
Let's check it in code. This function is the gradient of a mean squared error over whatever rows it gets. This line is the one big GPU: the gradient over all eight rows at once. These lines split the rows into two equal shards, and each simulated GPU computes its own gradient. Then sum and divide by the GPU count. The check prints True. The price: every GPU holds the whole model. And each step, each GPU sends about twice the gradient's size, whatever the batch size.
A mean of gradients is a gradient of a mean
Why the split is exact
Four tokens, two GPUs
Simulate it in numpy
import numpy as np
rng = np.random.default_rng(0)
X, y, w = rng.normal(size=(8, 3)), rng.normal(size=8), rng.normal(size=3)
def grad(Xs, ys, w): # gradient of mean squared error
return 2 * Xs.T @ (Xs @ w - ys) / len(ys)
full = grad(X, y, w) # one big GPU
G = 4
shards = zip(np.split(X, G), np.split(y, G)) # equal shards
local = [grad(Xs, ys, w) for Xs, ys in shards] # each GPU
allreduced = sum(local) / G # all-reduce sum, then / G
print(np.allclose(full, allreduced)) # 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