The position-wise feed-forward sublayer
Compute the per-token MLP d → d_ff → d with a nonlinearity in the middle. Explain that it holds about 2/3 of a block's parameters and does no mixing across positions.
Builds on: Activation functions: GELU and SwiGLU, The transformer block and the running vector
What the animation says
The other half of the block
Every transformer block has two steps. The first, attention, mixes information across the positions in the sequence. The second step is a small neural network: a feed-forward layer, an MLP. It looks at one token's vector at a time. It never looks at the neighbors. And yet it holds about two thirds of the block's weights. So what does it compute, and why is it so big?
One function, mapped over rows
Picture the sequence as a table: one row per token, each row d numbers wide, already normalized. The feed-forward layer is a map over those rows. The same function, with the same weights, runs on every row. Each row expands to a wider hidden layer, usually four times d. A nonlinearity, like ReLU, works on each number separately. ReLU simply turns negatives into zero. Then it projects back down to d, and the result is added to that token's running vector. Now suppose we change only row one's input. Which output rows change?
One token, by hand
Only row one changes. No row ever reads another row, so all mixing between positions is attention's job. Now follow one token through a tiny version: width two, hidden width four, ReLU, no biases. The token is one, minus one. Multiplying by the first matrix gives one, minus one, zero, minus two. ReLU zeros the negatives, leaving one, zero, zero, zero. Only the first hidden unit is on. Which row of the second matrix becomes the output? Tap it.
Added in, then counted
Row one it is: one, two. Every other row of the second matrix got multiplied by zero. That output is added to the token's running vector. One, minus one, plus one, two, gives two, one. Now count the weights. The first matrix is d by four d. The second is four d by d. Take attention's four weight matrices as given: together they hold four d squared. So how does the MLP's count compare?
Two thirds of the block
Each matrix holds d times four d, which is four d squared. Two of them make eight d squared, twice attention. The whole block is twelve d squared, so the MLP's share is eight out of twelve: two thirds. Many models use a variant called SwiGLU, with three matrices and a hidden width of eight thirds d. That's the same budget, eight d squared. This code checks it with d of eight. Row two computed alone matches the full run, and bumping the first token leaves the other rows unchanged. The count prints five hundred twelve for the MLP against two hundred fifty six for attention. One MLP per token, no mixing, most of the weights.
One small function, mapped over every token
The formula, row by row
One token through a tiny MLP
Check: no mixing, and 2/3 of the weights
import numpy as np
rng = np.random.default_rng(0)
T, d = 5, 8; d_ff = 4 * d
W1 = rng.normal(size=(d, d_ff)); W2 = rng.normal(size=(d_ff, d))
def ffn(X): return np.maximum(X @ W1, 0) @ W2 # ReLU in the middle
X = rng.normal(size=(T, d))
Y = ffn(X) # [T, d]
print(np.allclose(Y[2], ffn(X[2:3])[0])) # row 2 alone: True
X2 = X.copy(); X2[0] += 100 # change token 1 only
print(np.allclose(ffn(X2)[1:], Y[1:])) # other rows same: True
print(W1.size + W2.size, 4 * d * d) # 512 (MLP) vs 256 (attn)
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