8.4 — SwiGLU and GeGLU: the modern FFN
Goal: Define gated linear units (GLU), specialise to SwiGLU and GeGLU, and derive why modern LLMs use them in place of the two-matrix GELU FFN of AIAYN. Count parameters and FLOPs, fix the d_ff = 8d/3 rule that preserves the per-layer parameter budget, and connect to the RMSNorm partner pre-norm.
1. Why this session exists
The original Transformer FFN of 6.8 is a two-matrix MLP: expand from to , apply ReLU (in AIAYN) or GELU (in GPT-3), contract back to . Two matrices, one elementwise non-linearity. Total parameters , no biases in modern LLMs.
Every frontier LLM since LLaMA-1 (2023) has replaced this with a gated linear unit (GLU) variant — three matrices, two of which feed an elementwise gating product. SwiGLU (used by LLaMA, Mistral, Qwen, Gemma) and GeGLU (used by PaLM, T5-v1.1) are the two dominant choices. The change is small in code (one extra matmul plus a Hadamard product) and consistent in empirical effect (– perplexity improvement at fixed parameter count, free with no hyperparameter tuning).
The reason to do a full session on what looks like a syntactic detail is twofold. First: the move from two matrices to three matrices changes the per-layer parameter count by if is kept fixed, so the standard adjustment is to preserve the parameter budget — a number that surprises every reader who first sees it in a code base and is worth deriving. Second: SwiGLU is now the only surviving non-trivial 2026 modification of the AIAYN block, so understanding it is part of understanding what current LLMs are.
2. Gated linear units, from first principles
A GLU in its general form (Dauphin et al., 2017[2], "Language modeling with gated convolutional networks", arXiv:1612.08083) is the elementwise product of a linear transformation and a gate — another linear transformation passed through a sigmoid:
The gate is a smooth elementwise mask in that multiplicatively gates each unit of the un-gated linear projection . Conceptually: the model can "open" or "close" individual hidden units conditional on the input, in contrast to a fixed pointwise non-linearity like ReLU or GELU which applies the same shape at every unit.
Shazeer (2020, "GLU variants improve Transformer", arXiv:2002.05202) swept replacements for the sigmoid in the GLU formula and showed empirically that two variants outperformed the others and outperformed the AIAYN ReLU FFN:
- GeGLU: replace with GELU. .
- SwiGLU: replace with Swish (Ramachandran, Zoph & Le 2017, equal to SiLU, ). .
The full FFN sub-layer with a GLU gate uses a third matrix to project back to :
with and . (Many code bases use the symbol naming for ; we will use the numbered convention because it matches AIAYN's and reduces the chance of confusion.)
For comparison, the AIAYN FFN with GELU is
two matrices, no Hadamard product. The structural new thing in SwiGLU is the elementwise multiplication of two parallel projections of .
2.1 Why a Hadamard product helps
Mechanistically, the gate supplies a multiplicative interaction term that the original two-matrix FFN cannot produce: a single matmul-plus-nonlinearity cannot express products of two input components, while a gated unit can. Whether the model exploits this in any specific interpretable way is debated, but the empirical observation is that gating helps. The Shazeer paper concluded only "we offer no explanation as to why these architectures seem to work; we attribute their success, as all else, to divine benevolence" — a famously honest sentence that has been quoted in every subsequent FFN paper.
A useful intuition: a SwiGLU unit at index outputs where is a learned, input-dependent scale. The model can pick a direction to project onto and separately pick a predicate that decides whether that projection contributes at all (Swish when ) or with what magnitude. This is a richer per-unit computation than ReLU's "fire or don't fire" on a single linear combination.
3. Parameter count, and the rule
The standard AIAYN FFN has
at . The SwiGLU FFN with the same has
a increase. If we want SwiGLU to be a drop-in replacement that keeps the per-layer parameter budget fixed, we must shrink to compensate. The condition is
So the rule that surprises every reader of the LLaMA source code:
with the convention that is then rounded to a multiple of (or , or the local TP-friendly granularity) for hardware efficiency. LLaMA-1-7B: (multiple of ). LLaMA-2-70B: (much larger, because in larger models the SwiGLU FFN was given extra width). The "" rule sets the lower bound; many models exceed it for the larger sizes.
3.1 Why "exactly " is not sacred
The point of the rule is parameter parity at the block level — so that an MLM, LLaMA-1, or GPT-3 of the same advertised uses the same compute. There is no theorem that says "wider is worse" or "narrower is worse." Empirically, frontier LLMs use for SwiGLU (so around ) and for the GELU FFN of GPT-3 / OPT. Either choice is sensible; what is required is consistency about what " " means.
3.2 FLOPs count
Per-position per-layer compute in the SwiGLU FFN ():
- : FLOPs.
- : FLOPs.
- Swish + Hadamard: , negligible.
- : FLOPs.
Total: FLOPs per position per layer (vs for the AIAYN FFN at — same, by construction). So SwiGLU is compute-equivalent to the GELU FFN at the parameter-matched width.
(Note: in FLOPs is the multiply-add count times 2; the per-position FFN FLOPs in the 6.12 accounting was also , so is unchanged.)
4. SwiGLU vs GeGLU vs GELU vs ReLU: the empirical picture
Shazeer (2020) Table 1, on T5 pretraining (small-scale, but cleanly controlled):
| Variant | Activation | Pretraining log-perplexity | |---|---|---:| | ReLU FFN (AIAYN) | | | | GELU FFN (GPT-3) | | | | Swish FFN | | | | GLU (sigmoid) | | | | GeGLU | GELU as gate | | | SwiGLU | Swish as gate | |
The takeaways:
- Replacing ReLU with GELU or Swish in the standard two-matrix FFN gives a tiny ( log-perplexity) improvement.
- Switching from a single non-linearity to a gated form gives a larger ( log-perplexity) improvement at fixed parameter count.
- The choice of activation inside the gate (sigmoid vs Swish vs GELU) is a smaller effect than the gating itself; SwiGLU and GeGLU are essentially tied.
The numbers look small in absolute terms but at scale (and at fixed compute) they translate to a noticeable downstream-benchmark improvement. By 2023 every Western LLM had switched to SwiGLU, and GeGLU is the same idea with a different activation (PaLM picked it; LLaMA picked Swish; the field is non-religious about the choice).
5. The RMSNorm partner
The 2026 frontier block of 8.1 §6 uses SwiGLU paired with RMSNorm pre-normalisation (3.8 §6). The pairing is not a coincidence:
- The SwiGLU input is normalised by RMSNorm: . This is the same pre-norm placement as in pre-LN Transformer (6.10 §3); the only change is the swap LayerNorm RMSNorm.
- The choice depends on the parameter-counting convention; it does not depend on which normalisation is upstream.
The pairing is a coincidence of historical timing: SwiGLU (Shazeer 2020)[1], RMSNorm (Zhang & Sennrich 2019), and RoPE (Su et al. 2021) were all on the table by mid-2021, and LLaMA-1 (Touvron et al. 2023)[4] combined all three plus AdamW into a single architectural reset. Subsequent LLMs inherited the bundle.
6. Reference implementation, the entire block
For concreteness, here is the LLaMA-style block, with SwiGLU + RMSNorm + RoPE-aware MHA:
def block(x, params):
# x: (T, d)
# Pre-norm attention
h = rms_norm(x, params.attn_gamma) # (T, d)
h = mha_with_rope_and_gqa(h, params.attn) # (T, d)
x = x + h # residual
# Pre-norm FFN: SwiGLU
h = rms_norm(x, params.ffn_gamma) # (T, d)
gate = h @ params.W2 # (T, d_ff)
up = h @ params.W1 # (T, d_ff)
h = (silu(gate) * up) @ params.W3 # (T, d)
x = x + h # residual
return x
Where silu(z) = z * sigmoid(z) is Swish. The three FFN matmuls — W1, W2, W3 — are the only thing in this block that changed from the GPT-3-era code (where you would see two matmuls with gelu between them and no Hadamard product). Everything else is identical to a vanilla pre-LN Transformer with RMSNorm instead of LayerNorm and RoPE-modified Q, K.
In production code, W1 and W2 are often concatenated into a single matrix W_up_gate and computed in one matmul, then split — this saves one kernel launch and improves GPU utilisation. The mathematical content is unchanged.
7. What we keep going forward
-
SwiGLU is a 3-matrix gated FFN with a Hadamard product between an "up" projection and a Swish-gated "gate" projection, then a "down" projection. GeGLU is the same with GELU as the gate non-linearity. SwiGLU and GeGLU are empirically tied; the choice is cosmetic.
-
The rule preserves the AIAYN per-layer parameter count of — divide by 2 (because 3 matrices instead of 2) and multiply by the ratio to get . Frontier models round up to a hardware-friendly multiple.
-
It pairs with RMSNorm + RoPE in the 2026 frontier block. The bundle "RMSNorm + RoPE + SwiGLU + GQA" is the LLaMA-1 recipe and remains the default for dense decoder-only models.
The next session, MLA (§8.5), returns to the attention side with the most aggressive KV-cache compression in 2026 frontier models.
(a) Write the SwiGLU FFN with matrices explicit. Identify which is the "gate" projection. Now write GeGLU and the (non-gated) GELU FFN of AIAYN for comparison. How many matrices does each have?
(b) Derive the rule from "preserve FFN parameters." Check it for LLaMA-1-7B (); compare with the actual reported . Why the rounding?
(c) A SwiGLU unit at hidden index outputs . Write the analogous formula for a GELU FFN unit. Identify the one extra capacity SwiGLU has that GELU does not. (Hint: how does the output depend on a product of two linear combinations of ?)
(d) Count the FLOPs per position per layer of a SwiGLU FFN with . Show that it equals the FLOPs of a GELU FFN with . Conclude that is unchanged.
(e) State which of the following are true in a 2026 frontier LLaMA-family block: (i) bias terms are absent in linear projections, (ii) bias term is present in RMSNorm, (iii) RMSNorm is pre-, not post-, FFN. Justify each.
(f) Read Shazeer (2020) Table 1 (in your head, from §4 above). Which row has the lowest perplexity? Was the same paper able to explain why?
References
- [1]
Shazeer, N. (2020). "GLU variants improve Transformer". arXiv:2002.05202 — SwiGLU / GeGLU; modern FFN variant (6.8 Aside; full treatment in Cap 8.4).
↩ - [2]
Dauphin, Y. N., Fan, A., Auli, M., & Grangier, D. (2017). "Language modeling with gated convolutional networks". arXiv:1612.08083 — Gated linear units (GLU); ancestor of SwiGLU (8.4 §2).
↩ - [3]
Chowdhery, A., Narang, S., Devlin, J., et al. (2022). "PaLM: Scaling language modeling with pathways". arXiv:2204.02311 — Adopted GeGLU and dropped FFN biases; reference for the "no biases" convention (8.4 §5 Aside).
↩ - [4]
Touvron, H., Lavril, T., Izacard, G., et al. (2023). "LLaMA: Open and efficient foundation language models". arXiv:2302.13971 — Architectural bundle: RMSNorm + RoPE + SwiGLU; defaults for (8.4 §3, 8.4 §5).
↩