LLaMA Components › The SwiGLU Feed-Forward Network
Language AI HandbookAll chapters LLaMA Components › The SwiGLU Feed-Forward NetworkOne section · Sections in this chapter3 / 12 ↓
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
The feed-forward network (FFN) in each transformer block provides the model's primary source of nonlinearity and parameter capacity. While attention handles communication between tokens, the FFN handles computation within each token's representation. This is where the model stores and applies learned knowledge, from factual information to linguistic patterns.
LLaMA uses SwiGLU, a gated variant that has become the standard in modern language models. To understand why SwiGLU works better than simpler alternatives, we'll trace the evolution from basic FFNs to gated architectures, seeing how each improvement addresses specific limitations.
From Standard FFN to Gated Units
The original transformer FFN follows a simple recipe: project each token to a higher dimension, apply a nonlinearity, then project back. This "expand and contract" pattern allows the network to compute complex functions of the input through the nonlinearity in the high-dimensional space:
where:
- : input token representation with dimension
- : first projection, expanding to hidden dimension (typically )
- : second projection, compressing back to model dimension
- : rectified linear unit activation
This architecture works, but it has a limitation: every hidden dimension is processed identically by the ReLU. The network cannot selectively emphasize some features while suppressing others based on the input content.
Gated linear units (GLUs) address this limitation by introducing a learned control mechanism. Instead of applying a fixed activation to all hidden dimensions, a GLU learns which dimensions to activate and which to suppress. It does this by splitting the hidden layer into two parallel paths: one computes a "gate" signal that controls information flow, while the other computes the actual content. These paths are multiplied element-wise:
where:
- : input token representation
- : linear path projection
- : gate path projection
- : output projection
- : sigmoid activation function
- : element-wise (Hadamard) multiplication
The sigmoid-gated path acts as a learned filter, controlling how much of the linear path's information passes through. When is near zero for some hidden dimension, that dimension's contribution is blocked; when near one, it passes through fully.
SwiGLU: Swish-Gated Linear Units
While the sigmoid-gated GLU works well, researchers found an even better gating function: Swish (also called SiLU). The Swish function has an elegant property: it is "self-gating," meaning the input gates itself. Rather than having a separate sigmoid control the gate, Swish multiplies the input by its own sigmoid, creating a smooth, adaptive activation.
SwiGLU applies this insight to the GLU architecture:
where:
- : input token representation with model dimension
- : gate projection matrix
- : up projection matrix (the "linear path")
- : down projection, mapping back to model dimension
- : element-wise multiplication between gate and up projections
The Swish activation function is defined as:
where is the sigmoid function. Swish is smooth everywhere (unlike ReLU's sharp corner at zero) and self-gated: the input is multiplied by its own sigmoid, meaning large positive values pass through nearly unchanged while negative values are attenuated. This combination has been shown to improve training dynamics and final model quality compared to both ReLU and GELU-based FFNs.


SwiGLU requires three weight matrices (, , ) instead of two (, ). To keep the total parameter count similar, LLaMA reduces the hidden dimension from to approximately . The exact value is often rounded to be divisible by hardware-friendly numbers.

With the theory in place, let's implement SwiGLU and see how the gating mechanism works in practice. We'll first define the Swish activation, then build the complete SwiGLU module:
def swish(x):
"""Swish (SiLU) activation: x * sigmoid(x)"""
return x / (1 + np.exp(-x))
class SwiGLU:
"""
SwiGLU feed-forward network as used in LLaMA.
Implements: FFN(x) = (Swish(x @ W_gate) * (x @ W_up)) @ W_down
"""
def __init__(self, d_model, d_ff):
"""
Args:
d_model: Model dimension
d_ff: Hidden dimension (typically ~2.67 * d_model for SwiGLU)
"""
self.d_model = d_model
self.d_ff = d_ff
# Xavier/Glorot initialization
scale_up = np.sqrt(2.0 / (d_model + d_ff))
scale_down = np.sqrt(2.0 / (d_ff + d_model))
self.W_gate = np.random.randn(d_model, d_ff) * scale_up
self.W_up = np.random.randn(d_model, d_ff) * scale_up
self.W_down = np.random.randn(d_ff, d_model) * scale_down
def __call__(self, x):
"""Apply SwiGLU transformation."""
gate = swish(x @ self.W_gate) # Gating signal
up = x @ self.W_up # Linear projection
hidden = gate * up # Element-wise gating
return hidden @ self.W_down # Project back to d_model

The left plot shows why Swish is preferred over ReLU. Unlike ReLU's hard cutoff at zero, Swish has a smooth curve with non-zero gradients for negative inputs. This helps gradient flow during training, particularly for inputs that happen to fall in negative regions.
The right plot illustrates gating. When the gate input is negative, the gate output is near zero, blocking information. As the gate input increases, more of the up projection passes through. This learned gating allows the network to selectively process different aspects of the input.