TL;DR
A gated linear unit (GLU) computes two linear projections of the same input. One projection goes through a sigmoid, which turns it into a number between 0 and 1 for each output entry, and that number scales the other projection, entry by entry. The network learns how much of each signal to let through. GLU and its variants are now the default feed-forward block in many large Transformer language models.
Builds on: Activation Function, Sigmoid Function, Matrix Multiplication · Leads to: GLU variants
Where it's used
Language modelling. Dauphin and colleagues introduced GLU in 2016 for a convolutional Language Model that predicted the next word of English text. Their gated convolutional network was competitive with recurrent models on the benchmarks they reported, which made the gate idea a serious option for sequence models.
Large transformer language models. Many open models released in the early 2020s replace the plain feed-forward block of each Transformer layer with a gated version. The LLaMA family, for example, uses the SwiGLU variant of GLU in each block. Here the gate is applied once per hidden unit in every layer, so the gate is computed for every hidden unit at every position in the text.
Choosing a block design. Shazeer's 2020 study compared GLU and its variants inside the same transformer, on the same training data, against the plain ReLU and GELU blocks. The gated versions gave lower perplexity at a similar compute budget. This is why teams that build new transformers often start from a gated block.
How it works
A GLU layer takes an input vector, makes two copies of it through two different weight matrices, and multiplies them together in a gated way. One copy is squashed to between 0 and 1 and acts as a dimmer switch. The other copy carries the signal.
flowchart LR X[Input x] --> A[Linear projection xW] X --> B[Linear projection xV] A --> S[Sigmoid: values between 0 and 1] S --> M[Multiply entry by entry] B --> M M --> Y[Gated output]
Take a tiny instance with input . Use = the identity matrix and that swaps the two entries, .
- Put the input through the gate projection. . Check: with the identity matrix, nothing changes.
- Put the input through the value projection. . Check: the first output is , and the second is .
- Squash the gate values with the sigmoid. and . Check: both lie between 0 and 1, and the bigger input gives the bigger gate.
- Multiply the gate by the value, entry by entry. First entry: . Second entry: . Check: each output is a value scaled down by its gate.
- Name the idea. A GLU output is a linear signal whose strength is set by a learned, input-dependent gate. Nothing is cut to zero. A small gate shrinks the signal, a gate near 1 passes it almost unchanged.
The plot shows the sigmoid gate that does the squashing, next to the swish curve used by a GLU variant later on. Both curves have values between 0 and 1 when they gate and both rise with their input. The points mark the gate values from step 3.
- sigmoid gate
- swish (for SwiGLU)
The math
Let be an input vector, and let be two learned weight matrices. Then where is the sigmoid applied to each entry, and is the entry-by-entry (Hadamard) product of two vectors of the same length. The output lies in .
Some readers meet the original paper's version, where the sigmoid sits on the other branch: . It is the same idea with the roles of the two branches swapped. The bias terms and are optional, and the transformer version usually leaves them out.
A GLU is normally used as a block inside a larger layer. Following the paper's notation, a feed-forward GLU block with an output matrix is
Step 3 in symbols: the gate
The sigmoid maps any real number into the open interval . It is increasing, , and . So a gate value near 0 switches an entry almost off, and a value near 1 switches it almost fully on.
Step 4 in symbols: the product
For the tiny instance, the -th output is . With and , we get . The same rule gives .
Why the gradient survives
Training needs the derivative of the output with respect to each weight. Write and . By the product rule, the derivative of with respect to the value branch is itself, and the derivative with respect to the gate branch is , where . The value branch is linear, so its slope is never flattened by a bend. This is the argument Dauphin and colleagues made: the linear path lets gradients reach earlier layers without passing through a saturating squashing function at every layer, which reduces the risk of a vanishing gradient.
Worked examples
Take one pair of pre-activations: the gate input is and the value is . Compute the GLU output for three gate inputs.
- Gate input . Gate value . Output .
- Gate input . Gate value . Output .
- Gate input . Gate value . Output .
- Interpretation: the same value is passed at half strength, almost fully suppressed, or almost fully passed, depending only on the gate input. The gate decides, and the value sets the scale.
Use , , , and .
- Gate projection: .
- Value projection: .
- Gate values: and .
- Gated hidden vector: .
- Output projection: .
- Interpretation: the first hidden unit passes most of its value because its gate is open. The second unit is mostly closed, so its negative value barely counts. The final output is dominated by the first unit.
A transformer uses model width . Its usual feed-forward block has hidden width and two matrices, so the parameter count is (ignoring biases).
- Replacing it with a GLU block that has three matrices (, , ) of hidden width gives parameters.
- To keep the budget close, set . This is a common convention, used in many implementations.
- Check: . That is 512 fewer than the original, a difference of about 0.02 percent.
- Now trace one hidden unit for one word. The gate input is and the value is . Gate value . Output .
- Another hidden unit has gate input and value . Gate value . Output .
- Interpretation: at the same parameter cost, the block gives every hidden unit its own learned dimmer. The gate can take a strong negative value and keep it small, or pass a positive one.
- Try it: a GLU block has and hidden width . How many parameters does it have without biases? Answer: three matrices of size give parameters.
Common mistakes and tradeoffs
Mistake 1: "The sigmoid is the activation of the block." It is tempting to think a GLU is simply a sigmoid activation. In fact the sigmoid is applied only to the gate branch, and the value branch stays linear. So the output can be negative, and it is not confined between 0 and 1. The sigmoid only decides how much of the value to keep.
Mistake 2: "Gates are switches." It is tempting to think a gate value is either 0 or 1. In fact it is a number anywhere in , because the sigmoid is smooth. Most gates in a trained network sit between the two extremes, which is what lets the network blend signals.
Mistake 3: "A gated block is just a bigger ReLU block." It is tempting to swap the activation and keep the widths. In fact a GLU has three matrices, not two. If the width is kept, the parameter count rises by about 50 percent, and the comparison is no longer fair. Match the parameter budget, as in Example 3.
Mistake 4: "Gated means recurrent." It is tempting to think of GLU as the same idea as the gates of an LSTM or a GRU. In fact those gates decide what to keep in a memory over time. A GLU gate acts on one input at one position, with no memory carried between steps.
Mistake 5: "GLU always beats the plain block." The gain is real in the reported experiments, but it depends on the task, the training budget and the size of the model. Treat it as a strong default, and check it on your own data.
| Variant | Gate branch | Value branch | Typical use |
|---|---|---|---|
| Bilinear | (no bend) | Ablation baseline | |
| GLU | Gated convolutional language models | ||
| ReGLU | Cheap gating with ReLU | ||
| GEGLU | Transformers built with GELU | ||
| SwiGLU | Many recent large language models |
When to use it. Use a GLU block in the feed-forward part of a transformer when the model is large and you can afford the extra matrix. The quality gain has been worth the cost in the models that adopted it.
When not to use it. Avoid it in very small models or on memory-tight hardware, where the extra parameters matter more than a small quality gain. A plain feed-forward block with ReLU is simpler and often enough.
Alternatives. A plain feed-forward block with GELU is the usual baseline. A mixture of experts reaches a similar goal by routing each input to a subset of blocks.
History
Before 2016, neural language models were mostly recurrent. Researchers had found that the gates of the LSTM, introduced in 1997, helped signals and gradients survive many steps. Convolutional models were faster to train, but they struggled to match the recurrent ones on language.
In 2016 Yann Dauphin and colleagues proposed the gated linear unit inside a convolutional network for language modelling. They argued that the linear path helps the gradient flow, and reported competitive results against recurrent models.
In 2020 Noam Shazeer compared GLU with its variants inside the transformer's feed-forward block. He found that the GELU- and swish-based gates, now called GEGLU and SwiGLU, gave the best results in his experiments. These variants became common in later transformer models.
Recap
- A GLU projects the input twice, passes one projection through a sigmoid, and multiplies the two entry by entry.
- The sigmoid output lies between 0 and 1, so it acts as a learned dimmer on the value branch.
- The value branch stays linear, which helps the gradient flow.
- A GLU block has three matrices, so match the parameter budget when you compare it with a plain block.
- Variants swap the sigmoid for ReLU, GELU or swish. They are the feed-forward blocks of many large transformers.
Next, read GLU Variants Improve Transformer for the comparison, and Transformer for the block it lives in.
Check yourself
QuestionRecall: which branch of a gated linear unit passes through the sigmoid, and why does that matter?Show answer
In the standard form , the gate branch passes through the sigmoid. The sigmoid output lies between 0 and 1, so it scales the value branch without changing its sign. The value branch stays linear, which is why the output is not confined to a fixed range.
QuestionComputation: the gate input is and the value is . What is the GLU output?Show answer
The gate value is . The output is .
QuestionWhy: a colleague replaces a feed-forward ReLU block of width with a GLU block of the same width and says the model is now fairly compared. What is wrong?Show answer
The GLU block has three matrices, , and , while the ReLU block has two. At the same width the GLU block has about 50 percent more parameters, so any gain could come from the extra capacity. A fair comparison matches the parameter budget, for example by reducing the GLU width to about of .
QuestionApplied: a team trains a small model on a phone-sized memory budget and sees GLU raise accuracy by a small amount but increase memory use by half in the feed-forward blocks. What should they consider?Show answer
The gain is small and the memory cost is large. The memory budget is tight, so the extra matrix may not be worth it. They should compare a plain block at the same memory budget, and keep GLU only if the accuracy gain is worth the memory on their own data.
Sources
- Dauphin, Fan, Auli and Grangier, Language Modeling with Gated Convolutional Networks (2016): https://arxiv.org/abs/1612.08083
- Shazeer, GLU Variants Improve Transformer (2020): https://arxiv.org/abs/2002.05202