Search

Search papers and lessons

GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism

Yanping Huang, Youlong Cheng, Ankur Bapna et al. · 2018

GPipe (2018) splits a network into stages on separate accelerators and pipelines small micro-batches through them, so models far too big for one device train with synchronous updates.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Calculus
  • Probability
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

GPipe is a library for training neural networks that do not fit on one accelerator. It cuts the network into consecutive groups of layers, puts each group on its own accelerator, and splits every mini-batch into small micro-batches so that the accelerators work on different micro-batches at the same time. The gradients of all micro-batches are added up and applied in one synchronous update, so the training result does not depend on how many accelerators are used.

Builds on: Model Parallelism, Activation Checkpointing, Gradient Accumulation · Leads to: later pipeline-parallel training systems, and the mixes of layer splitting, within-layer splitting and data parallelism used for very large models

Why it mattered

The paper's headline results are a 557-million-parameter image classifier that reached 84.4% top-1 accuracy on ImageNet 2012, and a single 6-billion-parameter, 128-layer Transformer that translates between more than 100 languages and beats separately trained bilingual models on 100 language pairs. Both were trained with the same library, which is the point: GPipe does not need a custom design for each architecture.

Before this, the efficient ways to split a model across devices were tied to one architecture or one hardware setup. GPipe showed a general recipe that scales any network written as a sequence of layers, and that keeps close to linear speedup when the devices are connected by ordinary links. Later systems such as Megatron-LM chose a different split (inside each layer's large matrix multiplies), and large models trained since then often combine both kinds of split with data parallelism.

The problem

Deep learning kept improving by making models larger, but a single accelerator has a fixed memory. Training needs the weights, the optimiser state and the activations saved for the backward pass, and all of these must fit. Once a model is too large, you must divide it across accelerators, and the paper names the two costs of doing that badly.

The first cost is idle time. In a plain model-parallel setup, the accelerator holding the first layers works, then waits while the second accelerator works, and so on. With four accelerators, three of them are idle at any moment. The second cost is memory for activations. Backpropagation needs the activations of every layer from the forward pass, and for a deep network that can dwarf the weights. Activation Checkpointing reduces that memory by recomputing values, and the paper uses the same idea.

The paper also points out that the usual alternatives trade one problem for another. Splitting each matrix multiply across devices needs a lot of communication. Asynchronous pipelines such as PipeDream keep devices busy, but use different weight versions for different micro-batches, so the training result depends on the schedule. GPipe aims for the pipeline's utilisation with a single synchronous update.

How it works

Take a tiny network of four layers, split into two cells, one per accelerator, and a mini-batch of eight examples split into four micro-batches of two examples each. Every cell takes one time unit to run its forward pass on one micro-batch.

flowchart LR
  I[Mini-batch of 8 examples] --> M[Split into 4 micro-batches of 2]
  M --> C1["Cell 1: layers 1 and 2, accelerator 1"]
  C1 -->|boundary activations only| C2["Cell 2: layers 3 and 4, accelerator 2"]
  C2 --> L[Loss]
  L -->|gradients| C2
  C2 -->|gradients| C1
  C1 --> U[One synchronous update]
  1. Choose the partition. The user states the number of cells K and the number of micro-batches M, and lists the layers. GPipe groups consecutive layers into K cells and places cell k on accelerator k. Communication is inserted only at the cell boundaries. The grouping tries to make the estimated cost of each cell about equal, because the slowest cell sets the pace of the whole pipeline. In the tiny example, K = 2 and M = 4.
  2. Split the mini-batch. The mini-batch of N examples is cut into M micro-batches of N / M examples each. In the example, the eight examples become four micro-batches of two.
  3. Run the forward pipeline. Micro-batch 1 enters cell 1 at time 1. At time 2 it moves to cell 2, and micro-batch 2 enters cell 1. The pipeline fills like a staircase. The table shows which cell runs which micro-batch at each time.
TimeCell 1 (accelerator 1)Cell 2 (accelerator 2)
1micro-batch 1idle
2micro-batch 2micro-batch 1
3micro-batch 3micro-batch 2
4micro-batch 4micro-batch 3
5idlemicro-batch 4
  1. Keep only the boundary values. During the forward pass, a cell keeps the activations at its own input and output boundaries, not every layer's activations. This is the re-materialisation the paper uses to save memory.
  2. Run the backward pass. Gradients travel back through the cells, one micro-batch at a time, and each cell recomputes its own forward pass from the saved boundary values before it runs its backward pass. Every micro-batch uses the same weights as its forward pass, because no update has happened yet.
  3. Add up the gradients. Each micro-batch produces a gradient. GPipe accumulates them over the M micro-batches of the mini-batch.
  4. Update once. After all micro-batches, every accelerator applies its part of the accumulated gradient in one synchronous step. The next mini-batch then starts from the new weights.

The idle slots in the table are the bubble. Two of the ten cell-slots in the forward schedule are empty, one at the start on cell 2 and one at the end on cell 1. The bubble shrinks as M grows relative to K, which is why the paper pushes the number of micro-batches up.

The math

Definition: GPipe partition

A network is a sequence of L layers. Layer i has a forward function fif_{i} and parameters wiw_{i}. Choose K and split the sequence into K consecutive cells. Cell k holds layers i through j, and runs the composite function Fk=fj∘⋯∘fi+1∘fiF_{k}=f_{j}\circ\cdots\circ f_{i+1}\circ f_{i}. The mini-batch is split into M micro-batches. The paper's loss is written ℓ\ell, and the learning rate is η\eta.

Step 1 in symbols: the cells

The parameters of cell k are the union of the layers' parameters, ⋃l=ijwl\bigcup_{l=i}^{j} w_{l}. Because each cell owns its own parameters, a cell's backward function BkB_{k} can be computed from FkF_{k} by automatic differentiation, which is why the cell is the unit that moves between accelerators. Partition boundaries carry only the activations aia_{i} that leave cell k and enter cell k+1.

Step 2 in symbols: the synchronous update

Let gmg_{m} be the gradient of the loss on micro-batch m, with the weights ww fixed for the whole mini-batch. GPipe forms

G=∑m=1MgmG=\sum_{m=1}^{M} g_{m}

and applies w←w−η Gw \leftarrow w-\eta\,G once. If each example's loss enters the sum, G is the gradient of the whole mini-batch computed in one pass. That is why the paper can say the update is consistent regardless of the number of partitions: K and M change only how the work is scheduled, not the arithmetic of the update. Dividing G by M to take an average changes only the scale of the learning rate.

Step 3 in symbols: the bubble

The fraction of cell-time spent idle in the pipeline is

K−1M+K−1\frac{K-1}{M+K-1}

For K = 2 and M = 4 this is 1 / 5 = 20%, which matches the two empty slots out of ten in the table. The formula falls as M grows: with the same K, more micro-batches means a smaller share of time spent filling and draining the pipeline.

Step 4 in symbols: the memory

Without re-materialisation and partitioning, the activations needed for the backward pass grow as O(N×L)O(N \times L), where N is the mini-batch size and L the number of layers. With GPipe, the peak activation memory is

O ⁣(N+LK⋅NM)O\!\left(N+\frac{L}{K}\cdot\frac{N}{M}\right)

The first term is the boundary activations of the mini-batch. The second is the largest cell's recomputed activations for one micro-batch, with L / K layers and N / M examples.

Worked examples

Example
1. Tiny: two cells, four micro-batches, timed by hand

Two cells, one accelerator each. Each cell takes 1 unit to run its forward pass on one micro-batch, and there are 4 micro-batches. Run only the forward passes.

  1. Without pipelining, cell 1 processes all four micro-batches (4 units), then cell 2 processes all four (4 units). Total: 8 units.
  2. With pipelining, cell 1 starts micro-batch 1 at time 1 and cell 2 starts it at time 2. Using the staircase from the walkthrough, the last piece of work, micro-batch 4 on cell 2, finishes at time 5.
  3. Count the slots: 2 cells times 5 time units is 10 slots, and 8 are busy. That leaves 2 idle slots, a fraction 2 / 10 = 20%, which matches the bubble formula 1 / 5.
  4. Compare: 8 units against 5 units. The pipeline is 8 / 5 = 1.6 times faster in this tiny case. With more cells and more micro-batches the ratio gets closer to the number of cells.
  5. Interpretation: most of the gain comes from overlapping cell 1's work on later micro-batches with cell 2's work on earlier ones.
Example
2. Standard: memory for a deep network split into four cells

Take L = 8 layers, K = 4 cells, a mini-batch of N = 32 examples and M = 4 micro-batches, so each micro-batch has 8 examples. Use one unit for the activations of one layer for one example. The counts below apply the paper's expressions. They are not measured memory.

  1. Without re-materialisation or partitioning: N × L = 32 × 8 = 256 units.
  2. With GPipe: the boundary term is N = 32 units.
  3. The per-cell term is (L / K) × (N / M) = (8 / 4) × (32 / 4) = 2 × 8 = 16 units.
  4. Add the two terms: 32 + 16 = 48 units.
  5. Compare: 256 / 48 is about 5.3, so the activation memory in this example is about one fifth.
  6. Interpretation: the saving comes from two places, storing only boundaries and running smaller micro-batches. Adding more micro-batches shrinks the second term, but it costs schedule overhead through the bubble.
Example
3. Realistic: Transformer throughput on accelerators with and without more micro-batches

The paper's Table 2 reports normalised training throughput for a Transformer on TPUs. With M = 32 micro-batches, the throughput is 1.8 with K = 2 cells and 6.3 with K = 8 cells.

  1. Divide the throughput at K = 8 by the throughput at K = 2: 6.3 / 1.8.
  2. Compute it step by step: 1.8 × 3 = 5.4, and 6.3 − 5.4 = 0.9, and 0.9 / 1.8 = 0.5. So the ratio is 3.5.
  3. The number of accelerators went up by 8 / 2 = 4 times, so the efficiency is 3.5 / 4 = 0.875, or about 87%.
  4. Interpretation: quadrupling the accelerators gave close to 3.5 times the throughput. The paper calls this almost linear for the Transformer, whose layers have identical sizes.

Try it: in the same table, the Transformer at K = 8 gets throughput 4.8 with M = 4 and 6.3 with M = 32. By what percentage does throughput rise when M goes from 4 to 32? Answer: 6.3 / 4.8 = 1.3125, so about 31% more. The extra micro-batches cut the bubble that the M = 4 schedule still had.

Results

The paper reports these numbers from its own experiments.

  • Maximum model size, AmoebaNet on Cloud TPUv2 with 8 GB per accelerator: one accelerator without GPipe fits an 82-million-parameter model. GPipe makes one accelerator hold 318 million parameters, and 8 accelerators hold 1.8 billion, which the paper describes as 25 times the single-device limit.
  • Maximum model size, Transformer on Cloud TPUv3 with 16 GB per core: with 128 partitions GPipe trains an 83.9-billion-parameter Transformer, about 300 times the single-accelerator limit for that setup. The maximum size grows roughly linearly with the number of accelerators for the Transformer, since every layer has the same size.
  • Throughput, Transformer, M = 32 micro-batches: 1.8 with K = 2, 3.4 with K = 4, 6.3 with K = 8. For AmoebaNet the same settings give 1.21, 1.84 and 3.48, which the authors attribute to the uneven cost of AmoebaNet's layers.
  • Throughput without fast interconnect (P100 GPUs over PCI-E), M = 32: going from 2 to 8 partitions, AmoebaNet's throughput rises 2.7 times and the Transformer's 3.3 times, so communication at the partition boundaries did not become the bottleneck.
  • ImageNet 2012, AmoebaNet-B (18, 512) with 557 million parameters, 4 partitions, 480 by 480 inputs: 84.4% top-1 and 97% top-5 single-crop validation accuracy.
  • Transfer learning from that ImageNet model: CIFAR-10 reaches 99.0% accuracy (error about 1%), and CIFAR-100 reaches 91.3% (error 8.7%), both above the previous best the paper lists.
  • Multilingual translation: a 6-billion-parameter, 128-layer Transformer trained on 102 languages to English (and across pairs) beats 350-million-parameter bilingual Transformer Big models on 100 language pairs. The paper notes that scaling from 1.3 billion to 6 billion parameters gives further gains, with diminishing returns from 1.3 billion to 3 billion and 6 billion.
  • Batch size, same translation model: raising the mini-batch from 260K to 4M tokens lifts German-English BLEU from 30.92 to 32.71, and lowers the validation loss from 2.58 to 2.46.

What these numbers mean: throughput scales well when the number of micro-batches is large compared with the number of cells, and the Transformer scales better than AmoebaNet because its layers are equal in cost. The limits are real. The experiments use TPUs and P100 GPUs and one library, the partitioner is a heuristic, and the paper itself says that a layer which does not fit on one accelerator cannot be handled by GPipe as it stands.

Reading guide

Read the paper in this order, with the lessons it depends on.

  1. Section 1, Introduction. Needs Model Parallelism and Data Parallelism. The argument is that scaling capacity is limited by memory and bandwidth, not by the model's design.
  2. Section 2.1 and 2.2, Interface and Algorithm. Needs Backpropagation and Automatic Differentiation for how the cell's backward function BkB_{k} is obtained from FkF_{k}. The note on Batch Normalization at the end of 2.2 is easier once you know that statistics are computed per batch.
  3. Section 2.3, Performance Optimization. Needs Activation Checkpointing for re-materialisation, and Gradient Accumulation for how micro-batch gradients are summed. The bubble formula is the equation to slow down on.
  4. Section 3, Performance Analyses. Needs the Transformer lesson for the Transformer rows of Tables 1 to 3, and convolutional networks for the AmoebaNet rows. Read the tables against the Results section above.
  5. Section 4, Image Classification. Needs Convolutional Neural Network and Stochastic Gradient Descent for the training setup, and the transfer-learning idea of reusing pretrained weights.
  6. Section 5, Multilingual Machine Translation. Needs Transformer and BLEU Score. The notation T(L, H, A) gives layers, feed-forward width and attention heads; read it as a size code.
  7. Section 6, Design Features and Trade-Offs. Needs All-Reduce for the communication costs of SPMD, and PipeDream: Fast and Efficient Pipeline Parallel DNN Training for the asynchronous alternative the paper compares against.

Check yourself

QuestionIn GPipe, what does K count, what does M count, and which one does the bubble formula depend on?Show answer

K is the number of cells, one per accelerator. M is the number of micro-batches that a mini-batch is split into. The bubble fraction K−1M+K−1\frac{K-1}{M+K-1} depends on both: it grows with K and shrinks as M grows.

QuestionA pipeline has K = 4 cells and M = 12 micro-batches. What fraction of cell-time is idle, and what happens to the fraction if you use M = 4 instead?Show answer

With K = 4 and M = 12, the bubble is 3 / (12 + 3) = 3 / 15 = 20%. With M = 4 it is 3 / (4 + 3) = 3 / 7, which is about 43%. Fewer micro-batches leave more of the pipeline filling and draining.

QuestionWhy does GPipe's result not depend on how many partitions it uses, while PipeDream's can?Show answer

GPipe adds the gradients of all micro-batches with the same weights, then applies one update. The number of cells changes only where the work runs, not what the update is, so the arithmetic matches a single big batch. PipeDream starts new micro-batches before earlier ones finish their backward pass, so some gradients are computed from weights that have since changed, and the schedule affects the result.

QuestionA 20-layer model has one layer whose parameters alone need more memory than one accelerator. Can GPipe train it as described, and what would you try?Show answer

Not as described. The paper states that GPipe assumes a single layer fits on one accelerator. The paper suggests one possible workaround: split that single matrix multiplication into smaller ones and spread them across several layers and accelerators. Other options are a within-layer scheme such as the one in Megatron-LM or a different parallel strategy altogether.

Sources

  • Huang, Cheng, Bapna, Firat, Chen, Chen, Lee, Ngiam, Le, Wu, Chen. GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism. arXiv:1811.06965 (2018). arXiv abstract page, DOI 10.48550/arXiv.1811.06965.
  • Shen, Nguyen, Wu, Chen, Chen, Jia, Kannan, Sainath, Cao, Chiu, et al. Lingvo: a modular and scalable framework for sequence-to-sequence modeling. arXiv:1902.08295 (2019), the framework the paper implements GPipe in. arXiv abstract page.

Read the full paper

Yanping Huang, Youlong Cheng, Ankur Bapna et al., 2018. arXiv:1811.06965