Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
Distributed training: when the model doesn't fit, split it four ways.
A frontier model's weights, gradients and optimizer state can demand terabytes of memory — dozens of times what the biggest GPU holds. Training it means orchestrating thousands of GPUs to act as one, and there are exactly four ways to cut the problem up. Understand each cut, and what it costs in communication, and the whole intimidating world of ZeRO, FSDP and 3D parallelism becomes a small menu of trade-offs.
What actually eats the memory
Before splitting anything, know what you're splitting. Training an N-parameter model with Adam in mixed precision holds, per parameter: FP16 weights (2 bytes), FP16 gradients (2), and the optimizer's FP32 master weights + two moments (12) — roughly 16-20 bytes per parameter, before a single activation. A 70B model is ~1.2 TB of state; the biggest GPU holds 80-140 GB. Plus activations — the forward-pass values stashed for the backward — which scale with batch size and sequence length and often rival the weights.
This one calculation is why "just use a bigger GPU" runs out of road — and why every strategy below either shards this state or splits the model.
Distributed training solves two distinct things people conflate: "train faster" (throughput — spread the batch) and "it doesn't fit" (memory — spread the model/state). Different strategies target different problems; naming which one you have is the first move.
Data parallelism: copy the model, split the batch
The workhorse. Every GPU holds a full model copy and processes a different batch slice; after each backward pass, an all-reduce averages gradients across all GPUs so the copies stay identical. Simple, scales throughput near-linearly (until all-reduce saturates the network), and it's the default in every framework.
Its one fatal assumption: the whole model fits on one GPU. The moment it doesn't, pure data parallelism is dead — and every strategy below exists to lift that ceiling. The all-reduce also becomes the scaling wall at large GPU counts, which is why the communication-overlap tricks in section 06 matter.
ZeRO / FSDP: stop storing everything N times
Data parallelism is wildly redundant: every GPU stores the same optimizer states, gradients and weights. ZeRO (DeepSpeed) and FSDP (PyTorch) ask the obvious question — why? — and shard that state across GPUs, gathering each layer's parameters just-in-time for its forward/backward, then releasing them.
The beauty: it stays logically data parallel — nearly the same training code, dramatically higher memory ceiling, at the cost of extra all-gather traffic. For most teams training big-but-not-frontier models, FSDP/ZeRO-3 is the answer, full stop; the exotic strategies below are for when even this isn't enough.
Tensor parallelism: split the layer itself
When a single layer's matrices are too big, split within the operation: a matrix multiply is divided column- or row-wise across GPUs, each computes its shard, and they combine results with an all-reduce. This happens inside every layer, on every forward and backward — so tensor parallelism is communication-hungry and lives on NVLink, staying inside a single node (typically 8 GPUs). Cross-node tensor parallelism drowns in interconnect latency. It's the tool for making an individual layer fit, not for scaling out broadly.
Pipeline parallelism: split the model into stages
Cut the model by layers: GPU 0 runs layers 1-10, GPU 1 runs 11-20, and so on, passing activations down the line like a factory. It moves only activations at stage boundaries — bandwidth-light enough to cross nodes. The catch is the pipeline bubble: while stage 1 works on the first micro-batch, stages 2-4 sit idle waiting for it. The fix is micro-batching — split the batch into many small pieces so all stages stay busy processing different micro-batches at once, shrinking the idle bubble toward zero. Pipeline parallelism is how you scale across the datacenter where tensor parallelism can't reach.
And the bubble is quantifiable. With P stages and M micro-batches, fill and drain leave P−1 idle slots, so the wasted fraction is:
Same 4 GPUs, same model — the only change is splitting the batch into more micro-batches. That's the entire trick.
3D parallelism and the overlap that decides everything
No single axis scales far enough, so frontier training composes all three: tensor parallelism inside a node (hungry, needs NVLink), pipeline parallelism across a handful of nodes (layer stages), data parallelism across the resulting groups (replicas on different data). Each covers another's weakness, mapped deliberately onto the NVLink-fast / InfiniBand-slow topology.
Efficiency lives or dies on overlapping communication with computation: all-reduce gradients for layer N while computing layer N-1, prefetch the next shard while using the current one. A job that stalls on communication can burn half its GPU-hours doing nothing — which is why real MFU of 35-50% is a triumph, not a disappointment.
The memory tricks you'll reach for first
| Technique | What it buys |
|---|---|
| Mixed precision (BF16/FP8) | half+ the memory and ~2× compute on tensor cores — the free lunch, on by default |
| Gradient accumulation | simulate a huge batch on small memory — accumulate over K micro-steps, then update |
| Activation checkpointing | store fewer activations, recompute them in backward — trade compute for memory |
| CPU/NVMe offload | park cold optimizer state off-GPU (ZeRO-Infinity) — slow but makes the impossible fit |
| Gradient clipping | not memory, but the stability seatbelt that keeps big runs from spiking |
The honest order of operations: reach for mixed precision + FSDP + activation checkpointing first — that combination trains most large models teams actually build. Tensor and pipeline parallelism are the frontier-scale tools you add only when sharding alone runs out of room.
Size the memory and the bubble yourself
Both hard problems of distributed training are arithmetic. Run it: first, watch a 7B model's 112 GB of optimizer state — too big for any single GPU — shrink to 14 GB as ZeRO shards more of it across 8 GPUs. Then compute the pipeline bubble: how much of your expensive cluster sits idle, and how micro-batching drops it from 75% to under 9% without changing a single GPU. Change the model size, GPU count, or stage count and the whole budget recomputes.
Worked example: fitting a 13B model on one 8-GPU node
Say a team wants to fully fine-tune a 13B model with Adam in mixed precision on one node of 8 × 80 GB GPUs, at a global batch of 1,024 sequences. Use the page’s 16 bytes per parameter and walk the menu from cheapest to most sharded.
| Step | Question | Arithmetic | Answer |
|---|---|---|---|
| 1 | Total training state | 13×10⁹ × 16 bytes | 208 GB |
| 2 | Plain data parallel, per GPU | every GPU holds all of it | 208 GB: does not fit |
| 3 | ZeRO-1 (shard optimizer) | 13×10⁹ × (2 + 2 + 12/8) | 71.5 GB: 8.5 GB left for activations |
| 4 | ZeRO-2 (+ shard gradients) | 13×10⁹ × (2 + 14/8) | 48.75 GB: 31.25 GB left |
| 5 | ZeRO-3 / FSDP (+ shard weights) | 208 GB ÷ 8 | 26 GB: 54 GB left |
| 6 | Gradient traffic per GPU per step, ZeRO-1/2 | ring all-reduce: 2 × 7/8 × 26 GB of fp16 grads | 45.5 GB |
| 7 | Same, ZeRO-3 | two all-gathers + one reduce-scatter: 3 × 7/8 × 26 GB | 68.25 GB (1.5×) |
| 8 | Micro-steps per optimizer step | 1,024 ÷ (8 GPUs × micro-batch 4) | 32 accumulation steps |
| 9 | Grow to 4 nodes, ZeRO-3 | 208 GB ÷ 32 | 6.5 GB of state per GPU |
Step 3 is the trap. ZeRO-1 “fits” on paper, but 8.5 GB is rarely enough for a long sequence’s activations, so the job dies on the first long batch. ZeRO-2 is usually the sweet spot here: same traffic as plain data parallel, with room to breathe. ZeRO-3 buys more headroom for 1.5× the traffic, which is worth it only if step 4 still runs out. Pair it with activation checkpointing before reaching for tensor or pipeline parallelism. Step 8 is why gradient accumulation exists: the batch you want and the batch that fits are different numbers, and accumulation bridges them without changing the maths of the update.
Cost and trade-offs: what each split costs in traffic and memory
N = parameters, G = data-parallel GPUs, T = tensor-parallel degree, P = pipeline stages, M = micro-batches. Time here is communication or extra compute per step; Space is training state per GPU, before activations.
| Case | Time | Space | Why |
|---|---|---|---|
| Data parallel | One gradient all-reduce: about 2 × 2N bytes sent per GPU | 16N | Simplest; the traffic overlaps with backward, layer by layer. |
| ZeRO-1 | Same as data parallel | (4 + 12/G)·N | Free memory: the optimizer chunk was the biggest and needed no extra traffic. |
| ZeRO-2 | Same as data parallel (reduce-scatter + all-gather) | (2 + 14/G)·N | Gradients are reduced straight into their owner’s shard. |
| ZeRO-3 / FSDP | 1.5× data parallel: weights gathered for forward and again for backward | 16N / G | Needs prefetch to hide the gathers; a slow network shows up here first. |
| Tensor parallel | All-reduces inside every layer, forward and backward | Weights and their state ÷ T | Latency-bound, so it stays on NVLink inside one node. |
| Pipeline parallel | Point-to-point activations at stage edges; idle (P−1)/(M+P−1) | Layers ÷ P, plus activations of in-flight micro-batches | Cheap to cross nodes; the bubble is the cost. |
| Activation checkpointing | About one extra forward pass, roughly a third more compute | Activations kept only at checkpoints | Pure compute-for-memory trade, no extra traffic. |
Read the table as a ladder. Each row down saves memory and charges you something else: more bytes on the wire, a latency-bound collective, idle stages, or recomputation. Climb only as far as the memory arithmetic forces you.
Common mistakes
- Sizing by the weights alone
A 7B model is 14 GB in fp16, so it “fits” an 80 GB card. That is the inference number. Training with Adam needs 112 GB of state before activations, as the page shows. Always multiply by 16, then add activations.
- Tensor parallelism across nodes
Its all-reduces sit on the critical path of every layer, so it is only as fast as the slowest link. Keep T within the NVLink domain of one node, and let pipeline or data parallelism cross the slower inter-node network.
- All-reducing on every micro-step
With gradient accumulation, synchronising after each micro-batch sends the same traffic 32 times for one update. Skip the sync on all but the last micro-step (PyTorch DDP’s
no_sync()context does this). - Changing the global batch by accident
Global batch = micro-batch × accumulation steps × data-parallel GPUs. Double the GPUs and keep the rest, and the batch doubles too, so a learning rate tuned for the old batch no longer fits. Halve accumulation to hold the batch, or retune deliberately.
- Saving a sharded model through one GPU
Gathering the full state onto rank 0 to write a checkpoint recreates exactly the memory problem sharding solved, and the save step goes out of memory hours into a run. Write sharded checkpoints, one file per rank, and merge offline if you need a single file.
Where this shows up in production and interviews
- Fine-tuning stacks
Most teams meet this through PyTorch FSDP or DeepSpeed ZeRO behind a launcher such as Hugging Face Accelerate. The choice is a config file: sharding stage, mixed precision, checkpointing. The arithmetic in the worked example tells you which settings to pick.
- Megatron-style 3D layouts
NVIDIA’s Megatron-LM introduced the per-layer tensor split this page describes, and frameworks built on it expose T, P and data-parallel degrees as three numbers whose product is the GPU count. Choosing them is the topology mapping from section 06.
- Reading a training dashboard
MFU and step time are the health metrics. A drop in MFU with no code change usually means a straggler GPU or a congested link, since every collective waits for the slowest rank. That is why large runs watch per-rank step times, not just the average.
- The interview version
“How would you train a 70B model?” Start with 70×10⁹ × 16 = 1,120 GB of state. ZeRO-3 over 8 GPUs is 140 GB each, too big; over 32 it is 35 GB, leaving room for activations. Then say what you would add if activations still do not fit, and what each addition costs in traffic.
Check yourself
A 30B model, Adam, mixed precision, 8 × 80 GB GPUs. What is the least sharding that fits the training state?
Total state is 30×10⁹ × 16 = 480 GB. ZeRO-1 gives 30×10⁹ × 5.5 = 165 GB per GPU, ZeRO-2 gives 112.5 GB, both too big. ZeRO-3 gives 60 GB, leaving 20 GB for activations, so expect activation checkpointing too, or more GPUs.
You have 8 pipeline stages. How many micro-batches keep the bubble under 10%?
Bubble = 7 / (M + 7). At M = 63 it is exactly 10%, so you need M = 64, giving 7/71 ≈ 9.9%. More stages need proportionally more micro-batches, which is one reason pipelines are kept short.
Why is tensor parallelism placed inside a node and data parallelism across nodes, not the other way round?
Tensor parallelism communicates inside every layer, on the critical path, so it needs the fastest link: NVLink. Data parallelism communicates once per step and its all-reduce can overlap with backward, so it tolerates the slower inter-node network. Swap them and every layer waits on the slow link.
You move a plain data-parallel job from 8 to 16 GPUs with the same per-GPU micro-batch. What happens to memory per GPU and to the global batch?
Memory per GPU is unchanged: each GPU still holds a full 16N replica, which is why data parallelism never fixes “it doesn’t fit”. The global batch doubles, so either halve gradient accumulation to keep it, or retune the learning rate for the new batch.
Quick answers
What is data parallelism?
Every GPU holds the full model, processes a different batch slice, and all-reduces gradients to stay in sync. Simple and fast — but the model must fit on one GPU.
What do ZeRO / FSDP do?
Shard optimizer state, gradients and (stage 3) parameters across GPUs, gathering per layer on demand — near-data-parallel code, far higher memory ceiling.
Tensor vs pipeline parallelism?
Tensor splits inside a layer (bandwidth-hungry, one node, NVLink); pipeline splits by layers into stages (bandwidth-light, crosses nodes, has a bubble micro-batching hides).
What is 3D parallelism?
All three composed: tensor inside a node, pipeline across nodes, data across the groups — mapped onto the NVLink/InfiniBand topology.
Distributed training: how a model trains on thousands of GPUs
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Handbooks
- The Prompting HandbookA friendly, hands-on field guide for everyday humans — learn the CRISP framework, spot bad prompts, practice with real recipes, play a drag-and-drop game, and test yourself with a quiz. No code required.Read →
- The Senior AI Engineer Interview Handbook60 questions across architecture, production incidents, agentic systems, RAG, evals, cost, safety, and leadership — what staff-level AI interviewers actually probe for.Read →
- The LLM Serving HandbookHow to serve large language models fast and cheap — prefill vs decode, the KV cache, continuous batching and PagedAttention (vLLM), quantization, speculative decoding, and the latency-vs-throughput tradeoffs that decide your inference bill.Read →
- The Transformers HandbookThe architecture behind every LLM, built up from scratch — tokens and embeddings, positional encoding, self-attention (query/key/value), multi-head attention, the transformer block (FFN, residuals, layer norm), and how a decoder-only model generates text autoregressively.Read →
- The Fine-Tuning HandbookWhen to fine-tune vs RAG vs prompting, full fine-tuning vs parameter-efficient methods, how LoRA and QLoRA make it cheap enough for a single GPU, why the data matters more than the method, catastrophic forgetting, and how to evaluate and decide.Read →
- The On-Device AI HandbookSmall models, everywhere: why 1–14B models got frontier-adjacent (distillation + textbook data), the napkin law of local inference (tokens/sec ≈ bandwidth ÷ bytes), quantization and the llama.cpp / GGUF / Ollama / MLX stack, the honest local-vs-cloud decision table, four hybrid patterns that ship, and the evals craft of choosing a small model that actually fits the device.Read →