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.

Runs on a CPU or in the browser~30 min

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):

LL layersddheadsKV headsdhd_hdffd_{ff}vocab
324,096323212811,00832,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 MM tokens (the batch size in decode, the prompt length in prefill), per layer:

OpFLOPsBytes (BF16)
QKV projection2M⋅d⋅3d2M \cdot d \cdot 3d2 (3d2+Md+3Md)2\,(3d^2 + Md + 3Md)
Output projection2Md22M d^22 (d2+2Md)2\,(d^2 + 2Md)
MLP gate + up2M⋅d⋅2dff2M \cdot d \cdot 2d_{ff}2 (2d dff+Md+2Mdff)2\,(2 d\, d_{ff} + Md + 2M d_{ff})
MLP down2M⋅dff⋅d2M \cdot d_{ff} \cdot d2 (dff d+Mdff+Md)2\,(d_{ff}\, d + M d_{ff} + Md)
Attention, decode, context nnb⋅4ndb \cdot 4 n db⋅2⋅2ndb \cdot 2 \cdot 2 n d (read K and V)

Then multiply by LL and add the LM head, 2bdV2 b d V FLOPs over 2dV2dV bytes of weights. Each op’s roofline time is max⁡(F/P,  Q/B)\max(F / P,\; Q / B), and the step’s time is the sum.

labs/m03/3.5/derive_ai_7b.py
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 summed
Terminal window
python labs/m03/3.5/derive_ai_7b.py

Scenario 1: one user, decode

output
DECODE batch 1, context 1024
op GFLOP GB moved FLOP/B time (us) bound
QKV proj 3.2 3.22 1.0 962 memory
O proj 1.1 1.07 1.0 321 memory
MLP gate+up 5.8 5.77 1.0 1723 memory
MLP down 2.9 2.89 1.0 862 memory
attention 0.5 0.54 1.0 160 memory
LM head 0.3 0.26 1.0 78 memory
total 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

output
DECODE batch 64, context 1024
op GFLOP GB moved FLOP/B time (us) bound
QKV proj 206.2 3.29 62.7 982 memory
O proj 68.7 1.11 62.1 331 memory
MLP gate+up 369.4 5.88 62.8 1755 memory
MLP down 184.7 2.95 62.7 880 memory
attention 34.4 34.39 1.0 10267 memory
LM head 16.8 0.27 62.9 80 memory
total 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:

  1. 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.
  2. 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.
  3. 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

output
PREFILL one 2048-token prompt
op GFLOP GB moved FLOP/B time (us) bound
QKV proj 6597.1 5.37 1228.8 6670 compute
O proj 2199.0 2.15 1024.0 2223 compute
MLP gate+up 11819.7 9.19 1285.6 11951 compute
MLP down 5909.9 4.87 1214.7 5976 compute
attention 1099.5 2.15 512.0 1112 compute
LM head 0.3 0.26 1.0 78 memory
total 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 answered
  1. 1.At batch 1, what is the arithmetic intensity of every weight matmul in BF16?

  2. 2.In the batch-64 decode scenario for Llama-2-7B, which operation dominates step time?

  3. 3.Why does the LM head stay memory-bound even during prefill?

  4. 4.Why are the lab's times lower bounds, not predictions?