Skip to main content
issue 2026-09-12System Design40 minarXiv 2019interactive

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.

Shard Transformer GEMMs across GPUs — column then rowLeft: a full weight matrix XA plus GeLU does not fit on one GPU. Middle: column-parallel first GEMM keeps GeLU local on each shard; row-parallel second GEMM all-reduces. Right: the layer output is assembled with two collectives per pass.ENTER · FULL GEMMONE GPU OOMXACTAFULL WOOMBILLIONSREPLICATE FAILSRUN · TP SHARDSCOL THEN ROWA1COLA2COLGELULOCALNO SYNC BEFORE GELULEAVE · REDUCETWO AR / PASSYSUMMLP + ATTN FUSEDTensor parallelism: split GEMMs in-layer so GeLU and heads stay localMegatron shards GEMMs column then rowMobile layout: full-matrix OOM, column shards with local GeLU, then all-reduce output.ENTEROOMFULL ARUN · A1 | A2A1GELUA2LEAVEARY

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:

  1. Name the memory wall. GPT-2 / BERT-style stacks with GeLU and pre-attention LayerNorm still blow single-GPU memory once parameters reach billions.
  2. Reject the wrong GEMM split. For Y=GeLU(XA)Y=\mathrm{GeLU}(XA), splitting AA by rows forces a sync before GeLU because GeLU(X1A1+X2A2)GeLU(X1A1)+GeLU(X2A2)\mathrm{GeLU}(X_1A_1+X_2A_2)\neq\mathrm{GeLU}(X_1A_1)+\mathrm{GeLU}(X_2A_2).
  3. 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.
  4. Mirror that pattern in attention. Column-parallel Q,K,VQ,K,V so heads run locally; row-parallel output projection — no sync between QKV and Softmax.
  5. 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.

Data parallel vs tensor parallel vs pipelineThree panels: data parallel replicates the full model; Megatron tensor parallel shards GEMMs inside each layer; pipeline parallel slices layers across stages.Data parallelfull copy / rankALL LAYERSeasy · memory wasteMegatron TPshard GEMMs in-layerA1A2GeLU local · AR afterPipeline (GPipe)slice depth / stagesstage 0stage 1bubbles · micro-batches
Megatron sits on the tensor axis: split matmuls inside a layer, unlike DP copies or pipeline stages.

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

Y=GeLU(XA).Y = \mathrm{GeLU}(XA).

Bad split (rows of AA). Partition

X=[X1,X2],A=[A1A2]X=[X_1,X_2],\quad A=\begin{bmatrix}A_1\\ A_2\end{bmatrix}

so Y=GeLU(X1A1+X2A2)Y=\mathrm{GeLU}(X_1A_1+X_2A_2). You must reduce before GeLU.

Good split (columns of AA). Partition A=[A1,A2]A=[A_1,A_2] and compute

[Y1,Y2]=[GeLU(XA1),GeLU(XA2)][Y_1,Y_2]=[\mathrm{GeLU}(XA_1),\,\mathrm{GeLU}(XA_2)]

locally. The second GEMM is then row-parallel: it consumes [Y1,Y2][Y_1,Y_2] without an intervening collective, and an all-reduce (gg) sums partial outputs before dropout.

Conjugate collectives ff / gg: 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 QQ, KK, VV 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).

Interactive

How wide is each GPU's column shard?

Teaching dial: pick model-parallel degree Nmp and a toy hidden width H. Each column-parallel rank owns about H / Nmp features (before the row-parallel all-reduce) — pedagogy, not the paper's 8.3B scaling table.

Local columns2048
AR / layer (fwd)2
GeLU sync?no

Comfortable column shard — each GPU still sees a wide local matmul before the all-reduce.

Algorithm / figure walkthrough

  1. Form a model-parallel group of size NmpN_{\mathrm{mp}} (and optionally a DP group outside it).
  2. Shard MLP weight AA by columns across the group; each rank computes local GeLU(XAi)\mathrm{GeLU}(XA_i).
  3. Shard the second MLP weight by rows; locally multiply; all-reduce to assemble the MLP output.
  4. Shard WQ,WK,WVW_Q,W_K,W_V by columns so each rank owns a subset of heads; run attention locally.
  5. Shard the output projection by rows; all-reduce after the projection.
  6. Repeat for every layer; train with ordinary Adam/mixed-precision stacks as in the paper’s setup.
  7. Scale out: grow NmpN_{\mathrm{mp}} when a layer’s weights no longer fit; grow DP for throughput.
Collectives in a Megatron Transformer layerAttention path: column-parallel QKV, local Softmax, row-parallel projection with all-reduce. MLP path: column-parallel first GEMM with local GeLU, row-parallel second GEMM with all-reduce. Two AR forward, two backward.Self-attention1. Col-parallel Q K V2. Softmax local / head3. Row-parallel out + AR (g)1 AR forwardMLP1. Col-parallel XA_i2. GeLU local3. Row-parallel + AR (g)1 AR forward
Paper Figure 4 intuition: fuse GEMM pairs so each Transformer layer needs two all-reduces forward and two backward (f/g conjugates).

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 NmpN_{\mathrm{mp}}.
  • 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.

ClaimPaper figure
Largest converged Transformer in the study8.3B parameters on 512 GPUs
Sustained throughput15.1 PetaFLOPs application-wide
Scaling efficiency vs strong 1-GPU baseline76% (baseline 39 TFLOPs ≈ 30% of peak)
GPT-2-style LM — WikiText-103perplexity 10.8 vs prior SOTA 15.8 (abstract)
GPT-2-style LM — LAMBADAaccuracy 66.5% vs prior SOTA 63.2% (abstract)
BERT-style — RACEaccuracy 90.9% vs prior SOTA 89.4% (abstract)
BERT-scale model in study3.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 NmpN_{\mathrm{mp}}.
  • 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

  1. Abstract + §3 (model-parallel Transformers) — the GEMM partitioning argument and Figure 3.
  2. Figure 4 — count the collectives per layer.
  3. §5.1 — scaling with hybrid model + data parallelism.
  4. §5.2–5.3 — GPT-2 / BERT quality results and the LayerNorm placement note.
  5. 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