Multi-GPU from First Principles

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

When Does a Second GPU Actually Shorten a Step?

Motivation

  • The compute dividend: Splitting a minibatch of B examples across k GPUs cuts each GPU’s local forward and backward work to B/k examples.
  • The communication & launch tax: All k GPUs must sum their S-byte gradients over PCIe (\le 32\text{ GB/s} vs. 1\text{ TB/s} on-card DRAM), and a single-thread host loop pays k \cdot h_{\textrm{step}}.
  • Break-even rule: A second GPU speeds up a step if and only if compute time saved exceeds inter-GPU transfer plus added dispatch overhead.

Three Ways to Partition Training Across Devices

Parallelism Taxonomy

  • Data Parallelism (DP): Replicates the full model (16N bytes for fp32 Adam) on every GPU, shards the minibatch along the batch axis, and exchanges gradients once per step via an allreduce.
  • Pipeline & Tensor Parallelism (PP / TP): Partition the model’s depth (layer stages) or width (individual weight matrices) across GPUs when a model is too large for one card, communicating activations at layer boundaries.

Anatomy of a Data-Parallel Training Step

Data Parallelism

  • Four stages per step: (1) Split the minibatch of B examples into k disjoint shards of B/k examples; (2) run forward and backward passes independently on each GPU; (3) sum the k local gradients via an allreduce; (4) apply the identical optimizer step on every GPU.
  • Lockstep weight invariant: Because all replicas start from the same broadcast parameters and apply identical gradient updates, their weights stay identical forever without re-broadcasting.

Broadcast and Allreduce Primitives

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]]]

Exact Gradient Averaging Across k Equal Shards

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)

  • Dividing by k is mandatory: Because each device computes a local mean-loss gradient \mathbf{g}_j over B/k examples and allreduce computes their sum, dividing by k recovers the exact global gradient.
  • Silent bug if omitted: Forgetting 1/k silently multiplies the learning rate by k (k\eta), degrading convergence without crashing.

The One-Step Gradient Check: 1 GPU vs. 2 GPUs

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.

Why Star Allreduce Bottlenecks at the Hub Device

Star vs. Ring Topology

  • Star Allreduce (O(kS) at hub): Device 0 gathers (k-1) gradient tensors of size S, sums them, and scatters (k-1) copies back (12\text{ GB} sent/received at k=4 for S=4\text{ GB}).
  • Ring Allreduce (< 2S per GPU): All k GPUs circulate S/k chunks around a logical ring in 2(k-1) steps, sending \frac{2(k-1)}{k}S bytes each (6\text{ GB} at k=4).
  • Bandwidth optimality: Ring traffic per GPU stays bounded below 2S even as k \to \infty.

Ring Allreduce: Reduce-Scatter Followed by All-Gather

Ring Allreduce

  • Two phases of k - 1 steps on chunks of size S/k:
    1. Reduce-scatter (k-1 hops): Each GPU sends one S/k chunk to its ring neighbor and adds the incoming chunk into its local buffer; after k-1 hops, each GPU holds the complete sum for one unique 1/k shard.
    2. All-gather (k-1 hops): The completed S/k shards circulate around the ring without addition until every GPU holds all k summed shards.

Verifying Ring Traffic (< 2S) and Sequential Hop Count

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
  • Bandwidth vs. latency trade-off: Ring allreduce is bandwidth-optimal for large tensors (S independent of k as k grows), but executes 2(k-1) sequential hops (each paying start-up latency \alpha_{\textrm{hop}}). For small messages on many GPUs, NCCL switches to 2\log_2 k-hop trees.

The Latency-Bandwidth Cost Model for a Parallel Step

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}}

  • t_{\textrm{compute}}(B/k): Local forward, backward, and optimizer time on a shard of B/k examples.
  • c\,\alpha: Fixed start-up overhead across c separate allreduce calls per step (c = 0 when k = 1).
  • \frac{2(k-1)}{k} S \beta_{\textrm{link}}: Byte transfer time for S total gradient bytes per GPU.
  • When k = 2 beats k = 1: A second GPU shortens the step only if t_{\textrm{compute}}(B) - t_{\textrm{compute}}(B/2) > c\alpha + S\beta_{\textrm{link}}.

Why Small Gradient Tensors Must Be Concatenated

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.

Profiling LeNet: An Overhead-Bound Single-GPU Step

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 dominates: The GPU is busy for only a small fraction of the step; LeNet at B = 256 is overhead-bound, so its single-GPU step time is governed by host launch time h_{\textrm{step}} rather than GPU arithmetic.

The Single-Launcher Bottleneck: Host Work Doubles

Host Dispatch Ceiling

  • Halving an overhead-bound batch saves nothing: On 1 GPU, b = 128 takes nearly the exact same time h_{\textrm{step}} as b = 256.
  • One host thread driving k GPUs: When a single Python loop dispatches kernels to GPU 0 and then to GPU 1, host launch time scales as k \cdot h_{\textrm{step}}.
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

Predicting and Measuring the Two-GPU LeNet Slowdown

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.

Strong Scaling vs. Weak Scaling Conventions

Scaling Laws

  • Strong scaling (fixed global batch B, b = B/k): Preserves exact gradient statistics and aims to cut single-step wall time by k\times, but shrinks per-GPU compute relative to communication as k grows.
  • Weak scaling (fixed per-GPU batch b, B = kb): Keeps per-GPU compute constant to achieve k\times throughput (\text{examples/s}), subject to the critical batch size b_{\textrm{noise}}.

Crossing the Scaling Threshold with Larger Minibatches

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)
  • Three distinct regimes: At b = 256, t_{\textrm{dev}} \ll h_{\textrm{step}} (no gain); at b = 4{,}096, t_{\textrm{dev}} begins to overtake h_{\textrm{step}}; at b = 32{,}768, device compute dominates both host dispatch and the 247\text{ KB} allreduce, achieving \sim 1.9\times weak-scaling speedup on two GPUs.

Concept Check: Allreduce Traffic and Multi-GPU Speedup

Check Your Understanding

Use our multi-GPU cost model to answer two concrete scaling questions:

  1. A 1B-parameter GPT (N = 10^9, 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?
  2. Suppose one step of the 1B GPT processes 32{,}768 tokens globally (6N\text{ FLOPs/token}) at an achieved rate of 35\text{ TFLOP/s} per GPU in 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:

  1. Star hub sends (k-1)S = 7 \times 4 = \mathbf{28\text{ GB}} (and receives 28\text{ GB}). Ring sends \frac{2(k-1)}{k}S = \frac{14}{8} \times 4 = \mathbf{7\text{ GB}} per GPU (4\times less peak per-device traffic, strictly < 2S = 8\text{ GB}).
  2. 1 GPU compute time: \frac{6 \times 10^9 \times 32{,}768}{3.5 \times 10^{13}} \approx \mathbf{5.6\text{ s}}. 2 GPUs compute time: 2.8\text{ s}, plus S\beta_{\textrm{link}} (k=2 sends \frac{2(1)}{2}S = 4\text{ GB}):
    • (a) At 10\text{ GB/s}: t_{\textrm{comm}} = 0.4\text{ s} \implies t_{\textrm{step}}(2) = 3.2\text{ s} \implies \textrm{Speedup} = 5.6 / 3.2 = \mathbf{1.75\times}.
    • (b) At 2.5\text{ GB/s}: t_{\textrm{comm}} = 1.6\text{ s} \implies t_{\textrm{step}}(2) = 4.4\text{ s} \implies \textrm{Speedup} = 5.6 / 4.4 = \mathbf{1.27\times}.

Summary: Collectives, Ring Allreduce, and Cost Models

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