Inference Engineering

Lesson 1: The roofline · Workbook

The roofline: the workbook

The full working behind lesson 1. The two budgets of a GPU, arithmetic intensity, why decode is memory-bound and prefill compute-bound, how batching moves decode along the roof, what the floor leaves out, five exercises, and a measurement on a real machine.

The lesson told this with a clerk, a belt and a rulebook. Here are the numbers behind it, with the clerk translated back: the arithmetic units are the tensor cores, the archive is high-bandwidth memory (HBM), the belt is its bandwidth, and the rulebook is the weights.

For Parrot’s model, Llama 3.1 8B, the weights read per token add up to 15.0 GB at 16 bits (the full model is 16.1 GB, but one table in it, the input embedding, is only looked up). An H100’s memory moves 3.35 TB a second, so it can bring them up at most 3,350 ÷ 15 ≈ 223 times a second: the ceiling for one user, however fast the arithmetic is. Real software lands a little under it. On paper, using the H100’s peak ratings, the balance point is about 296 sequences; real software reaches neither peak, so it lands nearby.

The toy in numbers

The lesson’s toy has a six-page rulebook and a clerk who needs an eighth of a belt trip per chat for each page. With one chat a step takes about 6 trips: six for the belt, plus her work on the last page. With eight chats it takes 7 trips and writes eight words instead of one. Past eight chats her work per page is longer than a trip, so pages wait on her desk and the belt stands still.

The roofline, quantified

Every time a GPU runs a piece of a model, it pays two bills. One is arithmetic: multiplying and adding numbers in its tensor cores. The other is memory traffic: moving those numbers between high-bandwidth memory (HBM) and the chip. The two happen at the same time, so the slower one sets the pace.

That is the whole idea of the roofline. The rest of this lesson makes it quantitative, then uses it to answer the question every inference engineer starts with: why is generating text so slow when the hardware is so fast?

Two budgets

An NVIDIA H100 SXM has these two budgets:

For any piece of work that does FF FLOPs and moves BB bytes, each budget gives a minimum time, and the work takes at least the larger of the two:

t  ≥  max⁡ ⁣(Fpeak FLOP/s,  Bbandwidth)t \;\ge\; \max\!\left(\htmlClass{c-compute}{\frac{F}{\text{peak FLOP/s}}},\;\htmlClass{c-memory}{\frac{B}{\text{bandwidth}}}\right)

This is a floor, not a prediction. Real kernels (the individual GPU programs a model step is made of, such as one matrix multiply) land above it. But it is a hard floor: no software trick gets below it without changing FF or BB.

Arithmetic intensity

Divide the two sides and one number decides which bill dominates: the arithmetic intensity, FLOPs done per byte moved.

I=FBI = \frac{F}{B}

The hardware has its own version of this number. Divide peak compute by bandwidth and you get the intensity at which the two bills are equal:

Iridge=989×1012 FLOP/s3.35×1012 B/s≈295 FLOPs per byteI_{\text{ridge}} = \frac{\htmlClass{c-compute}{989 \times 10^{12}\ \text{FLOP/s}}}{\htmlClass{c-memory}{3.35 \times 10^{12}\ \text{B/s}}} \approx 295\ \text{FLOPs per byte}

Work with intensity below 295 is memory-bound on an H100: the tensor cores finish early and wait for data. Work above it is compute-bound. This number, the ridge point, is the most useful single fact about an accelerator for inference.

Where intensity comes from: one matrix multiply

Almost all of a transformer’s work is matrix multiplication. Multiply an M×KM \times K matrix of activations by a K×NK \times N weight matrix and you do 2MNK2MNK FLOPs (one multiply and one add per term). You must at least read both inputs and write the output, so in BF16 (2 bytes per number):

I=2MNK2 (MK+KN+MN)I = \frac{\htmlClass{c-compute}{2MNK}}{\htmlClass{c-memory}{2\,(MK + KN + MN)}}

In a language model, MM is the number of tokens you push through at once, and the weight matrix K×NK \times N is large, typically thousands by thousands. When MM is much smaller than KK and NN, the KNKN term (the weights) dominates the bytes, and the formula simplifies to

I≈2MNK2KN=M.I \approx \frac{2MNK}{2KN} = M .

In BF16, intensity is roughly the number of tokens per matrix multiply. One token means about one FLOP per byte, nearly three hundred times below the H100’s ridge.

The figure below draws this. The amber box is the arithmetic, so its volume is M×N×KM \times N \times K multiply-adds. The teal plates are what must be moved: the weight plate BB underneath, and the thin activation plates AA and CC on the sides. Start at M=1M = 1, where a full plate of weights is read to do a sliver of work, then drag MM up.

Box volume
multiply-adds:
Plate B, weights
K × N, read every time:
Plates A and C
activations in and out:

Arithmetic intensity FLOPs/byte

A 4096 × 4096 weight matrix in BF16, the size of one attention projection in Llama 3.1 8B. The box's volume grows with M, while the weight plate stays the same size. That is why batching pays.

Decode: one token at a time

When a model generates text, it produces one token per forward pass. This phase is called decode. Each step, every weight matrix in the model is multiplied by a single new token’s activations, which is the M=1M = 1 case above.

Take Llama 3.1 8B in BF16 on one H100. Its matrix-multiply weights hold 7.50 billion parameters, including the 525 million of the final vocabulary projection that turns the last hidden state into next-token scores (the remaining 526 million are the input embedding table, a lookup, plus small norm weights). One decode step for one user needs:

Memory takes about 295 times longer than the arithmetic. Add attention over a 1,024-token conversation (40.1 µs, also memory-bound because it reads the stored keys and values of every earlier token, the KV cache covered below) and the floor is 4.52 ms per token, or at most 221 tokens per second for one user. The tensor cores are idle over 99% of the time.

This is the central fact of LLM inference: single-user decode is memory-bound, and its speed is set by how fast you can stream the weights. That is why quantizing weights to fewer bits speeds up decode almost in proportion, and why a GPU’s bandwidth matters more than its FLOP rating for chat-style workloads.

Prefill: the whole prompt at once

Before decode starts, the model reads the prompt. All prompt tokens are known up front, so they go through each weight matrix together. This phase is prefill, and here MM is the prompt length.

A 2,048-token prompt on the same setup reads the same 15.0 GB of weights but pushes all 2,048 tokens through every layer’s matrices. (The vocabulary projection runs only for the last token, since that is the only one whose next token is needed.) That is 28.9 ms of compute against 4.48 ms of weight traffic.

Counting only the weight reads, its intensity is about 1943. At this length the activations each matrix multiply reads and writes are no longer small, so the true figure is lower: the single 4096 × 4096 matrix in the figure above sits near 1024 at M=2048M = 2048. Either way it is far past the ridge. Prefill is compute-bound. Add attention over the prompt (1.11 ms, which runs after the matmuls) and the floor is 30.0 ms, the minimum time to first token for that prompt.

So one model on one GPU has two phases that hit opposite walls. Most of the serving techniques in this course exist because of that split.

The roofline chart

Put intensity on the horizontal axis and achievable FLOP/s on the vertical axis, both on log scales. Below the ridge, achievable throughput is bandwidth × intensity: a teal line sloping up. Above it, throughput is capped at peak compute: a flat amber ceiling. That bent line is the roofline. Any workload sits on or under it.

Below, the circle is one decode step and the diamond is one prefill. Change the batch size and watch the circle.

One decode step

Prefill

compute-bound memory-bound

Llama 3.1 8B on an H100 by default. Points sit on the roof when one resource limits the whole step, and fall below it when the matmuls and attention are limited by different resources, since they run one after the other. The bars split the step into those two groups of kernels.

Batching moves decode to the right

If one user’s decode step reads all the weights to do one token’s worth of work, the fix is to serve many users in the same step. With a batch of bb sequences, the weights are read once and used bb times, so the matmul intensity is roughly bb.

On the H100 at a 1,024-token context:

BatchStep time floorTokens/s, all usersTokens/s, each user
14.52 ms221221
647.05 ms9,079142
25614.8 ms17,34868

Going from 1 to 64 users multiplies total throughput by about 41 while each user slows down only modestly. That trade, throughput against per-user speed, is the main dial of every serving system. The matmuls stop being memory-bound once the batch passes the ridge: around 296 sequences on an H100.

There is a catch, and the explorer shows it if you raise the context. Each sequence has its own KV cache, the stored keys and values of every earlier token, and attention must read all of it every step. Batching does not share those bytes. At 64 users with 32,768 tokens of context each, attention takes 82.1 ms of the 86.5 ms step, and the caches alone need 275 GB, more than three H100s hold. The KV cache gets its own lesson, and several after it.

What the floor leaves out

The roofline is a lower bound with three known gaps.

When a measurement sits far from the floor, one of these is usually why, and finding which one is most of performance engineering.

Check yourself

Type your prediction before opening the worked answer. Numeric answers are checked against the same formulas as the figures on this page, within 10%. Your progress stays in this browser.

Exercise 1. Llama 3.1 8B in BF16, batch 1, short context, on an A100 SXM 80GB (2.04 TB/s). What is the ceiling on tokens per second?

tokens/s
Show the worked answer

About 136 tokens/s. Decode is memory-bound, so divide bandwidth by bytes per token: 15.0 GB of weights at 2.04 TB/s is 7.35 ms. The A100’s lower FLOP rating does not matter here.

Exercise 2. Same model on an H100, but the weights are stored in FP8 (1 byte each). What is the new single-user decode ceiling?

tokens/s
Show the worked answer

It roughly doubles, to about 446 tokens/s, because half as many bytes cross the memory bus. Decode speed tracks bytes per weight.

Exercise 3. Weight-only INT4 quantization stores weights in 4 bits but converts them back to BF16 to multiply. Does it speed up a 2,048-token prefill on an H100? What about FP8 weights with FP8 tensor-core math, which peaks at twice the BF16 rate?

Show the answer

INT4 weight-only: no. Prefill is compute-bound and the math still runs in BF16, so the floor stays at 30.0 ms. FP8 with FP8 math: yes, about 2×, to 15.0 ms, because the H100’s FP8 tensor cores have twice the BF16 peak. Same bit savings, different bottleneck, different payoff.

Exercise 4. At short context, roughly what batch size makes the decode matmuls compute-bound on an A100 (312 TFLOP/s, 2.04 TB/s)? Once you have it, work out the H100 too.

sequences
Show the worked answer

Intensity is about the batch size, so the answer is near each ridge point: about 154 on an A100 and 296 on an H100. The H100 has relatively more compute per byte, so it needs larger batches to keep its tensor cores busy.

Exercise 5. From A100 to H100, BF16 compute went from 312 to 989 TFLOP/s while bandwidth went from 2.04 to 3.35 TB/s. What happened to the ridge point, and what does that mean for serving?

Show the answer

Compute grew about 3.2× and bandwidth about 1.6×, so the ridge moved right, from about 153 to 295. Single-user decode gets only the bandwidth gain, and you need roughly twice the batch to keep the tensor cores busy. That is not a law of every generation: the H200 kept the H100’s compute and raised bandwidth to 4.8 TB/s, pulling its ridge back to about 206. Whatever the chip, the ways to close the gap at decode are bigger batches, speculative decoding (more tokens per weight read) and fewer bytes per weight.

Lab: test the floor on your own machine

A model of performance is only worth trusting once it predicts a measurement. On any machine with llama.cpp, download a small model in GGUF format and run its benchmark:

llama-bench -m qwen2.5-0.5b-instruct-q8_0.gguf -p 512 -n 128

The tg128 row is decode throughput at batch 1. Predict it first: divide your device’s memory bandwidth by the bytes one decode step reads. That is close to the file size, minus anything that is only looked up rather than multiplied. If your measurement lands well below that ceiling, use the three gaps above to work out why. If it beats the ceiling, your bandwidth or byte count is wrong.

A worked measurement

We ran exactly this on an Apple M1 Pro (16-core GPU, 200 GB/s) with llama.cpp build 5600, on 26 November 2026, with other applications open.

The bytes. The file is 676 MB, but it stores the vocabulary matrix twice: once as the input embedding (a lookup) and once as the output projection (a matrix multiply). The original model shares one copy. Listing the file’s tensors gives 525.1 MB read per decode step. The formulas on this site give 525 MB (Q8_0 stores 8.5 bits per weight), within 0.1%.

The prediction. 200 GB/s ÷ 525 MB ≈ 381 tokens/s, an upper bound.

The measurement. 154 tokens/s, about 40% of the ceiling. Prefill (pp512) measured 3,961 tokens/s against a compute ceiling of about 7,000.

The diagnosis. Two quick probes with PyTorch on the same GPU separate the gaps. A plain memory copy reached 167 GB/s, so the hardware can stream close to its rated bandwidth. A sweep of matrix-vector multiplies shaped like this model’s decode step reached only 90 GB/s. Small matrix-vector kernels cannot keep a GPU’s memory system saturated. At 90 GB/s the ceiling becomes 171 tokens/s, within about 10% of the measurement. The rest is overhead between kernels.

So the floor held, and the gap was the first one on the list: kernels not reaching peak. Bigger matrices give each kernel more parallel work, so larger models tend to get closer to peak bandwidth. Measure rather than assume.

Sources

  1. Williams, Waterman, Patterson (2009). Roofline: an insightful visual performance model for multicore architectures. Communications of the ACM 52(4).. The original roofline paper
  2. NVIDIA H100 product page, specifications table. Peak numbers; halve the starred figures, which assume 2:4 sparsity
  3. NVIDIA A100 product page, specifications table. A100 SXM 80GB: 312 TFLOP/s BF16 dense, 2,039 GB/s. Used in exercises 1, 4 and 5
  4. NVIDIA H200 product page, specifications table
  5. Apple (2021). Introducing M1 Pro and M1 Max. M1 Pro: up to 200 GB/s memory bandwidth
  6. Llama 3.1 8B model card (Meta). config.json is under Files after you accept Meta's license; an ungated copy is at huggingface.co/unsloth/Llama-3.1-8B
  7. kipply (2022). Transformer inference arithmetic. The same reasoning applied to a 52B model; good for cross-checking
  8. Austin et al. (2025). How to Scale Your Model. Google DeepMind. Chapters on rooflines and inference
  9. Horace He (2022). Making deep learning go brrrr from first principles. The third regime, overhead

Back to lesson 1: The roofline