Gated Linear Units › GLU in the Feed-Forward Network
Language AI HandbookAll chapters Gated Linear Units › GLU in the Feed-Forward NetworkOne section · Sections in this chapter4 / 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
With the GLU mechanism understood, our next step is integrating it into the transformer's feed-forward network. This requires adapting the standard two-layer FFN architecture, and understanding this integration reveals why GLU is such a natural fit for transformers.
Recalling the Standard FFN
The standard FFN applies a simple pattern: expand the dimension, apply nonlinearity, contract back:
where is the input, and are weight matrices, and are bias vectors, and is a nonlinear activation function (e.g., ReLU or GELU).
The key insight is that the nonlinearity is applied after the expansion. This creates a high-dimensional feature space where the nonlinearity can carve out complex decision boundaries, before projecting back to the original dimension.
Replacing Activation with Gating
GLU replaces the simple activation function with something more sophisticated: a learned gating mechanism. Instead of uniformly transforming all features with the same nonlinearity, GLU lets the network selectively amplify or suppress features based on the input itself:
Let's trace through what each component does in this context:
- Expansion to Hidden Space: Both and project the input from to the larger hidden dimension
- Gated Combination: The sigmoid-gated multiplication produces a -dimensional hidden representation
- Contraction Back: projects back to for the residual connection
The variables are:
- : input from attention/normalization
- : the two projection matrices for value and gate
- : corresponding bias vectors
- : output projection back to model dimension
- : output bias
The Parameter Cost
Notice that the GLU FFN requires three weight matrices (, , and ) instead of the standard FFN's two ( and ). This means GLU-based FFNs have 50% more parameters for the same hidden dimension. We'll analyze this trade-off in detail later and see how modern models address it.
def ffn_glu(x, W, b, V, c, W2, b2):
"""
Feed-forward network with GLU activation.
Args:
x: Input, shape (..., d_model)
W, b: Value projection (d_model -> d_ff)
V, c: Gate projection (d_model -> d_ff)
W2, b2: Output projection (d_ff -> d_model)
Returns:
Output, shape (..., d_model)
"""
# GLU combines value and gate
value = x @ W + b
gate = sigmoid(x @ V + c)
hidden = value * gate
# Project back to model dimension
output = hidden @ W2 + b2
return output
# FFN dimensions
d_model_ffn = 512
d_ff = 2048
# Initialize all weights
W_ffn = np.random.randn(d_model_ffn, d_ff) * np.sqrt(2.0 / (d_model_ffn + d_ff))
b_ffn = np.zeros(d_ff)
V_ffn = np.random.randn(d_model_ffn, d_ff) * np.sqrt(2.0 / (d_model_ffn + d_ff))
c_ffn = np.zeros(d_ff)
W2_ffn = np.random.randn(d_ff, d_model_ffn) * np.sqrt(
2.0 / (d_ff + d_model_ffn)
)
b2_ffn = np.zeros(d_model_ffn)
# Process a sequence
seq_len = 16
X = np.random.randn(seq_len, d_model_ffn)
Y = ffn_glu(X, W_ffn, b_ffn, V_ffn, c_ffn, W2_ffn, b2_ffn)GLU FFN example: d_model: 512 d_ff: 2048 Sequence length: 16 Input shape: (16, 512) Output shape: (16, 512)
The output shape matches the input shape, confirming that the GLU FFN correctly projects back to the model dimension. This dimensional consistency is essential for the residual connection that adds the FFN output back to its input in a transformer block.