Activation functions: GELU and SwiGLU
Compare ReLU, GELU, and gated units (SwiGLU = (Swish(xW₁) ⊙ xW₂)W₃). Explain why SwiGLU needs three weight matrices instead of two, so its hidden layer is made about 8/3 rather than 4 times the input width to keep the same parameter count as a standard MLP.
Builds on: MLPs and why nonlinearities matter
What the animation says
ReLU: a hard switch
Stack linear layers with nothing between them, and they collapse into a single linear layer. So an MLP puts a nonlinearity after its first projection. Call a hidden unit's value before it z. ReLU is the simplest: keep positive z, replace negative z with zero. Its slope jumps from zero to one. A unit whose z is negative for every input has zero slope, so training can't move it. It's dead. Language models use GELU instead. What does GELU give back at z equals minus one?
GELU and Swish: smooth switches
GELU at minus one gives about minus zero point one six. Small, but not zero. GELU is z times a soft on-factor between zero and one: the chance that a standard normal value falls below z. Swish, also called SiLU, uses the sigmoid of z as that on-factor instead. At minus three, minus one, zero and two, ReLU gives zero, zero, zero, two. GELU and Swish leak small negatives and nearly pass the two. On the negative side their slope is small but not zero everywhere, so a unit stuck there can still learn.
SwiGLU: a learned gate
A gated unit projects x two ways, with two different matrices. The first goes through Swish and becomes a gate, multiplied unit by unit with the second. Say the first projection gives two: the gate is one point seven six. A second projection of three becomes five point two eight; minus three becomes minus five point two eight. The gate is positive here, so the sign comes from the second projection. A third matrix maps back to width d: three matrices, not two. For the same parameter count, what happens to the hidden width?
Paying for the third matrix
It shrinks. Leave out biases and just count weights. The standard MLP has two matrices, each d by four d. That's eight d squared. SwiGLU has three matrices, each d by d f f. That's three times d times d f f. Set them equal: three d times d f f equals eight d squared. So d f f is eight thirds of d. Your turn. With d equal to seven hundred sixty-eight, drag SwiGLU's hidden width until the two bars match.
Remember this
Twenty forty-eight: four million seven hundred eighteen thousand five hundred ninety-two weights either way. GELU and Swish are smooth ReLUs: z times a soft on-factor, so units on the negative side don't go dead. SwiGLU gates one projection with another, and that costs a third matrix. To keep the budget, shrink the hidden width from four d to eight thirds of d. Same size, same compute, and in practice it works better.
Smooth switches and learned gates
The formulas
Same budget: 4d vs 8d/3
The functions on actual numbers
import numpy as np
from math import erf
z = np.array([-3.0, -1.0, 0.0, 2.0])
sig = lambda u: 1 / (1 + np.exp(-u))
relu = np.maximum(0, z)
gelu = z * 0.5 * (1 + np.vectorize(erf)(z / np.sqrt(2)))
swish = z * sig(z)
print(relu, gelu.round(3), swish.round(3))
print(swish[3] * 3, swish[3] * -3) # gate: Swish(xW1)=Swish(2) scales xW2
d = 768
print(2 * d * 4 * d, 3 * d * (8 * d // 3))
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses