Search

Search papers and lessons

GLU Variants Improve Transformer

Noam Shazeer · 2020

Shazeer (2020) swaps the Transformer's feed-forward activation for gated linear units; several gated variants give lower perplexity and better scores than ReLU or GELU.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Probability
  • Linear algebra
  • Architectures
  • ReadReady to readNot yet

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.

  1. Start with a vector. The input xx is the hidden vector for one token, for example a list of 768 numbers.
  2. Project it twice. Multiply xx by two different learned matrices, WW and VV. You now have two vectors of the hidden width: the gate path xWxW and the value path xVxV.
  3. 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 (x σ(x)x\,\sigma(x), defined below).
  4. 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.
  5. Project back. Multiply by a third matrix, W2W_2, to return to the model width.

Tiny trace, one hidden unit, scalars only. Let x=2x = 2, W=1W = 1, V=3V = 3. The gate path is xW=2xW = 2. The sigmoid gives σ(2)≈0.881\sigma(2) \approx 0.881. The value path is xV=6xV = 6. The GLU unit is 0.881×6≈5.290.881 \times 6 \approx 5.29. Now swap the gate for ReLU, the ReGLU variant: max⁡(0,2)=2\max(0, 2) = 2, and 2×6=122 \times 6 = 12. Check: a ReLU gate on a positive input passes it unchanged, so the unit equals 2×6=122 \times 6 = 12.

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 x σ(βx)x\,\sigma(\beta x). The paper uses β=1\beta = 1 inside the feed-forward layers.

Worked examples

Each one shows every step as a numbered line.

Example 1: one hidden unit, by hand.

  1. Input x=2x = 2, gate weight W=1W = 1, value weight V=3V = 3.
  2. Gate path: xW=2×1=2xW = 2 \times 1 = 2.
  3. Value path: xV=2×3=6xV = 2 \times 3 = 6.
  4. GLU gate: σ(2)=11+e−2≈0.881\sigma(2) = \dfrac{1}{1 + e^{-2}} \approx 0.881.
  5. GLU unit: 0.881×6≈5.290.881 \times 6 \approx 5.29.
  6. Bilinear unit (no gate): 2×6=122 \times 6 = 12.

The sigmoid reduces the unit from 12 to about 5.3 because it shrinks the gate below 1.

Example 2: the matched-parameter check.

  1. Baseline: two matrices, each 768×3072768 \times 3072. One is 2,359,2962{,}359{,}296 weights, so two give 4,718,5924{,}718{,}592.
  2. Gated: three matrices, each 768×2048768 \times 2048. One is 1,572,8641{,}572{,}864 weights, so three give 4,718,5924{,}718{,}592.
  3. Compare: the totals are equal, so differences in the results come from the layer's form, not from more weights.
  4. 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.

  1. Subtract the logs: 1.677−1.633=0.0441.677 - 1.633 = 0.044.
  2. Undo the log to compare perplexities: e−0.044≈0.957e^{-0.044} \approx 0.957, so GEGLU's perplexity is about 4.3 percent lower.
  3. 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: 1.997−1.942=0.0551.997 - 1.942 = 0.055, 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

FFNReLU(x,W1,W2)=max⁡(xW1,0) W2\mathrm{FFN}_{\mathrm{ReLU}}(x, W_1, W_2) = \max(xW_1, 0)\,W_2

and the GELU version replaces the max⁡\max with GELU(x)=x Φ(x)\mathrm{GELU}(x) = x\,\Phi(x), where Φ\Phi 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:

FFNGLU(x,W,V,W2)=(σ(xW)⊗xV) W2\mathrm{FFN}_{\mathrm{GLU}}(x, W, V, W_2) = \big(\sigma(xW) \otimes xV\big)\,W_2

Here ⊗\otimes is the elementwise product. Swapping the gate function gives the others:

FFNReGLU=(max⁡(0,xW)⊗xV)W2,FFNGEGLU=(GELU(xW)⊗xV)W2\mathrm{FFN}_{\mathrm{ReGLU}} = \big(\max(0, xW) \otimes xV\big)W_2, \qquad \mathrm{FFN}_{\mathrm{GEGLU}} = \big(\mathrm{GELU}(xW) \otimes xV\big)W_2

FFNSwiGLU=(Swish1(xW)⊗xV)W2\mathrm{FFN}_{\mathrm{SwiGLU}} = \big(\mathrm{Swish}_1(xW) \otimes xV\big)W_2

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 dffd_{ff} by a factor of 23\tfrac{2}{3}. In the T5 base model dmodel=768d_{\mathrm{model}} = 768 and the baseline dff=3072d_{ff} = 3072, so the gated version uses dff=2048d_{ff} = 2048. Check: 2×768×3072=4,718,5922 \times 768 \times 3072 = 4{,}718{,}592 and 3×768×2048=4,718,5923 \times 768 \times 2048 = 4{,}718{,}592.

Results

The model is T5 base: 12 encoder and 12 decoder layers, dmodel=768d_{\mathrm{model}} = 768, 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

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, WW and VV. 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 xW=0xW = 0 and the value xV=5xV = 5, 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 0.5×5=2.50.5 \times 5 = 2.5. ReLU of 0 is 0, so the ReGLU unit is 0×5=00 \times 5 = 0.

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 23\tfrac{2}{3} 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

Read the full paper

Noam Shazeer, 2020. arXiv:2002.05202