Prefill
How it works
In the prefill phase the model runs one (or a few, with chunked prefill) forward pass over the entire prompt sequence of length N. Because all input tokens are known in advance, the attention and MLP computations are done as large matrix multiplications with high unit utilization (compute-bound). During this pass, for each layer the keys and values (K, V) of all N tokens are written to the KV cache. At the end the model produces a probability distribution over the next token and samples the first generated token. Control then moves to the decode phase, which generates subsequent tokens one at a time using the filled KV cache. The compute cost of prefill grows with prompt length (attention ~O(N^2)).
Problem solved
Autoregressive generation requires the model to process the entire prompt and build a context representation before producing the first token. The prefill phase does this efficiently by processing all prompt tokens at once (in parallel) rather than one by one, and by writing the attention keys and values into the KV cache so the decode phase need not recompute them. Prefill determines the time to first token and heavily loads the compute units.
Key mechanisms
Strengths & limitations
Components
Processing all N prompt tokens simultaneously in a single pass, realized as large matrix multiplications.
Writing the attention keys and values of all prompt tokens for every layer, to avoid recomputing them in the decode phase.
Computing the next-token distribution at the last position and sampling the first output, which determines TTFT.
Implementation
A single long prefill can occupy the GPU for a long time and increase token latencies of other requests in the batch.
Repeated prefixes (e.g. a shared system prompt) recomputed from scratch waste prefill compute.
Evolution
As generative transformers spread, inference splits visibly into compute-bound prefill and memory-bound decode.
Work on efficient serving (e.g. vLLM, Sarathi, DistServe) introduces chunked prefill and prefill/decode disaggregation for better GPU utilization.
Hyperparameters (configurable axes)
Number of prompt tokens processed per chunk when interleaving with decode.
Sharing the KV cache of common prefixes across requests.
Computational complexity
Time complexity: O(N^2 d) uwaga + O(N d^2) projekcje. Space complexity: O(N x L x d) na KV cache promptu.
TTFT dominated by prefill grows with prompt length; for long contexts the O(N^2) attention term becomes noticeable. Prefix caching of a shared system prompt can cut prefill time to nearly zero for repeated prefixes, while chunked prefill smooths other requests' token latencies by interleaving prefill fragments with decode steps.
Compute bottleneck
Prefill is compute-bound: the large matrix-matrix multiplications of attention and MLP saturate Tensor Core units.
Execution paradigm
All tokens and compute paths are active simultaneously.
Prefill runs a dense forward pass with no conditional routing (unless the model is MoE).
Parallelism
All prompt tokens are processed simultaneously, making prefill highly parallel and compute-bound.
Hardware requirements
The large matrix multiplications of prefill maximally utilize Tensor Cores; this is the phase where the GPU reaches high FLOPs utilization.
The parallel, compute-bound nature of prefill maps well to the MXU units in TPUs.