All courses

Tensor parallelism

Split the FFN and attention matmuls column-wise then row-wise across GPUs (Megatron style), so each sublayer needs only one all-reduce in the forward pass (two per block). Explain why this frequent communication needs fast links within a node.

Builds on: Collective ops: all-reduce, all-gather, reduce-scatter, The pre-norm transformer block

▶ Watch this lesson Start the whole course

What the animation says

Split one layer

One feed-forward matrix in a big model can be eight thousand by nearly twenty-nine thousand. On one GPU that's too slow, and the weights may not fit. So give each GPU a slice of every weight matrix, and let it compute part of each layer. The catch: GPUs must swap results mid-layer, and talking costs time. GPU one holds the left half of the first matrix's columns. Can it apply the activation without talking?

Columns first

Yes. Each column of the first matrix builds one hidden unit from the whole input. So with column blocks, GPU one owns hidden units one to four, complete. The activation acts on each unit alone, so no talking is needed yet. Now the second matrix. Its rows are where hidden units get multiplied in. Tap the slice of the second matrix that GPU one should hold.

Rows second, one all-reduce

Rows one to four. GPU one multiplies its four hidden units by its four rows. The result has the full output size, but it's only a partial sum. GPU two makes the other partial sum. One all-reduce adds them, and now every GPU holds the full output. Each GPU then does the norm and residual add itself, redundantly. Attention splits the same way: whole heads per GPU, plus the matching rows of the output projection. What if we split the first matrix by rows instead?

Why not rows first

Splitting the first matrix by rows forces the input to be split by columns. Then each GPU gets a partial sum before the activation: say three here, minus five there. ReLU of three plus ReLU of minus five is three. ReLU of the true sum, minus two, is zero. The activation of a sum isn't the sum of activations. That costs an extra all-reduce. Columns then rows: one all-reduce per sublayer, two per block. Backward mirrors this. With more GPUs, does each one send less per all-reduce?

Keep it inside one node

No. In a ring all-reduce, each GPU sends nearly twice the tensor, however many GPUs. One activation: four thousand tokens times eight thousand numbers, two bytes each. About sixty-seven megabytes. With eight GPUs: about a hundred seventeen megabytes sent per GPU, per all-reduce. Eighty blocks make a hundred sixty all-reduces: about nineteen gigabytes per forward pass. Over NVLink inside a node, that's forty-two milliseconds. Across nodes, about three hundred seventy-six. Compute is about a tenth of a second, and every matmul waits. So it stays inside one node.

Splitting one layer across GPUs

A big model's single FFN matrix can be $8192\times 28672$. Doing every block's matmuls on one GPU is too slow, and the weights may not fit. Could each GPU of a group hold a slice of every weight matrix and compute part of each layer? The catch is that GPUs must swap results mid-layer. **What is the split that needs the least talking, and why must that talking run over very fast links?**

Columns first, rows second

FFN: $\phi(XW_1)W_2$, where $\phi$ is the activation (e.g. GELU). - Give each GPU a block of **columns** of $W_1$. It computes its own slice of the hidden units. $\phi$ acts elementwise, so no talking is needed yet. - Give it the matching **rows** of $W_2$. Its slice times those rows is a full-size $[B,T,d]$ **partial sum**. - One all-reduce adds the partial sums. Now every GPU holds the full output, and each does the norm and residual add redundantly. Attention works the same way, with whole heads per GPU.

The Megatron split, precisely

With $n$ GPUs, GPU $i$ holds $W_1^{(i)}\in\mathbb R^{d\times d_{ff}/n}$ (columns) and $W_2^{(i)}\in\mathbb R^{d_{ff}/n\times d}$ (rows): $$Y=\sum_{i=1}^{n}\phi(XW_1^{(i)})\,W_2^{(i)}$$ Attention: GPU $i$ owns $h/n$ heads, meaning their $W_Q,W_K,W_V$, and the rows of $W_O$ for those heads. It computes $\mathrm{concat}(AV)\,W_O^{(i)}$, and these terms are summed. That gives one all-reduce per sublayer, so two per block in the forward pass. The backward pass mirrors this, with one all-reduce of the input gradient per sublayer.

Check: partial sums equal the full FFN

The column-then-row split matches the full FFN exactly. Splitting $W_1$ by rows instead forces $X$ to be split by columns, and it fails: $\phi$ of a sum is not the sum of $\phi$'s. That order would need an all-reduce *before* the nonlinearity, which adds a second all-reduce.
import numpy as np
rng = np.random.default_rng(0)
T, d, dff, n = 3, 4, 8, 2
X = rng.normal(size=(T, d)); W1 = rng.normal(size=(d, dff)); W2 = rng.normal(size=(dff, d))
relu = lambda a: np.maximum(a, 0)
full = relu(X @ W1) @ W2
parts = [relu(X @ a) @ b for a, b in zip(np.split(W1, n, 1), np.split(W2, n, 0))]
print(np.allclose(sum(parts), full))   # True: the sum is the all-reduce
Xs, W1r = np.split(X, n, 1), np.split(W1, n, 0)
print(np.allclose(sum(relu(x @ w) for x, w in zip(Xs, W1r)), relu(X @ W1)))  # False

Why it must stay inside one node

Take $d=8192$, $B\cdot T=4096$ tokens, bf16. One activation is $4096\cdot8192\cdot2\approx67$ MB. A ring all-reduce makes each GPU send $2\tfrac{n-1}{n}$ times the tensor. With $n=8$ that is $2\cdot\tfrac78\cdot67\approx117$ MB per GPU. With $L=80$ blocks there are 160 all-reduces, about $18.8$ GB per forward pass. - At ~450 GB/s (NVLink) that takes 42 ms. - If the same traffic crossed ~50 GB/s links between nodes, it would take 376 ms. For comparison, this model's forward matmuls are roughly 0.1 s of compute per GPU. And the next matmul waits on every all-reduce.

Remember

- Split the first matmul of each sublayer by columns and the second by rows. - Each GPU then computes independently and produces a full-size partial sum. One all-reduce per sublayer (two per block) combines them. - Compute per GPU shrinks by $n$, but the bytes sent per all-reduce do not. - These waits sit on every layer's path, so tensor parallelism needs the fast links inside a node.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.