SwiGLU is the feed-forward block that quietly replaced the classic two-matrix FFN in most modern language models — Llama, PaLM, Mistral, and their descendants. It swaps the plain activation(xW1)W2 sandwich for a gated computation: one projection is passed through a Swish nonlinearity and then multiplied, element by element, against a second, purely linear projection. That single change — a learned gate deciding how much of each hidden unit to let through — buys a consistent quality bump for no extra parameters, provided you shrink the hidden width by exactly one third. This piece works through the formula, why gating needs three weight matrices instead of two, the 2/3 rule that keeps the parameter budget flat, the Swish/SiLU activation underneath it, and a worked example with real Llama numbers — then draws the line back to plain GELU-FFN and the broader GLU family.
The FFN you already know: two matrices, one activation
Every transformer layer is an attention sublayer followed by a position-wise feed-forward network (FFN). The original Transformer, and models like BERT and GPT-2/3, use the same simple shape applied independently at every token position:
FFN(x) = activation(x · W1) · W2
x : [d] token vector (model dim d)
W1 : [d, d_ff] up-projection
W2 : [d_ff, d] down-projection
d_ff = 4d the usual expansion ratioThe vector is projected up into a wider hidden space (typically four times the model dimension), a nonlinearity is applied element-wise, and it is projected back down. The activation was ReLU originally and GELU in most later models. Two weight matrices, one activation, no gate. This block holds roughly two-thirds of a transformer’s parameters, so any change to its design — more expressive, or cheaper — pays off across the whole model. SwiGLU is exactly such a change.
The gating idea and the GLU family
A Gated Linear Unit (GLU), introduced by Dauphin et al. in 2016, replaces a single activated projection with a product of two projections, one of which acts as a multiplicative gate:
GLU(x) = (x · W) ⊙ σ(x · V)Here ⊙ is the element-wise (Hadamard) product and σ is the sigmoid. One branch, xW, carries the content; the other branch, σ(xV), produces a value between 0 and 1 for each hidden unit and gates the content — letting some units through fully, damping others toward zero. Crucially, the gate is data-dependent: it is computed from the same input, so the network learns to open and close channels per token. Swap the sigmoid for a different activation and you get the whole GLU family: ReGLU (ReLU gate), GEGLU (GELU gate), and SwiGLU (Swish gate). The sibling article on the GLU family (tm_glu_ffn) treats these variants side by side; here we focus on the Swish one that Llama and PaLM adopted.
Swish / SiLU: the activation underneath
The ‘Swi’ in SwiGLU is Swish, the activation that gates the content branch. Swish is defined as
Swish_β(x) = x · σ(βx) where σ(z) = 1 / (1 + e^(-z))When β = 1 this is called SiLU (Sigmoid Linear Unit), and that is the version Llama and PaLM use. Unlike ReLU, Swish is smooth and non-monotonic: it dips slightly below zero for small negative inputs before recovering, so it does not hard-clip negatives to exactly zero. For large positive x it behaves like the identity (σ(βx) → 1), and for large negative x it decays smoothly toward zero. That smoothness gives well-behaved gradients everywhere — no dead-ReLU regions where the gradient is flat zero — which helps optimization in deep stacks. In SwiGLU, Swish is applied only to the gate branch xW; the value branch xV stays linear.
The SwiGLU FFN: the full formula
Put the gate and the down-projection together and you get the SwiGLU feed-forward block used in production models:
FFN_SwiGLU(x) = ( Swish(x · W) ⊙ (x · V) ) · W2
W : [d, d_ff] gate projection (Swish-activated)
V : [d, d_ff] value projection (linear)
W2 : [d_ff, d] down-projection
biases: usually dropped in modern LLMsRead left to right: the input x is projected two different ways into the hidden space, xW and xV, both of shape [d_ff]. The first is squashed by Swish to become a per-unit gate; the second is left linear. Their element-wise product — the gated hidden state — is then projected back down to [d] by W2. The structural difference from the plain FFN is exactly the extra projection: where GELU-FFN had one up-matrix W1, SwiGLU has two, W and V. That third matrix is what makes gating possible — and what forces the parameter arithmetic in the next section.
Why three matrices — and the 2/3 rule
A plain FFN with expansion d_ff = 4d has two weight matrices, each of size d × d_ff, for a parameter count of
plain FFN: W1 + W2 = 2 · d · d_ff = 2 · d · (4d) = 8 d^2SwiGLU adds the value projection V, so it has three matrices of size d × d_ff: 3 · d · d_ff. If you kept the same d_ff = 4d, SwiGLU would cost 12 d^2 — 50% more parameters than the baseline — and any quality comparison would be unfair. The fix is the 2/3 rule: shrink the hidden width by a factor of two-thirds so the three matrices cost the same as the old two.
match params: 3 · d · d_ff_new = 2 · d · d_ff_old
d_ff_new = (2/3) · d_ff_old = (2/3)(4d) = (8/3) d ≈ 2.667 dSo the SwiGLU hidden dimension is 8/3 d instead of 4d. The gate buys expressiveness; the 2/3 rule pays for it by narrowing the hidden layer, keeping the total parameter and FLOP budget essentially flat.
A worked example: Llama-7B numbers
Take Llama-7B, with model dimension d = 4096. A classic 4× FFN would use d_ff = 4 · 4096 = 16384, at a per-layer cost of
plain FFN: 2 · 4096 · 16384 = 134,217,728 ≈ 134M paramsApplying the 2/3 rule: d_ff = (8/3) · 4096 = 10922.7, which Llama rounds up to a hardware-friendly multiple of 256, giving the actual value d_ff = 11008. The three-matrix SwiGLU cost is then
SwiGLU: 3 · 4096 · 11008 = 135,266,304 ≈ 135M paramsWithin a fraction of a percent of the 134M baseline — the rounding to 256 is the only reason it is not exact. So Llama gets the gating mechanism for effectively free: same parameter count, same compute, but a measurably better loss curve. This is why the seemingly odd hidden size 11008 appears in the Llama config; it is 8/3 × 4096 rounded, not an arbitrary choice.
Why gating improves quality
Why should multiplying two projections beat activating one? The honest answer from the literature (Shazeer’s 2020 GLU Variants Improve Transformer) is largely empirical: across language-modeling benchmarks, GEGLU and SwiGLU consistently edge out ReLU and GELU FFNs at matched parameters. But there is intuition. A plain FFN applies the same nonlinearity to every unit; the gate lets the network apply an input-dependent, per-unit multiplier on top of the content. That multiplicative interaction is more expressive than a fixed point-wise function — it can represent if this feature is present, amplify that one patterns that an additive activation cannot cheaply model. The gate can also suppress irrelevant hidden units to near-zero for a given token, acting like a soft, learned feature selector. Shazeer himself hedged, attributing the gain to ‘divine benevolence,’ but the result replicated widely enough that gated FFNs are now the default in frontier models.
SwiGLU vs plain GELU-FFN
Line the two up directly. The GELU-FFN (GELU(xW1)W2) has two matrices, one activation, hidden width 4d, and no gate: every unit is transformed by the same GELU curve. The SwiGLU-FFN has three matrices, a Swish-gated product, hidden width 8/3 d, and a data-dependent multiplicative gate. At matched parameters the two are the same size and roughly the same speed, so the comparison is clean: SwiGLU wins on quality, GELU-FFN wins on simplicity.
| GELU-FFN | SwiGLU-FFN | |
|---|---|---|
| Weight matrices | 2 (W1, W2) | 3 (W, V, W2) |
| Hidden width | 4d | (8/3)d ≈ 2.67d |
| Nonlinearity | GELU on xW1 | Swish gate ⊙ linear value |
| Gate | none | data-dependent, per-unit |
| Params (d=4096) | ~134M | ~135M |
| Used by | GPT, BERT, OPT | Llama, PaLM, Mistral |
The narrower hidden dimension is the subtle cost: SwiGLU trades some raw hidden width for the gating structure, and the empirical verdict is that the trade is worth it.
Practical and CPU-SLM implications
For anyone running or quantizing small models on CPU, a few consequences follow. First, the FFN now holds three matrices per layer instead of two, so weight-loading and memory-bandwidth accounting must count W, V, and W2 — though the narrower d_ff keeps the totals matched to a GELU baseline. Second, many inference engines fuse the gate and value projections into a single [d, 2·d_ff] matmul and split the result, improving cache and SIMD utilization on CPU; check whether your runtime expects the fused or unfused layout when converting weights. Third, because the hidden width is 8/3 d rounded to a multiple like 256, do not hard-code 4d anywhere in tooling; read intermediate_size from the config. These are small details, but getting the branch or the rounding wrong silently corrupts outputs.
Common pitfalls
The recurring mistakes are easy to name. Forgetting the 2/3 rule — wiring three matrices at d_ff = 4d — inflates the model by 50% and makes benchmark comparisons meaningless; always shrink to 8/3 d. Activating the wrong branch: Swish goes on xW, not on the value branch xV, and swapping them produces a different, worse function. Confusing Swish with SiLU: they are the same when β = 1, which is the LLM convention, but Swish generally has a learnable or fixed β. Mismatched fusion: exporting separate W and V tensors into an engine that expects a fused gate-up projection (or vice versa) scrambles the columns. Respect the formula, the 2/3 rule, and the branch order, and SwiGLU is a free quality upgrade over the classic FFN.
activation(xW1)W2 feed-forward with a gated one: (Swish(xW) ⊙ xV)W2, where a Swish-activated gate branch multiplies a linear value branch element by element. That gate is data-dependent and per-unit, which is more expressive than a fixed activation and reliably lowers loss. The price is a third weight matrix, paid for by the 2/3 rule — shrink the hidden width from 4d to 8/3 d so three matrices cost what two used to, keeping parameters and compute flat (Llama-7B: d_ff = 11008 ≈ 8/3 × 4096, still about 135M params per layer). Against a GELU-FFN it is the same size but measurably better; against the wider GLU family it is simply the Swish-gated member. Mind the branch order, the 2/3 rounding, and the fused-projection layout, and gating is a free upgrade.