Zenaique

Prefill and decode: name the two phases of LLM inference and say which one is compute bound

Flashcard·Easy·4.0 · 0·~30s·Asked atCharacter AiDeepseekPinterest·Relevant atNVIDIA
Attempt it
TL;DR

Prefill processes all prompt tokens in parallel and is compute-bound (saturates tensor cores). Decode generates one token at a time and is memory bandwidth bound (HBM read of the full KV cache per step).

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine ordering a meal at a restaurant. The kitchen has to do two very different jobs. First, when you place your whole order at once (the prompt), the chef can fire up every burner in parallel and cook all the dishes simultaneously; the bottleneck is how much heat the kitchen can put out. Then, once the meal is going, the server has to walk to the kitchen, pick up one dish, and bring it to your table, then walk back for the next; the bottleneck is no longer how fast the kitchen cooks but how fast the server can run. LLM inference works the same way: prefill is the parallel cooking burst, decode is the per-dish trip back and forth. The two have completely different bottlenecks, which is why they get tuned separately.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

The prefill vs decode split is the single most important mental model for reasoning about LLM serving performance. Almost every production optimization in modern LLM serving stacks (vLLM, TensorRT-LLM, SGLang, custom inference engines) is targeting either the prefill side or the decode side of this asymmetry, and confusing the two leads to optimizations that miss their intended target.

This deep dive walks the roofline picture that motivates the split, explains why prefill is compute-bound and decode is bandwidth-bound (and why this asymmetry is structural rather than implementational), maps the two phases to the production metrics TTFT and TPOT, and surveys the modern optimizations that target each side.

Mental model: prefill is a big parallel matmul that saturates tensor cores. Decode is a per-token loop that saturates HBM bandwidth. Different bottlenecks, different fixes.

The roofline picture

The roofline model is the standard tool for reasoning about whether a kernel is compute-bound or memory-bound. The two axes are FLOPS per second (peak compute) and arithmetic intensity (FLOPs per byte loaded from memory). The 'roof' is the minimum of peak compute and (peak bandwidth * arithmetic intensity).

Kernels with low arithmetic intensity are bandwidth-bound: the bottleneck is reading data, not computing. Kernels with high arithmetic intensity are compute-bound: the bottleneck is the FLOPS, not the bandwidth.

Prefill: high arithmetic intensity

Prefill processes N prompt tokens in parallel through every layer. The attention matmul is Q @ K^T of shape (N, d_head) @ (d_head, N), which is O(N^2 * d_head) compute. The bytes loaded are O(N * d_head) for Q, K, V. Arithmetic intensity is roughly N * d_head, which for N = 1000, d_head = 128 is 128k FLOPs per byte; deep in the compute-bound region of the roofline.

On an H100 (peak 989 TFLOPS FP16, peak 3 TB/s HBM), kernels need arithmetic intensity above 989/3 = 330 FLOPs/byte to be compute-bound. Prefill at N = 1000 has intensity ~128k FLOPs/byte. Decisively compute-bound.

Decode: low arithmetic intensity

Decode processes 1 token at a time. The attention compute is q @ K^T of shape (1, d_head) @ (d_head, N+T), which is O((N+T) * d_head) compute. The bytes loaded are O((N+T) * d_head) for the KV cache. Arithmetic intensity is roughly d_head, which for d_head = 128 is 128 FLOPs per byte; below the 330 FLOPs/byte threshold. Decisively bandwidth-bound.

Why this is structural

The ratio of compute to bytes loaded for attention scales like:

  • Prefill: N_query_tokens / 1 = N
  • Decode: 1 / 1 = 1

The ratio is N times larger for prefill than for decode, which is the entire reason for the asymmetry. As N grows, prefill becomes more compute-bound and decode becomes more bandwidth-bound. The structural fact is not a tuning issue; it cannot be optimized away.

Mapping to production metrics
Optimizations that target each side
How the asymmetry shows up in batch composition and scheduling
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • vLLM and TensorRT-LLM both separate prefill and decode at the scheduler level and use continuous batching to amortize decode bandwidth across requests.
  • Anthropic, OpenAI, and Google's production serving stacks publish TTFT and TPOT separately because they reflect different bottlenecks.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does decode batching scale throughput so dramatically while prefill batching does not?
A

Decode is bandwidth-bound: each request loads its KV cache from HBM independently, but the model weights are loaded once per step regardless of batch size. Adding requests amortizes the weight loading and improves arithmetic intensity. Prefill is already compute-bound at batch size 1 for moderate prompt lengths; adding more requests does not unlock additional compute, only adds queueing.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Treating LLM inference as one uniform workload and missing that prefill and decode have completely different bottlenecks (compute vs bandwidth) requiring different optimizations.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Define the prefill phase in autoregressive LLM serving

  • Define the decode phase and how it differs from prefill

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium