In a 7B transformer, do attention or MLP layers eat more FLOPs, and does context length change the answer?
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.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
6-8 min: per-block FLOP breakdown + concrete numbers at 2k and 128k for 7B + crossover length analysis + FlashAttention as memory optimization + MoE wrinkle + decode-phase bandwidth story.
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.
- Llama 4 Maverick MoE shifts the crossover earlier because the activated experts reduce effective per-token MLP cost.
- DeepSeek V4 with MLA (Multi-head Latent Attention) compresses the KV side so the n^2 attention term has a smaller effective constant.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the crossover shift with d_ff / d_model ratio?
QWhy does FlashAttention not change the FLOP count?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.