SysDesignAtlas

The Arithmetic of Inference

FLOPs per token, memory bandwidth, arithmetic intensity, and the roofline model — the calculations that tell you what a GPU can actually serve before you buy one.

CoreHard10 min readaiperformancerooflinecapacity
Confidence

#The two numbers a GPU has

Every performance question about inference reduces to a comparison between two hardware capabilities: how fast the chip can calculate, and how fast it can fetch the numbers to calculate with.

A FLOP is one floating-point operation — a single multiply or add. Chips are rated in TFLOP/s, trillions of them per second.

   A representative modern datacenter GPU:

     Compute:    ~1,000 TFLOP/s  (trillion operations per second)
     Bandwidth:  ~3 TB/s          (memory → compute units)

   Ratio:  1,000e12 FLOP/s ÷ 3e12 byte/s  ≈  330 FLOP per byte

   → To keep the compute units busy, you must do roughly
     330 floating-point operations for every byte you read
     from memory.

     Do fewer, and the GPU is idle waiting on memory.

That ratio — often called the machine balance — is the whole story. A workload's job is to clear 330 FLOP/byte, or it will be memory-bound, and modern accelerators have been getting worse at this over time: compute has grown faster than bandwidth for a decade, so the threshold keeps rising.

#Arithmetic intensity

Arithmetic intensity is how much calculating an operation does per byte it reads — operations divided by bytes moved. Compare that single number against the machine's 330 and you immediately know which resource is your limit: below it you're waiting on memory, above it you're waiting on the maths.

Plot achieved throughput against intensity and you get the roofline. Throughput climbs as intensity rises, because each byte fetched feeds more work. Then it hits a flat ceiling — the chip's peak compute — and stops climbing. The corner where the slope meets the ceiling is the ridge point, at the machine balance of ~330.

arithmetic intensity (FLOP/byte) → decode ≈ 1-2 · prefill ≈ 100s achieved throughput → bandwidth-limited, then flat ridge point ≈ 330 FLOP/byte
The roofline. Below the machine balance you are bandwidth-limited, and adding compute does nothing.

Now compute it for the two phases.

#Decode latency, from first principles

Because decode is bandwidth-bound, per-token latency — called TPOT, time per output token — follows directly from the weight bytes: every token requires reading every weight once, so the time is however long that read takes.

Three conclusions that fall straight out:

  1. Quantization is a latency optimisation, not just a memory one. Halving the bytes halves the time, because the bottleneck is bytes.
  2. Tensor parallelism reduces latency for a single request. Tensor parallelism splits every weight matrix into slices, one per GPU. That is unusual, since parallelism normally buys throughput (more requests at once) rather than speed for one request. It works here because each GPU reads only its slice, in parallel, so each token needs a quarter of the read time on four GPUs.
  3. Batch size does not change TPOT. The same weight read serves the whole batch, so 64 users each get ~8 ms/token while total throughput is 64× higher. This is the most counter-intuitive fact in serving, and the reason throughput is nearly free until memory runs out.

#Time to first token

TTFT — time to first token, how long the user waits before anything appears — is a different calculation, because it is mostly prefill, and prefill is compute-bound.

Prompt lengthPrefill computeNote
100 tokens~6 msNegligible
2,000 tokens~117 msNoticeable
32,000 tokens~1.9 sUsers feel this
128,000 tokens~7.5 sPlus quadratic attention on top

TTFT scales with prompt length; TPOT does not. So a RAG system (retrieval-augmented generation: fetch relevant documents and paste them into the prompt) that stuffs 30,000 tokens of retrieved context into every prompt has made a TTFT decision, not just a quality decision. Every user now waits for 30,000 tokens of prefill before seeing a word, and that wait should be measured.

#Putting it together: what one GPU serves

That last line is the operating tension, and it never goes away.

#Cost per token

Utilisation is the dominant term. You pay for the GPU-hour whether the GPU is busy or idle, so halving your utilisation doubles your unit cost. That is why keeping GPUs busy — smoothing traffic peaks, filling quiet hours with batch jobs, sharing one deployment across many customers — matters far more to margin than a 10% faster kernel. This gets developed in the AI cost model.

#Exercise

Worth checking: in (4) most people find memory binds before they reach compute saturation, which is the normal situation and the reason KV cache reduction is the highest-leverage optimisation available.

#Check yourself

What is machine balance, and why does it matter?Show answer
Compute ÷ bandwidth — about 1,000 TFLOP/s ÷ 3 TB/s ≈ 330 FLOP per byte. Workloads below it wait on memory; above it they wait on maths.
What is the arithmetic intensity of prefill and of decode?Show answer
Prefill: batch × prompt length (thousands — compute-bound). Decode: exactly the batch size — 1 at batch 1, which is ~0.3% utilisation.
How do you estimate time per output token?Show answer
TPOT ≈ weight bytes ÷ memory bandwidth (×1.3–1.6 for overheads). A 70B model is ~47 ms in BF16, ~23 ms in FP8, ~6 ms split four ways by tensor parallelism.
What changes TPOT, and what doesn't?Show answer
Quantization and tensor parallelism reduce it (fewer bytes per GPU); batch size doesn't, because one weight read serves the whole batch — so throughput is nearly free until memory runs out.
How do you estimate time to first token?Show answer
Queue wait + 2 × parameters × prompt tokens ÷ effective FLOPS. It scales with prompt length — a 32K-token prompt on a 70B model is ~1.9 s — so stuffing long context is a TTFT decision.
Why are throughput and TTFT in tension?Show answer
Bigger batches raise intensity and throughput (cheaper tokens) but lengthen queues (worse TTFT). Systems split interactive and bulk lanes — hence discounted batch APIs.
What dominates cost per token?Show answer
Utilisation: an 8-GPU node at ~$30/hour serving 64,000 tokens/s costs ~$0.13 per million output tokens at full load, ~$0.33 at 40%. Input is cheaper; cached input cheaper still.
Which GPU specs predict inference performance?Show answer
Memory bandwidth (TPOT), memory capacity (concurrency) and interconnect bandwidth (tensor-parallel scaling) — not peak TFLOPS, which decode never approaches.

#What to carry forward

  • Compare arithmetic intensity against the machine balance (~330 FLOP/byte) to know your bottleneck.
  • Prefill intensity = batch × length (compute-bound). Decode intensity = batch (memory-bound).
  • TPOT ≈ weight bytes / bandwidth. Quantization and tensor parallelism both reduce it; batch size does not.
  • TTFT scales with prompt length; TPOT doesn't. Long retrieved context is a TTFT decision.
  • Throughput and TTFT are in direct tension. Separate lanes rather than seeking one setting.
  • Utilisation dominates cost per token — more than any kernel-level optimisation.
  • Judge GPUs on bandwidth, capacity, and interconnect, not TFLOPS.
00:00
/ 10m