L1 Beginner · Module 3: The performance mental model

3.3The roofline model

One picture, two numbers. The roofline model tells you whether a kernel is waiting on memory or on math, and it explains why LLM decode leaves most of a GPU idle.

Runs on a CPU or in the browser✦ Interactive~20 min

By the end you will be able to

  • ✓Draw a roofline from a GPU's peak FLOP/s and memory bandwidth
  • ✓Place a matmul on it using arithmetic intensity
  • ✓Explain why decode is memory-bound and prefill is compute-bound, and what batching does about it

Every kernel does two things: it moves bytes between HBM and the chip, and it does math on them. Whichever takes longer sets the speed. The roofline model turns that observation into a single plot.

Two ceilings

A GPU has two hard limits:

  • Peak compute PP: how many FLOP/s the tensor cores can do. An H100 SXM does about 989 TFLOP/s in dense BF16.
  • Memory bandwidth BB: how many bytes/s HBM can deliver. An H100 SXM delivers about 3.35 TB/s.

A kernel’s arithmetic intensity II is the work it does per byte it moves:

I=FLOPsbytes moved[FLOPbyte]I = \frac{\text{FLOPs}}{\text{bytes moved}} \quad \left[\tfrac{\text{FLOP}}{\text{byte}}\right]

The best throughput it can reach is the lower of the two ceilings:

attainable FLOP/s=min⁡(P,  B×I)\text{attainable FLOP/s} = \min\left(P,\; B \times I\right)

On a log-log plot that is a slanted line (bandwidth-limited) meeting a flat roof (compute-limited). They cross at the ridge point I∗=P/BI^{*} = P / B, which is about 295 FLOP/byte on an H100. Below the ridge the tensor cores sit idle waiting for data.

Placing an LLM layer on the roof

Take one linear layer, Y=XWY = XW, with WW of size d×dd \times d and MM tokens in XX. Everything is FP16 (2 bytes).

FLOPs=2Md2bytes=2 (Md+d2+Md)I=Mdd+2M\text{FLOPs} = 2Md^2 \qquad \text{bytes} = 2\,(Md + d^2 + Md) \qquad I = \frac{Md}{d + 2M}

When M≪dM \ll d, this simplifies to I≈MI \approx M: arithmetic intensity is about the number of tokens processed per step.

  • Decode produces one token per sequence per step, so MM is the batch size. At batch 1, I≈1I \approx 1, and the H100 runs at about 0.3% of peak.
  • Prefill processes the whole prompt at once, so MM is the prompt length. At 2,048 tokens, I≈1,024I \approx 1{,}024, which is compute-bound at 100%.

Play with it

GPU989 TFLOP/s · 3.35 TB/s · ridge ≈ 295 FLOP/B
MEMORY-BOUNDCOMPUTE-BOUND0.11101001000100000.11101001000arithmetic intensity (FLOP / byte), log scaleattainable TFLOP/s, log scaleridgedecode · batch 8prefill · 2048 tokens
intensity
7.97
TFLOP/s
26.7
of peak
2.7%

memory-bound — the tensor cores wait on HBM.

intensity
1024
TFLOP/s
989
of peak
100%

compute-bound — HBM keeps up; math is the limit.

Model: one 4096×4096 FP16 linear layer. Drag the dots or use the sliders. Real kernels sit below the roof.

fig. 3.3A roofline for one 4096×4096 FP16 layer. Drag the red decode dot or the blue prefill dot, or switch GPUs and watch the ridge move.

What this means for serving

This one plot explains most of the inference stack:

  • Batching is the main lever for decode. Each extra sequence in a step reuses the same weight bytes, which raises II. That is why serving engines do continuous batching.
  • Prefill and decode want different things. Prefill wants FLOP/s. Decode wants bandwidth. That mismatch motivates chunked prefill and, at scale, disaggregated serving.
  • Quantization helps decode for free. INT4 weights move a quarter of the bytes, so II quadruples for the same work (see Module 12).

Lab: compute the roofline yourself 🟢

labs/m03/3.3/roofline.py
def intensity(m: int) -> float:
return flops(m) / bytes_moved(m) # 2*m*d*d / 2*(m*d + d*d + m*d)
def attainable(gpu: str, ai: float) -> float:
peak, bw = GPUS[gpu]
return min(peak, bw * ai) # TB/s * FLOP/B = TFLOP/s
Terminal window
python labs/m03/3.3/roofline.py
output
H100 SXM: peak 989 TFLOP/s, 3.35 TB/s -> ridge 295 FLOP/B
phase M AI (FLOP/B) TFLOP/s % peak bound
decode 1 1.0 3.3 0.3% memory
decode 8 8.0 26.7 2.7% memory
decode 64 62.1 207.9 21.0% memory
decode 256 227.6 762.3 77.1% memory
prefill 512 409.6 989.0 100.0% compute
prefill 2048 1024.0 989.0 100.0% compute

Sources

Check your understanding

0 / 4 answered
  1. 1.A GPU has 400 TFLOP/s of peak compute and 2 TB/s of bandwidth. Where is its ridge point?

  2. 2.For a d×d FP16 linear layer with M ≪ d, arithmetic intensity is about…

  3. 3.Why is single-user decode slow even on the most powerful GPU?

  4. 4.Which change raises the arithmetic intensity of decode weight matmuls?