All courses

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

▶ Watch this lesson Start the whole course

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.

What does the other half of each block do?

In every block, the attention step mixes information *across* positions. The other step is a small MLP, and it holds most of the block's weights. It sees one token's vector at a time. So what exactly does it compute? Why does it never look at neighboring tokens? And why does it end up with about two thirds of the block's parameters?

One small function, mapped over every token

Think of a `map` over the rows of the sequence. Every position's vector goes through the **same** function with the **same** weights. 1. Expand the $d$-wide vector to a wider hidden layer, usually $d_{ff}=4d$. 2. Apply a nonlinearity elementwise. 3. Project back down to $d$. The result is added into that token's running vector. No row ever reads another row, so all mixing between positions is attention's job.

The formula, row by row

$$\text{FFN}(X)=\phi(XW_1+b_1)\,W_2+b_2$$ - $X\in\mathbb{R}^{T\times d}$: one row per token. Each row is that token's running vector after the block's normalization step, which rescales it to a standard size. - $W_1\in\mathbb{R}^{d\times d_{ff}}$, $W_2\in\mathbb{R}^{d_{ff}\times d}$: weight matrices. - $b_1, b_2$: biases, added to every row. - $\phi$: ReLU or GELU, applied elementwise. Row $t$ of the output depends only on row $t$ of $X$. The block then adds the output into the running vectors: $r\leftarrow r+\text{FFN}(X)$.

One token through a tiny MLP

Take $d=2$, $d_{ff}=4$, ReLU, no biases, and token vector $x=[1,-1]$. For simplicity, skip normalization, so $x$ is also the running vector. $W_1=\begin{bmatrix}1&0&1&-1\\0&1&1&1\end{bmatrix}$, so $xW_1=[1,-1,0,-2]$. ReLU gives $[1,0,0,0]$. $W_2=\begin{bmatrix}1&2\\3&0\\0&1\\1&1\end{bmatrix}$, so the output is row 1 of $W_2$: $[1,2]$. Adding it to the running vector: $[1,-1]+[1,2]=[2,1]$.

Check: no mixing, and 2/3 of the weights

Take the attention step's four weight matrices as given for now: together they hold $4d^2$ parameters. The MLP's $W_1$ and $W_2$ hold $2\cdot d\cdot 4d=8d^2$. That makes $12d^2$ per block, and the MLP's share is $8/12=2/3$. SwiGLU keeps the same budget: $3\cdot d\cdot\tfrac{8d}{3}=8d^2$. The code below checks, for a ReLU MLP, that rows never mix and that the MLP has twice attention's $4d^2$ parameters.
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

The feed-forward sublayer applies one MLP, $d\to d_{ff}\to d$ with a nonlinearity in the middle, to each token's vector independently. Its output is added to the running vector. With $d_{ff}=4d$ (or $8d/3$ for SwiGLU) it has about $8d^2$ parameters against attention's $4d^2$, about 2/3 of the block. It does no mixing across positions.

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.