Distributed Training

Data, model and pipeline parallelism with PyTorch DDP, FSDP and DeepSpeed for training across many GPUs and machines.

Advanced · about 10 hours · difficulty 5/5

Learn first: GPUs and Efficient Training

When a model takes days on one GPU, or simply does not fit in its memory, you spread the work across devices. The options differ in what gets split.

Kinds of parallelism

  • Data parallelism: every GPU holds a full model copy, processes a different slice of the batch, and gradients are averaged with an all-reduce. PyTorch DistributedDataParallel (DDP) is the standard.
  • Sharded data parallelism: FSDP and DeepSpeed ZeRO split parameters, gradients and optimizer states across GPUs so much larger models fit.
  • Tensor (model) parallelism splits individual layers across devices.
  • Pipeline parallelism places different layers on different devices and streams micro-batches through them.

Large language model training combines all of these (often called 3D parallelism).

Memory tricks on any setup

Mixed precision (bf16), gradient accumulation to simulate larger batches, activation checkpointing to trade compute for memory, and 8-bit optimizers.

Practicalities

  • Launch with torchrun; use DistributedSampler so each process sees different data.
  • Scale the learning rate (and warmup) when the global batch grows.
  • Only rank 0 should log and save checkpoints.
  • Interconnect bandwidth (NVLink, InfiniBand) often limits speedup; measure scaling efficiency.
  • Accelerate, Lightning and Ray Train hide much of the boilerplate.

Good enough to move on when you can convert a single-GPU script to DDP, train with FSDP on a model that would not fit otherwise, and report the speedup you achieved.

Key concepts

  • Data parallelism and all-reduce
  • PyTorch DDP and torchrun
  • FSDP and DeepSpeed ZeRO
  • Tensor and pipeline parallelism
  • Gradient accumulation
  • Activation checkpointing
  • Accelerate and Ray Train

Mini project

Take a transformer fine-tuning script, run it with DDP on 2 or more GPUs (a rented cloud instance is fine), then switch to FSDP with activation checkpointing and record throughput, memory per GPU and final accuracy for each setup.

Check yourself

What is kept on every GPU under DDP that FSDP shards instead, and why does that let FSDP train larger models?

Free resources