Once a transformer’s weights and optimizer states exceed a single GPU’s memory, standard data parallelism stalls because every accelerator must replicate the full model. In 2019, NVIDIA researchers Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper, and Bryan Catanzaro showed that training multi-billion-parameter transformers does not require custom compilers like Mesh-TensorFlow or pipeline schedules with idle bubbles like GPipe. Inserting two conjugate all_reduce primitives per transformer layer in native PyTorch lets eight GPUs split heavy matrix multiplications cleanly across NVSwitch.
Megatron-LM’s column-parallel then row-parallel tensor sharding became the standard intra-node building block for modern LLM training and serving, sitting orthogonally alongside data parallelism, ZeRO optimizer state sharding, and later sequence parallelism and selective activation recomputation. Along the way, the team also diagnosed why scaling BERT past 336M parameters previously collapsed: moving LayerNorm off the residual trunk unlocked stable scaling to 3.9B parameters.
- 15.1 sustained petaflops on 512 V100 GPUs: an 8.3B GPT-2 model (
8-way model parallel by64-way data parallel) reaches76%scaling efficiency against a single-GPU 1.2B baseline at39teraflops (30%of V100 peak), while the 32-head sweep holds77%efficiency for 8-way model parallel alone and74%at 512 GPUs. - Four all-reduces per transformer layer: pairing a column-parallel first projection with a row-parallel second projection eliminates mid-block synchronization across both self-attention and the GeLU MLP, leaving
2all-reduces in forward and2in backward. - Fused cross-entropy cuts output traffic over 50,000x: sharding tied embeddings across a
51,200-token padded vocabulary and fusing parallel logit GEMMs with cross-entropy shrinks communicated tensors fromb x s x vlogits down tob x sscalar losses. - LayerNorm placement fixes BERT scaling and sets SOTA: moving LayerNorm onto the sublayer branch (
Architecture (b)) eliminates752M+training divergence, carrying 8.3B GPT-2 to10.81WikiText103 perplexity and66.51%LAMBADA accuracy and 3.9B BERT to90.9%RACE ensemble accuracy (89.5%single model).
Column-then-row sharding eliminates mid-block communication
Every transformer layer alternates between multi-head self-attention and a two-layer MLP (Y = GeLU(XA) followed by Z = Dropout(YB)). Naive tensor splitting breaks immediately on the nonlinearity: if you slice X = [X_1, X_2] by columns and A = [A_1; A_2] by rows (Equation 2), each GPU produces a partial sum X_k A_k. Because GeLU(X_1 A_1 + X_2 A_2) does not equal GeLU(X_1 A_1) + GeLU(X_2 A_2), you must run a costly all_reduce right before the activation.
Megatron-LM reverses the partition order so the nonlinearity stays strictly local. Keeping X duplicated across ranks and slicing A = [A_1, A_2] by columns (Equation 3) lets each GPU compute [Y_1, Y_2] = [GeLU(XA_1), GeLU(XA_2)] independently. Slicing the second weight matrix B = [B_1; B_2] by rows aligns each GPU’s local Y_k directly with B_k, so one all_reduce after Y_k B_k produces the full block output before dropout and the residual add.
Self-attention uses the exact same structure across attention heads. Query, key, and value projections (Q, K, V) are partitioned column-wise so each GPU holds complete weights for its assigned heads (4 of 32 heads per GPU at 8-way split). Dot products, softmax, attention dropout, and value weighting stay inside local GPU memory, and a row-parallel output linear layer merges them via one all_reduce.
In PyTorch, this contract reduces to two conjugate autograd functions, f and g (Code 1). Operator f enters each parallel block as an identity in forward and runs all_reduce on gradients in backward; g exits the row-parallel GEMM with all_reduce in forward and identity in backward. One full transformer layer executes just 2 forward all-reduces and 2 backward all-reduces.
Three supporting choices keep GPUs compute-bound. First, tied input/output embeddings (H x v, padded from 50,257 to 51,200 tokens so per-GPU slices are multiples of 128 at 8-way split) are partitioned by vocabulary E = [E_1, E_2]: input embeddings use one g all-reduce, while output GEMMs [XE_1, XE_2] fuse directly with cross-entropy to communicate b x s scalar losses instead of b x s x v logits. Second, LayerNorm, residual adds, and their optimizer updates are simply duplicated across ranks rather than scattered and broadcast. Third, dropout uses two random generators (Appendix B.2): shared seeds across ranks for residual dropout outside parallel blocks, and distinct per-rank seeds inside local attention dropout.
Scaling to 512 GPUs and fixing BERT’s LayerNorm trap
Experiments ran on 32 DGX-2H servers (512 Tesla V100 SXM3 32 GB GPUs) linking 8 GPUs per node over 300 GB/s NVSwitch (for model parallelism) and nodes via 100 GB/s InfiniBand (for 64-way data parallelism). Training used mixed precision, AdamW (lambda = 0.01), 1.0 global grad-norm clipping, 0.1 dropout, per-layer activation checkpointing, and a 174 GB deduplicated text corpus (Wikipedia, CC-Stories, RealNews, OpenWebText, plus BooksCorpus for BERT only, LSH Jaccard above 0.7).
Weak-scaling benchmarks (Table 1, Figure 5) measured throughput against a 1.2B single-GPU baseline (40 layers, 1536 hidden, 16 heads) sustaining 39 teraflops (30% of V100 peak). Pure model parallelism holds 96% efficiency at 2.5B (2 GPUs), 82% at 4.2B (4 GPUs), and 77% at 8.3B (8 GPUs, 32 heads). Adding 64-way data parallelism (batch size = 512) achieves 95% (128 GPUs), 83% (256 GPUs), and 74% (512 GPUs). Configuring the 8.3B GPT-2 model with 24 attention heads (128 dims/head, 72 layers, 3072 hidden) sustains 15.1 petaflops (76% efficiency), completing one 68,507-iteration epoch every 2.10 days.
Over 300k iterations (Table 3, Figure 6), larger GPT-2 models improved monotonically: WikiText103 test perplexity fell from 19.31 (355M) to 12.76 (2.5B) and 10.81 (8.3B, validation 9.27), beating the 15.79 prior SOTA, while LAMBADA cloze accuracy climbed from 45.18% (355M) to 61.73% (2.5B) and 66.51% (8.3B), surpassing the 63.24% prior SOTA. An 8-gram check confirmed at most 10.8% overlap on WikiText103 and 1.4% on LAMBADA.
Scaling bidirectional BERT uncovered why prior attempts degraded past BERT-large (336M). In Figure 7, the original layout (a) routes the residual trunk through LayerNorm, causing severe loss spikes and divergence at 752M parameters around 200k to 400k iterations. Moving LayerNorm inside the sublayer branch (Architecture (b)), leaving an unnormalized residual skip path (later formalized in Pre-LN transformer analysis), stabilized 752M+ training cleanly. Held-out perplexity fell across 336M (1.58), 1.3B (1.30), and 3.9B (1.16), and Megatron-3.9B set new benchmarks at half to one-third the tokens of RoBERTa and ALBERT: 91.4 / 91.4 MNLI, 92.7 QQP, 95.5 / 90.0 SQuAD 1.1 (95.8 / 90.5 ensemble), 91.2 / 88.5 SQuAD 2.0 (91.7 / 89.0 ensemble), and 89.5% single / 90.9% ensemble RACE test accuracy (beating 89.4% ALBERT ensemble).
Comparing Megatron-LM’s five design choices
Switch through the tabs below to inspect how each mechanism avoids synchronization bottlenecks alongside its measured cluster impact.
Select a component (or use Left and Right arrow keys) to compare the naive bottleneck, Megatron-LM's design, measured scaling or accuracy numbers, and operational tradeoffs.
Column-parallel then row-parallel MLP
Intra-layer TPPartitioning the first MLP weight matrix by columns and the second by rows keeps GeLU local to each GPU.
- Why naive split breaks
- Row-splitting A and column-splitting X forces an all-reduce before GeLU because GeLU(X_1 A_1 + X_2 A_2) != GeLU(X_1 A_1) + GeLU(X_2 A_2).
- Megatron mechanism
- Conjugate f keeps X duplicated forward (all-reduce backward); A = [A_1, A_2] produces local Y_k = GeLU(X A_k); B = [B_1; B_2] feeds one g all-reduce.
- Measured impact
- Cuts MLP synchronization to 1 forward and 1 backward all-reduce per layer across up to 8 NVSwitch-connected GPUs.
- Operational tradeoff
- Requires duplicating input activations X before f and running at fast intra-node NVSwitch speeds (300 GB/s) to stay compute-bound.
Head-wise self-attention sharding
Intra-layer TPSlicing Q, K, and V projections column-wise assigns complete attention heads to each GPU with zero intra-attention communication.
- Why naive split breaks
- Splitting inside a single head's hidden dimension forces extra communication for QK^T dot products and softmax normalization.
- Megatron mechanism
- Column-parallel Q, K, V projections keep softmax and per-head attention dropout (uniquely seeded per rank) local before a row-parallel output projection and g.
- Measured impact
- Table 7 shows 8-way TP efficiency on 8.3B stays high at 82% (16 heads, 192 dim), 80% (24 heads, 128 dim), and 77% (32 heads, 96 dim).
- Operational tradeoff
- Attention head count must be divisible by the tensor-parallel degree, and excessively small per-head GEMMs trim scaling efficiency.
Vocab-parallel embeddings and fused cross-entropy
Vocabulary TPSharding tied word embeddings across the vocabulary and fusing parallel logit GEMMs with cross-entropy avoids gathering full logits.
- Why naive split breaks
- Running an all-gather on [X E_1, X E_2] before the loss communicates b x s x v tensors (51,200 floats per token position).
- Megatron mechanism
- Vocab padded to 51,200 (divisible by 128 x 8 = 1024); input embedding uses 1 all-reduce while output logits fuse directly into cross-entropy to emit b x s scalars.
- Measured impact
- Shrinks output layer communication volume by more than 50,000x (from b x s x 51,200 down to b x s) while keeping Tensor Core GEMMs aligned.
- Operational tradeoff
- Requires minor padding of the vocabulary table so per-GPU slices remain multiples of 128 for Tensor Core throughput.
Moving LayerNorm off the residual trunk
Architecture fixPlacing LayerNorm inside the sublayer branch (Architecture b) prevents training collapse when scaling bidirectional BERT beyond 336M parameters.
- Why naive BERT breaks
- Original Architecture (a) routes the residual highway through LayerNorm, causing severe training loss spikes and divergence at 752M parameters (Figure 7).
- Megatron mechanism
- Architecture (b) leaves an unnormalized identity skip connection around LayerNorm + Sublayer, matching GPT-2's residual stability.
- Measured impact
- Held-out perplexity drops from 1.58 (336M) to 1.30 (1.3B) and 1.16 (3.9B), reaching 90.9% RACE test accuracy (ensemble) at 1x token ratio.
- Operational tradeoff
- Changes gradient flow across depth and requires a final LayerNorm before the output head, matching modern Pre-LN conventions.
8-way NVSwitch TP plus 64-way InfiniBand DP
Cluster scaleConfining high-frequency tensor all-reduces inside 8-GPU nodes while running data-parallel gradient all-reduces across 64 servers scales cleanly to 512 GPUs.
- Why naive scaling breaks
- Pure data parallelism OOMs past 1.2B parameters on 32 GB V100s; pure strong scaling at fixed batch 8 hits diminishing returns (2.98x on 8 GPUs, Table 8).
- Megatron mechanism
- GPUs 1..8 inside each DGX-2H form a 300 GB/s NVSwitch TP group; corresponding rank indices across 64 nodes form 100 GB/s InfiniBand DP groups (Figure 8).
- Measured impact
- Sustains 15.1 PetaFLOPs at 76% scaling efficiency on 512 GPUs for the 8.3B GPT-2 training run (74% for the 32-head weak-scaling grid).
- Operational tradeoff
- Cannot scale a single layer beyond one node's fast interconnect (max 16 GPUs in DGX-2H) without adding pipeline parallelism.
Source: Shoeybi et al. (2019), Sections 3 through 5 and Appendix B.
If you prefer the summary without clicking through the tabs, here are the five core mechanisms:
- MLP split: column-parallel
Afollowed by row-parallelBkeepsGeLUlocal and trims each MLP to twoall_reducecalls total. - Attention split: column-parallel
Q, K, Vkeeps softmax and per-rank attention dropout local before a row-parallel output projection. - Fused cross-entropy: vocabulary-sharded
51,200-token embeddings fused with cross-entropy slash output communication fromb x s x vtob x s. - BERT Pre-LN rearrangement: moving LayerNorm off the residual trunk eliminates
752M+divergence and drives 3.9B BERT to90.9%RACE accuracy. - Hybrid 2D topology: 8-way
300 GB/sNVSwitch TP plus 64-way100 GB/sInfiniBand DP sustains15.1petaflops (76%efficiency) on512V100s.
Limits of intra-layer tensor parallelism
Three constraints define where Megatron-LM tensor parallelism tops out on its own.
First, tensor parallelism requires fast intra-node interconnects. Because f and g place 4 synchronous all_reduce calls per layer on the critical path, Section 6 notes that models exceeding 16B parameters (beyond 16 V100s in one DGX-2H box) cannot simply stretch TP across slower inter-node InfiniBand links without adding pipeline parallelism or optimizer sharding (as in 17B Turing-NLG with ZeRO).
Second, strong-scaling latency gains taper quickly. While weak scaling 1.2B to 8.3B holds 77% efficiency on 8 GPUs, Table 8 shows that fixing batch size at 8 on the 1.2B model yields only 1.64x speedup on 2 GPUs, 2.34x on 4, and 2.98x on 8 as smaller GEMMs underutilize Tensor Cores and NVSwitch latency dominates. Similarly, splitting 8.3B into 32 heads (96 dims) instead of 16 (192 dims) drops 8-way efficiency from 82% to 77% (Table 7), underscoring why kernel-level tiling later mattered in FlashAttention-2 work partitioning.
Third, duplicating LayerNorm, dropout, and residual streams replicates activations. Every TP rank still stores redundant b x s x H tensors outside the parallel GEMMs and needs per-layer activation checkpointing (Section 4.2), a bottleneck later removed by sharding along the sequence axis in sequence parallelism.
For cluster architects today, Megatron-LM’s operational boundary remains clean: keep tensor parallelism inside one node’s high-bandwidth NVLink fabric (4 to 8 GPUs), pad vocabularies to Tensor Core multiples, fuse cross-entropy across vocabulary shards, and cross node boundaries with data, ZeRO, or pipeline parallelism.


