Sparse Models › Why Replace the Feed-Forward Network?
Language AI HandbookAll chapters Sparse Models › Why Replace the Feed-Forward Network?One 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
You might wonder why sparse architectures typically replace the FFN rather than the attention mechanism. As we explored in Part XIII, attention's computational cost comes from the quadratic relationship between sequence length and compute. But there's another consideration: the role each component plays in the model. Understanding these distinct roles helps explain why the FFN is the natural candidate for sparsification.
Attention mechanisms handle information routing, determining which tokens should attend to which other tokens. This function is inherently about relationships within the input and benefits from seeing all tokens simultaneously. Making attention sparse (as in Longformer and BigBird from Part XVII) requires careful design to ensure important relationships aren't missed. The attention mechanism needs to maintain a global view of the sequence to effectively aggregate information across positions.
Feed-forward networks, by contrast, operate on each token position independently. They're often interpreted as key-value memories that store factual knowledge and learned transformations. This independence is important: since each token's FFN computation doesn't depend on other tokens in the sequence, we can freely route different tokens to different experts without disrupting information flow. This makes them natural candidates for specialization: different experts can store different types of knowledge without interfering with the information routing that attention gives.
The FFN also dominates the parameter count in typical transformers. In a standard architecture with , the FFN contains roughly two-thirds of each layer's parameters. This makes the FFN the natural target for sparsification. Converting it to a mixture of experts yields the largest efficiency gains. To see why the FFN dominates, let's calculate the parameter counts step by step, starting with the FFN:
For comparison, the attention mechanism contains four projection matrices (Q, K, V, and output projection), each with dimension . These projections transform the input into queries, keys, and values for the attention computation, then project the attention output back to the model dimension. This gives:
where:
- : the number of projection matrices in multi-head attention (query, key, value, and output projections)
- : the dimensions of each projection matrix, mapping from the model dimension to itself
To compute the FFN's share of total layer parameters, we divide FFN parameters by the sum of FFN and attention parameters. This ratio tells us what fraction of each layer's capacity resides in the feed-forward network:
This means the FFN accounts for approximately 67% of each layer's parameters. This two-thirds dominance makes the FFN the natural target for sparsification: converting it to a mixture of experts allows us to scale the parameter count (by adding more experts) while keeping computational cost proportional to the number of active experts rather than total experts. Since the FFN already holds most of the parameters, this is where we get the biggest capacity gains from sparse computation. If we were to sparsify the attention mechanism instead, we would affect only one-third of the layer's parameters, yielding proportionally smaller efficiency gains.
def analyze_layer_parameters(
d_model: int = 4096, n_heads: int = 32, d_ff: int = 16384
):
"""Break down parameter distribution in a transformer layer."""
d_head = d_model // n_heads
# Self-attention parameters: Q, K, V projections and output projection
attention_params = 4 * d_model * d_model # 4 matrices of d_model × d_model
# FFN parameters: two matrices d_model by d_ff and d_ff by d_model
ffn_params = 2 * d_model * d_ff
# Layer norms: small contribution, included for completeness
norm_params = 4 * d_model # 2 layer norms with scale and bias
total = attention_params + ffn_params + norm_params
return {
"attention": attention_params,
"ffn": ffn_params,
"norms": norm_params,
"total": total,
"ffn_fraction": ffn_params / total,
}Attention: 67,108,864 params (33.3%) FFN: 134,217,728 params (66.7%) Total: 201,342,976 params FFN accounts for 66.7% of layer parameters

The FFN's dominance at approximately two-thirds of layer parameters makes it the natural target for sparsification. In typical transformer configurations, the FFN contains roughly twice as many parameters as the attention mechanism. This 2:1 ratio means that converting the FFN to a mixture of experts yields the largest efficiency gains. When we add more experts to the FFN, we scale the parameter count materially while keeping computational cost proportional to the number of active experts. Since the FFN already holds most parameters, this is where sparse computation gives the biggest capacity increase per unit of additional compute. Replacing the dense FFN with a sparse mixture of experts while keeping attention dense preserves the model's ability to route information effectively while sharply increasing its knowledge capacity.