Architecture Hyperparameters › Feed-Forward Network Dimensions
Language AI HandbookAll chapters Architecture Hyperparameters › Feed-Forward Network DimensionsOne section · Sections in this chapter5 / 10 ↓
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
While attention layers decide which tokens to combine, the feed-forward network (FFN) performs the actual transformation of each token's representation. This division of labor has a surprising consequence: the FFN contains far more parameters than attention, making it the dominant factor in model size.
Understanding why requires examining what each component does. Attention computes weighted averages of value vectors, a fundamentally linear operation (the only nonlinearity is softmax for computing weights). The FFN, by contrast, applies a nonlinear transformation that can reshape the representation space, activate or suppress features, and encode factual associations. Research has shown that specific FFN neurons activate for particular concepts: "Eiffel Tower," "Python programming," or "past tense verbs." The FFN is where the model stores much of its learned knowledge.
This knowledge-storage role explains why FFNs need large capacity. The original transformer paper established the convention . This provides the FFN with four times more hidden dimensions than the model's representation size. This 4x expansion gives each layer a wide intermediate space for computation before projecting back to dimensions.
The Standard Expansion Ratio
Let's derive exactly how this expansion affects parameter counts and understand why FFNs dominate model size.
For a standard two-layer FFN, the computation applies an up-projection and activation before the down-projection:
where:
- : the input vector (one token's representation)
- : the up-projection weight matrix
- : the down-projection weight matrix
- , : bias vectors
- : the Gaussian Error Linear Unit activation function
The total parameter count includes both weight matrices and bias vectors:
The first term accounts for the two weight matrices (each with parameters), while the remaining terms count the bias vectors. With the standard expansion ratio and ignoring the relatively small bias terms:
Compare this to attention parameters for standard multi-head attention (MHA), which has four projection matrices (QKV and output), each of size :
The FFN has twice as many parameters as attention. This 2:1 ratio is consistent across most transformer architectures and explains why FFN optimization is so important for efficiency.

The visualization confirms that regardless of architecture choice, FFN parameters dominate. Even with SwiGLU's reduced expansion ratio, the FFN still accounts for roughly twice the parameters of attention. This consistent 2:1 ratio has practical consequences for optimization techniques like mixture-of-experts, which specifically target FFN layers.
SwiGLU and Adjusted Ratios
The standard FFN applies a simple pattern: project up, apply nonlinearity, project down. But research has shown that adding a gating mechanism can improve performance significantly. The gating idea comes from LSTMs and GRUs: instead of passing all information through uniformly, learn which dimensions to emphasize and which to suppress.
SwiGLU (SiLU-Gated Linear Unit) implements this by computing two parallel projections of the input. One projection becomes the "content" that might pass through. The other projection, after applying the SiLU activation, becomes the "gate" that controls how much of each dimension passes. The element-wise product of content and gate creates a selective filtering effect.
This gating mechanism requires three weight matrices instead of two:
where:
- : the input vector
- : the gate projection matrix
- : the up-projection matrix
- : the down-projection matrix
- : the Sigmoid Linear Unit activation (also called Swish)
- : element-wise multiplication
With three matrices each containing parameters, the total count is:
To maintain the same parameter count as a standard FFN with 4x expansion, we can solve for the required SwiGLU expansion ratio. Setting the SwiGLU parameter count equal to the standard FFN count:
Dividing both sides by gives the required FFN dimension:
In practice, is often rounded to a multiple of 256 for efficient GPU computation. LLaMA uses (specifically 11008 for ), which is close to the theoretical value of .
def ffn_parameters(d_model, d_ff, ffn_type="standard"):
"""
Calculate FFN parameters for different architectures.
Args:
d_model: Model dimension
d_ff: Feed-forward hidden dimension
ffn_type: "standard" (2 matrices) or "swiglu" (3 matrices)
"""
if ffn_type == "standard":
# W1: d_model -> d_ff, W2: d_ff -> d_model
return 2 * d_model * d_ff
else: # SwiGLU
# gate, up, down: each d_model <-> d_ff
return 3 * d_model * d_ff
# Compare FFN configurations
d_model = 4096
configs = {
"Standard (4x)": {"d_ff": 4 * d_model, "type": "standard"},
"SwiGLU (2.67x)": {"d_ff": int(8 / 3 * d_model), "type": "swiglu"},
"SwiGLU (LLaMA)": {"d_ff": 11008, "type": "swiglu"}, # Actual LLaMA config
}
ffn_params = {
name: ffn_parameters(d_model, cfg["d_ff"], cfg["type"])
for name, cfg in configs.items()
}FFN parameter comparison (d_model = 4096): Configuration d_ff Ratio Parameters ------------------------------------------------------- Standard (4x) 16384 4.00x 134.22M SwiGLU (2.67x) 10922 2.67x 134.21M SwiGLU (LLaMA) 11008 2.69x 135.27M
The three configurations have nearly identical parameter counts. This shows that SwiGLU's third matrix is compensated by the smaller expansion ratio. This equivalence is intentional: it allows fair comparisons between architectures and predictable parameter budgets.