Pidoku

Megatron-LM (2019)

Module 30 Lesson 30.c 40 min Difficulty 3/5

Prerequisites 10.i, 9.f, 14.a


Before you start: words you need#

WordPlain meaning
Model parallelismSplitting one model across several GPUs because it does not fit, or is too slow, on one.
Tensor parallelism (TP)The kind of model parallelism this paper introduced for Transformers: split each weight matrix inside a layer across GPUs (14.c). The paper calls it intra-layer model parallelism.
Pipeline parallelismThe other kind: give each GPU a different group of layers (14.d).
Data parallelismEvery GPU holds a full copy and works on different inputs (14.b).
All-reduceA collective operation in which every GPU contributes a tensor and every GPU ends up with the sum (9.f).
Column split / row splitCutting a weight matrix into vertical strips (each GPU gets some output columns) or horizontal strips (each GPU gets some input rows).
Weak scalingGrowing the problem with the number of GPUs (bigger model on more GPUs) and checking that throughput per GPU stays high.
NVSwitchThe switch chip that connects all GPUs inside an NVIDIA server at full NVLink speed (9.c).

1. The paper#

  • Title: “Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism”
  • Authors: Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper, Bryan Catanzaro (NVIDIA)
  • Venue: arXiv preprint, September 2019 (v4, March 2020). The follow-up that combined it with pipeline and data parallelism is Narayanan et al., SC 2021 (arXiv:2104.04473).
  • Link: arXiv:1909.08053

It is a training paper. It is in an inference module because the same split, unchanged, is how every engine in 2026 runs a model across the GPUs of one server.


2. The problem#

By 2019 language models were outgrowing one GPU. A 32 GB V100 could not hold an 8-billion-parameter model together with its gradients and optimizer state. The existing ways to split a model needed a special framework: GPipe for pipelines, Mesh-TensorFlow with its own compiler. The authors wanted something you could add to ordinary PyTorch with a few lines.


3. The idea#

Look at the structure of a Transformer layer and split its matrices so that the GPUs need to talk as rarely as possible.

The MLP block is two matrix multiplies with a GeLU between: Y = GeLU(X·A), then Z = Y·B.

  • Split A by columns. Each GPU computes some columns of X·A from the full input, and can apply GeLU to its own columns with no communication, because GeLU acts on each element separately. (Splitting A by rows would need a sum before GeLU, an extra synchronisation.)
  • Split B by rows. Each GPU multiplies its slice of Y by its rows of B and gets a partial sum of Z. One all-reduce adds the partial sums.

Attention gets the same treatment. The Q, K and V projections are split by column, which in practice means by head: each GPU owns some attention heads and computes them entirely locally. The output projection is split by row, followed by one all-reduce.

flowchart LR
  X["Input X (copy on every GPU)"] --> A1["GPU 0: X·A1, GeLU"]
  X --> A2["GPU 1: X·A2, GeLU"]
  A1 --> B1["GPU 0: Y1·B1 (partial Z)"]
  A2 --> B2["GPU 1: Y2·B2 (partial Z)"]
  B1 --> AR["All-reduce: Z = sum of partials"]
  B2 --> AR
  AR --> NEXT["Next sub-layer (copy on every GPU)"]
  class X,NEXT memory
  class A1,A2,B1,B2 compute
  class AR queue

Result: two all-reduces per layer in the forward pass (one after attention, one after the MLP) and two in the backward pass. The paper implements the communication as two tiny autograd functions, f (identity forward, all-reduce backward) and g (all-reduce forward, identity backward).


4. How it works#

The details that made it practical:

  • Embeddings. The input embedding table is split along the vocabulary. The output layer’s logits would need an all-gather of batch × sequence × vocabulary numbers, which is huge, so the authors fused the split GEMM with the cross-entropy loss and only exchanged per-token losses.
  • Duplicated small work. Layer norm, dropout and residual adds are computed on every GPU rather than split, which is cheaper than communicating.
  • Padding the vocabulary. GPT-2’s 50,257-token vocabulary was padded to 51,200 (divisible by 128 × 8) so every GPU’s slice of the logit GEMM had an efficient shape.
  • Layer-norm placement in BERT. Scaling BERT past BERT-Large (336 M parameters) made accuracy get worse, not better, until they moved the layer normalization and the residual connection; with that change a 3.9 B BERT trained well.

For inference only the forward pass matters, so a TP-split layer costs two all-reduces of the hidden states per layer per step. During decode those messages are small (batch × hidden size), so the cost is mostly latency, not bandwidth (9.g). That is why tensor parallelism is used inside one NVLink domain and rarely across servers (14.c, 14.m).


5. Results#

From the abstract and sections 5–6. Hardware: up to 32 DGX-2H servers, 512 V100 SXM3 32 GB GPUs, 300 GB/s between GPUs inside a server (NVSwitch), 100 GB/s between servers (8 InfiniBand adapters).

MeasureValue
Single-GPU baseline (1.2 B model)39 TFLOP/s sustained, 30% of peak
8.3 B model, 8-way TP on 8 GPUs77% of linear scaling
8.3 B model, 8-way TP × 64-way data parallel on 512 GPUs15.1 PFLOP/s, 74% of linear in the text (76% in the abstract)
GPT-2-style 8.3 B, WikiText-103 perplexity10.8 (previous best 15.8)
GPT-2-style 8.3 B, LAMBADA accuracy66.5% (previous best 63.2%)
BERT-style 3.9 B, RACE accuracy90.9% (previous best 89.4%)

The paper quotes slightly different scaling efficiencies in the abstract (76%) and in section 5.1 (74% for the 512-GPU run, 77% for 8-way model parallel alone). Both say the same thing: roughly three-quarters of perfect scaling.


6. What held up and what did not#

Held up

  • Column-then-row splitting with one all-reduce per block. It is still the standard tensor parallelism in 2026, in training and in inference.
  • Splitting attention by head. Simple and communication-free inside attention. It is also why the number of KV heads limits how far you can usefully split a GQA model (30.d, 14.c).
  • TP inside the server, something else across servers. The paper already used data parallelism across nodes; later work added pipeline parallelism (Narayanan et al., 2021) and sequence parallelism (Korthikanti et al., 2022).

Did not, or changed

  • The all-reduce is not free at inference. On PCIe-only GPUs, or across nodes, it dominates decode time; 14.m covers when more GPUs make things slower.
  • Training-side, TP is now one dimension among many (data, pipeline, context, expert, plus sharded optimizers such as ZeRO/FSDP). The “8-way TP on one node” recipe is a component, not the plan.
  • Newer systems overlap or fuse the all-reduce with the GEMMs (async TP, fused all-reduce kernels; 25.i) instead of running it as a separate step. vLLM v0.32.0 removed its torch.compile AsyncTP pass, so where overlap happens is engine-specific (18.d).

7. Where it lives in 2026#

  • Taught in depth: tensor parallelism in 14.c, collectives in 9.f, the cost model in 9.g, vLLM’s implementation in 19.a.
  • In code: vLLM’s ColumnParallelLinear and RowParallelLinear layers, SGLang’s equivalents and TensorRT-LLM’s TP mapping all follow this paper’s split, and keep its names.
  • NVIDIA’s Megatron-Core library (the descendant of this repository) is still a major training framework for large models.
  • --tensor-parallel-size 8 on one 8-GPU HGX server is the default way to serve a model that does not fit on one GPU.

Checked on 13 November 2026.


8. Questions#

  1. Why split the first MLP matrix by columns rather than rows?
  2. How many all-reduces does one TP Transformer layer need in a forward pass? Where are they?
  3. Why does splitting attention by head need no communication inside attention?
  4. A model has 8 KV heads. What happens when you try TP = 16?
  5. Why is tensor parallelism usually kept inside one NVLink domain?
  6. During decode, is a TP all-reduce bandwidth-bound or latency-bound? Why?
  7. Why did the authors fuse the logit GEMM with the loss instead of all-gathering the logits?

9. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in