How to Estimate Prefill and Decode Inference Costs
This article explains why Prefill and Decode have different bottlenecks and how to estimate their costs separately. It breaks end-to-end latency into two parts, models each with compute and bandwidth formulas, uses the roofline model to identify the real bottleneck, and quantifies the upper bounds of two common optimizations—last-layer computation skipping and cross-layer KV cache sharing. A benchmark checklist is included to validate estimates with measurements.
Bottom line: Prefill and Decode have different bottlenecks, so the same architectural change can yield very different gains for each. To estimate cost, split end-to-end latency into Prefill and Decode, model each with compute and bandwidth estimates, and then confirm with measurements.
1. What Prefill and Decode Do, and Why Their Bottlenecks Differ
LLM text generation has two phases.
Prefill reads all N input tokens at once, computes Keys and Values for every layer in parallel, and writes them into the KV cache. The KV cache is a region of GPU memory that stores the Keys and Values of past positions for reuse in later steps. Prefill ends by emitting the first token; this interval is called TTFT (time to first token).
Decode generates one new token per step. Each step reads the entire existing KV cache and appends one new entry. The time between two consecutive output tokens is called TPOT (time per output token).
| Dimension | Prefill | Decode |
|---|---|---|
| Positions processed per step | N input positions in parallel | 1 position per sequence |
| Typical bottleneck | Compute (when N is large) | Memory bandwidth (when batch is small) |
| Compute vs. length | Linear terms grow with N; attention grows with N² | Per-step compute scales with batch; attention reads grow with history length S |
| KV cache operation | Writes all N positions | Reads all history, appends 1 position |
| Key latency metric | TTFT | TPOT and throughput |
“Typical bottleneck” is not a fixed conclusion. It depends on input length N, batch size, history length S, model parallelism, and hardware. Section 3 shows how to make this judgment with the roofline model.
2. Latency Breakdown and Cost Formulas
End-to-end latency can be decomposed as:
T_total ≈ T_prefill(N) + Σ T_decode,t (for t from 1 to G−1)
Where:
- N: number of input tokens
- G: number of output tokens
- S_t: history length at step t, roughly N + t
- b: number of concurrent sequences in the batch
An approximation for Prefill time:
T_prefill ≈ max(F_pre / P, B_pre / BW)
F_pre is the total Prefill compute (FLOPs), B_pre is the number of bytes read from and written to memory, P is effective compute throughput, and BW is effective memory bandwidth. Taking the max means the phase is limited by whichever term is slower.
An approximation for a single Decode step:
T_decode,t ≈ max(F_dec / P, (W + b × KV(S_t)) / BW)
W is the weight bytes read in one step. KV(S) is the KV cache size in bytes for a single sequence of length S. For standard multi-head or grouped-query attention:
KV(S) = 2 × L × n_kv × d_head × S × bytes_per_element
L is the number of layers, n_kv is the number of KV heads, d_head is the per-head dimension, and the factor of 2 accounts for one copy each of K and V.
These formulas use effective values. Nominal compute and bandwidth are rarely achieved in practice; real efficiency must be calibrated with measurements.
3. Using Roofline to Identify the Bottleneck
The core roofline metric is arithmetic intensity—the number of floating-point operations executed per byte of memory read:
I = F / B
When I is below the hardware ridge point I* = P / BW, the phase is bandwidth-bound; above the ridge point, it is compute-bound.
Take the nominal specs of an A100 80GB SXM as an example: 312 TFLOPS of bf16 dense compute and about 2.0 TB/s of memory bandwidth, giving a ridge point of roughly 156 FLOP/byte.
Decode: each parameter participates in one multiply-add, roughly 2 FLOPs, and a bf16 weight occupies 2 bytes, so the arithmetic intensity against weights for a single sequence is about 1. With b sequences batched, I ≈ b. When b is far below 156, Decode is bandwidth-bound.
Prefill: N tokens share a single weight read, so I ≈ N. When N is far below 156, Prefill is also bandwidth-bound; it only becomes compute-bound with longer inputs.
This analysis ignores activation reads and writes for an order-of-magnitude estimate. The takeaway: “Prefill is compute-bound, Decode is bandwidth-bound” is a common case, not an absolute law. Prefill with short inputs can be bandwidth-bound, and Decode with large batches can become compute-bound.
4. The Upper Bound of Skipping Last-Layer Computation
In a standard decoder, the last layer’s output is only used to compute the logits of the next token. During Prefill, only the output at the last position is needed; for the other N−1 positions, the last layer’s Q projection, attention output, O projection, and FFN can all be skipped. However, the last layer’s K and V must still be computed and written to the cache, because Decode will read them.
So the skippable part is the non-KV computation of the last layer. If K and V projections account for a fraction k of each layer’s linear computation, the upper bound on Prefill compute savings is:
Savings ≤ (1 − k) / L × (N − 1) / N
Using Llama-3-8B as an example (L=32, d=4096, d_ff=14336, 8 KV heads, d_head=128), K and V projections account for roughly k ≈ 4% of the per-token linear-layer parameters. Substituting gives an upper bound of about 0.96 / 32 ≈ 3%.
This is a theoretical upper bound estimated from linear layers. The attention term’s share rises with N, so the actual fraction must be recomputed. If the inference framework does not support skipping this computation, the gain is zero. This order of magnitude shows the optimization has limited impact on end-to-end latency and should not be a primary lever.
5. The Benefit Boundary of Cross-Layer KV Cache Sharing
Cross-layer sharing lets several layers reuse the same set of K and V. Its effects on different cost items vary:
- Memory capacity: fewer KV layers must be stored, so capacity drops in proportion to the sharing ratio. This is the main benefit.
- K and V writes: shared layers no longer write new K and V, so write volume drops proportionally.
- Per-step read bandwidth: shared layers still read this set of K and V with their own Queries; the same cache is read once per layer, so per-step read volume is essentially unchanged. Bandwidth only drops if the kernel can keep the cache on-chip and reuse it, which a layer-by-layer sequential implementation typically cannot do.
- Total Prefill compute: shared layers skip their K and V projections. Using the numbers from the previous section, if half the layers are shared, savings are about k × 1/2 ≈ 2%—a minor effect.
Therefore, the main benefit is memory capacity. The help for per-step Decode speed is small.
The applicable scenario is long context with large batches, where memory capacity limits concurrency. In that case, reduced capacity allows a larger batch and indirectly improves throughput. With small batches where weight reads dominate, it barely reduces TPOT. Additionally, this kind of structure must be adopted during training; a trained model cannot be retrofitted at inference time.
6. Comparison of Common Architectural Changes
| Change | Prefill compute | KV capacity & writes | Decode per-step reads | Main use case |
|---|---|---|---|---|
| Last-layer compute skipping | Small drop, capped at about (1−k)/L | Unchanged | Unchanged | Frameworks supporting skipped computation, long inputs |
| GQA (fewer KV heads) | K, V projections drop | Drops in proportion to head count | Drops proportionally | Long context, large batch |
| Cross-layer KV sharing | Drops by about k × sharing ratio | Drops in proportion to sharing ratio | Essentially unchanged | Memory-capacity-limited long context |
| Weight quantization (e.g., int8, int4) | Usually unchanged, unless low-precision compute is used | Unchanged | Weight read volume drops | Small-batch Decode |
| MLA (compressed KV) | Depends on implementation; may increase | Capacity drops | Read volume drops, but decoding requires extra compute | Long context; effect depends on implementation |
7. Benchmarking Checklist
- Fix the model, framework version, driver, GPU model, and clock settings. Warm up for a few rounds before timing each experiment.
- Record TTFT, TPOT, throughput, and peak memory separately. TTFT mainly reflects Prefill; TPOT mainly reflects Decode.
- Change only one variable at a time, such as input length N, concurrency b, or history length S. Run each configuration multiple times and report percentiles (p50 and p95), not just averages.
- Compare memory usage before and after the change. Verify KV cache capacity with the formula in Section 2 and cross-check it against the framework’s reported statistics.
- Use Nsight Systems or the framework’s built-in profiler to confirm the bottleneck. If GPU utilization is low during Prefill, first check whether bandwidth, kernel scheduling, or communication is the limiting factor.
- If measured results deviate significantly from formula estimates, first check effective bandwidth, effective compute, operator fusion, and communication overhead before concluding whether the architectural change works.
