The Roofline Model & Compute Efficiency
Arithmetic intensity, memory bandwidth, kernel fusion and FLOPs utilization.
A GPU that can do operations a second spends much of its life waiting: for weights to arrive from memory, for a kernel to launch, for a message from another chip. The roofline model turns that waiting into a picture with two lines, and tells you before running anything whether an operation can go faster by doing less arithmetic, by moving fewer bytes, or not at all. This lesson applies it to matrix products, text generation and attention, then counts FLOPs to measure how close real training runs get to the roof.
What you’ll learn
- State the roofline bound and place an operation on it from its arithmetic intensity, given a peak compute rate and a memory bandwidth.
- Derive the arithmetic intensity of matrix products, elementwise operations and autoregressive decoding, and show why batch-1 generation is memory-bound.
- Explain kernel fusion, tiling and compilation (FlashAttention, Triton, torch.compile) as ways to raise effective intensity.
- Count a transformer's FLOPs per token, compute model and hardware FLOPs utilization, and name the further ceilings: communication, memory capacity and energy.
Two ceilings
The bound follows from two facts: the work cannot finish faster than , and the data cannot arrive faster than , so the time is at least . With round, illustrative numbers ( FLOP/s in 16-bit, B/s), the ridge sits near FLOPs per byte: a kernel must do hundreds of operations on every byte it loads to use the arithmetic fully. Over successive hardware generations peak compute has tended to grow faster than memory bandwidth, so the ridge keeps moving right.
The model is a bound, not a prediction. Real kernels sit below the roof because of latency, launch overhead, poor overlap or imperfect caching; the roofline says which wall they are up against. Separate rooflines can be drawn for each level of the hierarchy (on-chip SRAM, L2, HBM, the network) by counting the bytes that cross it.
On the illustrative GPU ( FLOP/s, B/s), a kernel has intensity 30 FLOPs per byte. What is the best it can do?
- FLOP/s, since the roof is all that matters
- FLOP/s: it is memory-bound at , under a tenth of the peak
- FLOP/s, the bandwidth
- It depends only on the number of FLOPs
Show the answer
B. FLOP/s: it is memory-bound at , under a tenth of the peak
FLOP/s, below the ridge at . Speeding up its arithmetic would change nothing; moving fewer bytes, or doing more work per byte, would.
Intensity of the basic operations
Multiplying by takes multiply-adds, FLOPs. In 16-bit, reading both inputs once and writing the output once moves bytes:
For square matrices this is : about 1,365 FLOPs per byte at , comfortably compute-bound. If one dimension is a small batch of tokens multiplying a large weight matrix (), the weights dominate the traffic:
So a matrix–vector product, , has intensity about 1, and generating text one token at a time for one user runs every weight matrix at roughly memory speed: each 16-bit weight is fetched to be used in a single multiply-add. Reaching the ridge needs a batch of a few hundred tokens processed together.
A FP16 weight matrix multiplies one FP16 vector. Counting the matrix, the input and the output, what is the arithmetic intensity in FLOPs per byte?
Show the answer
1
FLOPs: . Bytes: . The ratio is , essentially 1, some 300 times below the illustrative ridge. Batching requests raises it to about (228 at for this shape), which is why serving systems batch continuously.
The rest of a transformer is worse:
- Elementwise operations and normalization (bias, GELU, residual adds, layer norm, softmax) do a handful of FLOPs per element while reading and writing 2 bytes each way: intensity of order 1. They hold a small share of the FLOPs but can take a large share of the time.
- Attention during decoding reads each sequence's cached keys and values, bytes per cached token in 16-bit, to do about FLOPs with them for one new query: intensity about 1 whatever the batch size, because every sequence has its own cache. Long contexts make decoding a memory-bandwidth problem, which is what grouped-query attention, latent attention and quantized caches attack (Serving Language Models).
- Training batches thousands of tokens per weight matrix, so its large matrix products are compute-bound; its losses come from the memory-bound operations around them and from communication.
Fusion, tiling and compilers
Intensity is not fixed by the mathematics; it depends on how often data crosses the slow boundary. Three techniques raise it:
- Kernel fusion. Computing as three kernels writes the product to HBM, reads it back to add the bias, writes it again, reads it again for GELU and writes the result. A fused kernel applies bias and GELU to each output tile while it is still on chip, removing four of five trips. Normalizations, residual adds and dropout are routinely fused the same way.
- Tiling. A matrix product computes each output block from blocks of and held in SRAM, so every loaded value is reused many times; larger tiles mean more reuse, up to the SRAM's capacity. FlashAttention applies tiling to attention: with an online softmax it never writes the score matrix to HBM, cutting HBM traffic from to for SRAM size , and recomputes scores in the backward pass instead of storing them (Dao et al., 2022; FlashAttention in Depth).
- Compilers and kernel languages. Triton, introduced as a tile-based language and compiler with a C-like front end (Tillet, Kung & Cox, 2019) and now written as Python functions, lets researchers write tiled GPU kernels while the compiler handles much of the low-level scheduling;
torch.compilecaptures a PyTorch program as a graph and generates fused Triton kernels for it (Ansel et al., 2024).
import torch
import torch.nn.functional as F
def mlp(x, w1, b1, w2):
h = F.gelu(x @ w1 + b1) # eager mode: the add and the GELU each read and write HBM
return h @ w2
mlp_compiled = torch.compile(mlp) # traces the graph; pointwise ops are fused into one generated kernelDepending on the device and data type, the compiler either folds the bias add into the matrix-product library call or fuses it with the GELU into one generated kernel; either way the intermediate stops making separate trips to HBM. Compilation helps most where many small memory-bound operations sit between the large matrix products; the products themselves are already compute-bound library calls.
Fusing the bias add and GELU into the matrix product's output stage speeds up an MLP. Why?
- It reduces the number of FLOPs
- It increases the GPU's peak FLOP/s
- It removes the memory-bound round trips of the intermediate tensor through HBM, so the elementwise work rides along with data already on chip
- It makes the matrix product itself compute-bound
Show the answer
C. It removes the memory-bound round trips of the intermediate tensor through HBM, so the elementwise work rides along with data already on chip
The FLOPs are unchanged; what disappears is writing the intermediate to HBM and reading it back twice. For a memory-bound operation, time is bytes over bandwidth, so fewer bytes means less time.
Counting FLOPs, and the other ceilings
A forward pass costs FLOPs per token for the weight matrices plus the attention scores and weighted sums: with a causal mask's average context (Kaplan et al., 2020), or counting the full . Training triples it; PaLM's count uses the full context:
Model FLOPs utilization divides the throughput, counted in these model FLOPs, by the peak. Hardware FLOPs utilization also counts recomputed work; full activation recomputation adds a forward pass, instead of per token, which raises HFU but not MFU:
Reported values: PaLM, 46.2% MFU and 57.8% HFU on 6,144 TPU v4 chips (Chowdhery et al., 2022); a 530-billion-parameter GPT-style model on 2,240 GPUs, 54.2% MFU with sequence parallelism and selective recomputation against 42.1% with full recomputation (Korthikanti et al., 2023). Well-tuned large runs therefore leave about half of the peak unused, to memory-bound operations, communication that is not hidden, pipeline bubbles and stragglers.
Further ceilings. The same reasoning applies beyond HBM:
- Communication is a roofline of its own: an operation's bytes on the wire per FLOP against the link bandwidth per unit of compute. Tensor parallelism needs bytes per FLOP on the critical path (Data, Tensor & Pipeline Parallelism), affordable inside a server and not between servers.
- Memory capacity caps the batch: in serving, the KV cache of every active sequence must fit, which limits how far batching can raise decoding's intensity.
- Energy follows data movement. In Horowitz's 45-nm figures, reading 64 bits from DRAM cost 1.3 to 2.6 nJ, against about 10 pJ from a small on-chip cache and 3.7 pJ for a 32-bit floating-point multiply: hundreds of times more for the memory access than for the arithmetic (Horowitz, 2014). Low-precision formats, fusion and tiling save energy for the same reason they save time.
A 7-billion-parameter model trains at 10,000 tokens per second per GPU on accelerators with a peak of FLOP/s. Ignoring the attention term, what is its MFU?
- About 7%
- About 14%
- About 42%
- About 56%, counting the recomputed forward pass
Show the answer
C. About 42%
FLOP/s, or 42% of the peak. If activations were fully recomputed, the hardware would do per token, 56% hardware FLOPs utilization, but MFU stays at 42%.
Key takeaways
- Attainable performance is at most ; operations below the ridge (hundreds of FLOPs per byte on modern accelerators) are memory-bound.
- A matrix product has intensity : when square, about for a batch of tokens; batch-1 decoding and the KV cache sit near 1 FLOP per byte.
- Fusion removes round trips of intermediates, tiling reuses data held in SRAM (FlashAttention never writes the score matrix), and compilers such as torch.compile with Triton automate much of it.
- Training costs about FLOPs per token; MFU counts only these, HFU adds recomputation. Large runs reach roughly 40–55% MFU, and communication, memory capacity and energy add ceilings of their own.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A chip has a peak of FLOP/s and 2 TB/s of memory bandwidth. Where is its ridge point?
- 2 FLOPs per byte
- 20 FLOPs per byte
- 200 FLOPs per byte
- 2,000 FLOPs per byte
Show the answer
C. 200 FLOPs per byte
FLOPs per byte. Kernels below that are memory-bound; above it, compute-bound.
Why is single-sequence text generation memory-bound even on a large model?
- Because the model has too many parameters to compute quickly
- Because each weight is loaded from HBM to be used for a single multiply-add per generated token, an intensity of about 1 FLOP per byte
- Because attention is quadratic in the context
- Because generation runs in FP32
Show the answer
B. Because each weight is loaded from HBM to be used for a single multiply-add per generated token, an intensity of about 1 FLOP per byte
With one token in flight, every matrix product is a matrix–vector product: intensity , hundreds of times below a GPU's ridge. Batching many sequences reuses each loaded weight times.
What does FlashAttention change about attention?
- It approximates the softmax to save FLOPs
- It reduces the FLOPs from quadratic to linear in the sequence length
- It computes attention in blocks held in SRAM with an online softmax, so the score matrix never goes to HBM; the result is exact
- It stores the attention matrix in 8 bits
Show the answer
C. It computes attention in blocks held in SRAM with an online softmax, so the score matrix never goes to HBM; the result is exact
It is an IO-aware exact algorithm: same FLOPs (slightly more, because of recomputation in the backward pass), far fewer HBM bytes, so a memory-bound computation moves toward the compute roof.
Why did PaLM introduce model FLOPs utilization instead of reporting hardware FLOPs utilization alone?
- Because hardware FLOPs depend on implementation choices such as recomputation, while model FLOPs depend only on the architecture and the tokens processed
- Because MFU is always higher
- Because hardware counters cannot measure FLOPs
- Because MFU includes communication time
Show the answer
A. Because hardware FLOPs depend on implementation choices such as recomputation, while model FLOPs depend only on the architecture and the tokens processed
Recomputing activations adds hardware FLOPs without adding progress, so HFU can be inflated by implementation choices; MFU's numerator is observed tokens per second times the model's own FLOPs, comparable across systems.
Further reading
- S. Williams, A. Waterman & D. Patterson, "Roofline: An insightful visual performance model for multicore architectures," Communications of the ACM 52(4) (2009).
- T. Dao, D. Y. Fu, S. Ermon, A. Rudra & C. Ré, "FlashAttention: Fast and memory-efficient exact attention with IO-awareness," NeurIPS (2022).
- P. Tillet, H. T. Kung & D. Cox, "Triton: An intermediate language and compiler for tiled neural network computations," MAPL (2019).
- A. Chowdhery et al., "PaLM: Scaling language modeling with Pathways," arXiv:2204.02311 (2022).
- J. Ansel et al., "PyTorch 2: Faster machine learning through dynamic Python bytecode transformation and graph compilation," ASPLOS (2024).
- M. Horowitz, "Computing's energy problem (and what we can do about it)," ISSCC (2014).