Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  Distributed Training
AI Engineering~13 min readAdvanced
Deep Dive

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.

01

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.

per param = fp16 W (2) + fp16 grad (2) + fp32 master (4) + Adam m (4) + Adam v (4) = 16 bytes
7B model → 7×10⁹ × 16 = 112 GB of state  — on a card that holds 80. It doesn't fit before you've trained a single step.

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.

→ The two problems, kept separate

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.

02

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.

03

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 sharding stages — more sharded, more memory saved, more communication
Stage 1shard optimizer states (the 12-byte chunk) — biggest saving per unit pain.
Stage 2also shard gradients.
Stage 3 / FSDPalso shard parameters themselves — gather per layer, use, discard. Trains models far past one GPU's memory.

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.

04

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.

05

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:

bubble = (P − 1) / (M + P − 1)  →  P=4, M=1: 3/4 = 75% idle  ·  M=32: 3/35 = 9%

Same 4 GPUs, same model — the only change is splitting the batch into more micro-batches. That's the entire trick.

06

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.

→ The metric that rules the cluster

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.

07

The memory tricks you'll reach for first

TechniqueWhat it buys
Mixed precision (BF16/FP8)half+ the memory and ~2× compute on tensor cores — the free lunch, on by default
Gradient accumulationsimulate a huge batch on small memory — accumulate over K micro-steps, then update
Activation checkpointingstore fewer activations, recompute them in backward — trade compute for memory
CPU/NVMe offloadpark cold optimizer state off-GPU (ZeRO-Infinity) — slow but makes the impossible fit
Gradient clippingnot 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.

RUN IT YOURSELF

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.

CPython · WebAssembly
By the numbers

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.

StepQuestionArithmeticAnswer
1Total training state13×10⁹ × 16 bytes208 GB
2Plain data parallel, per GPUevery GPU holds all of it208 GB: does not fit
3ZeRO-1 (shard optimizer)13×10⁹ × (2 + 2 + 12/8)71.5 GB: 8.5 GB left for activations
4ZeRO-2 (+ shard gradients)13×10⁹ × (2 + 14/8)48.75 GB: 31.25 GB left
5ZeRO-3 / FSDP (+ shard weights)208 GB ÷ 826 GB: 54 GB left
6Gradient traffic per GPU per step, ZeRO-1/2ring all-reduce: 2 × 7/8 × 26 GB of fp16 grads45.5 GB
7Same, ZeRO-3two all-gathers + one reduce-scatter: 3 × 7/8 × 26 GB68.25 GB (1.5×)
8Micro-steps per optimizer step1,024 ÷ (8 GPUs × micro-batch 4)32 accumulation steps
9Grow to 4 nodes, ZeRO-3208 GB ÷ 326.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.

Trade-offs

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.

CaseTimeSpaceWhy
Data parallelOne gradient all-reduce: about 2 × 2N bytes sent per GPU16NSimplest; the traffic overlaps with backward, layer by layer.
ZeRO-1Same as data parallel(4 + 12/G)·NFree memory: the optimizer chunk was the biggest and needed no extra traffic.
ZeRO-2Same as data parallel (reduce-scatter + all-gather)(2 + 14/G)·NGradients are reduced straight into their owner’s shard.
ZeRO-3 / FSDP1.5× data parallel: weights gathered for forward and again for backward16N / GNeeds prefetch to hide the gathers; a slow network shows up here first.
Tensor parallelAll-reduces inside every layer, forward and backwardWeights and their state ÷ TLatency-bound, so it stays on NVLink inside one node.
Pipeline parallelPoint-to-point activations at stage edges; idle (P−1)/(M+P−1)Layers ÷ P, plus activations of in-flight micro-batchesCheap to cross nodes; the bubble is the cost.
Activation checkpointingAbout one extra forward pass, roughly a third more computeActivations kept only at checkpointsPure 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.

Watch for

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.

In the wild

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.

Self-check

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.

Frequently asked

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.

▶  Watch it explained

Distributed training: how a model trains on thousands of GPUs

Distributed Training · AI Engineering · Vibe Engines · 2026
Finished this one? 0 / 209 Handbooks done
NextThe Synthetic Data Handbook Next on the “Training and adaptation” ladder · rung 4 of 7

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Handbooks