Skip to main content
AI on Kubernetes · GPU/NPULesson 10 of 20

Training One Model on Many GPUs

One training run spread over 4 GPUs on 2 nodes. Follow how each GPU gets its own slice of the data, how gradients are combined every step, how sharding fits a bigger model, and what happens when one worker dies. How the Job waits for GPU quota is in From Training Job to Model.

Layout1 GPU
Memory per GPUfits
Each step—
Jobplanning
Node A · 2 GPUs on NVLinkNode B · 2 GPUs on NVLinkDatasetSharded by rankTrainJobtorch-distributedCheckpointObject storageGPU · rank 0Node AGPU · rank 1Node AGPU · rank 2Node BGPU · rank 3Node BPod · torchrunnproc-per-node 2Pod · torchrunnproc-per-node 2Node networkInfiniBand · RoCE
RequestResponse / reportAppears in a later step

What you will learn

  • Data parallel: full copies, different data, averaged gradients
  • All-reduce, all-gather and reduce-scatter in NCCL
  • Why network bandwidth sets the speed of training
  • FSDP and ZeRO shard states to fit bigger models
  • torchrun ranks and a Kubeflow TrainJob on Kubernetes
  • One failed worker stops all, so checkpoint and restart

Choosing a parallelism​

ApproachWhat each GPU holdsCommunicationUse when
DDP (data parallel)Full model, gradients, optimizer stateAll-reduce of gradients once per stepThe model and its optimizer state fit on one GPU
ZeRO-1 / ZeRO-2Full weights, shard of optimizer state (and gradients)About the same as DDPOptimizer state is what does not fit
FSDP / ZeRO-3A shard of weights, gradients and optimizer stateAll-gather of weights, reduce-scatter of gradients, about 1.5× DDPEven the weights plus states do not fit on one GPU
Tensor parallelA slice of every layerCollectives inside every layerLayers are too large; keep inside one node on NVLink
Pipeline parallelA range of layersActivations between stagesThe model is too deep for one node; needs micro-batches

Try it yourself​

kubectl get clustertrainingruntimes # Runtimes a TrainJob can use, e.g. torch-distributed
kubectl get trainjobs -n <namespace> # Kubeflow Trainer v2 jobs
kubectl get jobset -n <namespace> # The JobSet the TrainJob created
kubectl get pods -n <namespace> -l jobset.sigs.k8s.io/jobset-name=<trainjob> -o wide # One Pod per node
kubectl logs -n <namespace> <pod> | grep -E "NCCL INFO|rank" # With NCCL_DEBUG=INFO set: transports and rings
kubectl exec -n <namespace> <pod> -- nvidia-smi topo -m # NVLink vs PCIe between the GPUs in a node
# Inside a container: one process per GPU, two nodes, rendezvous at the rank 0 Pod
torchrun --nnodes=2 --nproc-per-node=2 --rdzv-backend=c10d --rdzv-endpoint=<rank0-host>:29400 --rdzv-id=run1 train.py

Check yourself

  1. In DDP, why does every GPU run an all-reduce after the backward pass?
  2. A 7B model with Adam needs about 112 GB of states and each GPU has 80 GB. Why does adding more DDP GPUs not help?
  3. Rank 3 crashes in a 4-GPU training job. What happens to the other ranks?

Further reading: PyTorch DDP · PyTorch FSDP · torchrun · NCCL collectives · Kubeflow Trainer · From Training Job to Model · Splitting a Model Across GPUs (TP, PP, EP)