Feed-Forward Networks › Computational Cost
Language AI HandbookAll chapters Feed-Forward Networks › Computational CostOne section · Sections in this chapter7 / 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
Beyond parameter count, we need to consider computational cost, measured in floating-point operations (FLOPs). Understanding FFN compute requirements helps explain why these layers dominate inference time for short sequences, and why long-context models face a different bottleneck.
The FFN's computational cost per token follows directly from its structure. For each token, the FFN performs two matrix-vector multiplications. Each multiply-add operation consists of one multiplication and one addition, counting as 2 floating-point operations. Working through the arithmetic:
The first layer computes where is a -dimensional vector and is . This requires multiply-adds, so FLOPs. The second layer computes where is -dimensional and is , requiring another FLOPs. The total FLOPs per token for the FFN is therefore:
where:
- : the input and output dimension of the FFN
- : the hidden dimension (typically )
- The factor of 4 comes from: 2 layers 2 FLOPs per multiply-add operation
For a sequence of tokens, each token is processed independently, so the total FFN cost scales linearly with sequence length. Because the FFN has no interaction between positions, the computation at each position is identical and independent:
where is the number of tokens in the sequence. This is scaling: double the sequence length, double the FLOPs.
This linear scaling contrasts sharply with attention, which has quadratic complexity due to the attention matrix computation. For every pair of positions, attention must compute a query-key dot product. With positions, there are such pairs. As sequences grow longer, attention's quadratic term eventually dominates.
The crossover point depends on model dimensions. For a model with and , let's compute when attention FLOPs equal FFN FLOPs. FFN FLOPs scale as . Attention's quadratic term (for the attention matrix alone) scales as . Setting these equal: , giving tokens. At sequence lengths below 3000, FFN dominates. Above 3000, attention takes over.
def ffn_flops(n, d_model, d_ff):
"""Calculate FFN FLOPs for sequence length n."""
return 4 * n * d_model * d_ff
def attention_flops(n, d_model):
"""Calculate attention FLOPs (simplified)."""
# Q, K, V projections: 3 * 2 * n * d_model^2
# QK^T: 2 * n^2 * d_model
# Attention @ V: 2 * n^2 * d_model
# Output projection: 2 * n * d_model^2
projection_flops = 4 * 2 * n * d_model * d_model
attention_matrix_flops = 4 * n * n * d_model
return projection_flops + attention_matrix_flops
# Compare across sequence lengths
sequence_lengths = [128, 512, 1024, 2048, 4096, 8192]
d_model_test = 768
d_ff_test = 3072FLOPs comparison: FFN vs Attention (GPT-2 Small)
Seq Length FFN FLOPs Attn FLOPs FFN/Attn
------------------------------------------------------------
128 1,207,959,552 654,311,424 1.85x
512 4,831,838,208 3,221,225,472 1.50x
1024 9,663,676,416 8,053,063,680 1.20x
2048 19,327,352,832 22,548,578,304 0.86x
4096 38,654,705,664 70,866,960,384 0.55x
8192 77,309,411,328 244,813,135,872 0.32xFor short sequences (128-512 tokens), the FFN contributes over twice the FLOPs of attention. As sequences grow to 4096 or 8192 tokens, attention's quadratic cost catches up and eventually dominates. The crossover point around 1024-2048 tokens is where attention and FFN have comparable costs for this model configuration.

This crossover point has important practical implications. Short-context applications like question answering and classification, as well as code completion, typically operate with sequences well below the crossover point, meaning the FFN is the primary compute bottleneck. Research into FFN efficiency (quantization, pruning, low-rank approximation) offers the greatest gains here. Long-context applications like document summarization, long-form generation, and retrieval-augmented generation operate above the crossover, making attention efficiency (sparse attention, linear attention, sliding window attention) the priority.