Foundations to Frontier
Tests
E. Deep Learning › E1 Activation Functions (one page each)

Gated linear units: GLU, ReGLU, GEGLU and SwiGLU

A gated linear unit computes two linear projections of the same input and multiplies them, one passed through a gate function: g(xW)·(xV). With a sigmoid gate it is the original GLU; with ReLU, GELU or Swish gates it becomes ReGLU, GEGLU or SwiGLU; with no gate it is a bilinear layer. Gated feed-forward blocks are now standard in large language models (PaLM, LLaMA and their successors use SwiGLU). This page covers the forms, their gradients, the 2/3 rule that keeps parameter counts equal, and why multiplying two projections helps. In the exercises you implement the family and match parameter budgets, then show that, at equal parameters, gated units fit multiplicative structure about ten times better than a ReLU layer, while ReLU remains best on piecewise-linear structure.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Two projections, one gate

An ordinary layer computes one projection of its input and applies an activation. A gated linear unit computes two projections of the same input, a = xW and b = xV, and multiplies them: g(a)·b. One projection decides how much of the other passes through. Because the output is a product of two learned linear functions, such a layer can represent multiplicative interactions between inputs directly.

Activation lab: the curve, its slope, and what a network learns with it
z from −4 to 4 · orange f · dashed blue f′
SwiGLU (gated): training accuracy after 300 steps 1.000
Compare activations at the same number of steps. Smooth, non-saturating units usually fit the spirals fastest. Sigmoid and the hard-threshold units learn slowly or need surrogate gradients. Gated and maxout units use twice as many hidden pre-activations. Learning rates are fixed per activation and not tuned, so treat small differences as noise (press "new data and weights").
Why engineers care

SwiGLU feed-forward blocks are in most current large language and multimodal models, including those used for robot instruction following and mission-planning assistants. Gating is also an old idea in control and sensing: one signal modulating another, as in LSTM gates and attention. Knowing when multiplication helps tells you when a gated layer is worth its extra matrix.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A GLU computes…
  2. 2. SwiGLU uses as its gate…
  3. 3. A gated feed-forward block has how many weight matrices?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The family by hand

With a = xW + c and b = xV + d:

name output gate
GLU (Dauphin et al., 2017) σ(a)·b sigmoid
bilinear a·b none
ReGLU max(0, a)·b ReLU
GEGLU GELU(a)·b GELU
SwiGLU Swish(a)·b SiLU
  • Example: for a = 0, b = 3, GLU gives σ(0)·3 = 1.5, ReGLU gives 0 and SwiGLU gives 0.
  • Split form: implementations often compute one projection of width 2k and split it into halves (a, b).
  • Gradients: for y = g(a)·b, ∂y/∂a = g′(a)·b and ∂y/∂b = g(a). Each half receives a gradient scaled by the other: multiplicative interaction in the backward pass too.
  • Products are easy: one bilinear unit with w = eᵢ and v = eⱼ computes xᵢ·xⱼ exactly. A ReLU layer can only approximate a product with many pieces.
📝 Quick check (Level 1)
0/3 answered
  1. 1. GLU with a = 0, b = 4 outputs…
  2. 2. ∂y/∂b for y = g(a)·b is…
  3. 3. How many bilinear units compute x₁·x₂ exactly?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Matching the parameter budget

Exercise 1. The GLU variants, the split form and SwiGLU’s gradients (checked by finite differences) are implemented. A single bilinear unit computes x₁·x₃ exactly.

The 2/3 rule.

  • A plain transformer feed-forward block has two weight matrices, d_model × d_ff and d_ff × d_model: 2·d_model·d_ff weights.
  • A gated block has three, so its hidden width is reduced to (2/3)·d_ff to keep the same count.
  • For d_model = 512 and d_ff = 2048: hidden 1365, giving 2,096,640 weights against 2,097,152.
  • LLaMA rounds (2/3)·4·d_model up to a multiple of 256: 11008 for d_model = 4096.

Practical notes.

  • Fuse W and V into one matrix of width 2·hidden and split the result.
  • Gated units’ hidden activations are not sparse like ReLU’s, which matters for activation-sparsity tricks.
  • Shazeer (2020) found GEGLU and SwiGLU gave the best perplexity in T5-style transformers at equal compute.
class SwiGLU(nn.Module):
    def __init__(s, d, h): super().__init__(); s.wv = nn.Linear(d, 2 * h, bias=False); s.o = nn.Linear(h, d, bias=False)
    def forward(s, x): a, b = s.wv(x).chunk(2, -1); return s.o(F.silu(a) * b)
🐍 Exercise 1: the GLU family
Implement GLU variants and the split form, SwiGLU's backward pass, the parameter-matched hidden size, and a bilinear unit that computes a product exactly.
📝 Quick check (Level 2)
0/3 answered
  1. 1. To match a ReLU FFN with d_ff = 3072, a gated FFN uses hidden…
  2. 2. LLaMA-7B's FFN hidden size is…
  3. 3. Gated hidden activations, compared with ReLU's, are…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why multiplication helps. A ReLU layer computes a sum of hinge functions, which is piecewise linear. A product such as x₁x₂ is curved in every direction, and a piecewise-linear approximation to it on a region needs many pieces, with error that shrinks only polynomially in the number of units. A bilinear unit is a quadratic form, x ↦ (wᵀx)(vᵀx), so products and squares are exact. Gated units with a nonlinear gate combine both: g(a)·b is piecewise-linear-like in a and linear in b, and a product of the two.

Origins.

  • Gating goes back to LSTMs (Hochreiter and Schmidhuber, 1997), where sigmoid gates multiply cell contents, and to multiplicative and sigma-pi units.
  • Dauphin et al. (2017) proposed the GLU for convolutional language models. The linear path b gives gradients that do not vanish, since ∂y/∂b = σ(a) without a derivative factor.
  • Shazeer (2020) compared GLU variants in transformer feed-forward layers.
  • PaLM (Chowdhery et al., 2022) and LLaMA (Touvron et al., 2023) adopted SwiGLU.

Limits.

  • A bilinear layer is only quadratic, so it cannot represent |x| or ReLU-like kinks without other nonlinear layers.
  • The gate supplies the nonlinearity; the product supplies the interactions.
  • The empirical gains in transformers are a few percent in perplexity at fixed compute, not dramatic, and Shazeer offered no theoretical explanation.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A bilinear unit computes…
  2. 2. Dauphin et al. argued GLU's linear path helps because…
  3. 3. Which model families adopted SwiGLU?

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

When does gating pay off?

History.

  • S. Hochreiter and J. Schmidhuber (1997) used multiplicative gates in LSTMs.
  • Y. Dauphin, A. Fan, M. Auli and D. Grangier (2017) introduced the GLU for gated convolutional language models.
  • N. Shazeer (2020) compared GLU variants in transformers.
  • A. Chowdhery et al. (2022, PaLM) and H. Touvron et al. (2023, LLaMA) made SwiGLU standard.

Exercise 2. One hidden layer with about 400 parameters, regressing standardised targets from 8 Gaussian inputs. The ReLU layer has 40 hidden units; the gated layers have 21, since they need two input matrices. Training uses full-batch Adam for 400 steps on 512 points, and the table gives test MSE on 2000 new points. There are two targets:

  • multiplicative: x₀x₁ + x₂x₃ − x₄x₅;
  • piecewise-linear: |x₀| + max(x₁, 0) − |x₂ − x₃| + max(x₄ + x₅, 0).
unit multiplicative target piecewise-linear target
ReLU 0.085, 0.090, 0.080 0.019, 0.010, 0.001
GLU (sigmoid) 0.009, 0.015, 0.011 0.023, 0.013, 0.006
bilinear 0.008, 0.007, 0.003 0.110, 0.104, 0.090
SwiGLU 0.008, 0.008, 0.005 0.062, 0.037, 0.042
  • Gating won on multiplicative structure. Every gated run beat every ReLU run, typically by about 10× in MSE, here and in three further seeds.
  • ReLU won on piecewise-linear structure, with the lowest mean error in both sets of seeds. The sigmoid GLU was a close second.
  • The bilinear unit was worst there, about 10× worse than ReLU, because a quadratic model cannot represent kinks.
  • SwiGLU sat in between. Its Swish gate adds the nonlinearity the bilinear unit lacks, but with half as many hidden units as ReLU, it fitted the kinks less well.
  • Lesson: the gain from gating depends on whether the target involves interactions between features. Language modelling evidently does; whether a given control or sensing problem does is an empirical question.
🐍 Exercise 2 (research): when does gating pay off?
At an equal budget of about 400 parameters, fit a multiplicative and a piecewise-linear target with a ReLU layer and GLU, bilinear and SwiGLU layers, and compare test MSE.

Gated units in AI, robotics and aerospace

  • Language and vision-language models used for robot instruction following, operator assistance and documentation search use SwiGLU feed-forward blocks.
  • Recurrent state estimators: LSTM and GRU gates are multiplicative units, used for sequence modelling of telemetry and sensor streams.
  • Physics with products: dynamics with products of states (drag ∝ ρv², Coriolis terms, bilinear control inputs) are natural targets for multiplicative layers.

References

  1. Y. N. Dauphin, A. Fan, M. Auli, D. Grangier (2017). “Language modeling with gated convolutional networks.” Proc. ICML.
  2. N. Shazeer (2020). “GLU variants improve transformer.” arXiv:2002.05202.
  3. A. Chowdhery et al. (2022). “PaLM: scaling language modeling with pathways.” arXiv:2204.02311.
  4. H. Touvron et al. (2023). “LLaMA: open and efficient foundation language models.” arXiv:2302.13971.
  5. S. Hochreiter, J. Schmidhuber (1997). “Long short-term memory.” Neural Computation 9(8).