TL;DR
A transformer language model with billions of parameters does not fit in one GPU's memory, and data parallelism alone does not help, because every GPU still needs a full copy of the model. Megatron-LM splits the large matrix multiplies inside each transformer layer across GPUs, so each GPU holds only a slice of the weights. The split is arranged so that each layer needs only a few all-reduce operations, and it is written as a few changes to ordinary PyTorch code. The authors trained GPT-2 style models of up to 8.3 billion parameters on 512 V100 GPUs.
Builds on: Transformer, Multi-Head Attention, Model Parallelism, All-Reduce · Leads to: later large-model training systems that combine tensor, pipeline and data parallelism
Why it mattered
Many large models trained since then use this idea or its descendants. The reference code is open source in NVIDIA's Megatron-LM repository, so other groups could build on it. The paper's trained models were, at the time, the largest left-to-right language models reported, with the 8.3 billion parameter GPT-2 setting described as larger than any earlier transformer of that kind. Semantic Scholar records about 3,440 citations for it.
The paper also pushed back on a common assumption. Pipeline model parallelism (splitting the model by layers and passing activations down a chain of GPUs) was then the main published route to very large models, and it needs extra scheduling logic and can leave devices idle. The authors showed that a simpler split inside each layer could scale well on a fast intra-server link, and that it could be combined with data parallelism across servers.
The problem
Training a transformer needs its weights, their gradients, and optimizer state, such as the momentum and variance that Adam Optimizer keeps for every weight. For a model with billions of weights these take several times the memory of the weights alone. Data parallelism, where each GPU holds a full copy and processes a different slice of the batch, scales throughput but not model size. Activation checkpointing, which recomputes activations instead of storing them, helps memory but still needs the whole model on one device.
Model parallelism fixes the size limit by letting each device hold part of the model. Two families existed in 2019. The pipeline family (for example GPipe) assigns whole layers to devices and needs careful micro-batch scheduling and changes to the training loop. The tensor family partitions the operations within a layer, as in Mesh-TensorFlow, but it needed a new framework or compiler. The paper asks whether a tensor split can be done with a few explicit communication steps in plain PyTorch, with no new compiler, and still keep the GPUs busy.
How it works
The walkthrough follows one transformer layer through one forward pass, with the GPUs in a model-parallel group.
- The input to the layer is a batch of token vectors X. Every GPU in the group holds the same copy of X.
- The MLP block takes X, multiplies by a weight matrix A, and applies the GeLU nonlinearity. The matrix A is split by columns across the GPUs, so GPU 1 holds A1 and GPU 2 holds A2. Each GPU computes its own piece with no communication, and each then applies GeLU to its piece.
- The second MLP matrix B is split by rows, so GPU 1 holds B1 and GPU 2 holds B2. Each GPU multiplies its piece of the activation by its piece of B.
- The two partial results are summed across GPUs with one all-reduce, giving the full MLP output on every GPU. Splitting by columns first is what made this possible: GeLU is nonlinear, so it cannot be applied to partial sums, but it can be applied to each column block separately.
- The self-attention block works the same way. The query, key and value projections are split by columns, so each GPU owns a subset of the attention heads and computes them locally. The output projection is split by rows, and one all-reduce sums its partial results.
- Dropout and layer normalization are not split. Each GPU keeps a copy of their parameters and computes them on the full activation, which costs a little duplicated work but avoids a communication step.
- Over the forward and backward pass, a layer uses four all-reduce operations in total: two in the forward pass and two in the backward pass. Each is a sum across the model-parallel group.
A tiny example makes the GeLU point concrete. Take X equal to the row vector (1, 2) and A equal to the matrix with rows (1, 2) and (3, 4). The full product XA is (1 times 1 plus 2 times 3, 1 times 2 plus 2 times 4), which is (7, 10). Split A by columns: the first column (1, 3) gives 7, and the second column (2, 4) gives 10. Each GPU holds one column and gets the right answer with no communication. Now split by rows instead, with GPU 1 holding X1 = 1 and row (1, 2) and GPU 2 holding X2 = 2 and row (3, 4). The two partial products are (1, 2) and (6, 8), which sum to (7, 10). The sums are correct, but a nonlinear function applied to each partial product gives a different result from applying it to the sum. So the row split would need a synchronization before the nonlinearity. Column split avoids it.
The math
Write the MLP with input (b tokens in the batch, h the hidden size), first weight , second weight , and GeLU as the nonlinearity:
Step 2 in symbols: the column split of the first matrix
Split by columns, with each part of size . Then
Each GPU computes one block with no communication, because GeLU acts on each entry, and each entry of depends only on .
For the row split, the same step would need
which is the reason the column split is used.
Steps 3 and 4 in symbols: the row split and the all-reduce
Split by rows, , each of size . Each GPU uses the block of it already holds:
The sum is the all-reduce. It is exact, because matrix multiplication distributes over sums of column and row blocks.
The conjugate operators
The paper names the two communication points and . The operator is an identity in the forward pass and an all-reduce in the backward pass. The operator is an all-reduce in the forward pass and an identity in the backward pass. Placed at the entry and exit of each parallel block, they give one all-reduce per block in each direction. A layer has two parallel blocks, the MLP and the attention block, so it has two all-reduces in the forward pass and two in the backward pass, four in all, as the paper counts them.
Scaling arithmetic (derived from the paper's numbers)
The paper reports 15.1 PFLOPS sustained on 512 GPUs. Dividing by 512 gives about 29.5 TFLOPS per GPU. The single-GPU baseline sustains 39 TFLOPS, so the ratio is about 0.76, which matches the paper's stated 76 percent scaling efficiency.
Results
On a single V100 GPU, the 1.2 billion parameter baseline sustains 39 TFLOPS, which the paper says is 30 percent of the GPU's theoretical peak in its configuration. With 8-way model parallelism, the 8.3 billion parameter model on 512 GPUs sustains up to 15.1 PFLOPS over the whole run. The introduction reports 76 percent scaling efficiency against the single-GPU baseline, and Section 5 gives 77 percent for the 8-way model-parallel case and 74 percent for the full 512-GPU setting, depending on the comparison. The paper's weak-scaling figure shows model-parallel and model-plus-data-parallel curves close to each other.
The language model results are for GPT-2 style training on an aggregate corpus of 174 GB of deduplicated text. The 8.3 billion parameter model reaches a validation perplexity of 9.27 and, zero-shot, a WikiText103 perplexity of 10.81 and a LAMBADA accuracy of 66.51 percent. The 2.5 billion model gets 12.76 and 61.73 percent, and the 355 million model gets 19.31 and 45.18 percent. The paper's previous state of the art was 15.79 perplexity on WikiText103 and 63.24 percent on LAMBADA. These are single runs, evaluated with the paper's own settings.
For BERT-style models, the paper finds that the original placement of layer normalization makes training unstable for the larger sizes. Moving layer normalization so that it sits on the residual branch, as in Figure 7(b), stabilizes training and gives a lower training loss. With that change, the 3.9 billion parameter model reaches 89.5 percent on the RACE test set as a single model, and 90.9 percent as an ensemble.
Limits. The paper states that a model above about 16 billion parameters would need more memory than 16 GPUs of one DGX-2H server provide, and that such models would need model parallelism inside a server combined with pipeline parallelism across servers. The scaling numbers come from one hardware setup, with NVSwitch inside a server and an InfiniBand network between servers, and from one training run per model.
Common misreadings. It is tempting to think model parallelism makes the model faster to train. In fact it exists to fit a model that does not fit, and each split adds all-reduce time; the paper's efficiency numbers are the cost of that communication. It is tempting to think the split is arbitrary. In fact the column-then-row arrangement is chosen so that the nonlinearity needs no synchronization, which is why only four all-reduces per layer are needed. It is tempting to think the GPT-2 comparison proves the method scales to any size. In fact the paper shows scaling up to 8.3 billion parameters on V100 hardware, and it names larger models as needing a different layout.
Reading guide
The chain of ideas is: a transformer layer made of two large matrix blocks, a split of each block that needs one synchronization per block, the four all-reduce points that result, and the training recipe that makes the large models stable. Read the paper's sections in this order.
- Section 1, Introduction: the claim, the 39 TFLOPS baseline and the 15.1 PFLOPS result. Needs Model Parallelism.
- Section 2.2, Transformer language models: the encoder and decoder roles and the GeLU and layer normalization placement. Needs Transformer, Gaussian Error Linear Unit (GELU) and Layer Normalization.
- Section 2.3, Data and model parallelism: how the two differ, and why activation checkpointing alone is not enough. Needs Data Parallelism and Activation Checkpointing.
- Section 3, Model parallel transformers: Figures 3 and 4, the f and g operators, and the vocabulary split. Needs Multi-Head Attention and All-Reduce.
- Section 4.2, Training optimization: mixed precision, Adam with weight decay, gradient clipping and the initialization scaling. Needs Mixed-Precision Training and Adam Optimizer.
- Section 5.1, Scaling analysis: Table 1 and Figure 5. Read the numbers against the derived efficiency above.
- Section 5.2, GPT-2 results: Table 3 and the perplexity definition. Needs Language Model and Perplexity.
- Section 5.3, BERT results: Figure 7 and the residual placement. Needs Residual Connection and BERT: Pre-training of Deep Bidirectional Transformers.
- Appendix B.2, random number generation: why dropout inside a split layer must use a different seed on each GPU. Needs Dropout.
Check yourself
QuestionSuppose a layer's first matrix is split by columns across 2 GPUs and the nonlinearity is applied to each half. Why is no communication needed before the nonlinearity?Show answer
Each column block of the first matrix produces its own block of the output, and each output entry depends only on that block's columns of the weight matrix. So each GPU can compute its half and apply the nonlinearity to it without seeing the other half. A row split would produce partial sums that must be added before the nonlinearity, which is why the paper avoids it.
QuestionThe paper reports 15.1 PFLOPS sustained on 512 GPUs and a single-GPU baseline of 39 TFLOPS. What is the per-GPU average, and what fraction of the baseline does it represent?Show answer
Divide 15.1 PFLOPS by 512 GPUs. That is 15,100 TFLOPS divided by 512, which is about 29.5 TFLOPS per GPU. Dividing 29.5 by 39 gives about 0.76, or 76 percent, which matches the paper's stated scaling efficiency.
QuestionA transformer layer uses the column-then-row split on its MLP and the same pattern on its attention block. Count the all-reduce operations in one layer's forward pass, and explain why the layer norms add none.Show answer
There are two all-reduces in the forward pass: one after the MLP's second matrix, and one after the attention output projection. The layer normalizations and dropout are not split. Each GPU keeps a copy of their parameters and computes them on the full activation it already holds, so they need no communication. The backward pass has two more, for a total of four.
Sources
- Shoeybi et al., "Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism", arXiv:1909.08053, 2019: https://arxiv.org/abs/1909.08053
- NVIDIA Megatron-LM reference implementation: https://github.com/NVIDIA/Megatron-LM
- DOI record: https://doi.org/10.48550/arXiv.1909.08053