Gated Linear Units › The GLU Formulation

Michael BrenndoerferUpdated June 14, 20256 min read
Language AI HandbookAll chapters
Part 15 of 67Transformer Blocks
Chapter 109Gated Linear Units › The GLU Formulation
Reading section 3 of 16 · The GLU Formulation · 6 minSection 3 of 16 · 6 min

Book contents

Section 3 of 16 · 6 min read

Gated Linear Units › The GLU FormulationOne section · Sections in this chapter3 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Now that we understand what gating does, let's formalize how it works mathematically. The Gated Linear Unit was introduced by Dauphin et al. in 2017 for language modeling, and its elegance lies in a simple but powerful insight: instead of applying an activation function to transform features, let the network learn which features to keep and which to suppress.

The Core Idea: Two Pathways, One Output

Standard neural network layers compute a single transformation of their input and apply an activation function. GLU takes a fundamentally different approach: it computes two separate linear projections of the same input and combines them through multiplication. One projection produces the "values" we want to transmit, while the other produces "gates" that control how much of each value passes through.

Think of it like a mixing console in a recording studio. Each channel has both a signal (the audio content) and a fader (the volume control). The final output for each channel is the signal multiplied by the fader position. The GLU works the same way, but learns both the signals and the fader positions from data.

The Mathematical Formulation

The original GLU formula captures this two-pathway structure:

GLU(x)=(xW+b)⊗σ(xV+c)\text{GLU}(x) = (xW + b) \otimes \sigma(xV + c)

Let's unpack each component to understand what it contributes:

The Value Pathway: (xW+b)(xW + b)

The first term computes a linear transformation of the input. This is the "content" that we might want to pass through to the next layer. Without the gate, this would just be a standard linear layer:

  • x∈Rdmodelx \in \mathbb{R}^{d_{\text{model}}}: the input vector (a token's representation)
  • W∈Rdmodel×dW \in \mathbb{R}^{d_{\text{model}} \times d}: the "value" projection weights that transform the input
  • b∈Rdb \in \mathbb{R}^{d}: the value projection bias

The Gate Pathway: σ(xV+c)\sigma(xV + c)

The second term computes another linear transformation of the same input, but passes it through the sigmoid function to produce gate values:

  • V∈Rdmodel×dV \in \mathbb{R}^{d_{\text{model}} \times d}: the "gate" projection weights (separate from WW)
  • c∈Rdc \in \mathbb{R}^{d}: the gate projection bias
  • σ\sigma: the sigmoid activation function, which squashes values to the range (0,1)(0, 1)
  • dd: the output dimension of each projection

The Combination: ⊗\otimes (Element-wise Multiplication)

The ⊗\otimes symbol denotes the Hadamard product, which simply multiplies corresponding elements. If the value pathway produces [v1,v2,v3][v_1, v_2, v_3] and the gate pathway produces [g1,g2,g3][g_1, g_2, g_3], the output is [v1⋅g1,v2⋅g2,v3⋅g3][v_1 \cdot g_1, v_2 \cdot g_2, v_3 \cdot g_3].

Why Sigmoid for Gating?

The sigmoid function produces values between 0 and 1, making it a natural choice for gating:

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

where zz is the input value (applied element-wise when the input is a vector). The sigmoid's behavior at extreme values is what makes it effective as a gate:

  • As z→−∞z \to -\infty, σ(z)→0\sigma(z) \to 0 (gate closes completely)
  • As z→+∞z \to +\infty, σ(z)→1\sigma(z) \to 1 (gate opens fully)
  • At z=0z = 0, σ(z)=0.5\sigma(z) = 0.5 (gate is half-open)

When σ(xV+c)\sigma(xV + c) is close to 0, the corresponding element of xW+bxW + b is blocked, meaning that dimension contributes nothing to the output. When it's close to 1, the element passes through unchanged. Values between 0 and 1 allow partial transmission, giving the network fine-grained control over information flow.

Out[5]:
Visualization
2D heatmap showing GLU output surface as function of value and gate inputs. This shows how gating controls information flow.
The GLU output as a function of value and gate inputs. The horizontal axis shows the value pathway (xW), the vertical axis shows the gate input (before sigmoid), and the color shows the output. When gate input is negative (bottom), output is suppressed regardless of value. When gate input is positive (top), the output closely follows the value.

From Theory to Code

Let's implement the basic GLU and see these components in action:

In[6]:
Code
def glu(x, W, b, V, c):
    """
    Gated Linear Unit.

    Args:
        x: Input vector, shape (d_model,) or (batch, d_model)
        W: Value projection weights, shape (d_model, d)
        b: Value projection bias, shape (d,)
        V: Gate projection weights, shape (d_model, d)
        c: Gate projection bias, shape (d,)

    Returns:
        Output vector, shape (d,) or (batch, d)
    """
    value = x @ W + b
    gate = sigmoid(x @ V + c)
    return value * gate


# Example dimensions
d_model = 512
d_output = 256

# Initialize weights
W = np.random.randn(d_model, d_output) * np.sqrt(2.0 / (d_model + d_output))
b = np.zeros(d_output)
V = np.random.randn(d_model, d_output) * np.sqrt(2.0 / (d_model + d_output))
c = np.zeros(d_output)

# Test with a single input
x_test = np.random.randn(d_model)
y_glu = glu(x_test, W, b, V, c)
Out[7]:
Console
GLU basic example:
  Input dimension:  512
  Output dimension: 256
  Input shape:      (512,)
  Output shape:     (256,)
  Output range:     [-1.7685, 2.3423]

The output range shows that GLU produces both positive and negative values, unlike ReLU which only produces non-negative outputs. This is because the sigmoid gate modulates but doesn't constrain the sign of the value pathway.

The Dimension Trade-off

Notice something important about GLU's output dimension. If we project from dmodeld_{\text{model}} to dd, we get dd outputs, not 2d2d. This is because we use two projections (value and gate), each producing dd dimensions, and multiply them together element-wise to get dd outputs. The multiplication combines the two pathways rather than concatenating them.

This has clear implications for parameter efficiency that we'll explore shortly. First, let's see how GLU fits into the larger transformer architecture.