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.
★☆☆☆☆ 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
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. A GLU computes…
2. SwiGLU uses as its gate…
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. GLU with a = 0, b = 4 outputs…
2. ∂y/∂b for y = g(a)·b is…
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. To match a ReLU FFN with d_ff = 3072, a gated FFN uses hidden…
2. LLaMA-7B's FFN hidden size is…
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. A bilinear unit computes…
2. Dauphin et al. argued GLU's linear path helps because…
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:
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
Y. N. Dauphin, A. Fan, M. Auli, D. Grangier (2017). “Language modeling with gated convolutional networks.” Proc. ICML.
N. Shazeer (2020). “GLU variants improve transformer.” arXiv:2002.05202.
A. Chowdhery et al. (2022). “PaLM: scaling language modeling with pathways.” arXiv:2204.02311.
H. Touvron et al. (2023). “LLaMA: open and efficient foundation language models.” arXiv:2302.13971.
S. Hochreiter, J. Schmidhuber (1997). “Long short-term memory.” Neural Computation 9(8).
Products of projections capture feature interactions; piecewise-linear gates capture kinks. Which matters depends on the target.
Practice
Show:
Level 0
Problem 1Level 0
GLU output for a = 0, b = 3?
σ(0)·3 = 1.5.
Problem 2Level 0
ReGLU output for a = −2, b = 5?
max(0, −2)·5 = 0.
Problem 3Level 0
Bilinear output for a = 2, b = 3?
6.
Level 1
Problem 4Level 1
∂y/∂a for GLU at a = 0, b = 2?
σ′(0)·2 = 0.25 × 2 = 0.5.
Problem 5Level 1
SwiGLU output for a = 2, b = 1.3?
2σ(2) × 1.3 ≈ 1.7616 × 1.3 ≈ 2.290.
Problem 6Level 1
Matched gated hidden width for d_ff = 3072?
(2/3) × 3072 = 2048.
Problem 7Level 1
Weight matrices in a gated FFN?
3: W, V and the output.
Level 2
Problem 8Level 2
LLaMA-7B FFN hidden size for d_model = 4096?
(2/3)·16384 ≈ 10922, rounded up to a multiple of 256: 11008.
Problem 9Level 2
Weights in a gated FFN with d_model = 512 and hidden 1365?
3 × 512 × 1365 = 2,096,640.
Problem 10Level 2
ReLU test MSE on the multiplicative target, second seed, in exercise 2?
0.090.
Problem 11Level 2
Bilinear test MSE on the multiplicative target, second seed?
0.007.
Problem 12Level 2
Bilinear test MSE on the piecewise target, first seed?
0.110.
Problem 13Level 2
Hidden units in the gated layers of exercise 2?
400 // (2·8 + 3) = 21.
Level 3
Problem 14Level 3
Show that any quadratic form xᵀQx with symmetric Q of rank r can be written with r bilinear units.
Diagonalise Q = Σₖ λₖ qₖqₖᵀ over its r nonzero eigenvalues. Then xᵀQx = Σₖ λₖ (qₖᵀx)². Each term is a bilinear unit with w = λₖqₖ and v = qₖ, since (wᵀx)(vᵀx) = λₖ(qₖᵀx)². So r units suffice. ∎
Problem 15Level 3
Explain why a ReLU layer approximates x₁x₂ poorly with few units.
A one-hidden-layer ReLU network computes a continuous piecewise-linear function. x₁x₂ has a nonzero Hessian everywhere (its mixed second derivative is 1), so on any region where the network is linear the approximation error grows quadratically with the region’s size. Reducing the error on a fixed domain requires many small linear regions, and so many units. With 40 units the ReLU layer reached MSE ≈ 0.08–0.09 on the multiplicative target, while a 21-unit bilinear layer reached 0.003–0.008. ∎
Problem 16Level 3
Explain why the bilinear layer failed on the piecewise target and SwiGLU did better.
The bilinear layer computes a quadratic function of the input (plus linear terms from biases). |x₀| and max(x₁, 0) have kinks that a quadratic can only approximate by a parabola, leaving a large residual (MSE ≈ 0.1). SwiGLU’s Swish gate is close to ReLU for large |a|, so each unit can produce kink-like shapes, and its error was 2–3× lower than the bilinear layer’s. With 21 units instead of ReLU’s 40, it still fitted the kinks less precisely than ReLU. ∎
Problem 17Level 3
Advise whether to use SwiGLU in a small network that predicts actuator commands from sensor features.
Test both at equal parameters rather than following the transformer default. If the mapping involves products of features (gains that depend on operating point, dynamic pressure ∝ ρv², bilinear control effects), gated units can fit it far more efficiently, as exercise 2 showed. If it is dominated by saturations, dead zones and switches (piecewise-linear structure), ReLU was better at equal parameters. A gated unit also costs an extra matrix multiply and is less sparse, which matters on embedded hardware. Physics knowledge can decide: put products in by design where they are known. ∎
Level 4
Problem 18Level 4
SwiGLU test MSE on the multiplicative target, first seed?
0.008.
Problem 19Level 4
ReLU test MSE on the piecewise target, third seed?
0.001.
Problem 20Level 4
SwiGLU test MSE on the piecewise target, first seed?
0.062.
Coding practice
🐍 Code 1 (Level 0): GLU
σ(xW)·(xV).
🐍 Code 2 (Level 0): SwiGLU
Swish(xW)·(xV).
🐍 Code 3 (Level 0): FFN parameter count
2 or 3 matrices of size d_model × hidden.
🐍 Code 4 (Level 1): LLaMA hidden size
(2/3)·4·d_model rounded up to a multiple.
🐍 Code 5 (Level 1): GLU backward
Gradients of σ(a)·b.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Gated linear units: GLU, ReGLU, GEGLU and SwiGLU: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.