↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Cutting the model into stages

Pipeline parallelism (PP) gives each rank a run of consecutive layers, a stage. A rank keeps only its stage's weights and optimizer state, and the only thing that crosses between ranks is the hidden state at the cut going forward and its gradient coming back. That is a small message compared with the all-reduces of tensor parallelism, so pipelines are what span the slower links between machines.

The page cuts GPT-2 (124M)'s twelve blocks into 2, 3 or 4 equal stages (the GPT-2 in p stages panel): the first stage also embeds the tokens, the last applies the final LayerNorm, the LM head and the loss. It runs one training step on the first 1,024 GPT-2 tokens of SQuAD's Apollo program article, cut into eight micro-batches of 128 tokens, each stage a separate process on this Mac's CPU talking through gloo. A forward message is one micro-batch's hidden states, 128 × 768 floats, 384 KiB; the backward message is its gradient, the same size. Times come from the CPU run; the order of work and messages is what carries over to GPUs.

Micro-batches and the bubble

With one batch in flight, only one stage would work at a time. Cutting the batch into micro-batches lets stage 2 work on the first micro-batch while stage 1 starts the second. The pipeline still has to fill at the start and drain at the end, and during those times some stages wait: the bubble. If every stage takes for a forward and for a backward, a step takes on stages instead of , so the idle fraction is

3/11, about 27 %, for stages and micro-batches. The GPipe against 1F1B table sets this beside the idle time measured on each recorded stage; the hatched gaps in the schedule are where it happened. More micro-batches shrink the bubble, but each must stay big enough to keep a device busy.

The recorded stages are not equal, though. The last stage also runs the LM head, a 768 × 50,257 matrix and the loss, so on 4 stages with GPipe its forward took 45 ms per micro-batch against 26–30 ms on the others, and its backward 123 ms against 48–82 ms. It sets the pace: the three stages before it waited for 48–59 % of the step, more than the 27 % the formula gives, and the step took 1.64 s on 4 stages against 1.71 s on 2, because the head is not split. This is why pipelines often give the last stage fewer layers.

GPipe: all forwards, then all backwards

GPipe runs every micro-batch forward through the pipeline, then every one backward (the last one first, as in the paper's figure), and only then updates the weights. Each micro-batch's loss is divided by , so the summed gradients are exactly those of the mean loss over the whole batch, the same as on one device. The price is memory: at the turn from forward to backward, every stage holds the activations autograd saved for all micro-batches. The GPipe paper recomputes them in the backward pass instead of keeping them.

1F1B: one forward, one backward

The 1F1B schedule (PipeDream-Flush, Megatron-LM's default) starts each micro-batch's backward as soon as possible. Stage of first runs warm-up forwards, then alternates one forward and one backward, then drains the backwards left; the last stage runs a micro-batch's backward right after its forward. The bubble is the same as GPipe's, but a micro-batch's activations are freed after more forwards at most, so stage never holds more than of them, against for GPipe.

The Activations held chart is measured, not modelled: while each forward ran, the recorder counted the tensors autograd saved for backward (the parameters left out), plus the stage's input and output, and freed them at that micro-batch's backward. Neighbouring stages swap tensors with paired send-and-receive calls, as Megatron's send_forward_recv_backward does, so two stages that both wait for each other cannot deadlock; the recorded messages follow the schedule's order exactly.

The tied embedding and the same gradients

GPT-2's LM head is its token embedding transposed, so the first and the last stage each keep a copy of the same matrix. Each copy gets only part of the gradient, from the input side or from the output side; at the end of the step the two ranks add their gradients with an all-reduce, Megatron's embedding group, the line at the end of the schedule. After it, every stage's gradients equal those of the unsplit model on the same eight sequences for both schedules, and each micro-batch's loss equals the unsplit one, to float rounding (the The same gradients note).

Where it came from

GPipe (Huang et al., 2018) introduced synchronous micro-batch pipelining with re-materialisation. PipeDream (Harlap et al., 2018) kept stages busy with asynchronous 1F1B and stashed weight versions; PipeDream-Flush (Narayanan et al., 2020) made 1F1B synchronous, the schedule recorded here. Megatron-LM (Narayanan et al., 2021) interleaves several smaller stages per rank to shrink the bubble further, and Zero Bubble pipeline parallelism (Qi et al., 2023) splits the backward into its input and weight gradients to fill the gaps. Pipelines are combined with TP inside a machine and DDP or ZeRO across replicas.

Papers

  1. GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism Yanping Huang et al. · NeurIPS 2018 Micro-batch pipelining with all forwards before all backwards, the bubble, and recomputing activations instead of storing them.
  2. Memory-Efficient Pipeline-Parallel DNN Training Deepak Narayanan et al. · ICML 2020 PipeDream-Flush, the synchronous one-forward-one-backward schedule recorded here, and PipeDream-2BW.
  3. PipeDream: Fast and Efficient Pipeline Parallel DNN Training Aaron Harlap et al. · arXiv 2018 The 1F1B pattern, first asynchronous, with stashed weight versions per micro-batch.
  4. Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM Deepak Narayanan et al. · SC 2021 Megatron's pipeline schedules, the interleaved schedule, and pipelines combined with tensor and data parallelism.
  5. Zero Bubble Pipeline Parallelism Penghui Qi et al. · ICLR 2023 Splitting the backward pass into input and weight gradients to schedule the bubble away.
  6. Language Models are Unsupervised Multitask Learners Alec Radford et al. · 2019 GPT-2, the model cut into stages on this page; a technical report, not on arXiv.

See also