5. Parallelizing Training
Chapter 3 was one sharded matmul and Chapter 4 was the FLOPs of one transformer. Now we spread a whole training step over thousands of chips.
What Do We Mean By Scaling?
We want strong scaling: double the chips, double the throughput. On one chip the fight was FLOPs against HBM bandwidth; across chips it's FLOPs against the network. More chips means more to communicate and fewer tokens per chip to hide it behind, so for every scheme below we ask when overtakes .
To keep the algebra small, we pretend the transformer is just a stack of MLP blocks with no gating matrix, which is fair while (Chapter 4). Each layer is two matmuls:
is the TOTAL batch in tokens (not sequences, not per device), is FLOPs/s per chip and is bidirectional bandwidth ( or ). and are how many chips we shard over, spanning and physical mesh axes; more axes means more links, so comms time drops by roughly .
For a TPU v5p, FLOPs/s and bytes/s, so . Basically every rule below is built out of that one number. Also, the MLP doesn't care which sequence a token belongs to, so "batch" sharding can cut along the sequence too (context parallelism).
Data Parallelism
We split the batch and give every chip a full copy of the weights. No contracting dimension is sharded, so the forward pass is free (Case 1 from Chapter 3). The bill comes in the backward pass: each chip only has the gradient from its own slice of the batch, so we AllReduce the weight gradients, off the critical path since only the update needs them.
Per layer, that's two AllReduces of bytes (each costing two AllGathers) against four matmuls of FLOPs:
Meaning, the per-device batch has to beat 2550 tokens on a v5p. Over all three mesh axes the bar drops to , so a full 8960-chip pod needs a batch of about 7.6M tokens. Not hard.
The real problem is memory. bf16 parameters (2 bytes) plus Adam's two fp32 moments (8 bytes) is 10 bytes per parameter, on EVERY chip. With 96GB of HBM that caps us at parameters before a single activation.
Fully-Sharded Data Parallelism (FSDP)
FSDP (also called ZeRO-3) keeps the batch sharding and shards the weights and optimizer state too. Each layer's weights get AllGathered just before they're used (prefetched while the previous layer runs), and on the way back we ReduceScatter the gradients. Since , FSDP just splits the AllReduce that data parallelism was already paying for. In the forward pass that's two AllGathers of bytes against two matmuls:
Same ratio, same rule (), and we get the replicated memory back for free.
One thing that feels backwards: on a fixed number of chips, a SMALLER batch makes us more communication bound. And we can't just grow the batch, since past some size more tokens per step stop helping training.
Tensor Parallelism
Instead of moving weights, we move activations; the weights are sharded along and never travel. We AllGather In, run both matmuls locally (leaving Out unreduced along ), and ReduceScatter Out. The catch: these ARE on the critical path, since the layer can't start until the gather lands.
This time cancels, so tensor parallelism doesn't care about batch size; its ceiling is set by how wide the MLP is. With that's about 11.8 per axis, so 8-way works and 16-way doesn't.
Combining FSDP and Tensor Parallelism
FSDP pays its comms in weights and tensor parallelism pays in activations, and the two help each other: the activation gathers shrink as grows and the weight gathers shrink as grows. With chips:
The math doesn't depend on the split. If the two comms overlap, the best split is where they're equal, and plugging it back in gives the bound:
For and that's about 99 tokens per chip. So plain FSDP is fine down to 850 tokens per chip, and from there down to about 100 only the mix stays compute-bound. The plots on jax-ml.github.io/scaling-book/training/ show this well.
Pipelining
Pipelining shards the layers: chip 0 runs the first few and hands its activations to chip 1. Very little moves, which is why GPU clusters with weak links between nodes like it, but stages sit idle waiting on each other (the pipeline bubble) unless we microbatch. TPU pods are big enough that it rarely comes up.
Scaling Across Pods
Past a full pod (8960 chips) we go over DCN, about bytes/s per TPU. The usual recipe is FSDP and tensor parallelism inside each pod and pure data parallelism across pods. Every chip brings its own share of NIC bandwidth, so the rule ends up per slice:
For a full pod that's about 8 tokens per chip, so the ICI rules inside the pod bite first.
Takeaways from LLM Training on TPUs
On a v5p, data parallelism and FSDP need , tensor parallelism needs , the mix needs , and data parallelism across pods needs 73,440 tokens per slice.
With a big batch or a small model there isn't much to decide: data parallelism if the model fits on one chip at 10 bytes per parameter, FSDP if it doesn't. The hard case is a small batch on lots of chips, where we add tensor parallelism up to its 8 to 16-way ceiling and let FSDP take the rest.
Some Problems to Work
All three problems use LLaMA-2 13B: , , , , and , with separate embedding and output matrices and a gated MLP. Hardware is the TPU v5p: 96GB of HBM per chip, 4.59e14 bf16 FLOPs/s, and 1.8e11 bytes/s of bidirectional ICI per axis.
Question 1 [counting parameters]: How many parameters does LLaMA-2 13B have? Hint: a gated MLP has 3 big FFW matrices per layer, two up-projections and one down-projection, and the gating one behaves just like .
-
MLP: three matrices in each of the layers:
-
Attention: Q, K, V and O. Since and , each one is :
-
Vocab: separate embedding and output matrices, so .
-
Add them up:
-
So 13.0B, as the name says, and about 65% of it is the MLP.
Question 2 [training memory]: Say we train with a batch of 16M tokens using Adam. Ignoring parallelism for now, how much total memory do the parameters, optimizer state and activations take? Assume bf16 parameters, fp32 optimizer state, and activations checkpointed three times per layer (after each of the three big FFW matmuls).
-
bf16 parameters are 2 bytes and Adam's two fp32 moments are 4 bytes each, so 10 bytes per parameter:
-
Activations: the two up-projections each leave a bf16 tensor and the down-projection leaves a . Per layer that's bytes, and :
-
The total is around 42.1 TB, basically all activations. Meaning, at training batch sizes the model itself is a rounding error in memory.
Question 3 [choosing a sharding]: We want to train with a 32k sequence length and a total batch size of 3M tokens on a TPU v5p 16x16x16 slice, with bf16 weights and an fp32 optimizer as above. Assume gradient checkpoints only after the 3 big FFW matrices.
- The slice has chips, so we get tokens per chip.
- 3M tokens at 32k each is only about 92 sequences, far fewer than chips, so the batch sharding has to cut across sequences too.
Does pure data parallelism work here? Why or why not?
- No. Every chip would hold all 130 GB of parameters and optimizer state from Question 2, and it only has 96 GB of HBM.
Can pure FSDP do it without being comms-bound? Why or why not? How much memory does each device use with pure FSDP?
-
Swapping for in Question 2 gives bytes of activations, or with the 130 GB added. FSDP shards all of it with no duplication:
-
For comms, with all three axes on FSDP we need tokens per chip, and we only have 732, so .
-
So no, pure FSDP is comms-bound. We'd need a batch of at least tokens.
Does mixed FSDP + tensor parallelism work? Why or why not? If so, what should and be, and how much memory sits on each device? Using only roofline FLOPs estimates and ignoring attention, how long is each training step at 40% MFU?
-
Two axes on FSDP and one on tensor parallelism gives , so the bar is:
-
We have 732, so yes. For the split:
-
That leaves . Rounding to powers of two, we get for FSDP and for tensor parallelism.
-
Per layer at that split, against and . Both comms fit under the math (as long as they overlap), so we're compute-bound with a margin of about 1.36.
-
Memory per device is the same as pure FSDP, about 1.95 GB per chip, since neither scheme copies anything.
-
Step time is FLOPs (Chapter 4) over 40% of the slice's peak:
-
Each step takes around 312 ms. The solution rounds this to 300 ms.