Highlights

Contents

Part III
03

SwiGLU: A Gated MLP

An MLP can learn content and its gate separately. SwiGLU multiplies expanded content by a smooth learned factor. Follow that interaction and why matched-budget comparisons must account for its extra projection.

YOU ARE HEREMODERN DECODER BLOCKATTENTION HALFRMSNormq, k, v projRoPE (q, k)GQA: 8 q, 2 kvSDPAFlash kernelo proj+MLP HALFRMSNormgate, up projSiLU(g) ⊙ udown proj+residual stream: x + Attn(Norm(x)), then h + MLP(Norm(h))what changed since GPT-2: positions, norm, MLP, K/V sharing, the attention kernel, and no biases

#Section 12Two MLPs

Here are the two recipes side by side. The vanilla MLP of Finch-19 and GPT-2 has two matrices. SwiGLU, used by Finch-24, Llama, Mistral, Qwen and most current open models, has three:

vanilla: Wdown GELU(Wup x)SwiGLU: Wdown(SiLU(Wgate x)⊙Wup x)\text{vanilla: } W_{\text{down}}\, \text{GELU}(W_{\text{up}}\, x) \qquad \text{SwiGLU: } W_{\text{down}} \big( \text{SiLU}(W_{\text{gate}}\, x) \odot W_{\text{up}}\, x \big)

Read the SwiGLU version from the inside out. The token's vector xx is projected twice, by two different matrices, into two vectors of the same hidden width hh: the gate WgatexW_{\text{gate}} x and the up projection WupxW_{\text{up}} x. The gate passes through SiLU, a smooth switch function. Then the switched gate and the up vector are multiplied together entry by entry, which is what ⊙\odot means. Finally the down projection brings the result back to the model width. Modern versions drop all biases.

VANILLA MLP (FINCH-19) VERSUS SWIGLU MLP (FINCH-24), ONE TOKENvanilla: 2 matricesx512Wup512 → 2,048GELUWdown→ 512SwiGLU: 3 matricesx512WgateWup→ 1,536→ 1,536SiLU⊙WdW_down (W_d): 1,536 → 5122 × 512 × 2,048 = 2,097,1523 × 512 × 1,536 = 2,359,296
Figure 2.16The vanilla MLP and the SwiGLU MLP for one Finch token. SwiGLU splits the widening into a gate branch and a content branch and multiplies them; its hidden width is smaller to keep the parameter count comparable.

The idea of multiplying one learned projection by a squashed copy of another is called a gated linear unit, GLU, introduced by Yann Dauphin and colleagues in 2016 for language modelling with convolutional networks. SwiGLU is the member of that family that uses SiLU as its switch, named in Noam Shazeer's 2020 paper GLU Variants Improve Transformer.

#Section 13ReLU, GELU and SiLU

An activation function is the "bend" applied to each number in the MLP's hidden layer. Three are worth knowing.

ReLU, the rectified linear unit, is the simplest: negative inputs become zero and positive inputs pass unchanged, max⁡(0,x)\max(0, x), with a sharp corner at zero. GELU, from Chapter 1, multiplies xx by Φ(x)\Phi(x), the probability that a standard bell-curve variable is below xx; it behaves like a smooth ReLU and dips slightly below zero, to about −0.17-0.17, for small negative inputs. SiLU, the sigmoid linear unit, also called Swish, multiplies xx by the sigmoid σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}), the S-shaped curve that runs from 0 to 1:

SiLU(x)=x⋅σ(x)=x1+e−x\text{SiLU}(x) = x \cdot \sigma(x) = \frac{x}{1 + e^{-x}}

Some values: SiLU(1)=0.731\text{SiLU}(1) = 0.731, SiLU(−1)=−0.269\text{SiLU}(-1) = -0.269, and its lowest point is −0.278-0.278 at x=−1.28x = -1.28. For large positive xx, σ(x)≈1\sigma(x) \approx 1 and SiLU is almost xx; for large negative xx, σ(x)≈0\sigma(x) \approx 0 and SiLU is almost 0. So it is a soft switch: "on" for positive inputs, "off" for negative ones, with a smooth transition and no corner. Smooth slopes everywhere make training a little better behaved, because the gradient never jumps.

THREE ACTIVATIONS: ReLU, GELU, SiLU (SWISH)xSiLU min −0.278 at x = −1.28-4-202ReLU(x) = max(0, x)GELU(x) = x · Φ(x)SiLU(x) = x · σ(x)SiLU(1) = 0.731SiLU(−1) = −0.269GELU(−1) = −0.159
Figure 2.17ReLU, GELU and SiLU. All three pass large positive inputs nearly unchanged and suppress negative ones; GELU and SiLU do it smoothly and dip slightly below zero.

The name Swish comes from a 2017 Google Brain paper by Prajit Ramachandran and colleagues, who found it with an automated search over candidate activation functions; it had been proposed earlier under the name SiLU. The Swish family has a parameter β\beta, x⋅σ(βx)x \cdot \sigma(\beta x); SiLU is β=1\beta = 1.

#Section 14The Gate in Action

Follow one token through the gate and content branches in the figure.

Apply SiLU to the gate, multiply it by the up-projected content entry by entry, then project the product back to model width.

ONE TOKEN THROUGH THE GATE: 4 HIDDEN UNITS (TOY NUMBERS)gate g = W_gate x2.000-3.0000.5001.000SiLU(g)1.762-0.1420.3110.731up u = W_up x1.5002.000-1.0000.800h = SiLU(g) ⊙ u2.642-0.285-0.3110.585unit 1: the gate is shut (−3 → −0.142), so a big up value of 2.0 barely gets throughthen W_down: 4 → d
Figure 2.18One token through the gate. Where the gate is strongly negative (unit 1), SiLU nearly closes it and the large up value of 2.0 barely gets through.

A strongly negative gate suppresses even a large content feature. An open gate lets that content contribute. The model learns the gate and content independently.

The gate surface

Draw the output SiLU(g)×u\text{SiLU}(g) \times u for every combination of gate value gg and up value uu from −3-3 to 3. On the left half, where the gate is negative, everything is close to zero: the gate is closed. On the right half the gate is open, and the output follows uu: positive above the middle, negative below.

THE GATE SURFACE: SiLU(g) × u FOR g AND u FROM −3 TO 3gate g →up u3−3−33left half: gate closed,output near zero whatever u isright half: gate open,output follows u (orange up, blue down)one number decides whether,the other decides what
Figure 2.19The output of one gated unit over a grid of gate and up values. A closed gate (left) gives near zero; an open gate (right) passes the up value through.

A plain activation bends one number. Here two learned numbers work together, one controlling flow and one carrying content, so a single hidden unit can represent "pass this feature only when that condition holds". That multiplicative interaction is the best available explanation for why gated MLPs work better, although, as the next section says, the original paper did not claim to know.

#Section 15Sizing the Hidden Layer

The extra projection raises cost at a fixed hidden width. A matched-budget comparison therefore narrows the hidden representation to compensate.

Implementations round the hidden width for efficient matrix kernels and may apply another multiplier. Read intermediate_size from the actual config rather than deriving a universal width rule.

SIZING THE HIDDEN LAYER: KEEP THE PARAMETER BILL ROUGHLY EQUALFinch-24 (d = 512)4d = 2,048× 2/3 → 1,365round up to 256 → 1,536Llama 2 7B (d = 4,096)4d = 16,384× 2/3 → 10,922round up to 256 → 11,008Llama 3 8B (d = 4,096)× 2/3 → 10,922× 1.3 → 14,198round up to 1,024 → 14,336vanilla 4d, 2 matrices2,097,152SwiGLU exact 8d/3 (1,365)2,096,640SwiGLU rounded (1,536)2,359,296FINCH MLP PARAMETERS PER BLOCK
Figure 2.20Choosing the hidden width. Two thirds of 4d keeps the parameter bill equal to a vanilla MLP; rounding up for GPU efficiency adds a little. Finch-24 lands at 1,536.

#Section 16The Family, the Paper and the Code

The GLU family

SwiGLU is one member of a family that all compute (switch(xW)⊙xV) W2(\text{switch}(xW) \odot xV)\,W_2 and differ only in the switch applied to the gate. GLU uses the sigmoid itself, a switch between 0 and 1. Bilinear uses no switch at all, just the product of two projections. ReGLU uses ReLU. GEGLU uses GELU. SwiGLU uses SiLU.

THE GLU FAMILY: SAME SHAPE, DIFFERENT SWITCH ON THE GATEGLUBilinearReGLUGEGLUSwiGLUeach computes (switch(x W) ⊙ x V) W₂; only the switch differs
Figure 2.21The gated linear unit family. All share the same shape and multiply a gated projection by an ungated one; only the switch on the gate differs.

What the paper found

Shazeer's 2020 paper trained the same Transformer with each MLP variant on the same data and compared their log-perplexity, lower being better. After 65,536 training steps the plain MLPs scored worst: ReLU 1.997, Swish 1.994, GELU 1.983. Every gated variant beat them: GLU 1.982, Bilinear 1.960, ReGLU 1.953, SwiGLU 1.944 and GEGLU 1.942. The differences look small, but in language modelling a few hundredths of log-perplexity is the kind of gain people spend months chasing, and it came at no extra parameter cost.

SHAZEER (2020): LOG-PERPLEXITY AFTER 65,536 STEPS, LOWER IS BETTER1.997ReLU1.983GELU1.994Swish1.982GLU1.960Bilinear1.953ReGLU1.942GEGLU1.944SwiGLUaxis starts at 1.93 to make small gaps visibleplain MLPs on the left,gated variants on the right
Figure 2.22Results from Shazeer (2020). Gated variants beat plain MLPs, with GEGLU and SwiGLU best. The axis starts at 1.93 so the gaps are visible.

The paper is famously candid about why. Its conclusion says: "We offer no explanation as to why these architectures seem to work; we attribute their success, as all else, to divine benevolence." The gain has held up in every major model family since, which is why SwiGLU became the default even without a satisfying theory.

Fusing gate and up

Gate and up both take the same input xx. So instead of two matrix multiplications, code can do one, with a matrix twice as wide, and cut the result in half. One large multiply keeps the GPU busier and reads xx once instead of twice.

FUSING GATE AND UP: ONE WIDE MATMUL, THEN SPLIT IN HALFgu = x @ W_gate_up # (T, 512) @ (512, 3072)g, u = gu.chunk(2, dim=-1)h = F.silu(g) * u # (T, 1536)out = h @ W_down # (T, 512)xgateup512 × 3,072g (1,536)u (1,536)one big multiply beats two smaller ones on a GPU
Figure 2.23Gate and up fused into one 512 × 3,072 matrix. One multiply, then the output is split into the gate half and the up half.
python
class SwiGLU(nn.Module):
    def __init__(self, d=512, h=1536):
        super().__init__()
        self.gate_up = nn.Linear(d, 2 * h, bias=False)   # fused W_gate and W_up
        self.down = nn.Linear(h, d, bias=False)

    def forward(self, x):                                # x: (B, T, 512)
        g, u = self.gate_up(x).chunk(2, dim=-1)          # each (B, T, 1536)
        return self.down(F.silu(g) * u)                  # (B, T, 512)

Sharp edges of SwiGLU

The hidden size is not 4d. It is usually between 2.7 and 3.5 times dd. Guessing it from dd gives the wrong shapes, and the weights will not load.

Swapped halves. With a fused matrix, which half is the gate and which is the up projection depends on how the checkpoint was saved. Swap them and the model still runs, because both halves have the same shape, but it computes SiLU(u)⊙g\text{SiLU}(u) \odot g and quality collapses.

SAME SHAPE, DIFFERENT ANSWER: GATE AND UP ARE NOT INTERCHANGEABLEcorrectg = -3SiLU(g) = -0.142×u = 2-0.285swappedg = 2SiLU(g) = 1.762×u = -3-5.285the tensor shape passes; the computation has changed
Figure 2.24Swapping gate and up changes the answer even though their shapes match. With illustrative scalar projections g = −3 and u = 2, SiLU(g) × u is −0.285, while SiLU(u) × g is −5.285, rounded to three decimals. These are hidden activations before the down projection.

More activation memory. During training, the backward pass needs both gg and uu, so SwiGLU stores two hidden-width vectors per token instead of one. Activation checkpointing or fused kernels that recompute the gate help.

No biases. Modern MLPs drop biases entirely. Code that adds them anyway will not match the trained weights.

Names vary. The same three matrices are called w1, w3, w2 in Meta’s code (gate, up, down respectively, which surprises people), and gate_proj, up_proj, down_proj in Hugging Face’s. Map them explicitly when converting.