Inside a standard 7B decoder-only transformer block (multi-head attention + a GLU style MLP), which sub-layer dominates the FLOP budget at short context (say 2k tokens)? Does the answer flip at long context (say 128k tokens), and why?
MLP dominates at short context (about 2/3 of FLOPs at 2k). Attention's O(n^2) terms overtake at long context, crossover near 8-16k for typical 7B configs.
Picture two workers handling letters in a mailroom. The first worker sorts each letter by recipient, that work grows in step with how many letters there are. Ten letters means ten sorts, a hundred letters means a hundred sorts. The second worker checks every letter against every other letter to find duplicates, so a hundred letters means ten thousand checks. With only a few letters, the first worker is way busier because there is more to do per letter. Once the stack gets tall enough, the second worker takes the lead because their workload grows much faster. MLP and attention split the work the same way: MLP is the per-letter sorter, attention is the pairwise checker.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
A common interview question and a common source of confusion: which sub-layer of a transformer block, attention or MLP, eats more FLOPs? The answer depends on context length, and the reason is that attention has both O(n) and O(n^2) terms while MLP is strictly O(n).
This deep dive walks through the per-block FLOP breakdown for a standard 7B decoder, computes the split at 2k and 128k context, identifies where the crossover sits, and connects the analysis to why FlashAttention exists.
Mental model: MLP is heavy per-token. Attention is light per-token but has a quadratic term in n. Short context: per-token weight wins. Long context: n^2 wins.
Per-block FLOP decomposition
What goes into one transformer block
A decoder-only transformer block has two sub-layers, attention and MLP, each preceded by LayerNorm. The FLOP breakdown:
-
Attention sub-layer:
- Q, K, V projections: each is
(seq_len, d_model) @ (d_model, d_model), costing2 * n * d_model^2FLOPs per projection, three projections total. - Score matrix:
(seq_len, d_model_per_head) @ (d_model_per_head, seq_len)per head, total2 * n^2 * d_modelacross heads. - Softmax: O(n^2) in count but small constant; usually ignored in FLOP analysis.
- Attention output (weights @ V): another
2 * n^2 * d_model. - W_O projection:
2 * n * d_model^2.
- Q, K, V projections: each is
-
MLP sub-layer (SwiGLU): three matmuls.
- Up projection:
(n, d_model) @ (d_model, d_ff), costing2 * n * d_model * d_ff. - Gate projection: same shape, same cost.
- Down projection:
(n, d_ff) @ (d_ff, d_model), costing2 * n * d_model * d_ff. - Total:
6 * n * d_model * d_ff.
- Up projection:
Compact form
- MLP FLOPs per block:
6 * n * d_model * d_ff. - Attention FLOPs per block:
8 * n * d_model^2 + 4 * n^2 * d_model.
The MLP is strictly linear in n. Attention has a linear part (the four projections) and a quadratic part (the score matrix plus weighted-sum).
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Llama-3-8B (d_model = 4096, d_ff = 14336, 32 layers) shows MLP about 2/3 of FLOPs at 2k context, attention dominating past about 12k.
- Mistral 7B with d_ff = 14336 has a similar crossover near 12k.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the crossover shift with d_ff / d_model ratio?
Larger d_ff (e.g. 4x or higher) pushes the MLP share up, so attention takes longer to overtake. Smaller d_ff or MoE with low activation ratio pushes the crossover earlier. The relationship is roughly linear in d_ff.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Assuming attention is always the dominant cost. At short context the MLP wins comfortably; attention only overtakes once the n^2 terms grow past the linear MLP work.
60 second bullets to scan on the way to the call.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.