Skip to content
nonconvexcard

Splitting a Model Across Devices

Data, tensor and pipeline parallelism compared by the one quantity that decides between them — bytes communicated per FLOP of useful work.

AI SystemsGraduate⏱ 3 hours9 min readdata paralleltensor parallelpipeline parallelcommunicationbubble

Counting rules

Throughout, let NN be the parameter count, dd the model width, LL the layer count, FF the achievable FLOP rate per device, BB the achievable interconnect bandwidth per device, and bb the bytes per element communicated. For a decoder-only transformer we use the standard counts established in Inference Arithmetic and the Roofline:

  • parameters per layer ≈12d2\approx 12d^2 (attention 4d24d^2, MLP 8d28d^2), so N≈12Ld2N \approx 12Ld^2;
  • training cost ≈6N\approx 6N FLOPs per token (forward 2N2N, backward 4N4N).

Data parallelism

Every device holds a full replica and processes a different slice of the batch. After the backward pass the gradients must be summed across replicas — one all-reduce of NN elements per step.

PropositionData-parallel communication does not depend on model size

Let each device process TT tokens per optimiser step. Using a ring all-reduce, whose per-device volume is at most 2Nb2Nb bytes,

TcommTcompute  =  2Nb/B6NT/F  =  b F3 B T.\frac{T_{\text{comm}}}{T_{\text{compute}}} \;=\; \frac{2Nb/B}{6NT/F} \;=\; \frac{b\,F}{3\,B\,T} .

The parameter count cancels. The ratio depends only on the hardware ratio F/BF/B, the gradient precision bb, and the tokens per device per step.

Assumptions

  • The all-reduce is bandwidth-bound, so its latency term 2(p−1)α2(p-1)\alpha is negligible. For a multi-gigabyte gradient this holds; for small models it does not, and the latency term restores a dependence on pp.
  • Communication is not overlapped with computation. Overlapping changes the conclusion from "this is the cost" to "this is the amount that must be hidden", which is the same number.

Key idea

Data parallelism is amortised by tokens per device per step. Doubling the per-device microbatch halves the relative cost of gradient synchronisation. This is why data-parallel training degrades at small per-device batch — and why gradient accumulation, which raises TT without raising memory for activations proportionally, is the standard fix.

Tensor parallelism

Each weight matrix is sharded across tt devices, so a single layer's matmuls are computed jointly. The Megatron decomposition requires one all-reduce of the activation tensor after the attention block and one after the MLP block, in the forward pass, and the same in the backward pass.

PropositionTensor-parallel communication does not depend on batch size

With TT tokens in flight, a hidden size dd, and 44 all-reduces per layer per step (two forward, two backward) each of TdTd elements,

TcommTcompute  =  4L⋅2Tdb/B6 (12Ld2) T/F  =  8LTdb/B72Ld2T/F  =  b F9 d B.\frac{T_{\text{comm}}}{T_{\text{compute}}} \;=\; \frac{4L\cdot 2Tdb/B}{6\,(12Ld^2)\,T/F} \;=\; \frac{8LTdb/B}{72Ld^2T/F} \;=\; \frac{b\,F}{9\,d\,B} .

Both LL and TT cancel. The ratio falls like 1/d1/d: wider models tolerate tensor parallelism better.

Pipeline parallelism

The layer stack is cut into pp contiguous stages, one per device. A batch is split into mm microbatches which flow through the stages; only activations at stage boundaries are communicated, which is a small point-to-point transfer rather than a collective. The cost is not bandwidth but idleness: at the start, stages downstream have nothing to do, and at the end, upstream stages are finished.

PropositionThe pipeline bubble fraction

With pp stages, mm microbatches, and equal per-stage time τ\tau per microbatch, the schedule completes in (m+p−1)τ(m+p-1)\tau against an ideal mτm\tau. The fraction of wall-clock time lost to idle stages is

bubble  =  p−1m+p−1,\text{bubble} \;=\; \frac{p-1}{m+p-1},

and the overhead relative to the ideal is (p−1)/m(p-1)/m.

With p=4p = 4 and m=8m = 8 the bubble is 3/11≈27%3/11 \approx 27\%; raising mm to 3232 brings it to 3/35≈8.6%3/35 \approx 8.6\%.

Key idea

Pipeline parallelism is amortised by microbatch count. But mm cannot grow freely: every in-flight microbatch holds its activations for the backward pass, so memory grows with mm. The bubble and the activation memory pull in opposite directions, and that trade is what interleaved and 1F1B schedules exist to improve.

Why all three

CorollaryWhy the three are combined rather than chosen between

The three overheads are amortised by three different quantities:

data  ∝  1T,tensor  ∝  1d,pipeline  ∝  p−1m.\text{data} \;\propto\; \frac{1}{T}, \qquad \text{tensor} \;\propto\; \frac{1}{d}, \qquad \text{pipeline} \;\propto\; \frac{p-1}{m} .

Since dd is fixed by the model and TT and mm are bounded by memory, no single axis can be pushed to absorb an arbitrary device count; minimising total overhead subject to a fixed device budget ptotal=pdata⋅ptensor⋅ppipep_{\text{total}} = p_{\text{data}} \cdot p_{\text{tensor}} \cdot p_{\text{pipe}} therefore has an interior optimum in general.

What this does not say

This page counts bytes and FLOPs. It says nothing about convergence: a configuration that minimises time per step can still be worse per unit of progress, because the global batch size it implies may sit past the point where larger batches stop helping. Step time and steps-to-target are separate quantities, and a parallelism plan that optimises only the first is incomplete.

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page