4.1.8 · D4Transformer Architecture

Exercises — Feed-forward network sublayers

1,964 words9 min readBack to topic

Before we start, a two-line refresher on the notation the parent note uses, so line one makes sense:


Level 1 — Recognition

L1.1 — Dimension of each matrix

The FFN uses two weight matrices and . Given and , state the shape (rows columns) of each.

Recall Solution

A weight matrix that turns a vector of length into a vector of length must be shaped (you multiply the row vector of length on the left, , and the number of columns becomes the new length).

  • : input hidden , so .
  • : hidden output , so .

L1.2 — Which step adds non-linearity?

In , exactly one operation makes the whole thing non-linear. Name it, and say what would happen to the network if you removed it.

Recall Solution

The activation (ReLU / GELU / SiLU) is the only non-linear part; matrix multiplies and bias additions are all linear (a straight-line map: doubling the input doubles the output). Remove and you get . Two matrices multiplied together are just one matrix , so the FFN collapses into a single linear layer — no curved decision boundaries at all.

L1.3 — Position-wise recall

Does the FFN at position 5 read data from position 6? ::: No — the FFN is position-wise; it applies the same weights to each token independently, mixing nothing across positions.


Level 2 — Application

L2.1 — Compute a single hidden unit with ReLU

Let and let the first column of be with bias . Compute .

Recall Solution

The dot product multiplies matching entries and sums — it measures "how much does point along this weight pattern?" Add bias: . Then .

L2.2 — A negative pre-activation

Same , but now the weight column is with bias . Compute the ReLU output.

Recall Solution

Dot product: . Add bias: . Then . This neuron is dead for this input — the pattern it looks for is absent.

L2.3 — Compare ReLU vs SiLU at one point

Compute and where . Which one still passes a (small) non-zero signal?

Figure — Feed-forward network sublayers
Recall Solution

exactly — a hard cutoff, gradient zero, no learning signal. For SiLU: , so SiLU is the one that still passes a small non-zero value, so gradients can flow — look at the orange curve in the figure dipping gently below zero instead of snapping flat.


Level 3 — Analysis

L3.1 — Parameter count of one FFN

For , , count every learnable parameter in the FFN (both weight matrices and both bias vectors).

Recall Solution

Weights: has and has entries. Biases: has , has . Total: parameters. Note the weights dominate; the rule matches the weight part exactly.

L3.2 — Why 4× and not equal width?

Explain, using the idea of ReLU sparsity, why the hidden layer is made wider rather than the same width as .

Recall Solution

If , the FFN is an information bottleneck: no more feature slots than inputs, so complex combinations ("positive sentiment and past tense") can't get their own dedicated neuron. ReLU zeroes out roughly half its neurons for any given input (sparsity ). So of neurons, only about are effectively active per token. Expanding 4× keeps the live capacity at about the input width — enough room to represent rich feature combinations, while the second matrix compresses them back to fit the residual stream.

L3.3 — Compute a full tiny FFN forward pass

Let , , activation ReLU. Given Compute .

Recall Solution

Step 1 — expand. : with row vector , Add : . Step 2 — activate. . Step 3 — project. with : Add : .


Level 4 — Synthesis

L4.1 — FFN inside the residual + LayerNorm block

Given a token's attention output and the FFN result from L3.3's style equals , the block computes . First form , then apply LayerNorm with , , . (LayerNorm subtracts the mean and divides by the standard deviation across the feature dimension.)

Recall Solution

Add (residual): . Mean: . Variance: , so std . Normalize: each entry is — a ! With this is undefined. Lesson: A degenerate input (all features equal) is exactly why LayerNorm always keeps a tiny . With, say, : denominator , numerator , so the normalized output is , then scaled/shifted by gives . See Layer Normalization and Residual Connections for why this add-then-normalize order stabilizes deep stacks.

L4.2 — Why FFN ≈ 1×1 convolution

Argue in words why applying the FFN to a whole sequence is equivalent to a 1×1 convolution over the sequence, and why this makes it fully parallel.

Recall Solution

A 1×1 convolution slides a filter that looks at one position at a time (kernel width 1) and applies the same weights everywhere. The FFN does exactly this: the same are applied to each with no neighbor mixing — . Because each position's computation is independent and uses shared weights, all positions can be computed simultaneously as a single big matrix multiply — perfect parallelism, unlike anything that needs sequential neighbor access. This connects to the Universal Approximation Theorem: each position gets its own two-layer approximator.


Level 5 — Mastery

L5.1 — GELU vs its tanh approximation

The exact GELU is where is the standard-normal CDF; the tanh approximation is Evaluate the tanh approximation at and report the value.

Figure — Feed-forward network sublayers
Recall Solution

Inner term: . Cubic bit: . Argument of tanh: . . Then . The exact GELU at is — the approximation matches to about decimals, as the near-overlapping curves in the figure show.

L5.2 — Fraction of block parameters in the FFN

For one encoder block with attention costing parameters and FFN costing , compute the fraction of the block's parameters (ignoring LayerNorm) that live in the FFN. Then confirm with .

Recall Solution

Fraction . The cancels, so the answer is width-independent: two-thirds of each block's parameters are in the FFN. This confirms the parent note's claim that the FFN is where most learning happens and attention is "the router." Numerically for : FFN , attention , fraction . ✓

L5.3 — Design a bottleneck-free FFN under a parameter budget

You are given a budget of exactly weight parameters for a single FFN (ignore biases) with . The two weight matrices together cost . Find the largest integer expansion factor you can afford, and state whether it reaches the usual .

Recall Solution

Weight cost . Budget: . The largest integer is , which exactly reaches the standard rule (, weight cost ). Any budget below would force a narrower, more bottlenecked hidden layer.