What is Prefill?

The first stage of LLM inference, processing all input prompt tokens together; it tends to be compute-bound. The following token-by-token generation is called decode.

Field: AI Hardware & Software Stack

Japanese name: プリフィル

Why inference splits into two stages

A language model handles text as small units called tokens. In inference, the tokens of the input text (the prompt) are first passed through the model together. This stage is prefill. Every input token is known from the start, so they can be laid side by side and computed at the same time. An output token cannot be computed until the token before it has been chosen. The decode stage that follows therefore advances one token at a time.

What happens during prefill

In prefill, each layer computes attention for the input tokens. The keys and values obtained along the way are saved in the KV cache. Decode reads this cache, so the input does not have to be computed again. When prefill ends, the first output token can be chosen. The wait before a user sees the first output therefore includes the prefill time. The longer the input, the longer this time becomes.

Why it tends to be compute-bound

In prefill, a weight read from memory is reused for many input tokens. With a 1000-token input, for example, each weight in a matrix takes part in the multiply-add work of 1000 tokens. The computation becomes a matrix-matrix product and keeps the arithmetic units busy. Speed is therefore set mainly by compute performance. In decode, one step computes only one new token. When the batch is small, little computation is done for the amount of weights read, so speed tends to be set by memory bandwidth.

Try it: questions about Prefill

LLM inference splits into a stage that processes the input prompt together (prefill) and a stage that generates output one token at a time (decode). With small batches, what mainly limits each stage?

  1. APrefill by memory bandwidth, decode by compute
  2. BPrefill is limited by compute, decode by memory bandwidth
  3. CBoth by storage speed
Show answer and explanation

Answer: B. Prefill is limited by compute, decode by memory bandwidth

In prefill, hundreds or thousands of input tokens are computed together, so weights are reused across many tokens, the work becomes matrix-matrix products and it is compute-bound. In decode, every new token requires reading all the weights for just one token's worth of computation, so intensity is low and it is memory-bound. Decode speed is therefore roughly memory bandwidth divided by model size (with long contexts, reading the KV cache adds to this).

FAQ

How do prefill and decode differ?

They differ in how many tokens are processed at once. Prefill processes the input tokens together, while decode produces the output one token at a time. As a result, prefill tends to be limited by compute performance and decode by memory bandwidth.

Is this the same as 'prefill' meaning writing the start of the response in advance?

No, that is a different thing. Some chat APIs let the caller of the API supply the beginning of the model's response in advance, and this is also called prefill. In this glossary, prefill means the inference stage that processes the input together.

Questions that mention Prefill (1)

Prefill appears in 2 practice questions across 1 topic, including AI Hardware & Software Stack.

  1. In LLM inference, why does weight-only 4-bit quantization (W4A16, activations kept in FP16) especially speed up decode?

Books on AI Hardware & Software Stack Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary