Dive into Deep Learning · §13.5
Distributed Collectives and Data Parallelism from Scratch
parallelism taxonomy · broadcast & allreduce · ring allreduce · step cost model · strong & weak scaling
Motivation
Parallelism Taxonomy
fp32 Adam) on every GPU, shards the minibatch along the batch axis, and exchanges gradients once per step via an allreduce.Data Parallelism
Collective Primitives
Data parallelism relies on two collective communication operations across k devices:
broadcast(params, devices): Copies initial model parameters from device 0 to all k devices once at the start of training.allreduce(grads): Sums one tensor x_j from each device j \in \{0, \ldots, k-1\} and leaves the full sum \sum_{j=0}^{k-1} x_j on every device.before: [[[1.0, 1.0]], [[2.0, 2.0]]]
after: [[[3.0, 3.0]], [[3.0, 3.0]]]
Mathematical Invariant
\frac{1}{k} \sum_{j=1}^{k} \mathbf{g}_j \;=\; \nabla_{\boldsymbol{\theta}} \left(\frac{1}{B} \sum_{i \in \mathcal{B}} \ell_i(\boldsymbol{\theta})\right)
allreduce computes their sum, dividing by k recovers the exact global gradient.Verification Discipline
Before timing any multi-GPU loop, we run one SGD step on B = 256 Fashion-MNIST images on 1 GPU and on 2 GPUs (in strict float32 without tf32 rounding) from the exact same initial weights:
largest update 2.4e-02, largest difference 3.0e-08
The single-GPU and two-GPU parameter updates agree to float32 floating-point rounding (\sim 10^{-7} vs. update magnitude \sim 10^{-2}), proving that our shard slicing, loss scaling, allreduce, and 1/k division are exact.
Star vs. Ring Topology
Ring Allreduce
Ring Simulation
Each of the 2(k-1) ring steps transmits one chunk of S/k bytes per GPU, so total bytes sent per GPU is \frac{2(k-1)}{k} S < 2S:
k = 2: every device holds the sum: True; sent per device: 1.00 S
k = 4: every device holds the sum: True; sent per device: 1.50 S
k = 8: every device holds the sum: True; sent per device: 1.75 S
k = 16: every device holds the sum: True; sent per device: 1.88 S
Step Cost Model
Using Hockney’s latency–bandwidth model, each allreduce call pays a fixed start-up latency \alpha plus a transfer time \beta_{\textrm{link}} per byte sent per device (the inverse of the allreduce bus bandwidth):
t_{\textrm{step}}(k) \;\approx\; t_{\textrm{compute}}(B/k) \;+\; c\,\alpha \;+\; \frac{2(k-1)}{k}\,S\,\beta_{\textrm{link}}
Link Calibration
We calibrate \alpha with a 1-float (4\text{ B}) allreduce and \beta_{\textrm{link}} with a 2^{26}-float (268\text{ MB}) allreduce across two RTX 4090s:
alpha = 56 us, beta_link = 0.11 ns/byte (bus bandwidth 9.5 GB/s)
Envelopes vs. Bytes
LeNet has 10 parameter tensors (5 weights, 5 biases) totaling 61{,}706 float32 parameters (S = 247\text{ KB}). Allreducing them one by one (c = 10) versus concatenated into a single flat vector (c = 1) moves the exact same 247\text{ KB}:
separately: 572 us measured, 587 us predicted
at once : 81 us measured, 82 us predicted
The price is in the envelopes (9\alpha): For a 247\text{ KB} gradient, S\beta_{\textrm{link}} \lesssim 100\,\mu\text{s}, so launching 9 extra allreduces costs more time in start-up latency than transferring all the gradient bytes. Always fuse or bucket small gradients before communicating.
Local Step Profile
Before timing LeNet on two GPUs at global batch size B = 256, we profile its local computation (communicate=False) on one GPU:
wall clock 2.91 ms per step, device busy 0.30 ms (10%), matrix products 43%
0.05 ms 16% Kernel<cutlass_tensorop_s1688dgrad_optimized_tf32_12
0.04 ms 12% implicit_convolve_sgemm<float, float, 128, 5, 5, 3,
0.03 ms 10% wgrad_alg0_engine<float, 128, 5, 5, 3, 3, 3, false,
0.03 ms 9% avg_pool2d_backward_out_cuda_frame<float, float, int
Host Dispatch Ceiling
1 GPU(s), 256 examples each: 2.91 ms
1 GPU(s), 128 examples each: 2.80 ms
2 GPU(s), 128 examples each: 5.60 ms
Accounting for Slowdown
Replacing t_{\textrm{compute}}(B/k) by \max(k\,h_{\textrm{step}},\, t_{\textrm{dev}}(B/k)) accounts for both single-thread host dispatch and device execution:
t_{\textrm{step}}(k) \;\approx\; \max\!\bigl(k\,h_{\textrm{step}},\; t_{\textrm{dev}}(B/k)\bigr) \;+\; c\,\alpha \;+\; \frac{2(k-1)}{k}\,S\,\beta_{\textrm{link}}
1 GPU(s): 2.88 ms per step, test accuracy 0.87
2 GPU(s): 5.69 ms per step, test accuracy 0.87
2 GPUs predicted: 2.88 ms per step by eq_dp_cost, 5.85 ms with one launcher
At global batch B = 256, two GPUs run nearly 2\times slower than one GPU—not because of the 247\text{ KB} gradient exchange (\sim 0.1\text{ ms}), but because the single host launcher takes 2h_{\textrm{step}} to issue work to two cards.
Scaling Laws
Amortizing Overheads
Increasing the per-GPU batch size b from 256 \to 4{,}096 \to 32{,}768 pushes device execution time t_{\textrm{dev}}(b) past both 2h_{\textrm{step}} and the gradient exchange \alpha + S\beta_{\textrm{link}}:
b = 256: 1 GPU 2.83 ms (device busy 0.30 ms), 2 GPUs 5.83 ms
speedup 0.99 predicted, 0.97 measured (windows 0.92 to 0.98)
b = 4096: 1 GPU 3.01 ms (device busy 3.00 ms), 2 GPUs 6.11 ms
speedup 1.05 predicted, 0.99 measured (windows 0.98 to 0.99)
b = 32768: 1 GPU 25.26 ms (device busy 25.35 ms), 2 GPUs 25.53 ms
speedup 1.99 predicted, 1.98 measured (windows 1.98 to 1.98)
Check Your Understanding
Use our multi-GPU cost model to answer two concrete scaling questions:
float32 gradients S = 4\text{ GB}) trains on k = 8 GPUs. How many bytes does the busiest GPU send per step in a star allreduce versus a ring allreduce?bfloat16. Without communication overlap, what is the strong-scaling speedup of k = 2 GPUs over 1 GPU when the inter-GPU bus bandwidth is (a) 10\text{ GB/s} vs. (b) 2.5\text{ GB/s}?Answers:
Key Takeaways
| Concept / Mechanism | Mathematical Form / Rule | Value / Behavior | Role in Multi-GPU Training |
|---|---|---|---|
| Data Parallelism | \mathbf{g} = \frac{1}{k}\sum_{j=1}^k \mathbf{g}_j | Replicates 16N state on k GPUs | Splits batch B into k shards of B/k; sums grads once/step |
| One-Step Check | \|\Delta \boldsymbol{\theta}_{k=2} - \Delta \boldsymbol{\theta}_{k=1}\|_\infty | \le 10^{-7} in true fp32 |
Catches missing 1/k division or shard-slicing bugs |
| Ring Allreduce | Reduce-scatter + All-gather | \frac{2(k-1)}{k}S < 2S bytes/GPU | Distributes traffic evenly across k GPUs in 2(k-1) hops |
| Step Cost Model | \max(kh_{\textrm{step}}, t_{\textrm{dev}}) + c\alpha + \frac{2(k-1)}{k}S\beta_{\textrm{link}} | \beta_{\textrm{link}} = 1 / \textrm{bus BW} | Predicts when adding GPUs speeds up or slows down a step |
| Gradient Fusion | Concatenate c tensors \to 1 buffer | Saves (c - 1)\alpha latency | Eliminates per-tensor start-up overhead for small gradients |
| Strong vs. Weak | Fixed B vs. fixed b = B/k | Speedup t(1)/t(k) vs. k t(1)/t(k) | Distinguishes faster single steps from higher total throughput |