Foundations to Frontier
Tests
C. Programming, Tools and HPC › C5 High-Performance Computing

The Roofline Model and Arithmetic Intensity

Before optimizing a kernel, find out what limits it. The roofline model bounds attainable performance by the lesser of peak compute and arithmetic intensity (FLOPs per byte moved) times memory bandwidth. A single number, the ridge point, separates memory-bound kernels (element-wise operations, reductions, matrix-vector products) from compute-bound ones (large matrix multiplications). This topic covers computing arithmetic intensity, reading roofline plots, hierarchical rooflines, and using the model to predict and explain the performance of real workloads. In the exercises you implement the model with A100 and H100 specifications and compute the intensity of standard kernels; then you place every operation of a transformer layer on the A100 roofline, contrasting prefill with token-by-token decoding and measuring how far batching can push decode toward peak.

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.

The roofline: memory-bound or compute-bound?
1e-21e-11101001000100001e91e101e111e121e131e141e151e16ridge 153 FLOP/Bvector addsoftmaxlayernormGEMV 4096²GEMM 4096³decode, batch 1: weightsdecode: KV-cache attentionarithmetic intensity (FLOP per byte)
vector add0.17 FLOP/B340 GFLOP/smemory-bound (0.1% of peak)
softmax1.25 FLOP/B2.5 TFLOP/smemory-bound (0.8% of peak)
layernorm2.00 FLOP/B4.1 TFLOP/smemory-bound (1.3% of peak)
GEMV 4096²1.00 FLOP/B2.0 TFLOP/smemory-bound (0.7% of peak)
GEMM 4096³1365 FLOP/B312 TFLOP/scompute-bound (100.0% of peak)
decode, batch 1: weights1.00 FLOP/B2.0 TFLOP/smemory-bound (0.7% of peak)
decode: KV-cache attention0.99 FLOP/B2.0 TFLOP/smemory-bound (0.6% of peak)
Orange points sit under the slanted memory roof, blue points under the flat compute roof. Element-wise operations never leave the memory roof, so they are made faster by moving fewer bytes (fusion, C5.8), not by faster math. A GEMM climbs past the ridge as it grows; batching decode lifts the weight GEMMs, but each sequence's KV-cache attention stays near 1 FLOP per byte.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A computation can be limited by…
  2. 2. Adding two big arrays is usually limited by…
  3. 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.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Arithmetic intensity is…
  2. 2. If I is below the ridge point, the kernel is…
  3. 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.

🐍 Exercise 1: the roofline model
Implement attainable performance, the ridge point, bound classification and lower-bound kernel time; compute the arithmetic intensity of vector add, axpy, dot, GEMV, GEMM and softmax; check A100 and H100 ridge points and which kernels are memory- or compute-bound.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Arithmetic intensity of a square FP16 GEMM of size n?
  2. 2. A GEMM with a single row (m = 1) is effectively…
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Lower bound on decode latency per token for a 7B-parameter FP16 model on 2 TB/s?
  2. 2. Batching decode helps the weight GEMMs because…
  3. 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.

🐍 Exercise 2 (research): a transformer layer on the roofline
Model every operation of one transformer layer (norms, QKV, attention scores, softmax, attention × V, output projection, MLP, GELU, residuals) as FLOPs and minimum bytes in FP16; for prefill and decode compute arithmetic intensity, memory/compute bound and roofline time on an A100; summarize achieved throughput, model FLOP utilization and memory-bound fraction; sweep decode batch size.

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

  1. S. Williams, A. Waterman, D. Patterson (2009). “Roofline: an insightful visual performance model for multicore architectures.” Communications of the ACM, 52(4).
  2. A. Ilic, F. Pratas, L. Sousa (2014). “Cache-aware roofline model.” IEEE Computer Architecture Letters, 13(1).
  3. NVIDIA (2020, 2022). A100 and H100 Tensor Core GPU Architecture whitepapers.
  4. R. Pope et al. (2023). “Efficiently scaling transformer inference.” MLSys.
  5. N. Ding, S. Williams (2019). “An instruction roofline model for GPUs.” IEEE/ACM PMBS Workshop.