In one line: Made tensor (intra-layer model) parallelism simple for transformers in plain PyTorch: column-split the GEMM feeding each nonlinearity and row-split the one after it, so a transformer layer needs only 4 all-reduces (2 forward, 2 backward), training an 8.3B GPT-2 on 512 V100s at 76% weak-scaling efficiency against one GPU.
How far to trust it: Trust the mechanism fully: it is exact algebra, and every later training and serving stack uses it. Read the 76% as weak scaling in model size against a strong baseline, not as a speedup, and the state-of-the-art results as single runs on different training data. The evidence, point by point.
The idea in one sentence: split the matrix that feeds a nonlinearity by columns and the matrix after it by rows, so each GPU runs its slice of the block with no communication and one all-reduce restores the output; a whole transformer layer then costs 2 all-reduces forward and 2 backward, written in plain PyTorch.
You need: data parallelism and what an all-reduce is (Distributed training, sections 2 and 3). New terms: column-parallel (each GPU holds whole output columns of a weight matrix), row-parallel (each GPU holds input rows and produces a partial sum), f and g (the two communication operators), weak scaling (the model grows with the GPU count) against strong scaling (the same model on more GPUs).
Redrawn from Figures 3 and 4 for 2 GPUs, with the shapes of the 8.3B scaling model (H = 3,072, 32 heads, Table 1); the paper runs it on 8. Blue: work on this GPU's own slice. Orange, dashed: a partial sum. Grey: identical on both GPUs. f and g are the paper's two operators: g all-reduces in the forward pass, f in the backward pass, once per block, so 4 per layer.
By 2019 the useful frontier of language models (BERT-large at 336M parameters, GPT-2 at 1.5B) had reached the memory of a single accelerator. Training keeps the weights, their gradients and the Adam optimiser's extra state per parameter, plus the activations, and a 32 GB V100 was running out even with activation checkpointing (§1, §2.3; what recomputation trades is on Distributed training, section 10). Data parallelism, the usual way to scale out, has a hard limit here: "the model must fit entirely on one worker" (§2.3). Sharing parameters across layers, as ALBERT does, shrinks the footprint but also the model's capacity.
The existing ways to split a model across devices each had a cost (§2.3):
The calculator makes the memory wall concrete with the Table 1 models. It counts 16 bytes per parameter for mixed-precision Adam (fp16 weights and gradients, fp32 master weights, momentum and variance, the accounting in ZeRO §3.1), divides it by the number of GPUs splitting each layer, and adds the activations that checkpointing after every layer still holds.
The gap: a way to split a transformer layer itself across GPUs that is simple enough to write in native PyTorch with a handful of communication calls: no new compiler, no new framework, and orthogonal to pipeline parallelism so the two can be combined (§1).
Try: pick each model and find the fewest GPUs that bring it under the 32 GB line, then compare with the GPU count the paper used in Table 1.
Defaults reproduce: the fewest GPUs that fit is 1, 2, 4 and 8 for the four models, Table 1's model-parallel GPU column, independently (recompute.py).
Per GPU: 16 × parameters / t for weights, gradients and Adam state; 2sbh bytes per layer for the checkpointed fp16 layer inputs (not split by Megatron); one layer's full activations during its recomputation, sbh(10 + 24/t + 5as/(ht)) bytes (Korthikanti et al. 2022, §4.1). Batch 8 and sequence 1,024 as in the paper's model-parallel scaling runs (§5.1). Ignores fragmentation and framework buffers.
The core idea is intra-layer model parallelism, now called tensor parallelism (TP): partition the weight matrices of every transformer layer across GPUs, choosing the direction of each split so that no nonlinearity ever sees a partial sum. Then the GPUs only need to synchronise a few times per layer (§3).
A transformer layer is a self-attention block followed by a two-layer MLP (Figure 2). The MLP's first half is a matrix multiply (GEMM) followed by a GeLU:
There are two ways to split that GEMM across two GPUs:
Megatron takes option 2 for the first GEMM (column-parallel) and then splits the second GEMM, B (4H × H), by rows (row-parallel): B = [B1; B2], so GPU i computes YiBi straight from its own GeLU output and the full result Y1B1 + Y2B2 is recovered by a single all-reduce before dropout (§3, Figure 3a). The general recipe: column-split the layer that feeds a nonlinearity, row-split the layer that follows it, and the elementwise nonlinearity lives entirely between them with zero communication.
Try: these are real matrices (seeded random, small enough to see every entry), multiplied in your browser. Switch the split and the nonlinearity: with no nonlinearity both splits are exact, because a matrix product is linear; with GeLU or ReLU only the column split is.
X is 3 × 8 (3 tokens, H = 8), A is 8 × 16 (4H), entries drawn from a normal distribution with a fixed seed. GeLU is the tanh approximation used by GPT-2 and BERT. The reference is GeLU(XA) computed on one device; the error is the largest absolute difference over all 48 outputs. Illustrative sizes; the equations are the paper's.
The MLP block is Z = Dropout(GeLU(XA)B), with A: H × 4H and B: 4H × H. The whole scheme is packaged as two conjugate autograd functions placed around each block (Figure 3):
| Operator | Where | Forward pass | Backward pass | Why |
|---|---|---|---|---|
| f | block input | identity | all-reduce of the gradient | every GPU needs the full X, and each contributes part of the gradient with respect to X, which must be summed |
| g | block output | all-reduce | identity | sums the row-parallel partials YiBi; its gradient is already complete on every GPU |
Each is a few lines of PyTorch. The paper prints f (Code 1); g swaps the two methods:
class f(torch.autograd.Function):
def forward(ctx, x):
return x
def backward(ctx, gradient):
all_reduce(gradient)
return gradient
With one g and one f per block and two blocks per layer, a transformer layer needs exactly 4 all-reduces in total: 2 in the forward pass (one g per block) and 2 in the backward pass (one f per block), covering both attention and the MLP (§3, Figure 4).
Megatron against the split it rejected, step by step (Fig. 2). One MLP block runs forward and backward on 2, 4 or 8 GPUs, at a Table 1 model's own sizes. Megatron splits A by columns and B by rows (§3); Option 1 splits A by rows and must synchronise before the GeLU (Eq. 2). The plain two-GPU forward pass, with sequence parallelism beside it, is drawn on Distributed training (Fig. 7); this figure adds the backward pass, the rejected split and what each sends. Predict before Option 1 plays: it adds one synchronisation; does it send about 1.5, 2 or nearly 4 times Megatron's bytes per block?
In Fig. 2 each lane is one GPU. Glass plates: the weights that GPU holds, drawn to the model's shape (rows up, columns across; the outline is the whole matrix). Slabs on the lane: what it holds for one token, blue its own slice, orange a partial sum (full width but not yet the answer), grey the full tensor, identical on every GPU. Dots along a lane are local work; dots between lanes are the only communication. The column on the right is what each GPU has sent so far; its outline, the other split's whole block.
Elements sent per GPU use the ring algorithm: an all-reduce of n elements sends 2(t − 1)/t × n, an all-gather (t − 1)/t × n, at 2 bytes (fp16) for b × 1,024 tokens. Time is elements ÷ link speed, with no overlap and no latency. Megatron's two collectives are the paper's (Figure 3a); Option 1's backward collectives are derived here (the conjugate of each forward step), not given in the paper.
3.75 times for any t. Megatron sends 4(t − 1)/t × H numbers per token per block (two all-reduces of H); Option 1 sends 15(t − 1)/t × H (all-reduces of 4H and H, all-gathers of 4H and H), and every GPU evaluates all 4H GeLUs instead of 4H/t. With activation checkpointing, as the paper trains, each layer's forward pass runs again during the backward pass, and its 2 all-reduces with it: 6 per layer in practice (our reading of how checkpointing works; the paper counts 4). The ring formula behind the byte counts is Interconnects and scaling's.
Multi-head attention is embarrassingly parallel over heads. The query, key and value projections are split by columns so that each GPU holds the full Q, K and V projections for a subset of heads; the softmax, attention dropout and the weighted sum over V for those heads then run entirely locally. The output projection is split by rows and consumes the local head outputs directly, followed by the g all-reduce: the same fused pair of GEMMs, the same single forward synchronisation (§3, Figure 3b). It also means the number of heads must be divisible by the number of GPUs.
Yes, a little: at 8.3B on 8 GPUs, 16 heads of 192 scale at 82%, 24 of 128 at 80% and 32 of 96 at 77%, because more heads mean smaller GEMMs and a larger softmax (Table 7).
The output embedding is H × v and GPT-2's vocabulary is 50,257 tokens, so it is worth splitting; and because the input and output embeddings share weights, both are split. The input embedding EH×v is split along the vocabulary, E = [E1, E2]: each GPU looks up the tokens in its slice and an all-reduce (g) merges them. For the output, the parallel GEMM [Y1, Y2] = [XE1, XE2] gives each GPU the logits of its slice. All-gathering them for the loss would communicate b × s × v elements (batch × sequence × vocabulary), huge because v is large; instead the GEMM is fused with the cross-entropy, which reduces what is communicated to b × s (§3).
The vocabulary is padded so each GPU's slice is a multiple of 128 for efficient GEMMs: for up to 8-way splits that means a multiple of 128 × 8 = 1,024, and 50,257 becomes 51,200 (§5.1).
fp16 logits at 2 bytes; losses and statistics at 4 bytes (fp32). Bars to scale on a log axis. The paper counts b × s; Megatron's current cross_entropy.py all-reduces three b × s tensors (the maximum logit, the target's logit and the sum of exponentials), which is still a factor v/3 smaller. Llama 3's vocabulary is from its paper.
"Much of our model parallel approach can be characterized as techniques aimed at reducing communication and keeping the GPUs compute bound" (§3). LayerNorm, dropout and the residual connections are cheap, so instead of one GPU computing them and broadcasting the result, every GPU keeps a duplicate copy of the LayerNorm parameters and runs dropout and the residual on the output of the parallel region before feeding the next one. Each model-parallel worker optimises its own set of parameters; since every value is either local to a GPU or duplicated on all of them, no updated parameter values are ever communicated.
Dropout needs care because some of it sits outside the parallel regions (before the residual connections) and some inside (attention dropout). Outside, the GPUs must drop the same elements, so all random number generators are seeded identically at the start; inside, each GPU must draw a different pattern, so a separate generator is kept for dropout in the parallel regions, seeded uniquely per worker (Appendix B.2).
Tensor parallelism is orthogonal to data parallelism (and to pipeline parallelism), so they compose. Two or more GPUs in the same server form a model-parallel group holding one copy of the model; GPUs with the same position in every model-parallel group form a data-parallel group, whose weight gradients are all-reduced together, several such all-reduces running in parallel. The total GPU count is the product. For the 8.3B model: 8 GPUs per model-parallel group times 64-way data parallelism is 512 GPUs. All communication is PyTorch calling NCCL (Appendix B.1, Figure 8). The 8-way groups sit inside one DGX-2H server, where NVSwitch gives 300 GB/s between GPUs, against 100 GB/s per server between servers (§5).
Fig. 3 builds the 8.3B run's layout of Figure 8 on its real hardware: 32 DGX-2H servers of 16 V100s, GPUs numbered as in the figure.
Try: pick a GPU. Fig. 3 lights its model-parallel group (blue, inside one server) and its data-parallel group (green, across every server).
One slab per DGX-2H server (16 V100s in two rows of 8), 32 servers, numbered as in Figure 8 (GPUs 1 to 8 form the first model-parallel group; 1, 9, ..., 505 a data-parallel group).
Data (§4.1): an aggregate of Wikipedia, CC-Stories, RealNews and OpenWebText, with the Wikipedia articles of the WikiText103 test set removed to avoid leakage and stray newlines removed from CC-Stories; BooksCorpus is added for BERT but excluded for GPT-2 because it overlaps with LAMBADA. Documents shorter than 128 tokens are dropped, near-duplicates (Jaccard similarity above 0.7) removed with locality-sensitive hashing: 174 GB of deduplicated text.
Recipe (§4.2):
Hardware (§5): up to 32 DGX-2H servers, 512 Tesla V100 SXM3 32 GB GPUs; 300 GB/s between GPUs in a server through NVSwitch, 100 GB/s between servers through 8 InfiniBand adapters per server.
Scaling configurations (§5.1, Table 1): four GPT-2 models from 1.2B to 8.3B parameters, keeping the hidden size per head at 96 so attention GEMMs stay the same size; 1.2B fits on one GPU, 8.3B needs 8-way model parallelism. Model-parallel runs use a fixed batch of 8; model-plus-data-parallel runs a global batch of 512, which is 64-way data parallelism.
The baseline is the 1.2B model on a single GPU, a strong one: it sustains 39 TFLOP/s, 30% of the theoretical peak of a V100 in a DGX-2H server (§5.1.1), which makes the peak 130 TFLOP/s (39 / 0.30). Scaling is weak scaling in model size: about 1 billion parameters per GPU, so 2 GPUs train the 2.5B model and 8 the 8.3B (Figure 1), rather than the usual weak scaling in batch size.
The 8.3B model on 8 GPUs keeps 77% of linear scaling. Adding data parallelism costs a little more, for the gradient all-reduces, and the 8.3B model on all 512 GPUs keeps 74% by Figure 5 and the text beside it, sustaining up to 15.1 PFLOP/s over the whole application (§1).
Model parallelism also speeds up a model that already fits (strong scaling): the 1.2B model at batch 8 runs 1.64 times faster on 2 GPUs, 2.34 on 4 and 2.98 on 8, with returns diminishing "as the per-GPU computation decreases and the memory bandwidth and communication overheads begin to dominate" (Appendix D.2, Table 8). That is 82%, 59% and 37% of linear: tensor parallelism is for fitting big models, not for speeding up small ones.
Try: the bars are what the paper measured; the lines are a model that charges only for communication, with every GEMM running at the single-GPU baseline's 39 TFLOP/s. Switch the link to InfiniBand to see why tensor parallelism stays inside one server.
Per layer and iteration at batch 8, sequence 1,024: compute 96bsh²(1 + s/6h) FLOPs ÷ (t × 39 TFLOP/s), the forward, recomputed forward and backward GEMMs (Narayanan et al. 2021, Eq. 3, without the logit term); communication 4 or 6 all-reduces of bsh fp16 values. Weak-scaling efficiency is compute ÷ (compute + communication), since the 1-GPU baseline has no communication. Measured: Figure 5 model-parallel bars, Table 8 speedups ÷ t.
Three GPT-2 models were trained (Table 2): 355M (the size and shape of BERT-large), 2.5B (bigger than the previous largest GPT-2) and 8.3B ("larger than any left-to-right transformer language model ever trained, to the best of our knowledge"), the last with 24 heads rather than the 32 of the scaling study. An epoch is 68,507 iterations; for the 8.3B model on 512 GPUs it takes about two days (2.10). Larger models converge noticeably faster and to lower validation perplexity; the 8.3B reaches 9.27 (§5.2, Figure 6).
| Model | WikiText103 perplexity ↓ | LAMBADA accuracy ↑ |
|---|---|---|
| 355M | 19.31 | 45.18% |
| 2.5B | 12.76 | 61.73% |
| 8.3B | 10.81 | 66.51% |
| Previous state of the art | 15.79 | 63.24% |
Zero-shot, Table 3; previous best from Khandelwal et al. 2019 (WikiText103) and GPT-2 (LAMBADA). The abstract rounds these to 10.8 against 15.8 and 66.5% against 63.2%.
Two evaluation details matter for reading these numbers (Appendix E). WikiText103 perplexity is normalised by the test set's original word-level token count, To = 245,566, not the 270,329 subword tokens the model actually sees, so it is comparable with earlier word-level models; it is computed with overlapping 1,024-token windows that advance by 32 tokens (Eq. 4). LAMBADA counts a word right only when every one of its subword tokens is predicted correctly. Test-set leakage was checked with 8-gram overlap, as GPT-2 did: at most 10.8% for WikiText103 (whose own training set already overlaps its test set by 9.09%) and 1.4% for LAMBADA (§5.2). Appendix C prints text samples from the 8.3B model (Appendix C), and v4 of the paper notes that Microsoft and NVIDIA have since trained the 17B Turing-NLG "using Megatron".
The often-forgotten second contribution. ALBERT had reported that BERT degrades when made larger than BERT-large's 336M parameters, and answered with parameter sharing (§5.3). This paper finds instead that the order of the layer normalisation and the residual connection is what matters: in Figure 7 a 752M BERT with the original arrangement (a) trains well for a while, then its loss jumps and does not recover, while the rearranged architecture (b) trains stably to a lower loss.
Fig. 4, step by step
In (a) the residual is taken from the LayerNorm's output, which, unrolled, is the original post-LN BERT, every block's output renormalised; in (b) it is taken from the block's input, the pre-LN arrangement GPT-2 already used. Why the pre-LN path trains more stably is taught, with Xiong et al.'s gradient argument, on Normalisation, residuals and stability. With (b), and the hidden size per head fixed at 64, three BERT models were trained (Table 4): 336M (BERT-large's size), 1.3B (BERT-xlarge's configuration, which ALBERT had found worse than 336M) and 3.9B (wider and deeper); the first two for 2 million iterations, the 3.9B for 1.5 million and "still training". Validation perplexity on a 3% held-out set falls monotonically: 1.58, 1.30, 1.16 (§5.3).
Fine-tuned as RoBERTa was (batch size and learning rate tuned, median of 5 seeds reported; Table 6 lists the settings), every downstream score improves with size, and the 3.9B model sets the best development-set results among BERT-style models on MNLI (91.4/91.4), QQP (92.7), SQuAD 1.1 (95.5/90.0) and SQuAD 2.0 (91.2/88.5) (Table 5). On the RACE test set the single 3.9B model scores 89.5% against ALBERT's 86.5%, and the 5-way ensemble 90.9% against ALBERT's ensemble at 89.4%: the 90.9% the abstract quotes is the ensemble. Megatron's models also saw fewer training tokens (ratio 1 against 2 to 3 for the others).
Structure as drawn in Figure 7. The count is structural (how many LayerNorms sit on the path from a block's input to the top of the stack), not a measurement; why that path matters is beyond the paper (Xiong et al. 2020).
What holds up, and why.
What to read with care.
Verdict: believe the method completely; it is exact and has been the standard for seven years. Treat "77%" and "76%" as "a 7 times larger model on 8 GPUs, and on 512, at about three quarters of the per-GPU throughput", not as a speedup, and treat the accuracy records as single runs on different data that showed scale helps, not as controlled comparisons.
Every number below is transcribed from the arXiv HTML (v4) of the paper, except Figure 5's labels, which come from the PDF; every derived number is recomputed in recompute.py. Each section links the table it rebuilds.
Parameters recounted as vh + 1,024h + l(12h² + 13h) + 2h: tied embedding with the padded vocabulary v = 51,200, learned positions, per layer the QKV, output and two MLP matrices with biases and two LayerNorms, and a final LayerNorm. Defaults reproduce: 1.2, 2.5, 4.2 and 8.3 billion independently; and the fewest GPUs under 32 GB (the memory calculator's accounting) equals the model-parallel column for every row.
An epoch is 68,507 iterations of 512 × 1,024 tokens, 35.9 billion tokens (§5.2). FLOPs per iteration 96Bslh²(1 + s/6h + v/16lh), which counts the recomputed forward pass of activation checkpointing (Narayanan et al. 2021, Eq. 3, a later paper by the same group). Training days = days per epoch × 300,000 / 68,507 iterations.
The 8.3B model cuts WikiText103 perplexity by 4.98 (32% relative) and adds 3.27 points on LAMBADA over the previous best (recompute.py). Perplexity is per original word-level token (To = 245,566); per subword token (T = 270,329) the same loss would read 10.81To/T = 8.69 (Appendix E.1).
Recount: word (30,522), position (512) and token-type embeddings with their LayerNorm, l(12h² + 13h) for the layers, the pooler, the masked-LM transform with its LayerNorm and output bias (output weights tied), and a 2-way sentence-order head. Defaults reproduce: 336M, 1.3B and 3.9B independently (Megatron pads the vocabulary for efficient GEMMs as it does for GPT-2, which would add a few million parameters).
Try: pick a column; rows sort by it and each shows its distance from the best non-Megatron model of the same kind (single models against single models, ensembles against ensembles). Where a cell has two numbers the first (m, F1, or the overall RACE score) is used.
Degrees and GPU counts are as each paper states them; the captions quote or link the line.
Each step is one later training run or system, with the parallel layout its paper reports. The box is one server (its GPUs coloured by the axis that lives inside it); the bars are the degree of each axis across the whole run, on a log scale. A degree the paper does not state is drawn dashed and marked "not stated".
| Change | 2019 | Later | Source |
|---|---|---|---|
| TP plus pipeline | TP inside a server, DP across | TP inside a server, PP across servers, DP on top (PTD-P, "3D") | Narayanan et al. 2021; MT-NLG (8-way TP, 35-way PP per 280-GPU replica) |
| Sequence parallelism | LayerNorm and dropout duplicated on every TP rank; g is an all-reduce | they run on a slice of the sequence; g becomes an all-gather and its conjugate a reduce-scatter, the same bytes; together with selective recomputation, activation memory falls 5× | Korthikanti et al. 2022 |
| Context parallelism | whole sequence on every rank | the sequence split across GPUs for long-context training (CP 16 at 131,072 tokens) | Llama 3, Table 4 |
| Sharded data parallelism | each data-parallel rank keeps full optimizer state for its TP shard | ZeRO and FSDP shard optimizer state, gradients and weights across data-parallel ranks | ZeRO; Llama 3 uses FSDP; DeepSeek-V3 ZeRO-1 |
| Expert parallelism | dense models only | MoE experts spread across GPUs; DeepSeek-V3 trains with EP 64 and no TP | DeepSeek-V3 §3.2 |
Every link opens in a new tab. Reading time of the resources below: 4h 00m.
arXiv preprint, the paper itself; the HTML version has the anchors this page links to. abstract page, PDF.
(45 min)
the authors' code (repo, about 25 minutes for the README and entry path); still developed, now as Megatron-Core, and the reference implementation of everything in the paper.
(25 min)
Hugging Face (nanotron), 2025: the clearest modern walkthrough of column- and row-parallel linears, with communication-cost analysis and profiles.
(40 min)
Lilian Weng, 2021: situates Megatron-style tensor parallelism among the other parallelism and memory-saving techniques.
(45 min)
Narayanan et al., 2021 (arXiv 2104.04473): the follow-up: this paper's tensor parallelism combined with pipeline and data parallelism (PTD-P) and an interleaved 1F1B schedule, 502 PFLOP/s on 3,072 A100s.
(45 min)
Korthikanti et al., 2022 (arXiv 2205.05198): sequence parallelism: the same f and g idea, with each all-reduce split into a reduce-scatter and an all-gather so LayerNorm and dropout are no longer duplicated.
(40 min)
2017: the layer structure being partitioned (attention heads and a two-matrix MLP).
2018: the architecture whose LayerNorm and residual placement this paper changes to scale it past 336M.
2019, a month later: the memory-side alternative, sharding optimizer state, gradients and weights under data parallelism instead of splitting layers; Turing-NLG used both.
2020: trained with "a mixture of model parallelism within each matrix multiply and model parallelism across the layers", at the scale this line of work enabled.
2021: expert parallelism, the axis that later replaced tensor parallelism in some MoE training runs.
2024: 4D parallelism with tensor parallelism of 8 inside each NVLink machine, citing this paper.
2024: trains without tensor parallelism (pipeline 16, expert 64, ZeRO-1 data parallel), but serves with 4-way TP plus sequence parallelism for attention.
2026: the same memory accounting (weights, optimizer state, activations per GPU) applied to MoE training.
distributed training: the tensor-parallel axis.
tensor parallelism as the default way to serve a model across several GPUs.
NVLink and NVSwitch bandwidth as the constraint that keeps tensor parallelism inside one machine.
pipeline schedules (1F1B, DualPipe) and expert placement: the axes that took over from TP there.
This page is one row of the Papers database; its properties (Paper, Takeaway, Topics, Year) live there.