What Actually Happens Inside the Transformer MLP Layer?

What Actually Happens Inside the Transformer MLP Layer?

An explanation of what happens inside the MLP (feed-forward network) component of a Transformer, contrasting it with self-attention. Attention moves information between tokens while the MLP transforms it locally through up-projection, non-linear activation, and down-projection.

Walks through why non-linearity prevents stacked layers from collapsing into a single linear equation, explains modern gated MLPs like SwiGLU used in LLaMA and Mistral, and covers the key-value memory interpretation of MLPs from Geva et al.'s research.

Questions this post answers

What is the difference between what self-attention and the MLP layer do inside a transformer?

Self-attention gathers relevant context by moving information between tokens across a sequence, while the MLP (feed-forward network) transforms and enriches that information locally for each token. Attention determines what information to look at; the MLP applies learned non-linear transformations to process it, then adds the result to the residual stream. Explore more transformer architecture breakdowns on daily.dev when studying how attention and MLPs interact.

Why do modern LLMs like LLaMA and Mistral use SwiGLU instead of a simple ReLU MLP?

SwiGLU uses a dual-path gated design instead of a single-path activation, splitting the input into a content path (W_up) and a gate path (SiLU activation on W_gate), then multiplying them element-wise before the down-projection. This acts like a volume slider rather than a hard on/off switch, giving finer control over which features pass through compared to plain ReLU MLPs. Developers comparing activation functions for LLM architectures can track these details on daily.dev.

Why does a transformer need non-linear activation functions between layers instead of just stacking linear layers?

Without a non-linear activation, stacking any number of linear layers collapses mathematically into a single linear equation, making depth useless. For example, two linear layers h=10x+3 and y=8h+2 simplify to y=80x+26. Inserting ReLU between them (y=8*ReLU(10x+3)+2) breaks this collapse by zeroing out negative signals, letting the network selectively turn features on or off based on context. Anyone building intuition for neural network depth and activations can follow deep-dives like this on daily.dev.