Feedforward Layers and Layer Normalization
The Feedforward Network
After attention gathers information across positions, a position-wise feedforward
network processes each token's vector independently. It is a two-layer MLP: expand
from d to 4d dimensions, apply a nonlinearity, then project back to d. Despite its simplicity,
the feedforward layer holds more parameters than the attention layer for most models
(8d² vs 4d² per block).
Division of labor. Each transformer block alternates between two categorically
different operations. Attention is the only place tokens communicate — it routes information
across positions, assembling context. The feedforward network then operates on each position
in isolation, with no awareness of neighboring tokens, transforming what attention assembled
into a richer local representation. This alternation — gather, then process; gather, then
process — repeats L times and is the core computational motif of the transformer.
Figure 5 — Feedforward Expansion and Compression
The input vector of dimension d is first expanded to 4d by the first linear layer, a
nonlinearity is applied element-wise, then it is compressed back to d. This expansion-compression pattern can act as a kind of memory: research suggests each hidden unit works like a key–value pair, with the key responding to particular input patterns and the value promoting likely next tokens (
Geva et al., 2021).
Feedforward networkFFN(x) = GELU(x Wup) Wdown
Wup ∈ ℝd×4d
Wdown ∈ ℝ4d×d
GPT-2-era models (GPT-2, GPT-3, BERT) use GELU (Gaussian Error Linear Unit) rather
than ReLU; most recent models use the gated SwiGLU variant described below.
GELU is smoother and approximates ReLU while keeping a nonzero gradient for almost all inputs
(unlike ReLU's flat zero for x < 0).
It is defined as x · Φ(x) where Φ is the standard normal CDF — in practice approximated
as 0.5x(1 + tanh(√(2/π)(x + 0.044715x³))).
For a detailed treatment of ReLU, GELU, and activation functions more broadly, see Lesson 1, tab 2.
Gated MLPs and Wgate
Many recent models — LLaMA, PaLM, Mistral — replace the two-matrix FFN with a
gated variant that adds a third weight matrix, Wgate. Instead of
applying the nonlinearity directly to x Wup, the hidden layer is element-wise
multiplied (⊙) by a learned gate:
SwiGLU feedforward (gated)FFNSwiGLU(x) = ( x Wup ⊙ SiLU(x Wgate) ) Wdown
Wup, Wgate ∈ ℝd×dff
Wdown ∈ ℝdff×d
dff ≈ (8/3)d
SiLU(z) = z · σ(z)
The SiLU gate selectively opens or closes each hidden dimension based on the
current input. Wgate learns which intermediate directions are worth passing through;
Wup learns what content to project into those directions. The result is that the
network has dynamic, input-dependent control over which "neurons" contribute — a richer
computational motif than a fixed nonlinearity applied uniformly. Because the gate adds a third
matrix, the hidden size is reduced from 4d to about (8/3)d (as in LLaMA) so the FFN keeps
roughly 8d² parameters. At that matched parameter count SwiGLU outperforms plain GELU (Shazeer, 2020), which is
why it is now the dominant FFN design in frontier models.
FFN as memory: Geva et al. (2021) found that the feedforward layers act like key-value memories. W
up columns are "keys": each fires on particular input patterns (shallow ones such as a shared last word in lower layers, more semantic ones in upper layers). W
down rows are "values": what is written back to the residual stream when a key fires, which raises the probability of tokens that tend to follow that pattern. (An illustrative example: a key that fires on "The Eiffel Tower is in" could write a direction that promotes "Paris".) Later work links factual recall to such mid-layer FFN memories (
Lesson 7, tab 7).
Layer Normalization
Before each sub-layer, a copy of the residual stream is normalized across its d
dimensions to have zero mean and unit variance, then rescaled by learned parameters γ and β.
This gives every attention and feedforward sublayer an input of consistent scale, however
large the stream has grown — useful when composing dozens of transformations (24 sublayers in
GPT-2 small, 96 in GPT-2 XL). Only the copy fed into the sublayer is normalized; the residual
stream itself is not. Many recent models (Llama, Mistral) use RMSNorm, a
simpler variant that skips the mean subtraction and the shift β.
Layer normalizationLayerNorm(x) = γ ⊙ (x − μ) / σ + β
μ = mean(x)
σ = std(x)
γ, β ∈ ℝd
Figure 6 — Layer Norm Step by Step
A 5-dimensional token vector walked through each arithmetic step. Note that γ and β
restore the network's ability to express any mean and scale — LayerNorm does not lock
representations to zero mean, it removes uncontrolled scale before
reintroducing learned scale. Layer norm operates independently at each
position, normalizing across the d feature dimensions (not across the sequence).
Pre-norm vs post-norm
The original "Attention Is All You Need" paper (Vaswani et al., 2017) placed layer norm after each sublayer
(post-norm). Modern models — GPT-2, GPT-3, LLaMA — place it before (pre-norm). Pre-norm
is easier to train at scale because it leaves a clean identity path: each sublayer's output is
simply added to the stream, nothing normalizes the stream itself, and gradients pass straight
back through the additions. Post-norm puts a layer norm on that path after every addition,
which makes gradients near the output large at the start of training and calls for a careful
learning-rate warm-up (Xiong et al., 2020).
The price of pre-norm is that the stream's norm grows with depth, which is one reason a final
layer norm is applied before the unembedding.
Neuroscience parallel: The FFN's expand–nonlinearity–compress structure
(d → 4d → d) resembles the local circuit motif of a
cortical column: inputs
arrive at a position, are fanned out across a larger population of neurons (layer 4 →
supragranular layers), a nonlinearity shapes how strongly each unit responds, and the
result is projected back to a lower-dimensional output. Crucially, this processing is
local — each token's FFN computation is independent of every other position,
just as a cortical column's recurrent processing is largely confined to its own
spatial neighborhood before long-range projections (the analog of attention) carry
information across areas. Unlike ReLU, GELU has no hard threshold: negative inputs give
small nonzero outputs, so the 4d units are not silenced, and their activity is graded and
fairly dense. The closer analog of
Olshausen & Field's sparse coding — cortex representing
stimuli with a small fraction of active neurons — is found not in single units but in
features: directions recovered by sparse autoencoders (
Lesson 7, tab 8).