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
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.
Columns first, rows second
The Megatron split, precisely
Check: partial sums equal the full FFN
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
Remember
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.
Bytewise
All courses