TL;DR
Every Transformer block has a small two-layer network, the feed-forward sublayer, that runs on each position separately. Its middle activation is usually ReLU or GELU. Shazeer asks whether a gated unit, which multiplies two linear projections of the input and passes one of them through a nonlinearity, does better in that slot. He tests a family of gated variants in a T5 encoder-decoder with the parameter count held fixed. Several variants, GEGLU and SwiGLU in particular, reach lower pre-training perplexity and better scores on language benchmarks than the ReLU and GELU baselines. The paper does not explain why they work.
Builds on: Transformer, Gated Linear Unit (GLU), Gaussian Error Linear Unit (GELU) · Leads to: later large language models that use SwiGLU feed-forward layers in place of the original design
Why it mattered
The feed-forward sublayer holds about two thirds of the parameters of a standard Transformer block, so changing it is a large lever. The gated variants became common in later language model designs, and SwiGLU in particular is a frequent default in open models. The change costs no extra parameters or compute in the paper's setup, so it is cheap to adopt. The paper has about 2,600 citations on Semantic Scholar.
The problem
A Transformer block alternates attention, which mixes information across positions, with a feed-forward network (FFN), which transforms each position on its own. The original feed-forward network has two linear layers with a ReLU between them. Later work swapped in GELU and other smooth activations. Each swap changed only the nonlinearity. The question here is whether a different structure, one where the signal is multiplied by a learned gate, helps more than a new nonlinearity alone.
How it works
Take one feed-forward block on one position. The steps run in the order the computation runs.
- Start with a vector. The input is the hidden vector for one token, for example a list of 768 numbers.
- Project it twice. Multiply by two different learned matrices, and . You now have two vectors of the hidden width: the gate path and the value path .
- Gate one path. Apply a nonlinear function to the gate path only. In the original GLU that function is the sigmoid, which squashes each number into the range 0 to 1. In the variants it can be ReLU, GELU (a smooth curve close to ReLU that lets small negative inputs through a little) or Swish (, defined below).
- Multiply the two paths elementwise. Each hidden unit becomes the product of its gate and its raw value. A gate near zero switches that unit almost off.
- Project back. Multiply by a third matrix, , to return to the model width.
Tiny trace, one hidden unit, scalars only. Let , , . The gate path is . The sigmoid gives . The value path is . The GLU unit is . Now swap the gate for ReLU, the ReGLU variant: , and . Check: a ReLU gate on a positive input passes it unchanged, so the unit equals .
The diagram shows the layer.
flowchart LR X["x"] --> W["times W, gate path"] X --> V["times V, value path"] W --> G["nonlinearity: sigmoid, ReLU, GELU or Swish"] G --> M["elementwise product"] V --> M M --> W2["times W2, back to model width"]
A word on the names. A bilinear layer has no nonlinearity in the gate, so it is a product of two linear maps. A GEGLU uses GELU in the gate, and a SwiGLU uses Swish. Swish is . The paper uses inside the feed-forward layers.
Worked examples
Each one shows every step as a numbered line.
Example 1: one hidden unit, by hand.
- Input , gate weight , value weight .
- Gate path: .
- Value path: .
- GLU gate: .
- GLU unit: .
- Bilinear unit (no gate): .
The sigmoid reduces the unit from 12 to about 5.3 because it shrinks the gate below 1.
Example 2: the matched-parameter check.
- Baseline: two matrices, each . One is weights, so two give .
- Gated: three matrices, each . One is weights, so three give .
- Compare: the totals are equal, so differences in the results come from the layer's form, not from more weights.
- Interpretation: a fair comparison fixes the total weights, so the gated variants do not win by being bigger.
Example 3: reading the pre-training table. The paper reports heldout log-perplexity on the span-filling task: the natural log of perplexity, so lower is better. After 524,288 training steps the ReLU baseline is 1.677 and GEGLU is 1.633.
- Subtract the logs: .
- Undo the log to compare perplexities: , so GEGLU's perplexity is about 4.3 percent lower.
- Interpretation: a 4 percent drop is small, but the ordering repeats across variants and across training lengths.
Try it: at 65,536 steps the ReLU baseline is 1.997 and GEGLU is 1.942. The paper's parentheses list the spread across its four short runs, 0.005 for the ReLU baseline. Is the gap larger or smaller than that spread? Answer: , which is about eleven times 0.005, so the gap is unlikely to be run-to-run noise alone.
The math
Without bias terms, following the T5 code the paper uses, the baseline ReLU feed-forward layer is
and the GELU version replaces the with , where is the standard normal cumulative distribution function.
Step 2 in symbols: the gated layer
The gated variants use two matrices on the input side and one on the output side:
Here is the elementwise product. Swapping the gate function gives the others:
Step 4 in symbols: keeping the size fixed
A gated layer has three weight matrices where the baseline has two. To keep the parameter count and compute the same, the paper shrinks the hidden width by a factor of . In the T5 base model and the baseline , so the gated version uses . Check: and .
Results
The model is T5 base: 12 encoder and 12 decoder layers, , 12 attention heads. Each variant is pre-trained on C4 with the span-filling objective, then fine-tuned once on a mixture of SQuAD, GLUE and SuperGLUE.
- Pre-training log-perplexity (Table 1). At 65,536 steps the best variants are GEGLU at 1.942 and SwiGLU at 1.944, against 1.997 for the ReLU baseline and 1.983 for GELU. At 524,288 steps GEGLU is 1.633, SwiGLU 1.636, and ReLU 1.677. Remember these are log values, so a 0.04 gap is a few percent of Perplexity (see Example 3).
- GLUE average (Table 2). ReGLU scores highest at 84.67. SwiGLU is 84.36, GEGLU 84.12, and the ReLU baseline 83.80.
- SuperGLUE average (Table 3). SwiGLU scores 74.56, GEGLU 73.96, against 72.76 for the ReLU baseline.
- SQuAD v1.1 (Table 4). ReGLU gives the best F1 at 91.18, against 90.87 for ReLU.
What these numbers mean. The pre-training ordering is the most consistent evidence: every GLU-family variant, bilinear included, beats the ReLU baseline at both training lengths, and GEGLU and SwiGLU lead at both. The downstream gains are smaller and less uniform. The sigmoid-gated GLU sits behind GEGLU, SwiGLU, ReGLU and the bilinear layer on perplexity, so the gate function matters.
The limits. The paper uses one model size and one training recipe, and it reports single fine-tuning runs. The authors call the benchmark results noisy, so a difference of a few tenths may not hold under another seed. The paper does not explain why the variants work. The authors also drop dropout during pre-training because they found it gives better results. The original T5 run used dropout and scored notably worse, so the comparison with T5 is not like for like.
Reading guide
- Section 1, Introduction. The Transformer feed-forward layer and the ReLU, GELU and Swish baselines. Needs Transformer, Feedforward Neural Network and Rectified Linear Unit (ReLU).
- Section 2, Gated Linear Units and variants. The GLU family and the matched-width rule. Needs Gated Linear Unit (GLU), Sigmoid Function and Linear Transformation.
- Section 3.1, Model architecture. The T5 base setup and the reduced hidden width. Needs Exploring the Limits of Transfer Learning (T5).
- Section 3.2, Pre-training and perplexity. Table 1 and the training recipe. Needs Perplexity and Gaussian Error Linear Unit (GELU).
- Section 3.3, Fine-tuning. Tables 2 to 4 and the benchmark scores. Needs Transfer Learning and Fine-Tuning.
- Section 4, Conclusions. The authors' admission that the success is not explained. Read it as a statement of what is and is not known.
Check yourself
QuestionRecall: what is the main structural difference between a GLU feed-forward layer and the original ReLU feed-forward layer?Show answer
The GLU layer has two input projections, and . One is passed through a nonlinearity and then multiplied elementwise by the other. The original layer has one input projection followed by a nonlinearity, and then the output projection.
QuestionCompute: with the gate and the value , what is the output of a sigmoid GLU unit, and what is it for a ReGLU unit?Show answer
Sigmoid of 0 is 0.5, so the GLU unit is . ReLU of 0 is 0, so the ReGLU unit is .
QuestionWhy does the paper shrink the hidden width to two thirds of the baseline?Show answer
A gated layer has three weight matrices instead of two. Shrinking the hidden width by keeps the total parameters and compute equal to the baseline, so the comparison is fair.
QuestionA team replaces GELU with SwiGLU in a new model and keeps the old hidden width. What should they check, and why might the model grow?Show answer
With three matrices at the old hidden width, the layer has 50 percent more weights than the two-matrix baseline, so the model grows. To match the old size, they should shrink the hidden width by two thirds as the paper does, and then compare perplexity at equal parameters.
Sources
- Shazeer (2020), GLU Variants Improve Transformer: https://arxiv.org/abs/2002.05202
- Dauphin, Fan, Auli, Grangier (2016), Language Modeling with Gated Convolutional Networks: https://arxiv.org/abs/1612.08083
- Vaswani et al. (2017), Attention Is All You Need: https://arxiv.org/abs/1706.03762