advanced31 min studyLesson 22 in Engineering

Distributed training parallelism

Choose data, sharded, tensor, and pipeline parallelism by reasoning about memory, communication, and synchronization.

On this page

What you’ll understand

  • Explain what DDP replicates and what FSDP shards.
  • Estimate a collective's bandwidth cost and useful scaling efficiency.
  • Match a parallelism strategy to memory and network constraints.

Eight accelerators do not automatically make a training step eight times faster. Each worker needs the correct data, weights, and gradients at the correct time. The engineering question is what to replicate, what to partition, and how much communication the resulting dependency graph requires. Begin with a single-device memory and timing profile; otherwise a complicated distributed design may merely distribute an unmeasured bottleneck.

Separate statistical work from synchronization

Distributed data parallelism, or DDP, places a model replica on each worker; the application must arrange data partitioning. Workers synchronize gradients so compatible optimizer updates keep replicas aligned. PyTorch DDP buckets gradients to overlap communication with backward computation. For equal local batch sizes, averaging local mean gradients produces the global mean. With unequal valid-token or example counts, explicitly normalize for the intended global loss; worker averaging does not automatically supply those weights.

Global batch size is local batch size times data-parallel workers times accumulation steps, assuming equal batches. Increasing it changes optimization, so compare convergence at a matched token or example budget, not just steps per second. Accumulation trades activation memory and synchronization frequency against update frequency. All workers must participate in compatible collective sequences; one rank skipping a backward pass while others synchronize can hang a job rather than simply omit its contribution.

g=∑i=1pnigi∑i=1pni,Bglobal=pbag=\frac{\sum_{i=1}^{p} n_i g_i}{\sum_{i=1}^{p}n_i},\qquad B_{\mathrm{global}}=pba
Worker i reports a mean gradient over ni equally weighted examples or valid tokens. The batch formula assumes p data-parallel workers, b local examples, and a accumulation steps; tensor-parallel workers do not multiply this batch.↔ Scroll wide equations horizontally.

Shard state when replication cannot fit

A model's training memory includes weights, gradients, optimizer state, activations, temporary buffers, and communication workspaces. FSDP shards persistent model state over a data-parallel group. In the usual fully sharded execution, workers gather parameters for computation and reduce-scatter gradients afterward. Peak memory still includes temporarily gathered layers and activations. Dividing the checkpoint size by device count is therefore an incomplete peak-memory estimate; the wrapping and prefetch schedule affect which buffers coexist.

Tensor parallelism partitions operations inside a layer, introducing collectives along the forward and backward critical paths. It benefits from fast links between cooperating devices. Pipeline parallelism assigns layers to stages and sends activations between them; microbatches reduce idle pipeline bubbles but introduce scheduling and memory tradeoffs. These methods can be combined in separate dimensions. Put frequent, latency-sensitive communication on fast links and examine stragglers before assuming a larger cross-node group will improve throughput.

Budget the network and measure overlap

For an ideal ring all-reduce, each rank transfers approximately 2(p−1)G/p2(p-1)G/p bytes for a gradient buffer of GG bytes. Dividing by effective link throughput gives a bandwidth-time estimate. This is a model of a particular algorithm, not a guarantee about a collective library: topology, startup latency, contention, and chunking matter. Use effective bytes per second measured under the job's communication pattern rather than the link's advertised bit rate.

A step's exposed synchronization cost is communication that remains after possible overlap with computation. Perfect overlap is limited by dependency order, not just by total compute time. Profile timelines and compare step time, examples per second, peak memory, and validation behavior. Strong scaling holds global work fixed; weak scaling grows work with workers. Report which experiment you ran. A fast training step that silently drops examples, changes loss normalization, or resumes incorrectly is not a successful infrastructure optimization.

tring≈2(p−1)Gpβ,η=T1pTpt_{\mathrm{ring}}\approx\frac{2(p-1)G}{p\beta},\qquad \eta=\frac{T_1}{pT_p}
G is gradient bytes, β is effective bytes/s per rank for the ideal ring, and time is seconds. Strong-scaling efficiency η compares equal work taking T1 on one worker and Tp on p workers; latency and imperfect overlap are omitted from the ring estimate.↔ Scroll wide equations horizontally.

Estimate an eight-worker gradient exchange

Eight DDP workers synchronize a 4 GB gradient buffer over an effective 25 GB/s path. Use decimal GB. Assume a ring and no latency overhead; 0.12 s of communication can overlap computation.

  1. The ideal per-rank traffic is 2 × 7/8 × 4 = 7 GB, not merely 4/8 GB.

  2. At 25 GB/s, the bandwidth estimate is 7/25 = 0.28 seconds.

  3. Subtract only the measured 0.12-second overlap, leaving approximately 0.16 seconds exposed on the step's critical path.

  4. If useful computation takes 0.40 seconds and no other cost exists, estimate a 0.56-second step. Validate that profile before changing worker count or collective settings.

The result

The approximate exposed communication is 160 ms. Sharding gradients does not remove the need to move and combine them.

Explore the model

Interactive lab

Interactive lab · queue

Explore communication backlog

Treat each item as one equal-sized communication buffer. Change arrival rate λ and service rate μ to inspect the fluid backlog max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time readout assumes Poisson arrivals and exponential service; neither model implements collectives, topology, or compute overlap.

Explore communication backlogFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forCommunication cannot stay hidden indefinitely when buffers are produced faster than they can be transferred.

Check your understanding

Why can an FSDP job run out of memory even if persistent state divided by worker count appears to fit?

Try it yourself

Four data-parallel workers use a local batch of eight examples and accumulate three steps. A tensor-parallel group of two devices implements each worker. What is the global batch? In a separate test without tensor parallelism, one device takes 1.8 s for fixed work and four devices take 0.6 s. Find strong-scaling efficiency for that test.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Tensor-parallel devices cooperate on each example rather than adding distinct examples. The separate timing experiment compares speedup with its four-device count.

Compare with the solution

The global batch is 4 × 8 × 3 = 96 examples, independent of the tensor-parallel factor. In the separate four-device equal-work test without tensor parallelism, speedup is 1.8/0.6 = 3 and efficiency is 3/4 = 75%. Keep device counts and baselines consistent when reporting a physical-device efficiency.

Take it with you

  • DDP replicates computation across different data; FSDP shards persistent state.
  • Tensor and pipeline parallelism change different dependencies and require appropriate topology.
  • Measure exposed communication and convergence, not worker count alone.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.