Linear algebra for LLMsPart 6: The rest of the transformer
The MLP and SwiGLU
6 min
The classic MLP
Widen each token to , apply a nonlinearity entry by entry, and come back down to . GPT-2 small: . The nonlinearity is what stops the two matrices from collapsing into one (Part 3).
Write
Its parameters
Twice the attention’s . Together they make the classic per block that the counting deck uses.
SwiGLU: add a gate
Two up-projections instead of one. One goes through SiLU and then multiplies the other entry by entry (the of Part 2), so each hidden unit can switch the other on or off. Llama calls the three matrices gate_proj, up_proj and down_proj.
Write
Shapes through SwiGLU
Parameters, and the 8/3 rule
Three matrices instead of two, so to keep the same parameter count as a MLP, the hidden width drops to . LLaMA’s 7B models used for (, rounded up). Llama 3 8B goes wider: 14,336.
Write
✏️ Your turn
Size Llama 3 8B's MLP
How many parameters in one layer’s MLP?
Work it out on paper, then go to the next card to check.
Answer
About 176 million per layer. The attention in the same layer (with GQA) has about 42 million: the MLP holds most of the model.
In PyTorch
h = F.silu(x @ W_gate.T) * (x @ W_up.T) # (B, T, d_ff)
y = h @ W_down.T # (B, T, d)📌 Recap
The MLP and SwiGLU in one card
- Classic: , , params.
- SwiGLU: , params.
- keeps the count equal; Llama 3 8B uses 14,336.
- The MLP holds most of a transformer’s parameters.
Write