Dive into Deep Learning · §13.6
Distributed Data Parallel (DDP) in Practice: Bucketing and Overlap
multi-process vs. compiler SPMD · gradient bucketing · backward overlap · BatchNorm semantics · scaling & SM contention
Motivation
backward() finished.torchrun), launching bucketed allreduces during backward().Execution Models
torchrun + DDP): Spawns k independent Python processes (1 per GPU); autograd hooks fire asynchronous NCCL bucket allreduces during backward().PyTorch DDP
torchrun ranks drives its own GPU concurrently, eliminating the k \cdot h_{\textrm{step}} single-thread bottleneck.Multi-Process Workflow
torchrun --standalone --nproc-per-node=k spawns k processes; each binds to its assigned GPU via LOCAL_RANK and wraps the model in DDP:
rank, k, device = d2l.init_distributed() # join the NCCL process group
model = d2l.resnet18(10).to(device)
model = DDP(model, device_ids=[device]) # broadcasts weights from rank 0
for s in range(len(y) // (k * b)): # rank r takes slice (s*k + r)*b
i = perm[(s * k + rank) * b:][:b] # disjoint shard of global batch k*b
F.cross_entropy(model(X[i]), y[i]).backward() # hooks launch bucket allreduces
opt.step(); opt.zero_grad(set_to_none=True)DDP broadcasts initial parameters and buffers from rank 0 at construction time and registers autograd hooks on every parameter to fire bucketed NCCL allreduces during backward().
Communication Overlap
Bucket Model
t_{\textrm{exposed (ideal)}} \;=\; \frac{2(k-1)}{k}\, S_{\textrm{last\_bucket}}\, \beta_{\textrm{link}}
DDP Buckets
PyTorch DDP closes the first bucket at 1\text{ MiB} (so communication starts almost immediately) and subsequent buckets at bucket_cap_mb = 25 (25\text{ MiB} = 26.2\text{ MB}):
reversed(list(net.parameters())) forms three buckets of 9.4\text{ MB}, 26.2\text{ MB}, and 9.0\text{ MB} (a single 9.4\text{ MB} late-stage Conv2d weight tensor overshoots the 1\text{ MiB} first cap on its own).BatchNorm Semantics
Unlike LeNet, ResNet-18 contains BatchNorm2d layers. Comparing one 2-GPU DDP step on 256 images against (a) one GPU on all 256 images and (b) one GPU accumulating two 128-image halves reveals what BatchNorm2d computes:
largest update 1.7e-02
largest difference from one GPU on all 256: 1.0e-03
largest difference from one GPU on two accumulated halves: 1.2e-07
Synchronizing Statistics
When does per-shard BatchNorm matter, and what does synchronizing it cost?
nn.SyncBatchNorm is needed: In object detection or segmentation with tiny per-GPU batches (b = 1\text{--}4 high-resolution images/GPU), local statistics are too noisy; nn.SyncBatchNorm.convert_sync_batchnorm(model) inserts an allreduce at every BatchNorm layer during the forward and backward passes (adding dozens of latency-bound \alpha synchronization points).Practical Pitfalls
rank in the slice offset (or sharding indices with P()) makes all k GPUs compute on the exact same b examples, wasting k-1 GPUs.Paper Predictions
First, we measure \beta_{\textrm{link}} on a bare 256\text{ MB} NCCL allreduce (\sim 2.4\text{ GB/s} bus bandwidth, or \beta_{\textrm{link}} \approx 0.42\text{ ns/B}) and compute our two theoretical bounds on exposed communication t_{\textrm{step}}(k) - t_{\textrm{step}}(1) under weak scaling:
bus bandwidth = 2.44 GB/s (beta_link = 0.41 ns/byte)
k=2: exposed 18 ms without overlap (1.0 S beta_link), 4 ms with full overlap (last bucket)
k=4: exposed 27 ms without overlap (1.5 S beta_link), 5 ms with full overlap (last bucket)
Weak Scaling Measured
Fixing the per-GPU batch size at b = 256 images/GPU across k \in \{1, 2, 4\} GPUs measures weak-scaling throughput and exposed communication t_{\textrm{step}}(k) - t_{\textrm{step}}(1):
1 GPU(s): 2285 examples/s, speedup 1.00, efficiency 100%
2 GPU(s): 4039 examples/s, speedup 1.77, efficiency 88%
predicted 86% without overlap, 97% with full overlap; exposed 15 ms
4 GPU(s): 7573 examples/s, speedup 3.31, efficiency 83%
predicted 80% without overlap, 95% with full overlap; exposed 23 ms
Four GPUs achieve 3.1\text{--}3.3\times single-GPU throughput (77\text{--}83\% scaling efficiency), and the measured exposed communication matches the no-overlap prediction (\sim 18\text{ ms} at k=2, \sim 28\text{ ms} at k=4) rather than the ideal bucket model.
Strong Scaling Measured
Strong scaling at a fixed global batch B = 512 (b = 512/k per GPU), k \in \{1, 2, 4\}:
1 GPU(s), 512 examples each: speedup 1.00, efficiency 100%
2 GPU(s), 256 examples each: speedup 1.78, efficiency 89%
4 GPU(s), 128 examples each: speedup 2.81, efficiency 70%
Testing Bucket Assumptions
To discover why exposed communication matched the no-overlap formula even though buckets launched during backward(), we profile GPU 0 across three runs: (1) 1 GPU, (2) 2 GPUs without communication (no_sync / shard_map), and (3) 2 GPUs with full communication:
1 GPU: 112.1 ms per step; GPU 0 computes 111.9 ms, communicates 0.0 ms
2 GPUs without communication: 111.9 ms per step; GPU 0 computes 111.5 ms, communicates 0.0 ms
2 GPUs: 126.8 ms per step; GPU 0 computes 124.6 ms, communicates 20.0 ms
Assumption 3 of the bucket model failed: While NCCL’s communication kernels overlap in time with backpropagation, the backward compute kernels running alongside NCCL slow down by 8\text{--}13\text{ ms}, clawing back most of the time saved by overlap.
Transport Bottleneck
Why does running an NCCL allreduce slow down concurrent convolution kernels on our workstation?
SHM) by running dedicated GPU SM kernels that read/write over PCIe and sum chunks on the SMs.bus bandwidth 2.44 GB/s by default, 11.53 GB/s with the copy engines (4.7x)
Offloading host-staged copies to DMA copy engines (NCCL_SHM_USE_CUDA_MEMCPY=1) boosts bare allreduce bandwidth 4\text{--}5\times, but deadlocks PyTorch DDP when overlapped with backward compute; always verify transport flags on the actual training loop.
Interconnect Impact
Check Your Understanding
Test your grasp of production data parallelism with three diagnostic questions:
model.no_sync() on the first 4 micro-batches), what does the weak-scaling efficiency E(4) become?BatchNorm, which single-GPU reference does default DDP match?Answers:
backward() produces gradients from layer L back to layer 1. A forward-ordered bucket containing layer 1 could not launch until the very end of backward().DDP uses per-shard statistics by default (matching 1 GPU accumulating two B/2 halves).Key Takeaways
torchrun + DDP (k processes) eliminates the single-thread k \cdot h_{\textrm{step}} bottleneck.