Feed-Forward Networks › Parameter Count Analysis
Language AI HandbookAll chapters Feed-Forward Networks › Parameter Count AnalysisOne section · Sections in this chapter6 / 13 ↓
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
Feed-forward networks are the largest component of transformer models by parameter count. In a standard transformer, the FFN contains significantly more parameters than the attention mechanism. This matters for model scaling and efficiency optimization, and it helps explain why so many recent research directions (mixture of experts, weight sharing, factorization) target the FFN specifically.
For a single FFN layer, we count the total number of learnable parameters by summing the sizes of all weight matrices and bias vectors. The calculation is straightforward. Matrix has shape , contributing parameters. Matrix has shape , contributing an equal parameters. The two bias vectors contribute and parameters respectively. Adding these together:
where:
- : the number of elements in weight matrix (rows columns)
- : the number of elements in weight matrix (rows columns, equal to the count in )
- : the number of elements in bias vector
- : the number of elements in bias vector
- The factor of 2 in front of accounts for both weight matrices and having the same total element count
With the standard expansion factor , we can simplify this expression. Substituting :
For large models where is in the hundreds or thousands, the quadratic term dominates and the linear bias terms become negligible, giving approximately parameters per FFN layer.
Why does this formula make sense? Notice that the parameter count grows quadratically with : if you double the model dimension, you quadruple the FFN parameter count. This is the same quadratic scaling that makes attention expensive with respect to sequence length, but here the quadratic factor is in the model dimension rather than sequence length. Doubling from 512 to 1024 increases FFN parameters from roughly 2 million to 8 million per layer, a 4x increase for a 2x dimension increase.
def count_ffn_params(d_model, d_ff, include_bias=True):
"""Count parameters in a feed-forward network."""
weight_params = 2 * d_model * d_ff # W1 and W2
bias_params = d_ff + d_model if include_bias else 0
return weight_params + bias_params
def count_attention_params(d_model, num_heads, include_bias=True):
"""Count parameters in multi-head attention."""
# Q, K, V projections and output projection
weight_params = 4 * d_model * d_model # W_Q, W_K, W_V, W_O
bias_params = 4 * d_model if include_bias else 0
return weight_params + bias_params
# Compare for different model sizes
model_configs = [
("GPT-2 Small", 768, 3072, 12),
("GPT-2 Medium", 1024, 4096, 16),
("BERT-Base", 768, 3072, 12),
("GPT-3 (175B)", 12288, 49152, 96),
]Parameter count comparison: FFN vs Attention (per layer) Model d_model d_ff FFN Params Attn Params FFN/Attn -------------------------------------------------------------------------------- GPT-2 Small 768 3072 4,718,592 2,359,296 2.0x GPT-2 Medium 1024 4096 8,388,608 4,194,304 2.0x BERT-Base 768 3072 4,718,592 2,359,296 2.0x GPT-3 (175B) 12288 49152 1,207,959,552 603,979,776 2.0x
The FFN consistently contains about twice as many parameters as the attention mechanism per layer. This 2:1 ratio emerges from a straightforward calculation: attention uses four weight matrices of size for QKV and output projections, totaling parameters. The FFN uses two matrices with total size parameters. The ratio is exactly , so the FFN always has twice the weight parameters of attention when the 4x expansion factor is used.
For large models like GPT-3, each transformer layer has over 2.4 billion parameters in the FFN alone. Multiplied across 96 layers, the FFN accounts for the vast majority of the model's 175 billion total parameters. This makes FFN optimization critical for model efficiency, both in terms of memory and computation.
Let's visualize how FFN parameters scale with model dimension:

Let's also visualize the parameter distribution in a transformer block to see the FFN's dominance at a glance:
