Concept · Chapter 9: How an LLM Is Actually Built
Data Parallelism and All-Reduce
Data parallelism runs a copy of the model on every GPU, gives each a different slice of the batch, and averages their gradients with an all-reduce before every update, so all copies stay identical.
The problem
A trillion-token run would take centuries on one GPU; the work has to be shared across thousands of them without changing what is computed.
The solution
Split each batch across GPUs, let each compute gradients on its share, average the gradients across all GPUs with a collective all-reduce, and apply the same update everywhere.
The consequence
Training scales to thousands of GPUs with no change to the maths, but every GPU needs the whole model's states and the network must carry a full set of gradients every step.
You should understand first
- Derivatives and Gradients
- Loss Functions
- Gradient Descent
- Probability and Distributions
- Expected Value and Variance
- Stochastic Gradient Descent (SGD)
- The Chain Rule
- Vectors
- Dot Product
- The Turing Test
- Symbolic AI
- Logic and Rules
- Expert Systems
- Knowledge Representation
- The Knowledge-Acquisition Bottleneck
- From Rules to Learning
- Supervised, Unsupervised and Self-Supervised Learning
- Features, Labels and Tasks
- Linear Regression
- Entropy
- Softmax
- Cross-Entropy Loss
- Logistic Regression
- The Perceptron
- Activation Functions
- The Artificial Neuron
- Matrix Multiplication
- Multilayer Perceptron (MLP)
- The Forward Pass
- Computational Graphs and Autodiff
- Backpropagation
- Momentum and Adam
- The Pretraining Loop
- Data Parallelism and All-Reduce
Map, then reduce
To a data engineer this is familiar. Each GPU (a mapper) takes its shard of the batch, runs the forward and backward passes, and produces a full set of gradients. Then all GPUs reduce: they average those gradients, and every GPU applies the same averaged update. Because the average of per-shard gradients equals the gradient of the whole batch, the result is exactly what one enormous GPU would have computed.
Tiny example. Four GPUs each process 4 sequences of 4,096 tokens: 16 sequences and 65,536 tokens per step. Each GPU's gradient for some weight is 0.8, 1.2, 1.0 and 1.0; the all-reduce gives every GPU the average, 1.0, and every copy takes the identical step.
All-reduce
An all-reduce is a collective operation: every participant contributes a vector and every participant receives the sum (or average). Efficient implementations pass pieces around a ring or tree so that each GPU sends and receives roughly twice the size of the gradients however many GPUs there are, which is why the pattern scales to thousands.
- Each GPU holds
- A full copy of the model, and a different slice of the batch.
- What they exchange
- Once per step, every GPU averages its gradients with all the others (an all-reduce), so all copies stay identical.
- Used
- Always, as the outermost layer of parallelism: it is how a run uses thousands of GPUs.
- The catch
- The whole model, its gradients and optimizer states must fit on every GPU.
Real runs combine them. Llama 3 405B used tensor parallelism across the 8 NVLink-connected GPUs of each server, 16 pipeline stages, and sharded data parallelism (FSDP) across the rest, plus a fourth kind, context parallelism, for very long sequences: up to 16,384 GPUs.
The catches
Memory. Every GPU holds a full copy of the weights, gradients and optimizer states: 16 bytes per parameter. For anything beyond a few billion parameters that doesn't fit, which is what sharded training fixes.
Batch size. Adding GPUs adds to the global batch. Goyal and colleagues showed that very large batches can match small-batch accuracy if the learning rate is scaled with the batch size and warmed up gradually Established, but there are limits beyond which bigger batches stop helping.
Lockstep. Every GPU waits for the slowest at each all-reduce, and in synchronous training a single GPU failure may require restarting the entire job Established, a theme of training stability.
Why should I care?
As a researcher
It is why batch size and learning rate become entangled with the number of GPUs, and why large-batch training became a research topic.
As an engineer
It is the first and simplest scaling tool (DistributedDataParallel in PyTorch), and the same map-then-reduce pattern as distributed data processing.
Modern systems that depend on it
- ZeRO and FSDP
- global batch size
- 3D parallelism
- the compute budget
Historical context
Before
One GPU per training run, or ad hoc parameter servers that collected and redistributed gradients.
After
Synchronous data parallelism with efficient all-reduce collectives, as the outer layer of every large run.
Used today
Every multi-GPU training job. In Llama 3 405B, data parallelism (with sharding) spanned up to 128 groups of 128 GPUs.
What to remember
- Each GPU: full model, different data. After backward: all-reduce (average) the gradients.
- Mathematically the same as one big batch on one GPU.
- Global batch = micro-batch × gradient-accumulation steps × number of data-parallel GPUs.
- Catch: every GPU must hold the full model states (fixed by sharding).
Key papers
Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour
Priya Goyal, Piotr Dollár et al. · 2017
Showed how to train with very large batches across many GPUs without losing accuracy, using a linear learning-rate scaling rule and a gradual warmup.
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
Samyam Rajbhandari, Jeff Rasley et al. · 2019
Explained where training memory goes (16 bytes per parameter with mixed-precision Adam) and how to remove the redundant copies. Its stages became DeepSpeed's ZeRO and PyTorch's FSDP.
How to read it: Section 3 ('Where did all the memory go?') and Figure 1 are the essentials; the rest is engineering detail.
Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM
Deepak Narayanan, Mohammad Shoeybi et al. · 2021
Showed how to compose tensor, pipeline and data parallelism to train trillion-parameter models efficiently on thousands of GPUs.
How to read it: Read the takeaways in Section 3; they summarise how to choose the parallel sizes.
The Llama 3 Herd of Models
Aaron Grattafiori, Abhimanyu Dubey et al. · 2024
The most complete public account of building a frontier-scale model end to end: data pipeline, scaling-law experiments, 16,384-GPU training, failures and all.
How to read it: It is 90+ pages. For this chapter read Section 3 (pre-training) only: data, scaling laws, infrastructure and the training recipe.
Watch
Stanford Online
Stanford CS336 Language Modeling from Scratch | Spring 2025 | Lecture 7: Parallelism 1
A clear university lecture on how training is split across many GPUs, with the trade-offs between the methods.