Lecture guides›
✎

Lecture guide: Parallelism II#

Lecturer: course staff · Course: Stanford CS336 Language Modeling from Scratch, Spring 2026, lecture 8 · Length: 1:22:00 Video: watch on YouTube Chapters that cite this lecture: 06. Parallelism

In one sentence: Tatsu Hashimoto shows how each way of sharding a training run trades memory for communication, from the free savings of ZeRO and FSDP through pipelines and tensor, sequence and expert parallelism, and how large runs combine them to stay compute-bound.

Before you watch#

The previous lecture supplies the collectives, the link speeds and the by-hand versions of the three basic strategies. This one is algorithmic rather than coded: it accounts for the communication of each strategy and ends with a prescription and the settings of published training runs. The chapter Parallelism merges both lectures; its sections on ZeRO and FSDP, pipelines, tensor parallelism and putting it together follow this lecture.

The lecture, part by part#

0:00–4:00 Two bottlenecks, and one identity#

Hashimoto's subject is the details of parallelism for training huge models on huge clusters. Many strategies are used at once, which is why a slide near the end says 4D parallelism, and the largest scale needs most of them; the assignment asks for the best strategy given a network topology and a model. Two bottlenecks force several machines: compute, since the fastest supercomputers have exaflops and a single chip nowhere near, and memory, since models must be sharded to fit. The conceptual object of the lecture is the difference between fast intranode links, on which communication-expensive methods are allowed, and slow internode links, which call for methods that respect the constraint. Everything is counted at the level of collectives. One identity from the previous lecture matters: an all-reduce equals a reduce-scatter followed by an all-gather, so an algorithm written with the two steps costs the same as one written with the one. Other decompositions exist; this one serves the algorithm that follows. In this book: Parallelism › The core idea · Parallelism › 1. The vocabulary.

4:00–11:07 Two hardware philosophies#

The discussion is mostly hardware-agnostic, but the hardware leads companies to different strategies. A TPU network is a toroidal mesh: a grid whose ends wrap around, in three dimensions, with every chip connected to its neighbours. It is simple, scales up indefinitely, and keeps the same number of neighbours however large it grows. GPU networking is an all-to-all philosophy, organized as a fat tree: GPUs connected very fast at the bottom, then groups in a pod, then spine switches between pods, so the topology grows more complicated with the nodes. Talk only to neighbours and TPUs are cost-effective, each link beefier for the same power; talk in unpredictable ways and the GPU network is more flexible. Bill Dally and Jeff Dean have said as much: GPUs suit mixtures of experts, whose routing is unclear in advance, and TPUs suit dense models partitioned predictably. That morning Google announced two chips that move toward the GPU view: an inference TPU with a tree topology, since serving a mixture of experts sends bandwidth everywhere, and a training TPU whose cross-rack network, called Virgo, looks like an all-to-all switch. The workloads define the network. Huawei's Ascend 910 shows the brute-force alternative: each chip is much worse than an H200, but a rack of 384 is joined by fiber-optic switches, at a power consumption he gives as four times the equivalent NVIDIA system. The unit of compute is now the data center, and the goal is to control memory and compute while wasting neither. In this book: Parallelism › What the practitioners say.

11:07–16:00 Data parallelism and the memory it wastes#

Data parallelism distributes the data; model parallelism chops the model, though one algorithm in the second bin cuts parameters too. Take plain SGD with a batch of B: cut the batch over M machines, let each compute gradients on its B/M examples, and synchronize. Compute scales perfectly as long as each GPU has enough examples; every batch communicates twice the number of parameters; and no memory is saved, since every GPU holds the model and its own activations. The memory situation is terrible rather than bad: parameters, gradients, a higher-precision accumulator and, with Adam, two moments of the gradient, often in high precision. The optimizer state is most of it, and mixed precision takes on the order of 16 bytes per parameter. Naive data parallelism replicates all of it, so memory grows linearly with the accelerators. The remedy is to shard in stages: the optimizer state, then the gradients, then everything, which on his slide takes the memory from 120 to 1.9. In this book: Parallelism › 2. Data parallelism.

16:00–20:00 ZeRO stage 1: a free memory saving#

ZeRO stage 1 shards only the optimizer state; every GPU still has the parameters and the gradients, and each GPU updates one slice of the parameters and no other. Every GPU computes a full gradient on its own data. Then a reduce-scatter deals out the gradients, so that each GPU receives only the slice it updates, at the cost of one transfer of the parameter size. Each GPU updates its slice with its own state, and an all-gather hands the updated parameters to everyone, another transfer of the parameter size. Naive data parallelism does one all-reduce, which costs two parameter sizes. Stage 1 does a reduce-scatter and an all-gather, which is an all-reduce. The communication is identical, and the memory for parameters and optimizer state is divided by the number of GPUs: free. In this book: Parallelism › 3. Shard the state: ZeRO and FSDP.

20:00–26:00 ZeRO stages 2 and 3: FSDP#

Stage 2 shards the gradients as well. The whole gradient can no longer be materialized, so it is produced incrementally: sweeping backwards through the network, each layer's gradients are reduce-scattered to their owners as soon as they exist and then freed, which costs the same as doing it all at once; the parameters are all-gathered at the end. Stage 3, which looked like magic to him, shards the parameters too: they are requested when the compute graph reaches them, and each GPU holds only a slice of parameters, gradients and state at any time. This is ZeRO-3, better known as FSDP. Per layer it needs two all-gathers and one reduce-scatter: gather a layer's weights, run the forward, free the weights; in the backward pass gather them again on demand, compute, reduce-scatter the gradients, free. One all-gather more than before, and communication in every layer, which seems expensive. Two ideas make it nearly free: the sweep with immediate freeing, and overlapping communication with computation. A plot from the paper on PyTorch's FSDP shows three streams, CPU, GPU computation and GPU communication: layer 0 is gathered, then computed while layer 1 is gathered, and so on. With enough computation and a fast enough network, FSDP costs almost nothing but some bubbles. In this book: Parallelism › 3. Shard the state: ZeRO and FSDP.

26:00–28:56 What FSDP costs, and what it fits#

In the accounting, naive data parallelism costs two parameter sizes, stages 1 and 2 are free in the literal sense, and stage 3 has one extra all-gather, hidden under the computation. In practice FSDP reaches GPU utilization close to a single GPU, and it is conceptually simple: a wrapper around any module that gathers, computes, frees and repeats, which the assignment implements. In questions he draws the line to pipelining: here every GPU goes through the whole model, but no GPU holds the whole parameters at once, and every GPU holds a part of every layer. The per-layer communications are more numerous but smaller, and add up to the cost of all-reducing the whole network. On A100s the baseline cannot fit a 7-billion-parameter model, where stage 3 fits models of 50 billion; the arithmetic scales linearly with a chip's memory. In this book: Parallelism › 3. Shard the state: ZeRO and FSDP.

28:56–34:00 Pipeline parallelism and the bubble#

FSDP is clean, but two problems remain. Data parallelism consumes the batch size: with a batch of 8 there can be no more than 8 accelerators, and growing the batch runs into the critical batch size, past which one more example gains less than another optimization step on an existing one. The choice is between idle GPUs and worse optimization. And activations and other memory are not sharded. So model parallelism: parameters are split across GPUs, but now activations travel, since one layer lives on one GPU and the next on another. Pipeline parallelism cuts by layers, tensor parallelism by matrices, expert parallelism by experts. Pipelining is conceptually simple: activations pass forward, partial gradients back. The picture without care is depressing: with four accelerators on a quarter of the model each, one GPU works at a time, forward and back. Splitting the batch into micro-batches repairs it: each stage hands a finished micro-batch on and starts the next. The idle time, the bubble, shrinks in proportion to the number of stages over the number of micro-batches. Batch size is a resource, and pipelining spends it. In this book: Parallelism › 4. Pipeline parallelism.

34:00–39:44 Why pipelines anyway; scheduling and zero bubbles#

By folklore, parallelization code stays understandable until someone implements pipeline parallelism. It is used for three reasons: it saves memory against data parallelism, it composes with data parallelism, and it has the best communication properties. It moves only activations, batch times sequence times hidden size, point to point between neighbouring stages rather than all to all. So pipelines live on the slowest links, across pods or data centers. The Megatron paper's sweeps show that large batches with a large pipeline size reach utilization close to no pipelining at all, and that small batches degrade quickly. To a question: pipelines are cheaper because activations are almost always smaller than a whole parameter matrix. Two refinements cut the bubble further. Clever scheduling interleaves the forward and backward steps of different micro-batches, as in DeepSeek's paper. Zero-bubble pipelining splits the backward pass into two tasks: passing the partial derivatives to the previous stage, which is urgent because that stage waits for them, and computing a stage's own weight gradients, which can happen at any time. Do the first at once and fill the gaps with the second, and the pipeline almost fills; the schedule is much more complicated. In this book: Parallelism › 4. Pipeline parallelism.

39:44–44:00 Tensor parallelism#

Pipeline parallelism cuts along the depth; tensor parallelism cuts along the width: a matrix product splits into smaller products whose partial sums are added, the same idea as tiling. A block computes X times A, a nonlinearity, then times B. Tensor parallelism cuts A and B into submatrices. In the forward pass the input is copied to both halves and the outputs are combined by an all-reduce; in the backward pass the roles flip, an identity where the combination was and an all-reduce of the partial derivatives where the copy was, a duality to keep in mind when writing it. Cuts are column-wise at the inputs of a block, the MLP input projection and the attention projections, and row-wise in the second stage, the MLP down-projection and the attention output. Small layers such as layer norms, nonlinearities and mixture-of-experts routers are replicated. Tensor parallelism is extremely communication hungry: all-reduces of activation size at every matrix product, so it belongs inside a node, up to the eight tightly networked GPUs, and performance drops past that. TPUs have no such boundary: their mesh carries high bandwidth in a regular pattern, so tensor parallelism runs over far larger numbers there. In this book: Parallelism › 5. Tensor parallelism.

44:00–46:00 Tensor against pipeline, and where memory really goes#

Both split the model and save parameters and, potentially, activations. Tensor parallelism has no bubble and can reach full utilization on a fast enough network, and it is low in complexity; its cost is much larger communication, about eight times batch times sequence times hidden size, in all-reduces rather than point to point. Tensor parallelism for fast interconnects, pipeline parallelism the rest of the time. On memory, the naive view counts only the parameters; the optimizer state is already more. A profiler's plot of a real run shows dynamic memory too: the activations as large humps, and the gradients as the backward sweep starts. The peak falls a little after the point of most activations, when the sweep has begun but the activations are still needed. In this book: Parallelism › 5. Tensor parallelism · Parallelism › Worked example.

46:00–52:00 Activation memory, tensor and sequence parallelism#

For bigger models at moderately long sequence lengths, activations dwarf the parameter memory, so a strategy must reason about activations to be effective. If everything is stored, the activations per layer are about (34 + 5 · A · S / H) times S · B · H bytes, for sequence length S, batch size B, hidden size H and A attention heads. The S · B · H dependence is fundamental: something is stored per sequence element, per batch element and per hidden unit. The second term comes from the quadratic attention scores, and recomputation with FlashAttention removes it. Tensor parallelism with size T splits the attention and MLP products, so most of the memory is divided by T. It does not split the layer norms, the dropout and the inputs kept as residuals for the backward pass, so a term of about 10 · S · B · H remains however many GPUs are used. Sequence parallelism, a name he finds misleading since context parallel fits it better, splits these lightweight terms along the sequence axis, with all-gathers and reduce-scatters before each operation that needs them: FSDP's idea applied to activations, stored sharded and materialized on demand, with the same duality in the backward pass. Together with tensor parallelism the memory then scales linearly, and with recomputation it reaches 34 · S · B · H / T, the practical lower bound for ordinary training, and the first term of a by-hand estimate of whether a model fits. In this book: Parallelism › Worked example · Parallelism › 6. The rest.

52:00–58:00 Expert parallelism#

In questions: the MLP's activations can be recomputed too, but at the price of a second run of the MLP during the backward pass, while recomputing attention is cheaper, since it is done tile by tile. Expert parallelism is the last standard ingredient. Since most large models are mixtures of experts, the MLPs can be split as whole experts across devices; the systems behaviour resembles tensor parallelism, a high-bandwidth method that also reduces activations. Megatron's guidelines prefer it to tensor parallelism for such models, and he lists tensor parallelism's drawbacks: matrices cut too finely lose GPU utilization; sparse token activations are easier to route than the dense activations of a tensor-parallel product; and with experts present anyway, they may as well be spread over every device. It is still complicated. DeepSeek's DeepEP library routes and dispatches experts on top of low-level GPU networking primitives, and NVIDIA has a comparable library, Hybrid EP. The difficulty is many all-to-all dispatches at every MLP layer, latency-sensitive because computation waits for tokens to arrive. His trivia: the DeepSeek engineers found undocumented PTX instructions to speed their communication further. In this book: Parallelism › 6. The rest · Parallelism › What the practitioners say.

58:00–62:00 Combining expert parallelism; context parallelism#

Most strategies combine like building blocks, but expert parallelism has constraints. The naive arrangement makes the data-parallel and expert-parallel replicas the same: with data parallelism of 8, the experts are sharded across those 8 replicas, which bounds expert parallelism. It also applies unevenly: mixtures of experts change only the MLPs, not attention, so attention still needs tensor parallelism, and a high tensor parallelism combined with a high expert parallelism cuts matrices into tiny pieces. The conflict, high for attention and low for MLPs, is resolved by systems of the last few years that give attention layers and expert layers separate tensor-parallel sizes. Context parallelism, or ring attention, splits the activations of a very long sequence across accelerators and passes them around a ring that follows the TPU mesh; it is used in long-context extension and in serving, and he skips it as conceptually familiar. In this book: Parallelism › 6. The rest.

62:00–68:00 Doing the math: 3D and 4D parallelism#

A closing table marks each strategy's drawbacks in red: none dominates. FSDP is wonderful but does nothing for activations and consumes the global batch size; tensor parallelism cuts activations and leaves the batch size alone, at the price of fast networking; other strategies use the slow links, and each has a place in large architectures. The strategies apply to mixtures of experts too, and FSDP, pipelines and tensor parallelism are used in all the frontier models. The arithmetic: compute and communication per layer for each strategy, and how they scale with the devices allotted to it. Plots of GPU utilization against batch size, for one strategy or a combination, have a dashed line where compute time equals communication time; above it computation hides communication, below it the GPUs wait. FSDP alone is fully compute-bound at about 2,000 examples per chip and becomes communication-bound as the batch shrinks; adding tensor parallelism pushes the curve into smaller batches, and further strategies push it further. Stacking them is 3D or 4D parallelism. The prescription is simple: cut the model by whatever means until it fits, with tensor or expert parallelism over the fast interconnect, eight per machine, and pipeline parallelism or FSDP the rest of the way; once it fits, data-parallelize the remaining GPUs; if the batch is then too small, accumulate gradients. Megatron's guide says the same in reverse order: minimize model parallelism, maximize data parallelism, keep expert and tensor parallelism inside one box, use pipelines to go multi-node, prefer expert parallelism for mixtures of experts and context parallelism for long sequences. In this book: Parallelism › 7. Putting it together.

68:00–72:00 Evidence at scale#

In questions: sequence parallelism is usually an add-on to tensor parallelism rather than a method on its own, and a model that loops over the same weights would upset FSDP, whose rhythm is to gather weights and discard them. An old paper by NVIDIA researchers with colleagues from Stanford, which he calls one of the best resources since networking fundamentals have changed little, runs large training jobs across many configurations. The picture is the prescription: data parallelism maxed out, tensor parallelism rising to 8 and stopping there, pipeline parallelism increasing with scale, and at the very end data parallelism shrinking to 6, since only that much tensor and pipeline parallelism is needed to fit the model. Utilization stays flat at ludicrous numbers of GPUs, which is why the gigantic data-center build-outs work. Pipeline parallelism needs large batches, and its gap widens with its size. And activation recomputation, done cleverly, buys a bigger batch and so better utilization: more computation for higher utilization, because memory can be turned into batch size. In this book: Parallelism › 7. Putting it together · Parallelism › What the practitioners say.

72:00–1:22:00 Training runs in the wild#

OLMo, a 7-billion model trained on the Dolma data set, used FSDP alone, and many models of that size still do. DeepSeek V1 used ZeRO stage 1 with tensor, sequence and pipeline parallelism; V3, a mixture of experts, replaces tensor parallelism with a 64-way expert parallelism spanning eight machines, kept busy with the pipelining tricks, the infrastructure that later let others scale expert parallelism. Yi, an open Chinese model, used the classic data, tensor and pipeline combination. The Llama 3 report is one of the few with a full breakdown by phase: in pretraining tensor parallelism 8, context parallelism 1, pipeline parallelism 16 and data parallelism 128 for the dense 405-billion model; in the long-context extension, context parallelism up and data parallelism down, since that stage is memory-hungry. GPUs failed 148 times during that training, so redundancy matters as much as speed. Gemma 2 used only FSDP, tensor and sequence parallelism, the Google claim that a large TPU mesh needs no pipelines, though whether that scales forever is less clear to him. NVIDIA's Megatron-Bridge repository lists recommended configurations: Mixtral 8×22B takes expert parallelism 8, pipeline 4 and tensor parallelism 4 for the attention layers; Nemotron 3 Super follows DeepSeek V3 with expert and context parallelism; Qwen3 takes expert parallelism 32, pipeline 8 and tensor 2. Across all of them: data parallelism is maximized, tensor parallelism stays below eight, and expert parallelism can now be large. The highest-level point: multi-GPU, multi-node, even multi-data-center training needs every approach, and simple rules of thumb combine them into nearly full utilization. Next week: scaling laws. In this book: Parallelism › What the practitioners say · Parallelism › Key takeaways.

What to take away#

Sources#