Transformer Decoder Explained
← /learn

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:

  1. Up-projection to a wider dimension d_ff (here d_ff = 2 · d_model).
  2. GELU non-linearity, which lets the FFN model curved decision surfaces. Without it the whole model would collapse to a single linear layer.
  3. 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 x∈Rdmodelx \in \mathbb{R}^{d_{\text{model}}}:

pre=xW1+b1[dff]act=GELU⁡(pre)[dff]out=act W2+b2[dmodel]\begin{aligned} \text{pre} &= x W_1 + b_1 && [d_{\text{ff}}] \\ \text{act} &= \operatorname{GELU}(\text{pre}) && [d_{\text{ff}}] \\ \text{out} &= \text{act}\, W_2 + b_2 && [d_{\text{model}}] \end{aligned}

We use the tanh approximation of GELU (the variant GPT-2 uses):

GELU⁡(x)≈12 x(1+tanh⁡ ⁣(2/π (x+0.044715 x3)))\operatorname{GELU}(x) \approx \tfrac{1}{2}\, x \left(1 + \tanh\!\left(\sqrt{2/\pi}\,\left(x + 0.044715\, x^{3}\right)\right)\right)

The same W1,b1,W2,b2W_1, b_1, W_2, b_2 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.

Q1In this toy, where the hidden layer is twice the model width, which sub-layer has more weights in its matrices?
Reveal the answer

Answer: They are the same size

The same: the FFN's two matrices hold 2 d dff=2\,d\,d_{\text{ff}} = 1,024 weights and attention's four d×dd \times d projections 1,024. With the usual dff=4dd_{\text{ff}} = 4d the FFN would hold twice attention's.

Where the parameters live →

Pick one, then reveal.

Q2What is GELU(−1)? (ReLU would give 0.)
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.

GELU →

Commit to a number, then reveal.

Q3Does the feed-forward network at position 3 read position 2’s vector?
Reveal the answer

Answer: No: the same weights act on each position on its own

No. The FFN is position-wise: the same W1,b1,W2,b2W_1, b_1, W_2, b_2 act on every position, each alone, like a 1-D convolution with a kernel of size 1. Mixing across positions is attention's job.

Position-wise →

Pick one, then reveal.

Comments

Be the first to leave a comment on this section.

Sign in (top-right) to leave a comment.