Feed-forward
Position-wise W₂·GELU(W₁·x + b) + b.
Check yourself ↓ three questions and what to remember, at the end
The feed-forward network works on one position at a time. Below, one position's vector is expanded to twice its width, every hidden neuron is passed through GELU in turn (the dot on the curve), the result is projected back down, and the neurons that fired are picked out.
Loading the animation…
Concept
After attention has mixed information across positions, the feed-forward network (FFN) processes each position on its own. It's a small two-layer MLP, applied identically at every sequence position.
You can think of attention as the who-talks-to-whom sub-layer, and the
FFN as the what-each-position-thinks sub-layer. Most of the parameters
in a Transformer live here — typically about twice as many as in attention
(with d_ff = 4 · d_model the FFN holds 8·d² weights against attention's
4·d²; a gated SwiGLU FFN at 8/3 · d_model is also about 8·d²) — and most
of the model's "knowledge" sits in these matrices. In this toy
d_ff = 2 · d_model, so the two sub-layers are the same size.
The network has three steps:
- Up-projection to a wider dimension
d_ff(hered_ff = 2 · d_model). - GELU non-linearity, which lets the FFN model curved decision surfaces. Without it the whole model would collapse to a single linear layer.
- Down-projection back to
d_model, producing the contribution that gets added to the residual stream.
Click any position below to see its three vectors light up. The bars to the right of centre are positive values, to the left are negative. The pre-activation and after-GELU charts share width — notice how GELU softly zeroes most of the bars.
Maths
For one position with input :
We use the tanh approximation of GELU (the variant GPT-2 uses):
The same are applied at every sequence position independently — that's why we say "position-wise". Mathematically this is identical to a 1-D convolution with a kernel size of 1.
Code
// src/lib/transformer/ffn.ts (excerpt)
export function ffn(x: Matrix, w: FFNWeights, trace?: FFNTrace): Matrix {
const pre = addRowBias(matmul(x, w.W1), w.b1);
const act = geluMat(pre);
const out = addRowBias(matmul(act, w.W2), w.b2);
if (trace) {
trace.pre = pre;
trace.act = act;
trace.out = out;
}
return out;
}
The whole FFN is three named lines. The widget above hits
/api/compute/ffn, which calls this function with the same weights the
attention page used.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: They are the same size
The same: the FFN's two matrices hold 1,024 weights and attention's four projections 1,024. With the usual the FFN would hold twice attention's.
Pick one, then reveal.
Reveal the answer
Answer: -0.159 (counted right within ± 0.03)
About -0.159: GELU lets small negative inputs through, a little, and bends smoothly instead of switching at 0. The FFN's non-linearity is what stops the whole stack collapsing into one linear map.
Commit to a number, then reveal.
Reveal the answer
Answer: No: the same weights act on each position on its own
No. The FFN is position-wise: the same act on every position, each alone, like a 1-D convolution with a kernel of size 1. Mixing across positions is attention's job.
Pick one, then reveal.
Comments
Be the first to leave a comment on this section.
Sign in (top-right) to leave a comment.