Repository navigation
DDP Gradient Synchronization
The same model parameters are replicated on each rank. Each rank sees different examples chosen by DistributedSampler and computes a local loss. During backward, DDP's gradient synchronization (typically bucketed AllReduce) ensures ranks receive consistent averaged gradients before their optimizer updates.
For R ranks with per-rank gradients g_r (under DDP's usual reduction behavior), the synced gradient is approximately:
g_synced = (1/R) * sum(g_r for r in ranks)
The implementation performs an additional dist.all_reduce on a detached loss scalar after the optimizer update, solely to display mean training loss. Do not confuse that reporting collective with gradient synchronization.
For this project, with no gradient accumulation:
effective_global_batch = --batch-size × WORLD_SIZE
For example, two ranks with --batch-size 2 process four training examples per synchronous optimizer step, subject to sampler padding and end-of-epoch behavior.
- Model parameters, optimizer state and activations must fit on each GPU; this is not FSDP.
-
DistributedSamplermay duplicate indices when the dataset length is not evenly divisible by world size. - Each rank must execute the same number of training steps and collectives.
-
sampler.set_epoch(epoch)changes the shuffle seed each epoch. - The code does not implement gradient accumulation, ZeRO, or fault-tolerant recovery.
Distributed LLM Training · PyTorch DDP (educational) — Home · System-Architecture · Low-Level-Design · Publishing-the-Wiki
This project is licensed under the Apache License, Version 2.0.© 2026 Sudhanshu Shukla · Licensed under the Apache License, Version 2.0.