Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism
Data parallelism replicates the whole model; pipeline parallelism slices layers. Megatron shards weight matrices inside each layer so multi-billion parameter Transformers fit and scale without a new compiler.
Language models get better as they get bigger — until the weights no longer fit on one GPU. Classic data parallelism (DP) replicates the full model on every rank: great when the model is small, useless when even one copy OOMs. Pipeline parallelism slices layers across devices and fights bubbles. Megatron-LM attacks a different axis: intra-layer model parallelism — split the large GEMMs inside each Transformer block across GPUs, keep GeLU and attention heads local, and synchronize with a handful of all-reduces.
The paper’s punchline (abstract): converge Transformers up to 8.3 billion parameters on 512 GPUs, sustain 15.1 PetaFLOPs at 76% scaling efficiency versus a strong single-GPU baseline, and show SOTA-at-the-time WikiText-103 / LAMBADA / RACE numbers for GPT-2- and BERT-style models trained this way.
What this paper explains
Shoeybi et al. give a practical recipe for multi-billion-parameter Transformers in native PyTorch:
- Name the memory wall. GPT-2 / BERT-style stacks with GeLU and pre-attention LayerNorm still blow single-GPU memory once parameters reach billions.
- Reject the wrong GEMM split. For , splitting by rows forces a sync before GeLU because .
- Use column-then-row parallelism. Split the first MLP (and QKV) GEMM along columns so each GPU applies GeLU locally; split the second GEMM along rows and all-reduce the sum.
- Mirror that pattern in attention. Column-parallel so heads run locally; row-parallel output projection — no sync between QKV and Softmax.
- Compose with DP. Model-parallel groups sit inside data-parallel replicas; the approach is orthogonal to pipeline parallelism (complementary, not a replacement).
This is not ZeRO (shard optimizer/grad/param replicas across DP ranks) and not GPipe (stage the depth of the network). It is tensor / model parallelism inside each layer’s matmuls.
Prior limits
- Data parallelism alone keeps communication simple but replicates every parameter, gradient, and optimizer tensor — multi-billion models never leave the station on one device’s memory budget.
- Naive model parallelism that slices activations or weights without respecting nonlinearities inserts extra synchronizations and kills scaling.
- Pipeline parallelism (GPipe-style) partitions layers across stages; useful, but Megatron stresses an intra-layer method that needs no new compiler and works with a few collective ops in PyTorch.
- Cross-node MP pain is real in the broader literature (ZeRO’s related-work notes slow Megatron-style MP across DGX-2 nodes) — Megatron’s contribution is the partitioning pattern and demonstrated scale on large GPU counts, not a claim that NVLink physics changed.
The mechanism
MLP: why column split wins
Write the first feed-forward multiply as
Bad split (rows of ). Partition
so . You must reduce before GeLU.
Good split (columns of ). Partition and compute
locally. The second GEMM is then row-parallel: it consumes without an intervening collective, and an all-reduce () sums partial outputs before dropout.
Conjugate collectives / : identity one way, all-reduce the other — a few lines of torch.autograd.Function (paper Code 1).
Self-attention: exploit heads
Column-parallel projections for , , place each head’s matmul on one GPU. Softmax attention stays local. The output projection is row-parallel, again avoiding a sync between attention and the projection GEMM.
Layer budget
Fusing pairs of GEMMs this way means a Transformer layer needs only two all-reduces in the forward pass and two in the backward pass (paper Figure 4).
Algorithm / figure walkthrough
- Form a model-parallel group of size (and optionally a DP group outside it).
- Shard MLP weight by columns across the group; each rank computes local .
- Shard the second MLP weight by rows; locally multiply; all-reduce to assemble the MLP output.
- Shard by columns so each rank owns a subset of heads; run attention locally.
- Shard the output projection by rows; all-reduce after the projection.
- Repeat for every layer; train with ordinary Adam/mixed-precision stacks as in the paper’s setup.
- Scale out: grow when a layer’s weights no longer fit; grow DP for throughput.
The opening visual is the same arc: ENTER with a full GEMM that OOMs; RUN with column/row shards and local GeLU/heads; LEAVE with a reduced output tensor and a model that fits across the group.
What to notice when reading
- Nonlinearity placement decides the cut. The entire MLP argument is “don’t reduce across a GeLU.” That one inequality drives the column-then-row pattern.
- Attention parallelism is “free” between QKV and Softmax only because heads partition cleanly — head count should play nicely with .
- Orthogonal ≠ free lunch. Megatron + pipeline + ZeRO-style sharding combine in modern stacks; this paper’s claim is the intra-layer primitive, demonstrated up to 8.3B on 512 GPUs.
- LayerNorm placement (BERT path). Beyond parallelism, §5.3 stresses that rearranging LayerNorm in BERT-like models matters for quality as width grows — systems paper with a modeling footnote.
- SOTA numbers are dated. WikiText / LAMBADA / RACE figures are 2019-era comparisons from the abstract; treat them as evidence the method trained competitive models, not as today’s leaderboard.
Results and evidence
Numbers below are from the paper (arXiv:1909.08053) — not re-measured here.
| Claim | Paper figure |
|---|---|
| Largest converged Transformer in the study | 8.3B parameters on 512 GPUs |
| Sustained throughput | 15.1 PetaFLOPs application-wide |
| Scaling efficiency vs strong 1-GPU baseline | 76% (baseline 39 TFLOPs ≈ 30% of peak) |
| GPT-2-style LM — WikiText-103 | perplexity 10.8 vs prior SOTA 15.8 (abstract) |
| GPT-2-style LM — LAMBADA | accuracy 66.5% vs prior SOTA 63.2% (abstract) |
| BERT-style — RACE | accuracy 90.9% vs prior SOTA 89.4% (abstract) |
| BERT-scale model in study | 3.9B parameters |
Read the scaling plots in §5.1 for how model-parallel degree interacts with data parallelism; the abstract numbers above are the headline anchors.
Limitations
- Communication fabric matters. Intra-layer all-reduces love high-bandwidth links; weak interconnects erode the 76%-style efficiency the paper reports on its cluster.
- Not a memory panacea alone. Activations, optimizer states, and pipeline bubbles still need ZeRO / checkpointing / PP in today’s largest runs.
- Head / width divisibility. Column-parallel attention assumes you can split heads (and hidden width) across .
- Complementary, not universal. Pipeline and expert parallelism solve different bottlenecks; Megatron does not replace them.
- Benchmark drift. Language-modeling SOTA lines moved after 2019 — use the paper’s tables as historical evidence of successful large-model training.
How to read the paper
- Abstract + §3 (model-parallel Transformers) — the GEMM partitioning argument and Figure 3.
- Figure 4 — count the collectives per layer.
- §5.1 — scaling with hybrid model + data parallelism.
- §5.2–5.3 — GPT-2 / BERT quality results and the LayerNorm placement note.
- Appendix B — hybrid MP+DP and RNG details if you implement.
Knowledge check
Why does Megatron prefer splitting the first MLP weight by columns instead of by rows?
How does Megatron’s axis differ from ZeRO’s primary idea?
In the paper’s attention partitioning, when is the first required collective after QKV?
Keep reading / Sources
- Paper: arXiv:1909.08053 — Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism
- Related Daily: ZeRO (optimizer/state sharding), GPipe (pipeline micro-batches), Ring Attention (sequence-parallel KV)
- LayerNorm background: Ba, Kiros, Hinton — Layer Normalization