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.
#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:
Read the SwiGLU version from the inside out. The token's vector is projected twice, by two different matrices, into two vectors of the same hidden width : the gate and the up projection . 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 means. Finally the down projection brings the result back to the model width. Modern versions drop all biases.
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, , with a sharp corner at zero. GELU, from Chapter 1, multiplies by , the probability that a standard bell-curve variable is below ; it behaves like a smooth ReLU and dips slightly below zero, to about , for small negative inputs. SiLU, the sigmoid linear unit, also called Swish, multiplies by the sigmoid , the S-shaped curve that runs from 0 to 1:
Some values: , , and its lowest point is at . For large positive , and SiLU is almost ; for large negative , 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.
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 , ; SiLU is .
#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.
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 for every combination of gate value and up value from 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 : positive above the middle, negative below.
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.
#Section 16The Family, the Paper and the Code
The GLU family
SwiGLU is one member of a family that all compute 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.
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.
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 . 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 once instead of twice.
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 . Guessing it from 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 and quality collapses.
More activation memory. During training, the backward pass needs both and , 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.