# Distributed Training

> Data, model and pipeline parallelism with PyTorch DDP, FSDP and DeepSpeed for training across many GPUs and machines. Free notes, key concepts, resources and a...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/distributed-training
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/machine-learning/gpu-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

- [PyTorch distributed overview](https://pytorch.org/tutorials/beginner/dist_overview.html) (PyTorch)
- [Accelerate documentation](https://huggingface.co/docs/accelerate/index) (Hugging Face)
- [DeepSpeed documentation](https://www.deepspeed.ai/) (Microsoft)

Previous: [Feature Stores](https://www.internhack.in/roadmaps/tracks/machine-learning/feature-stores) · Next: [Model Compression and Fast Inference](https://www.internhack.in/roadmaps/tracks/machine-learning/model-optimization-inference)

## More in Scaling Up: Distributed Training and Efficient Inference

- [Distributed Training](https://www.internhack.in/roadmaps/tracks/machine-learning/distributed-training)
- [Model Compression and Fast Inference](https://www.internhack.in/roadmaps/tracks/machine-learning/model-optimization-inference)
- [Cloud ML Platforms](https://www.internhack.in/roadmaps/tracks/machine-learning/cloud-ml-platforms)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
