What is the role of the feed-forward sublayer in a Transformer block?
Last updated
The feed-forward sublayer is a per-token MLP applied after attention. Attention mixes information across tokens, and the FFN then transforms each token's representation nonlinearly. It holds most of the block's parameters and much of the model's stored knowledge.
Structure:
FFN(x) = W_2 · act(W_1 x + b_1) + b_2, with an inner widthd_ffof about4·d_model. The original used ReLU; later models use GELU or SwiGLU.It is position-wise: the same weights apply to every token independently, with no cross-token interaction.
Division of labor: Attention routes information ("which tokens matter"). The FFN processes it ("what to compute from it").
Adds nonlinearity and capacity:
Attention output is a weighted average of value vectors, so it is linear in the values. Without FFNs, stacked layers would have limited expressive power.
The FFN accounts for roughly two-thirds of the parameters in a standard block.
Interpretation as key-value memory: Rows of
W_1act like keys that detect patterns, and columns ofW_2write the associated values into the residual stream (Geva et al., 2021). Factual knowledge is often localized here.Modern variants:
SwiGLU gating (LLaMA, Mistral).
Mixture-of-Experts replaces the single FFN with many expert FFNs and a router, which scales parameters without scaling per-token compute.