↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Splitting one layer across devices

Tensor parallelism (TP) splits the weight matrices inside every layer across several devices, called ranks, so that each rank stores and multiplies only a slice of each matrix. It is the answer when a single layer's weights, or the time to run them, are too much for one device, and it is the first split Megatron-LM made for GPT-style models.

The page runs GPT-2 (124M) split four ways for one training step: forward, the next-token loss and backward on the opening clause of SQuAD's Apollo program article, 19 tokens. Each rank is a separate process on this Mac's CPU, and the four talk through torch.distributed's gloo backend. Every all-reduce on the page was really sent and logged; the times are those of the CPU run, and what carries over to GPUs is the pattern of messages, not the speed.

Columns, then rows

A linear layer can be split by the columns of : rank keeps and computes , its own columns of the output, with no communication as long as every rank already has . A second layer can then be split by the rows of , matching the columns each rank already holds:

Each rank's is a full-width partial sum, and one all-reduce adds the four so that every rank ends with . GPT-2's MLP is exactly this pair: c_fc (768 → 3,072) split by columns, 768 per rank, then GELU on each rank's own columns, then c_proj (3,072 → 768) split by rows. Splitting the first matrix by rows instead would leave each rank with a partial sum before the GELU, and , so it would need a second all-reduce. The Block n, split four ways panel draws both pairs as the Dense tab's MLP bands: the input band fans out into each rank's columns and each rank's full-width partial sum flows into the Σ.

Heads are independent

Attention splits the same way along its heads. GPT-2 has 12 heads of 64 dimensions, and nothing in one head's computation reads another's, so rank takes heads to from start to end. c_attn stores its 2,304 output columns as Q, K and V, 768 each, so a rank takes its three heads' 192 columns from each of the three: a 768 × 576 slice. It computes causal softmax attention for those heads alone, and its 192 rows of the output projection c_proj (192 × 768) turn their values into a partial sum, added by the second all-reduce of the block.

The head card (Head h of block n) is the Dense tab's: keys on top, queries in the middle, values below. Its weights are the ones the owning rank computed; they match the unsplit model's to float rounding. The The all-reduce adds four partial sums strip shows one token's four partial outputs for the selected block, first for attention and then for the MLP, and their sum plus the bias, which is the unsplit layer's output.

f and g: four all-reduces per block

Megatron writes the communication as two conjugate operators. g, after a row-parallel layer, is an all-reduce in the forward pass and the identity in the backward pass. f, before a column-parallel layer, is the identity forward; in the backward pass each rank's gradient with respect to the shared input comes only from its own columns, so the four are added by an all-reduce:

Each block therefore costs two all-reduces forward and two backward, 48 for GPT-2's 12 blocks in a training step, each of a (tokens × 768) tensor: 19 × 768 × 4 bytes = 58,368 bytes here. A ring all-reduce over ranks sends of the tensor from every rank. The The step on 4 ranks panel draws every recorded all-reduce between the ranks' compute; all four sit on the critical path, which is why TP is kept to the GPUs of one machine, joined by fast links such as NVLink.

What each rank holds

The split matrices hold almost all of a block's weights, so each rank keeps a quarter of them. The embeddings, the LayerNorms and the biases added after a sum are kept whole on every rank here: the token embedding alone is 147 MiB, so a rank holds 231.5 MiB of the model's 474.7 MiB. Megatron also splits the vocabulary of the embedding and the LM head across ranks, and sequence parallelism splits the LayerNorms' and dropout's activations, which TP leaves replicated, along the sequence.

The split model is the same model: the ranks' logits, loss and attention weights match the unsplit model's, and the gradient of every shard matches the matching slice of the unsplit gradient, to float rounding; the Same model, split card lists the errors.

Where it came from

Mesh-TensorFlow (Shazeer et al., 2018) described layers as tensors split along named mesh dimensions. Megatron-LM (Shoeybi et al., 2019) gave the column-then-row split of attention and the MLP with one all-reduce per half block and trained an 8.3-billion-parameter GPT-2 with it. Narayanan et al. (2021) combined it with pipeline and data parallelism (the PP and DDP tabs) to train models of a trillion parameters, keeping TP inside a server; Korthikanti et al. (2022) added sequence parallelism for the parts TP leaves whole. EP splits a mixture of experts by whole experts instead, and CP splits the sequence.

Papers

  1. Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism Mohammad Shoeybi et al. · arXiv 2019 The column- and row-parallel split of attention and the MLP, the f and g operators, and the all-reduce after each half block.
  2. Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM Deepak Narayanan et al. · SC 2021 Tensor, pipeline and data parallelism together, and why tensor parallelism stays within one server.
  3. Reducing Activation Recomputation in Large Transformer Models Vijay Korthikanti et al. · MLSys 2022 Sequence parallelism: the LayerNorm and dropout activations that tensor parallelism leaves on every rank, split along the sequence.
  4. Mesh-TensorFlow: Deep Learning for Supercomputers Noam Shazeer et al. · NeurIPS 2018 Splitting every tensor of a model along named dimensions of a mesh of devices, the general form of tensor parallelism.
  5. Language Models are Unsupervised Multitask Learners Alec Radford et al. · 2019 GPT-2, the model split on this page; a technical report, not on arXiv.

See also