Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Two ceilings
A factory’s output is limited either by how fast its machines work or by how fast trucks can bring raw material, whichever is slower. A computation is the same: it is limited either by arithmetic speed or by how fast memory delivers numbers. If each number you fetch is used for many calculations, the machines are the limit; if each number is used once, the trucks are. Knowing which ceiling you are under tells you what to fix.
| vector add | 0.17 FLOP/B | 340 GFLOP/s | memory-bound (0.1% of peak) |
| softmax | 1.25 FLOP/B | 2.5 TFLOP/s | memory-bound (0.8% of peak) |
| layernorm | 2.00 FLOP/B | 4.1 TFLOP/s | memory-bound (1.3% of peak) |
| GEMV 4096² | 1.00 FLOP/B | 2.0 TFLOP/s | memory-bound (0.7% of peak) |
| GEMM 4096³ | 1365 FLOP/B | 312 TFLOP/s | compute-bound (100.0% of peak) |
| decode, batch 1: weights | 1.00 FLOP/B | 2.0 TFLOP/s | memory-bound (0.7% of peak) |
| decode: KV-cache attention | 0.99 FLOP/B | 2.0 TFLOP/s | memory-bound (0.6% of peak) |
Sizing hardware for a flight computer, deciding whether a simulation needs a GPU, predicting whether a language model can respond in real time on a ground-station server, and judging whether an optimization is worth trying all start with a back-of-the-envelope roofline estimate.
- 1. A computation can be limited by…
- 2. Adding two big arrays is usually limited by…
- 3. In the demo, what moves a big matrix multiplication to the compute roof?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Arithmetic intensity
Arithmetic intensity I = FLOPs / bytes moved between the processor and (slow) memory. Attainable performance P = min(P_peak, I × B), where B is memory bandwidth. The ridge point I* = P_peak / B is the intensity needed to be compute-bound:
| Hardware | Peak | Bandwidth | Ridge |
|---|---|---|---|
| A100 80 GB, FP32 | 19.5 TFLOP/s | 2.04 TB/s | 9.6 FLOP/B |
| A100, FP16 tensor cores | 312 TFLOP/s | 2.04 TB/s | 153 FLOP/B |
| H100 SXM, FP16 tensor cores | 989 TFLOP/s | 3.35 TB/s | 295 FLOP/B |
Vector addition in FP32 does 1 FLOP per 12 bytes (I ≈ 0.08): at most 0.17 TFLOP/s on an A100, under 1% of its FP32 peak, whatever the code.
- 1. Arithmetic intensity is…
- 2. If I is below the ridge point, the kernel is…
- 3. A100 FP16 ridge point?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Intensity of common kernels
Counting each operand read once and each result written once (the minimum traffic):
- Element-wise (add, GELU, scaling): a few FLOPs per element, I ≈ 0.1–1. Always memory-bound on GPUs.
- Reductions and normalizations (sum, softmax, layernorm): I ≈ 1.
- Matrix-vector y = Ax: 2mn FLOPs for mn matrix elements: I ≈ 2/elem (1 FLOP/B in FP16). Memory-bound.
- Matrix-matrix (m×k)(k×n): 2mnk FLOPs for (mk + kn + mn) elements; for square matrices I = 2n/(3·elem), which is n/3 in FP16. Compute-bound once n is a few hundred.
The real traffic depends on the implementation: a kernel that re-reads data from DRAM has lower achieved intensity than the algorithm’s minimum. Hierarchical rooflines draw one memory roof per level (L1, L2, HBM) to show where data is actually served from.
- 1. Arithmetic intensity of a square FP16 GEMM of size n?
- 2. A GEMM with a single row (m = 1) is effectively…
- 3. Making a memory-bound kernel faster requires…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Using the model
- Predict: time ≥ max(FLOPs/peak, bytes/bandwidth). If a kernel runs far slower than this bound, it’s an implementation problem (poor coalescing, low occupancy, launch overhead); if it’s close, only a different algorithm or more reuse helps.
- Raise intensity: tiling for reuse (C5.1), fusing element-wise chains so intermediates stay on chip (C5.8), lower precision (fewer bytes per value, C5.5), batching requests so weights are read once for many inputs.
- Know the ceilings below the roof: without tensor cores, without SIMD, or with poor instruction mix, the attainable compute roof is lower.
Language models on the roofline
Generating text has two phases. Prefill processes the whole prompt at once: its projections are large GEMMs, mostly compute-bound. Decode generates one token at a time: each layer multiplies a single token vector by every weight matrix (a GEMV), so every weight byte is read for about one FLOP. At batch 1 decode is limited by weight bandwidth (time per token ≈ model bytes / bandwidth); batching several sequences reuses each weight read, but each sequence’s attention over its own key-value cache stays memory-bound.
- 1. Lower bound on decode latency per token for a 7B-parameter FP16 model on 2 TB/s?
- 2. Batching decode helps the weight GEMMs because…
- 3. A kernel runs at 5% of its roofline bound. The likely problem is…
Level 4: Expert
★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.A transformer layer on the A100 roofline
Exercise 1. A100-80GB ridge points: 9.6 FLOP/B (FP32) and 153 FLOP/B (FP16 tensor cores). Intensities: vector add 0.083, FP16 GEMV 1.0, FP16 4096³ GEMM 1365 FLOP/B. A 4096³ FP16 GEMM is compute-bound; the same GEMM with one row is memory-bound. A 10⁸-element FP32 vector add needs at least 0.59 ms.
Exercise 2: prefill versus decode. One layer of a d = 4096, 32-head model (attention + 4× MLP) in FP16 on an A100, minimum-traffic model:
| Phase | achieved | of peak | memory-bound time |
|---|---|---|---|
| prefill, 8 sequences × 2048 tokens | 268 TFLOP/s | 86% | 21% (norms, softmax, GELU, attention) |
| decode, batch 1, 2048 cached tokens | 2.0 TFLOP/s | 0.65% | 100% |
At batch 1, every operation of decode has intensity of about 1 FLOP/B; 32 layers take 6.9 ms per token, the time to read the weights once. Batching decode (2048 cached tokens per sequence):
| batch | 1 | 8 | 32 | 64 | 128 | 256 | 512 |
|---|---|---|---|---|---|---|---|
| TFLOP/s | 2.0 | 10.5 | 19.0 | 21.9 | 23.7 | 24.2 | 24.2 |
Throughput plateaus at 7.7% of peak: the weight GEMMs become compute-bound near batch 153 (the ridge), but each sequence’s key-value-cache attention keeps an intensity of about 1 and takes 93% of the time at batch 512. This is why long-context serving relies on KV-cache compression, grouped-query attention and attention kernels that read the cache once.
The roofline in AI, robotics and aerospace
- Model serving: choosing batch sizes, quantization (fewer bytes per weight) and hardware from the decode bound.
- Scientific kernels: stencils and sparse solvers in CFD and structural codes are memory-bound; dense linear algebra in Kalman filters for many states and in Gaussian processes is compute-bound.
- Edge hardware selection: comparing an embedded GPU, an NPU and a CPU for an onboard network by their ridge points and the network’s intensity.
- Profiling: Nsight Compute and Intel Advisor draw measured kernels on the roofline (C5.4).
References
- S. Williams, A. Waterman, D. Patterson (2009). “Roofline: an insightful visual performance model for multicore architectures.” Communications of the ACM, 52(4).
- A. Ilic, F. Pratas, L. Sousa (2014). “Cache-aware roofline model.” IEEE Computer Architecture Letters, 13(1).
- NVIDIA (2020, 2022). A100 and H100 Tensor Core GPU Architecture whitepapers.
- R. Pope et al. (2023). “Efficiently scaling transformer inference.” MLSys.
- N. Ding, S. Williams (2019). “An instruction roofline model for GPUs.” IEEE/ACM PMBS Workshop.