L1 Beginner · Module 3: The performance mental model
3.5Lab: derive the arithmetic intensity of a 7B model
Put the whole module to work on one model. Count every FLOP and byte in a Llama-2-7B forward step, place each operation on an H100 roofline, and predict decode and prefill speed before touching a GPU.
By the end you will be able to
- ✓Count FLOPs and HBM bytes for every operation in a transformer step
- ✓Compute each operation's arithmetic intensity and roofline time
- ✓Predict single-user decode speed, batched throughput and prefill time
- ✓Find which operation dominates each scenario, and what would change it
This is the deliverable of the first week of the time-to-first-token course, done in code: a full accounting of one forward step of a 7B model. Do the first table by hand before running the lab, then use the script to check yourself.
The model
Llama-2-7B, BF16 (2 bytes per value):
| layers | heads | KV heads | vocab | |||
|---|---|---|---|---|---|---|
| 32 | 4,096 | 32 | 32 | 128 | 11,008 | 32,000 |
Target hardware: H100 SXM, 989 TFLOP/s and 3.35 TB/s, so the ridge is 295 FLOP/byte (3.3).
The accounting
For a step that processes tokens (the batch size in decode, the prompt length in prefill), per layer:
| Op | FLOPs | Bytes (BF16) |
|---|---|---|
| QKV projection | ||
| Output projection | ||
| MLP gate + up | ||
| MLP down | ||
| Attention, decode, context | (read K and V) |
Then multiply by and add the LM head, FLOPs over bytes of weights. Each op’s roofline time is , and the step’s time is the sum.
def matmul(m, k, n): return 2 * m * k * n, B * (m * k + k * n + m * n) # FLOPs, bytes
ops = { "QKV proj": matmul(M, D, D + 2 * kvd), "O proj": matmul(M, D, D), "MLP gate+up": matmul(M, D, 2 * FF), "MLP down": matmul(M, FF, D),}t = max(flops / PEAK, bytes / BW) # per op, then summedpython labs/m03/3.5/derive_ai_7b.pyScenario 1: one user, decode
DECODE batch 1, context 1024op GFLOP GB moved FLOP/B time (us) boundQKV proj 3.2 3.22 1.0 962 memoryO proj 1.1 1.07 1.0 321 memoryMLP gate+up 5.8 5.77 1.0 1723 memoryMLP down 2.9 2.89 1.0 862 memoryattention 0.5 0.54 1.0 160 memoryLM head 0.3 0.26 1.0 78 memorytotal 13.8 13.76 1.0 4106-> step 4.11 ms at the roofline; 244 tokens/s (per-user 244 tok/s)Every operation has intensity 1.0: every byte loaded is used for exactly one multiply-add. The step is simply “read 13.8 GB at 3.35 TB/s”. The tensor cores are busy for about 0.3% of it. The MLP accounts for 63% of the time because it holds most of the weights (1.2).
Scenario 2: 64 users, decode
DECODE batch 64, context 1024op GFLOP GB moved FLOP/B time (us) boundQKV proj 206.2 3.29 62.7 982 memoryO proj 68.7 1.11 62.1 331 memoryMLP gate+up 369.4 5.88 62.8 1755 memoryMLP down 184.7 2.95 62.7 880 memoryattention 34.4 34.39 1.0 10267 memoryLM head 16.8 0.27 62.9 80 memorytotal 880.1 47.88 18.4 14293-> step 14.29 ms at the roofline; 4,478 tokens/s (per-user 70 tok/s)Three things happened:
- The weight matmuls went from intensity 1 to about 63 and take about the same time as before: 64 times the work for free. They are still memory-bound, since the ridge is 295.
- Attention became 72% of the step. Each of the 64 users brings 0.5 GB of KV cache, and attention’s intensity stays at 1.
- Throughput rose 18×, from 244 to 4,478 tokens/s, while each user’s speed fell from 244 to 70 tokens/s.
Scenario 3: prefill
PREFILL one 2048-token promptop GFLOP GB moved FLOP/B time (us) boundQKV proj 6597.1 5.37 1228.8 6670 computeO proj 2199.0 2.15 1024.0 2223 computeMLP gate+up 11819.7 9.19 1285.6 11951 computeMLP down 5909.9 4.87 1214.7 5976 computeattention 1099.5 2.15 512.0 1112 computeLM head 0.3 0.26 1.0 78 memorytotal 27625.5 23.99 1151.8 28011-> step 28.01 ms at the roofline; 73,115 tokens/s (prefill)Now everything except the LM head (which only runs for the last position) is far above the ridge. Prefill processes 73K tokens/s, about 300 times the single-user decode rate, with the same weights and the same GPU. TTFT for this prompt is at least 28 ms.
Exercises
Sources
Check your understanding
0 / 4 answered1.At batch 1, what is the arithmetic intensity of every weight matmul in BF16?
2.In the batch-64 decode scenario for Llama-2-7B, which operation dominates step time?
3.Why does the LM head stay memory-bound even during prefill?
4.Why are the lab's times lower bounds, not predictions?