Half of every Transformer layer, and most of its weights: the attention mixes the tokens, the FFN works on each token on its own.
Linear, activation, linear
Every layer of the Transformer ends in a small network of its own, the position-wise feed-forward network, FFN for short: a linear layer that widens each token’s vector, an activation function, and a linear layer that narrows it back. The attention before it mixes the tokens; the FFN then works on each token on its own.
X has one row per token, dmodel numbers each. W1 is dff × dmodel and widens every row to dff numbers, φ is the activation applied to each of them, and W2, dmodel × dff, narrows them back. In the paper dmodel = 512, dff = 2,048 and φ is the ReLU; later models often use GELU or SwiGLU.
How it differs from an MLP
In structure the FFN is a multilayer perceptron with one hidden layer. Three things make it the Transformer’s own. It is applied to every position separately and identically: the same weights for every token, so a sentence of 20 tokens is 20 independent rows through one small network (within a layer; each layer has its own). Its shape is fixed by its place, dmodel → dff → dmodel, so that its output can be added back onto the residual stream it came from. And it never stands alone: the residual connection and a layer normalisation are always wrapped around it.
Why widen first? The hidden layer of 2,048 numbers gives the activation many directions to bend before the result is folded back into 512; together the two matrices hold about two thirds of a Transformer layer’s weights (2 × 512 × 2,048 against 4 × 512 × 512 in the attention).
Forward and backward, from the blocks
Nothing new is needed for the backward pass: the FFN is three blocks in a row, and its backward runs their backward passes in reverse order, each handing its input gradient to the block before it. That is the whole of backpropagation through a stack, and the reason every block keeps the same interface.
Try it
The first cell is the package’s nn/ffn.py, built on nn.linear and nn.core from the linear layer’s page. The second checks every gradient against finite differences, then trains the FFN on a small regression with plain gradient descent.
nn/ffn.py: ReLU, FeedForward
Check the gradients, then train
Test yourself
A sentence of 20 tokens passes through an FFN with dmodel = 512 and dff = 2,048. How many rows go through, and do they see each other?
Twenty rows, one per token, each through the same two matrices; they never see each other in the FFN. Mixing between tokens happens only in the attention layers.
Why must the FFN’s output have dmodel numbers again?
Because it is added back onto the residual stream, which has dmodel numbers per token; a sum needs two vectors of the same size.
Why does the FFN’s backward need no formula of its own?
It is three blocks in a row; its backward calls theirs in reverse order, each returning the gradient for its input, which is the gradient arriving at the block before.
Where it leads
Kept in this browser only, never sent.