Linear algebra for LLMsPart 6: The rest of the transformer

The MLP and SwiGLU

6 min

The classic MLP

MLP⁡(x)=GELU⁡(xWup) Wdown\displaystyle \operatorname{MLP}(x) = \operatorname{GELU}(xW_{\text{up}})\,W_{\text{down}}
→ →

Widen each token to dff=4dd_{\text{ff}} = 4d, apply a nonlinearity entry by entry, and come back down to dd. GPT-2 small: 768→3072→768768 \to 3072 \to 768. The nonlinearity is what stops the two matrices from collapsing into one (Part 3).

Write

MLP⁡(x)=act⁡(xWup)Wdown\operatorname{MLP}(x) = \operatorname{act}(xW_{\text{up}})W_{\text{down}}, d→dff→dd \to d_{\text{ff}} \to d.

Its parameters

2×d×dff=2×d×4d=8d2\displaystyle 2 \times d \times d_{\text{ff}} = 2 \times d \times 4d = 8d^2

Twice the attention’s 4d24d^2. Together they make the classic 12d212d^2 per block that the counting deck uses.

SwiGLU: add a gate

SwiGLU⁡(x)=(SiLU⁡(xWgate)⊙xWup) Wdown\displaystyle \operatorname{SwiGLU}(x) = \big(\operatorname{SiLU}(xW_{\text{gate}}) \odot xW_{\text{up}}\big)\,W_{\text{down}}
SiLU⁡(z)=z σ(z)\displaystyle \operatorname{SiLU}(z) = z\,\sigma(z)

Two up-projections instead of one. One goes through SiLU and then multiplies the other entry by entry (the ⊙\odot 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

(SiLU⁡(xWg)⊙xWu) Wd(\operatorname{SiLU}(xW_g) \odot xW_u)\,W_d: three matrices, one elementwise gate.

Shapes through SwiGLU

→ gate, through SiLU → up⊙ → down

Parameters, and the 8/3 rule

3×d×dff\displaystyle 3 \times d \times d_{\text{ff}}
3 d dff=8d2  ⇒  dff=83d\displaystyle 3\,d\,d_{\text{ff}} = 8d^2 \;\Rightarrow\; d_{\text{ff}} = \tfrac{8}{3}d

Three matrices instead of two, so to keep the same parameter count as a 4d4d MLP, the hidden width drops to 83d\tfrac{8}{3}d. LLaMA’s 7B models used dff=11008d_{\text{ff}} = 11008 for d=4096d = 4096 (83⋅4096≈10923\tfrac{8}{3} \cdot 4096 \approx 10923, rounded up). Llama 3 8B goes wider: 14,336.

Write

SwiGLU params: 3 d dff3\,d\,d_{\text{ff}}. Same as a 4d4d MLP when dff=83dd_{\text{ff}} = \tfrac83 d.

✏️ Your turn

Size Llama 3 8B's MLP

d=4096,dff=14336\displaystyle d = 4096, \quad d_{\text{ff}} = 14336

How many parameters in one layer’s MLP?

Work it out on paper, then go to the next card to check.

Answer

3×4096×14336=176,160,768\displaystyle 3 \times 4096 \times 14336 = 176{,}160{,}768

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: act⁡(xWup)Wdown\operatorname{act}(xW_{\text{up}})W_{\text{down}}, dff=4dd_{\text{ff}} = 4d, 8d28d^2 params.
  • SwiGLU: (SiLU⁡(xWg)⊙xWu)Wd(\operatorname{SiLU}(xW_g) \odot xW_u)W_d, 3 d dff3\,d\,d_{\text{ff}} params.
  • dff=83dd_{\text{ff}} = \tfrac83 d keeps the count equal; Llama 3 8B uses 14,336.
  • The MLP holds most of a transformer’s parameters.

Write

The four lines above.