---
title: Distributed training and parallelism
url: https://gurkanozkan.ai/learn/topics/distributed-training/
markdown: https://gurkanozkan.ai/learn/topics/distributed-training.md
kind: topic
tracks:
  - Research
  - ML engineering
  - Applied LLM
json: https://gurkanozkan.ai/api/learn/topics/distributed-training.json
---

# Distributed training and parallelism

Once a model, its optimiser state or its activations exceed one accelerator, you need sharding and parallelism. Interviewers expect quick memory arithmetic (about 16 bytes per parameter for mixed-precision Adam, before activations), the menu of techniques from cheap to complex (bf16, gradient accumulation, activation checkpointing, ZeRO/FSDP, tensor, pipeline, sequence or context, and expert parallelism), what each technique splits and communicates, what collectives cost, how to keep convergence as the global batch grows, and the operational side: topology-aware placement, stragglers, failures and checkpoints. Applied roles meet the same ideas when sizing fine-tuning jobs and multi-GPU serving.

## Key concepts

### Memory accounting

Mixed-precision training with Adam holds bf16 weights (2 bytes per parameter), bf16 gradients (2), an fp32 master copy (4) and Adam's two moments (8): about 16 bytes per parameter, so 7B needs about 112 GB and 70B about 1.1 TB before activations. Activations scale with micro-batch × sequence length × hidden size × layers, and attention scores add a term quadratic in sequence length unless a fused kernel such as FlashAttention avoids storing them. 8-bit optimisers and FP8 training shrink the totals.

### Data parallelism

Every GPU holds a full replica and processes a different slice of the batch; gradients are averaged with an all-reduce, bucketed and overlapped with the backward pass. Throughput scales with GPU count, but per-GPU memory does not fall at all, so plain data parallelism cannot fit a model that is too big for one GPU.

### ZeRO and FSDP

ZeRO stage 1 shards optimiser state across the data-parallel group, stage 2 also shards gradients, and stage 3 (equivalent to PyTorch FSDP) shards parameters too, all-gathering each layer's weights just before use and reduce-scattering its gradients afterwards. Stages 1–2 communicate the same volume as plain data parallelism; stage 3 adds about 50%. CPU or NVMe offload trades speed for further memory savings.

### Tensor parallelism

Megatron-style tensor parallelism splits weight matrices inside a layer: the first MLP matrix by columns, the second by rows, and attention by heads, so each transformer layer needs two all-reduces of the activations in the forward pass and two in the backward pass. That traffic needs NVLink-class bandwidth, so tensor parallelism normally stays within a node (up to 8 GPUs). It also cuts per-GPU activation memory and latency, which is why serving uses it.

### Pipeline parallelism

Layers are split into p stages on different devices and each batch into m micro-batches that flow through the pipeline. Only stage-boundary activations are sent, point to point, so stages can span nodes. Idle "bubbles" occur while the pipeline fills and drains; with the 1F1B schedule the bubble overhead is about (p − 1)/m of the ideal compute time, and interleaved schedules divide it further at the cost of more communication.

### Sequence, context and expert parallelism

Sequence parallelism splits the non-tensor-parallel parts of a layer (normalisation, dropout) along the sequence to save activation memory; context parallelism (for example ring attention) splits very long sequences across GPUs. Mixture-of-experts models add expert parallelism: experts live on different devices and tokens are routed with all-to-all exchanges. All experts must sit in memory, so memory scales with total parameters while compute scales with active parameters.

### Collectives and their cost

A ring all-reduce is a reduce-scatter followed by an all-gather; each GPU sends and receives about 2(N − 1)/N × the message size, nearly constant in N, plus a latency term that grows with N. Tree and hierarchical algorithms cut latency to order log N. Bandwidth falls by roughly an order of magnitude from NVLink inside a node to InfiniBand or Ethernet between nodes, which drives where each kind of parallelism is placed.

### Large-batch optimisation and reliability

When the global batch grows k-fold, scale the learning rate roughly linearly with warmup (Goyal et al.), or use layer-wise adaptive methods (LARS, LAMB) for very large batches; beyond a critical batch size, extra batch stops buying faster convergence. Gradient accumulation emulates large batches but must average correctly. At thousands of GPUs, failures are routine: checkpoint frequently and asynchronously, detect stragglers and plan for automatic restarts. Low-communication methods relax lock-step training: DiLoCo synchronises replicas only every H steps (2025 scaling laws show it matching data parallelism when tuned), and RL post-training often runs rollout workers asynchronously from the trainer, where policy lag and rollout–trainer numerics mismatch must be corrected (importance sampling) or removed (bitwise-matched kernels) to stay stable.

## Worked examples

### Memory budget for fine-tuning a 7B model, with activations

Estimate GPU memory to fine-tune a 7B, Llama-2-like model (32 layers, hidden size 4,096, 32 heads) at sequence length 4,096 and micro-batch 1: full fine-tuning with Adam, LoRA (rank 16 on the q, k, v, o projections) and QLoRA. Then estimate activation memory with and without FlashAttention and activation checkpointing.

**Solution**

**Full fine-tune, model states.** 7 × 10⁹ × 16 bytes = **112 GB**: weights 14 + gradients 14 + fp32
master 28 + Adam moments 56. That exceeds one 80 GB GPU before any activations. With ZeRO-3 or FSDP over
8 GPUs: 112 / 8 = **14 GB per GPU**, plus activations and the transient all-gathered layer.

**LoRA.** Each adapted matrix adds r × (d_in + d_out) = 16 × (4,096 + 4,096) = 131,072 parameters;
4 matrices × 32 layers → **16.8M** trainable parameters (0.24% of the model). Their weights, gradients
and Adam state at 16 bytes each ≈ 0.27 GB. The frozen bf16 base is 14 GB, so model states ≈ **14.3 GB**.

**QLoRA.** The base in 4-bit NF4 ≈ 7 × 10⁹ × 0.5 bytes = 3.5 GB plus quantisation constants →
about **3.6–4 GB** of model states.

**Activations.** For a GPT-style layer storing 16-bit activations, Korthikanti et al. give about
s·b·h·(34 + 5·a·s/h) bytes per layer (s = sequence length, b = micro-batch, h = hidden size,
a = heads). Here s·b·h = 4,096 × 1 × 4,096 = 16.8M and 5·a·s/h = 5 × 32 × 4,096 / 4,096 = 160:
- No optimisation: 16.8M × 194 ≈ 3.25 GB per layer × 32 ≈ **104 GB**
- FlashAttention (no stored s × s attention matrices): 16.8M × 34 ≈ 0.57 GB per layer × 32 ≈
  **18 GB**
- Plus full activation checkpointing (store only each layer's input, 2·s·b·h bytes): 33.5 MB × 32 ≈
  **1.1 GB**, plus one layer's activations rebuilt during backward, at the cost of about one extra
  forward pass (~33% more compute)

So LoRA with FlashAttention and checkpointing fits in roughly 14.3 + 1.1 + 0.6 + workspace ≈ 20 GB, a
single 24–48 GB GPU, while full fine-tuning needs several GPUs with sharding. Exact constants differ for
SwiGLU MLPs and grouped-query attention, so measure with the profiler before committing to a plan.


### How long does the gradient all-reduce take?

You train the 7B model with ZeRO-2 (optimiser state and gradients sharded; same communication volume as plain data parallelism), reducing 14 GB of bf16 gradients per step. Estimate communication time on one 8-GPU node with about 200 GB/s of effective per-GPU bandwidth, and on 32 GPUs across 4 nodes with 50 GB/s per GPU (400 Gb/s networking). One optimiser step takes 1.5 s of compute.

**Solution**

**Ring volume per GPU** = 2(N − 1)/N × M.
- N = 8: 2 × 7/8 × 14 GB = 24.5 GB → 24.5 / 200 ≈ **0.12 s**
- N = 32: 2 × 31/32 × 14 GB = 27.1 GB → 27.1 / 50 ≈ **0.54 s**

Going from 8 to 32 GPUs barely changes the bytes per GPU (24.5 → 27.1 GB); the slower inter-node link is
what quadruples the time. That is why the claim "ring all-reduce traffic per GPU grows linearly with N"
is wrong.

**Latency term.** About 2(N − 1) sequential steps; at roughly 5 µs each, N = 32 adds 62 × 5 µs ≈
0.3 ms, negligible for 14 GB but dominant for many small tensors. Hence gradient bucketing (PyTorch DDP
defaults to 25 MB buckets) and tree or hierarchical algorithms at large N.

**Overlap.** Buckets are reduced while backward continues on earlier layers, so 0.54 s of communication
can hide largely inside 1.5 s of compute. Exposed communication shows up as falling MFU when you add
nodes. With ZeRO-3 the volume rises by about 50% (an extra parameter all-gather), to roughly 0.8 s
across nodes.


### Laying out a 175B model on 1,024 GPUs

Plan a layout for training a 175B dense transformer on 128 nodes of 8 GPUs (80 GB each, NVLink within a node, InfiniBand between nodes), with sequences of 2,048 tokens. Choose tensor, pipeline and data parallel degrees, estimate per-GPU model-state memory and the pipeline bubble, and state the global batch.

**Solution**

**Model states.** 175 × 10⁹ × 16 bytes = 2.8 TB, so sharding is mandatory.

**Layout.** Tensor parallelism t = 8 inside each node (its per-layer all-reduces need NVLink); pipeline
parallelism p = 16 across nodes (only stage-boundary activations cross InfiniBand); data parallelism
d = 1,024 / (8 × 16) = **8**.

**Per-GPU memory.** Each GPU holds 175B / (t × p) = 175B / 128 ≈ 1.37B parameters.
- Without optimiser sharding: 1.37B × 16 bytes ≈ **21.9 GB**
- With ZeRO-1 across the 8 data-parallel replicas (shard the 12 bytes of fp32 master weights and Adam
  state): 1.37B × (4 + 12/8) = 1.37B × 5.5 ≈ **7.5 GB**, leaving room for activations (which tensor and
  sequence parallelism also divide by t) and selective recomputation.

**Pipeline bubble.** With the 1F1B schedule, bubble overhead ≈ (p − 1)/m of the ideal time:
- m = 64 micro-batches per step: 15 / 64 ≈ **23%**
- Interleaved schedule with v = 4 model chunks per GPU: 15 / (4 × 64) ≈ **5.9%**, at the cost of more
  point-to-point traffic

**Global batch** = d × m × micro-batch size = 8 × 64 × 1 = 512 sequences × 2,048 tokens ≈
**1.05M tokens** per step. Raising m to 128 halves the bubble but doubles the batch to about 2M tokens,
which is acceptable only if it stays below the critical batch size for this stage of training.

**Operations.** Place pipeline neighbours on nearby switches, checkpoint every few hundred steps
asynchronously, watch for stragglers (one slow GPU stalls its whole pipeline) and keep spare nodes for
automatic restarts.


## What changed recently

- **Nov 2026: Asynchronous RL made stable by matching numerics.** NVIDIA Research and Inferact's REINFORCE Zero aligns rollout and trainer kernels so both compute bit-identical log-probabilities, then corrects policy lag with unclipped importance sampling; it trained stably for 20,000 updates, learned at a policy lag of 64 and reached 2.4× the step throughput of synchronous training. With standard, mismatched kernels, online AIME24 accuracy fell from 34.3% to 14.0%. Rollout/trainer separation is now common in RL post-training, and numerics mismatch is the failure mode to know. [Source](https://vllm.ai/blog/2026-11-06-zero-mismatch-async-rl)
- **Apr 2026: Decoupled DiLoCo: pre-training without lock-step.** Google researchers, with Jeff Dean as final author, proposed splitting pre-training across independent learners that send parameter fragments asynchronously to a synchronizer, which merges them with a minimum quorum, a grace window and token-weighted averaging; simulations with millions of chips showed no global downtime from failures or stragglers. Low-communication training is moving into the design space for multi-datacentre runs. [Source](https://arxiv.org/abs/2604.21428)
- **Dec 2025: INTELLECT-3: large-scale asynchronous agentic RL in the open.** Prime Intellect post-trained INTELLECT-3, a 106B-total, 12B-active MoE built on GLM-4.5-Air-Base, using its open prime-rl framework for asynchronous, multi-turn, tool-using RL on up to 512 H200 GPUs. It is a reference design for decoupling inference workers that generate rollouts from the trainer that consumes them. [Source](https://arxiv.org/abs/2512.16144)
- **Sep 2025: A 4-bit pretraining recipe at 10 trillion tokens.** NVIDIA trained a 12B model on 10T tokens in NVFP4 with loss and downstream accuracy comparable to an FP8 baseline, using random Hadamard transforms, two-dimensional weight scaling, stochastic rounding of gradients and a few layers kept in higher precision. Low-precision training now changes memory, communication and kernel choices across the whole parallel layout. [Source](https://arxiv.org/abs/2509.25149)
- **Mar 2025: Scaling laws for DiLoCo.** Google researchers showed that DiLoCo, which synchronises replicas only every H inner steps, scales predictably with model size and, when well tuned, can match or beat data-parallel training even at small sizes, with larger optimal batch sizes. It answers the interview question of how to train across data centres with limited bandwidth. [Source](https://arxiv.org/abs/2503.09799)

## Pitfalls

- Counting only the weights and forgetting gradients, the fp32 master copy, Adam state and activations.
- Assuming data parallelism reduces per-GPU memory; it only adds throughput.
- Running tensor parallelism across slow inter-node links.
- Ignoring pipeline bubbles, or using too few micro-batches to hide them.
- Growing the global batch 8× without adjusting learning rate and warmup, or mis-averaging accumulated gradients.
- Saying ring all-reduce traffic per GPU grows linearly with the number of workers, or claiming mixture-of-experts cuts memory.

## How interviewers probe it

- 'The model does not fit — list your options from cheapest to most complex', with memory numbers for each.
- 'What is split and what is communicated?' for data, ZeRO/FSDP, tensor, pipeline and expert parallelism (all-reduce, all-gather, reduce-scatter, point-to-point, all-to-all).
- 'Lay out a model of a few hundred billion parameters on this cluster', including topology, bubbles and failures.
- 'The global batch grows 8× — how do you keep convergence?'
- Research-maths variants: ring versus tree all-reduce cost, and bounding the rounds needed over noisy links.
- Applied variants: memory for full fine-tuning versus LoRA versus QLoRA, and tensor parallelism for serving a 70B model.

## Reading

- [Transformer Math 101](https://blog.eleuther.ai/transformer-math/) (Blog, 30 min, Intro): Compute and memory rules of thumb (bytes per parameter, activations, communication) for training transformers.
- [The Ultra-Scale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook) (Book, 240 min, Deep): A hands-on, measured walk through data, tensor, pipeline, context and expert parallelism on GPU clusters.
- [How to Scale Your Model](https://jax-ml.github.io/scaling-book/) (Book, 300 min, Deep): A systems view of how hardware limits shape parallelism choices for training and inference.
- [ZeRO: Memory Optimizations Toward Training Trillion Parameter Models](https://arxiv.org/abs/1910.02054) (Paper, 50 min, Core): The stage-by-stage sharding of optimiser state, gradients and parameters, with communication analysis.
- [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053) (Paper, 40 min, Core): The column/row split of transformer layers that defines tensor parallelism.
- [Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM](https://arxiv.org/abs/2104.04473) (Paper, 60 min, Deep): Combining tensor, pipeline and data parallelism, with the 1F1B and interleaved schedules and bubble analysis.
- [PyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel](https://arxiv.org/abs/2304.11277) (Paper, 45 min, Deep): How full sharding is implemented in PyTorch and what limits its performance.
- [Pretraining Large Language Models with NVFP4](https://arxiv.org/abs/2509.25149) (Paper, 60 min, Deep): The recipe for 4-bit pretraining at scale: Hadamard transforms, 2-D block scaling, stochastic rounding and which layers stay in higher precision.

## Practise this topic

- [Roughly how much GPU memory does full fine-tuning of a 7B model with Adam in mixed precision need, compared with LoRA and QLoRA?](https://gurkanozkan.ai/learn/questions/applied-llm-ft-09.md)
- [Will a 70B dense model serve 32 concurrent users at 32k context on two 80 GB GPUs? Explain tensor, pipeline and data parallelism along the way.](https://gurkanozkan.ai/learn/questions/applied-llm-srv-12.md)
- [Your model plus optimiser state exceeds one GPU's memory. List the options, from cheapest to most complex.](https://gurkanozkan.ai/learn/questions/mle-training-a-model-that-does-not-fit-in-gpu-memory-17.md)
- [Compare data parallelism, FSDP/ZeRO, tensor parallelism and pipeline parallelism: what is split, what is communicated, and when to combine them.](https://gurkanozkan.ai/learn/questions/mle-data-tensor-pipeline-parallelism-and-sharding-55.md)
- [How does ring all-reduce scale with the number of workers? How do you keep convergence when the global batch grows 8×?](https://gurkanozkan.ai/learn/questions/mle-all-reduce-gradient-accumulation-and-large-batch-training-56.md)
- [How does a mixture-of-experts layer work, what problems does routing introduce, and how does MoE change the memory, compute and communication profile compared…](https://gurkanozkan.ai/learn/questions/research-mixture-of-experts-038.md)
- [You must train a model that doesn't fit in one accelerator's memory. Lay out your options and how you would combine them for a model of a few hundred billion…](https://gurkanozkan.ai/learn/questions/research-model-that-doesnt-fit-040.md)
- [Explain how all-reduce works across many nodes (ring vs tree) and what it costs in bandwidth and latency. Then reason about how many rounds you need for a…](https://gurkanozkan.ai/learn/questions/research-all-reduce-and-noisy-communication-044.md)
- [Roughly how much accelerator memory do weights, gradients and Adam state need for a 70-billion-parameter model trained in mixed precision? Show the accounting…](https://gurkanozkan.ai/learn/questions/research-memory-arithmetic-for-training-041.md)
- [Midway through a large pretraining run, the loss suddenly spikes and sometimes diverges. How do you diagnose the cause, and which mitigations would you…](https://gurkanozkan.ai/learn/questions/research-loss-spikes-in-a-large-run-046.md)
- [Explain the Muon optimiser. What does it do to the momentum update, which parameters does it apply to, and what extra changes were needed to use it for…](https://gurkanozkan.ai/learn/questions/recent-a-dl-01.md)
- [NVIDIA reported pretraining a 12B model on 10 trillion tokens in NVFP4 with loss and accuracy comparable to FP8. Explain the format, why naive 4-bit training…](https://gurkanozkan.ai/learn/questions/recent-a-dist-01.md)
- [Explain DiLoCo-style low-communication training and what the 2025 scaling-law results showed. When would you use it instead of standard data parallelism?](https://gurkanozkan.ai/learn/questions/recent-a-dist-02.md)
- [Your GRPO run generates rollouts with a fast inference engine and computes gradients in a separate FSDP trainer, partly asynchronously. Reward climbs for a few…](https://gurkanozkan.ai/learn/questions/recent-b-reinforcement-learning-02.md)
- [Qwen3.5-397B-A17B activates 10 of 512 routed experts plus one shared expert per token. Compared with Mixtral-style 2-of-8 routing, what do fine-grained and…](https://gurkanozkan.ai/learn/questions/recent-b-transformers-03.md)
- [During a pre-training run on thousands of accelerators you see occasional loss spikes with no NaNs and no hardware errors reported. How do you decide whether…](https://gurkanozkan.ai/learn/questions/recent-b-systems-reliability-03.md)
- [Muon has gone from small-scale experiments to training Kimi K2 and DeepSeek-V4. Explain what Muon computes and for which parameters, why it needed weight decay…](https://gurkanozkan.ai/learn/questions/recent-b-training-01.md)
- [You must shuffle a training set of hundreds of terabytes (for example, cached model activations) that cannot fit in memory on any one machine. Design an…](https://gurkanozkan.ai/learn/questions/research-shuffling-hundreds-of-terabytes-043.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
