Lecture guide: Parallelism I#
Lecturer: course staff · Course: Stanford CS336 Language Modeling from Scratch, Spring 2026, lecture 7 · Length: 1:22:00 Video: watch on YouTube Chapters that cite this lecture: 06. Parallelism
In one sentence: Percy Liang extends the single-GPU picture to many GPUs, with the collective operations, the interconnects and the bandwidth numbers that distributed training rests on, and then writes data, tensor and pipeline parallelism by hand on a small network.
Before you watch#
The lecture continues directly from the two GPU lectures: the same game, keeping computation away from data-transfer bottlenecks, is played on a larger board. It steps through prepared code, so the printed outputs of the code are part of the explanation. The chapter Parallelism merges this lecture with the next one; its sections on the vocabulary and on data parallelism follow this lecture, those on ZeRO, FSDP and pipelines the next.
The lecture, part by part#
0:00–5:46 The same game on a larger board#
Last week made one GPU fast; this week uses four or a thousand, connected. Zoomed out, the situation is the same: the compute units are far from the data. On one GPU the data sits in high bandwidth memory; with several, what a GPU needs may sit on another GPU and has to be moved. The game is to orchestrate the computation so that data transfer does not become the bottleneck. Using many GPUs is easy; using them effectively is hard. The memory hierarchy grows a few levels: next to the SM, fast; high bandwidth memory, lamented last week and fast from now on; a single node of several GPUs joined by NVLink and an NVSwitch; several nodes joined by InfiniBand or Ethernet. Last week cut memory accesses by fusion and tiling; this week cuts communication between GPUs by replicating and sharding well. Two reasons drive the use of several GPUs: the parameters, activations, gradients and optimizer state do not fit, a B200 has 192 GB and a trillion-parameter model does not; or the model would fit but more GPUs train it faster. Spreading out costs communication bandwidth, a calculation to be made. The lecture has two parts: the building blocks of distributed computation and communication, then three ways to parallelize training, on MLPs rather than full transformers. In this book: Parallelism › The core idea · GPUs, TPUs and Kernels › The core idea.
5:46–12:04 Collective operations: the warm-ups#
Collective operations are primitives of distributed programming that go back to the 1980s: parallel programming was not invented for language models. A collective names a communication pattern across devices, instead of managing which GPU talks to which. The terminology: a rank is a device, here a GPU, and the world size is the number of devices, four in the example. Eight operations follow. The first four are warm-ups. Broadcast sends the tensor of one rank to all ranks; it is off the core path of training and generally used once, for initialization: to distribute an initial checkpoint. Scatter splits a tensor on one rank into world-size pieces and deals them out. Gather is its inverse, concatenating the pieces onto one rank. Reduce applies an operation such as a sum to the pieces and puts the result on one rank; gather is a reduction whose operation is concatenation. To a question: NumPy's broadcasting is the same idea, one value going to many places, in a different setting. In this book: Parallelism › 1. The vocabulary.
12:04–21:53 All-gather, reduce-scatter, then all-reduce and all-to-all#
The prefix "all" means that the destination is every device. All-gather gives every rank all the pieces; later, when each rank holds part of the parameters, it rebuilds the full parameters for a forward pass, and training will gather, do something and scatter again. Reduce-scatter reduces each component separately and puts each result on a different rank, 6, 10, 14 and 18 in the example; after a backward pass it will sum the gradients from the shards and share out the storage. All-reduce is the two in sequence: sum and replicate on every rank. Data parallelism uses it first; the sharded variants of the next lecture split it back into its halves so that one can intervene in between. All-to-all is the most general: each rank sends a slice to every other rank, and the position of a slice names its destination. With balanced traffic it is a transpose; it handles unbalanced splits too, but balance is the goal, which is what load balancing in a mixture of experts is for, since each rank holds a shard of data and a subset of experts and routing is dynamic. To remember: reduce applies an associative, commutative operation; scatter distributes and gather centralizes; "all" names the destination. In questions: the destination of a gather or reduce is an argument, chosen at the call; and these are not only concepts, the code follows. In this book: Parallelism › 1. The vocabulary.
21:53–30:00 How GPUs are connected#
The classic picture of a server: CPUs on a PCIe bus, GPUs and memory attached, and Ethernet to other computers. There, GPUs on the same node talk over PCIe and GPUs on different nodes go all the way through Ethernet: what one gets by hooking a gaming GPU to a friend's. A serious setup has eight GPUs per node, each connected by NVIDIA's NVLink to a switch. NVLink 5 gives 1.8 terabytes per second, against the 8 of a B200's high bandwidth memory, about four times slower. The switch makes every GPU reachable from every GPU. Larger clusters group nodes into pods joined by InfiniBand: traffic goes through PCIe and InfiniBand cables at much lower speeds; pods are joined by Ethernet, through the CPU, slower still. The more nodes, the slower, as with memory. Traditional Ethernet routes a GPU's data through the CPU's kernel buffers to the network interface, which adds latency. Remote direct memory access lets a GPU read or write another GPU's memory without the CPU; NVLink and InfiniBand support it, standard Ethernet does not. Two notable advances: NVL72 puts 72 GPUs of B200 and B300 generation into one NVLink domain, nine trays of eight; and RDMA over converged Ethernet lets Ethernet bypass the CPU, an answer to the very expensive InfiniBand that still performs pretty well, which Meta has explored in papers, so Llama may have been trained over it. In this book: Parallelism › The core idea · Parallelism › What the practitioners say.
30:00–36:15 NCCL, and questions on the hardware#
At the lowest level sits NCCL, pronounced nickel, NVIDIA's collective communications library. It turns each collective into the low-level messages that travel between GPUs: it works out the hardware topology and the paths, and it launches the kernels that send and receive, since on a GPU every piece of work is a kernel. The questions: an NVL72 rack holds trays with Grace CPUs, two per tray, each with four GPUs; remote direct memory access is the goal, and NVLink with its switch and InfiniBand are two kinds of hardware that reach it; whether NCCL is optimized for multi-node clusters he does not know, though he would be surprised if NVIDIA had not thought of it, since it optimizes its whole stack for inference and training of large models, its main customers being the major providers of language models; a ninth GPU on a second node without NVLink is bad, a node of little compute that is expensive to talk to; and TPUs are simpler objects, whose correspondence to these parts he does not know well enough to describe. In this book: nothing beyond this guide; nearest: Parallelism › What the practitioners say.
36:15–46:40 Distributed code in PyTorch#
PyTorch's distributed library gives a clean interface to the collectives, so NCCL is never touched directly; backends exist for GPUs and, with Gloo, for CPUs. Higher-level algorithms such as FSDP are in the library too, but the course builds from scratch. A spawn call runs one function on every rank; world size processes run the same function at once, told apart by rank. The setup names a master address and port, used for metadata and coordination only, since data travels through NCCL, and chooses the backend. A barrier is a synchronization point: processes run asynchronously and may interleave in any order, and a barrier makes sure one piece of code has run before another starts; more barriers mean more possibly needless waiting. The all-reduce example gives each rank its own tensor; the prints arrive in whatever order the hardware likes; the call with the sum operation writes in place, the column sums replicated on every rank. The asynchronous variant would scramble the prints. A reduce-scatter writes into a separate output tensor, and an all-gather of its result reproduces the all-reduce: proof by example that one equals the other two in sequence. Cleanup ends the program. For this class, a rank is a GPU. In this book: Parallelism › 1. The vocabulary · Parallelism › Try it.
46:40–55:13 Benchmarking communication#
How fast is an all-reduce of 100 million elements? Warm up, then time it with both kinds of asynchrony stopped: synchronize CUDA and pass a barrier before and after. Every rank measures for itself, so the ranks report different times; average them for one number. Effective bandwidth is to communication what model FLOPs utilization is to compute: the bytes that should be sent, divided by the time. For an all-reduce of a tensor of a given size, each rank sends the size times 2, for sending and reducing, times world size minus 1, the number of additions; the total duration is the wall-clock time times the world size, the waiting of all ranks. The result is about 400 gigabytes per second. As the world size grows, world size minus 1 over world size tends to one, so the figure approaches twice the size over the measured time: independent of the world size and of the topology, which NCCL chooses, a ring or a tree. Reduce-scatter is measured the same way without the factor 2, and gives, he says, a very similar figure, in the 400s: an all-reduce moves twice the data and takes twice the time, and the two cancel. To a question on why synchronize: CUDA operations are asynchronous by default, and a barrier before they finish does nothing. In this book: Parallelism › 1. The vocabulary.
55:13–62:00 Data parallelism#
Now training, on a bare-bones MLP, which is the compute bottleneck of a transformer and so stands for it. A schematic shows how data and parameters are cut: data parallelism splits the data and leaves every GPU a full copy of the model. The example has batch size 128 and dimension 1024. The rows are split into world-size pieces, a local batch of 32 each; in practice each rank would load its own data rather than receive a slice. The model is a stack of square weight matrices, one per layer, with an optimizer. The loop is ordinary: forward pass on the local data, backward pass. Then the step that makes it work: every rank has different data, so different gradients, and an all-reduce averages them; afterwards every rank has the same gradients and makes the same update. Distributed data parallel is standard training with one line added after the backward pass, and it is modular: it does not care what the forward pass looks like. In questions: the batch size must be at least the world size, ideally a multiple of it, or padded. In this book: Parallelism › 2. Data parallelism.
62:00–69:35 Tensor parallelism by columns#
In summary of data parallelism: the losses differ per rank, the gradients differ at first and are then made identical, so the parameters stay identical. It needs room for all the parameters; when they do not fit, the next lecture's fully sharded data parallel and ZeRO take over. Tensor parallelism cuts each layer instead of the data, so every rank holds part of every layer, and moves much more data. Each rank has all the data, by simplifying assumption, and a slice of the weight columns: parameters of full width by local width, column-parallel, though rows would do too. The forward pass applies the slice, then the elementwise nonlinearity, then all-gathers the activations: every rank allocates a list of buffers, one per rank, the all-gather fills each slot with the matching slice, and the concatenation gives the full activations. This happens in every layer. Unlike data parallelism, the model itself must be touched: it leans on the fact that a matrix product splits into smaller products whose results can be gathered. In the backward pass the gradients are reduce-scattered, a duality: all-gather forward, reduce-scatter backward. A plain backward call does none of it, since the code holds no parallelism: here the reduce-scatter would be called by hand, by design, as the course builds from scratch; in practice PyTorch would do it for you. In this book: Parallelism › 5. Tensor parallelism · Parallelism › 3. Shard the state: ZeRO and FSDP.
69:35–74:00 Pipeline parallelism and micro-batches#
Pipeline parallelism splits by layers: each rank holds a subset of the layers, whole, with all their dimensions. The batch is split too, into micro-batches. The first rank chunks the data; for each micro-batch a rank receives from the rank before it, runs the forward pass through its own layers and sends to the rank after it, with point-to-point send and receive calls. It is a natural way to divide a deep network, but it produces pipeline bubbles: ranks waiting for tensors to arrive, which is inefficient. Micro-batches shrink the bubbles, since a small chunk can be processed and passed on quickly; Hashimoto's next lecture says more. The naive version does not overlap communication with computation, which matters most here: an asynchronous send would let a rank compute while it transfers, at the price of more code to manage. In this book: Parallelism › 4. Pipeline parallelism.
74:00–1:22:00 What is missing, and which parallelism to choose#
Three things are missing from the code. The overlap of communication and computation, crucial for pipelines and useful in data parallelism too, where a gradient can be sent as soon as it is ready in the backward pass; the assignment explores this. Generality: the MLP gives most of what is needed, and some larger models need far more bookkeeping. And the other kinds of parallelism: sequence parallelism, which chops a sequence into pieces to parallelize attention; expert parallelism for mixtures of experts, where all-to-all comes in; and combinations of all of them. The choice depends strongly on the hardware. Tensor parallelism sends sizeable activations in every layer, so it runs within a node, over NVLink, and not beyond that domain. Pipeline parallelism tolerates slow links; some decentralized training uses it with GPUs far apart. So one combination is tensor parallel within a node, then data parallel or FSDP, then pipeline if needed. Data parallelism also runs into the critical batch size, past which larger batches no longer help and tensor parallelism is the better use of the GPUs. The other approach, in the JAX and TPU world, declares the model and a sharding strategy and lets the compiler insert the communication; appealing, but it would take the joy out of building from scratch. The summary: cut by data, by tensor, by expert, by pipeline or by sequence; tensor parallel needs fast links, pipeline needs hard work on the bubbles. One pattern recurs: recompute or store, as with activation checkpointing, extended here to storing on a different GPU; data parallelism does redundant work, every rank keeping all the parameters, to avoid moving optimizer state. Hardware gets faster and models get bigger, so the hierarchy will always be there. Hashimoto continues on Wednesday. In this book: Parallelism › 6. The rest · Parallelism › 7. Putting it together · Parallelism › Key takeaways.
What to take away#
- Using many GPUs is easy and using them well is hard: the compute is far from the data, now across NVLink, InfiniBand or Ethernet, each step slower than the last.
- Collectives name communication patterns: broadcast, scatter, gather and reduce are warm-ups; all-gather, reduce-scatter and all-reduce (the other two in sequence) do the work of training; all-to-all matters for mixtures of experts.
- An all-reduce's effective bandwidth is about twice the tensor size over the time taken, independent of world size and topology; measure with both asynchronies stopped.
- Data parallelism is standard training plus one all-reduce of the gradients after the backward pass; tensor parallelism all-gathers activations forward and reduce-scatters gradients backward; pipeline parallelism moves micro-batches through stages and fights bubbles.
- Tensor parallelism stays inside a node, pipelines tolerate slow links, and the techniques are often combined.
Sources#
- Percy Liang and the course staff, "Parallelism I", Stanford CS336 Language Modeling from Scratch, Spring 2026: video.
- Course page: https://cs336.stanford.edu/.