DL0129 SwiGLU, GELU, and ReLU FFNs

Compare SwiGLU, GeLU, and ReLU activation functions in the Feed-Forward Network (FFN) blocks of LLMs, as used in models like Llama 3 and GPT-3.

Answer

All three sit in the same place, namely the two-layer FFN that follows attention in every transformer block, but they differ in smoothness and in whether the nonlinearity is elementwise or multiplicative. ReLU is \max(0,x): cheapest, produces exact zeros (roughly 90-95% of FFN units are zero on a typical token), but has a kink at the origin and zero gradient for negative inputs, which gives dead units. GELU is x\Phi(x): smooth, non-monotonic, keeps a small negative response, and was the default through BERT, GPT-2, and GPT-3. SwiGLU is not an elementwise function at all but a gated layer: the up-projection is split into a gate branch passed through SiLU (x\sigma(x)) and a value branch left linear, and the two are multiplied elementwise. That third weight matrix is why practitioners shrink the hidden width by 2/3 to stay parameter- and FLOP-neutral, and the payoff reported by Shazeer’s GLU-variants ablation is a consistent but modest gain, about 0.05 nats of pretraining log-perplexity on T5’s span-corruption objective at matched budget, which is enough that PaLM, Llama, Mistral, and Qwen2 all adopted it. The cost is one extra matmul, one extra saved activation for the backward pass, loss of exploitable sparsity, and a wider activation range that makes low-bit quantization harder.

(1) Two Matrices Versus Three: a dense FFN is W_{up} then W_{down}; a gated FFN adds W_g, so parameters go from 2 d\, d_{ff} to 3 d\, d_{ff} at the same width.
(2) The 2/3 Width Rule: to keep the block budget fixed you set d_{ff} = \frac{8}{3}d instead of 4d, which is exactly why Llama 2 7B uses 11008 with d = 4096.
(3) Smoothness Changes Optimization: ReLU’s derivative is a step with no gradient for negative inputs, while GELU and SiLU are C^\infty and pass a small negative signal, so units recover instead of dying.
(4) Gating Is A Multiplicative Interaction: the output is a product of two learned projections of the same input, giving a data-dependent, per-channel scaling that no elementwise function can express.
(5) Sparsity Is A ReLU-Only Asset: exact zeros let inference systems skip entire rows of W_{down}; GELU and SwiGLU produce values that are small but almost never exactly zero.
(6) SwiGLU Costs Memory Traffic And Range: three matmuls plus an extra stored tensor per layer, and the product of two unbounded projections widens the dynamic range that quantization must cover.

Two-panel line chart: left panel plots ReLU, GELU and SiLU over the range minus four to four, showing ReLU flat at zero for negative inputs while GELU and SiLU dip slightly below zero; right panel plots their derivatives, showing ReLU's step from zero to one at the origin versus the smooth GELU and SiLU derivatives that briefly exceed one and go slightly negative

Figure 1: Shape (left) and derivative (right). ReLU’s derivative is a step function with a kink at the origin and no gradient for negative inputs; GELU and SiLU are smooth, non-monotonic, and dip below zero (SiLU’s minimum is -0.278 at x = -1.28), so a unit pushed into the negative region still receives gradient and can come back.

The historical progression is ReLU → GELU → gated GLU variants, and it is worth being honest that only the first step has a clean story. Replacing ReLU with GELU removes the dead-unit problem and gives a slightly better-conditioned loss surface. Replacing an elementwise nonlinearity with a gate is different in kind: the value branch stays linear, and the gate branch modulates it, so the layer computes a second-order interaction of the input with itself. Shazeer’s paper explicitly declines to explain why this helps, attributing the gain to luck rather than theory, and later analyses mostly note that gating gives the block a cheap multiplicative pathway similar in spirit to the gates in an LSTM. What made SwiGLU the production default is that the gain survives at scale, needs no extra hyperparameters, and is free once you apply the 2/3 width rule. Llama 3 8B keeps the gated form but widens back out to d_{ff} = 14336 with d = 4096, a deliberate choice to spend more parameters in the FFN rather than a parameter-neutral swap.

Mathematical Formulation:
\mathrm{FFN}_{\mathrm{ReLU}}(x) = \max(0, xW_{up})\,W_{down}
\mathrm{GELU}(x) = x\,\Phi(x)
\mathrm{SiLU}(x) = x\,\sigma(x)
\mathrm{SwiGLU}(x) = \mathrm{SiLU}(xW_g) \odot (xW_u)
\mathrm{FFN}_{\mathrm{SwiGLU}}(x) = \mathrm{SwiGLU}(x)\,W_{down}
d_{ff} = \frac{2}{3}\cdot 4d = \frac{8}{3}d

Where:

  • x \in \mathbb{R}^{d} is the post-norm hidden state for one token, and the FFN output has the same dimension d so it can be added back through the residual connection.
  • W_{up}, W_g, W_u \in \mathbb{R}^{d \times d_{ff}} are up-projections and W_{down} \in \mathbb{R}^{d_{ff} \times d} the down-projection; the dense block uses W_{up} only, the gated block uses both W_g (gate) and W_u (value).
  • \Phi is the standard normal CDF and \sigma the logistic sigmoid; GELU is often shipped as the tanh approximation 0.5x(1+\tanh(\sqrt{2/\pi}(x+0.044715x^3))), which is why two frameworks can disagree in the last few digits.
  • \odot is the elementwise product over the d_{ff} hidden channels, the operation that turns a gate into a multiplicative interaction rather than a pointwise map.
  • d_{ff} is the FFN hidden width; the last line is the parameter-neutral setting, since 3 d \cdot \frac{8}{3}d = 8d^2 = 2 d \cdot 4d, and real implementations round the result to a hardware-friendly multiple such as 256.
Architecture diagram with two stacked left-to-right flows: the top flow is a dense FFN with hidden state, one up-projection, an elementwise ReLU or GELU, a down-projection and output, annotated with two d times d_ff parameters; the bottom flow is a gated SwiGLU FFN where the hidden state branches into a SiLU gate path and a linear value path, the two are combined by an elementwise product, then a down-projection and output, annotated with three d times d_ff parameters and the two-thirds width rule

Figure 2: The structural difference is a branch, not a curve. A dense FFN applies one elementwise function between two matrices; a gated FFN splits the up-projection into a SiLU gate path and a linear value path whose elementwise product feeds W_{down}. The third matrix is what forces the 8d/3 width if the block budget must stay fixed.

PropertyReLUGELUSwiGLU
FormElementwise, piecewise linearElementwise, smooth, non-monotonicGated layer: SiLU branch times linear branch
Matrices per FFN223, so width drops to 8d/3 to stay budget-neutral
Gradient behaviour0 or 1; dead units never recoverSmooth, briefly exceeds 1, small negative regionSmooth, plus a gradient path through the gate itself
Exact-zero sparsityYes, typically 90-95% of units, exploitable at inferenceNo; values are small but nonzeroNo; near-zero mass only, so row skipping needs a threshold and loses accuracy
Quantization friendlinessBest: bounded below, no multiplicative amplificationGoodHardest: the product of two unbounded projections widens outliers into W_down
Representative modelsOriginal Transformer, T5 v1.0, OPTBERT, GPT-2, GPT-3, ViTPaLM, Llama 2 and Llama 3, Mistral, Qwen2 (Gemma uses the GELU-gated GeGLU)

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *