Why can a model process a long input together but still generate a short answer one token at a time? The input is already known; later outputs depend on earlier results. Following a very short request makes prefill, decode, and caching easier to distinguish.

Start with an ordinary causal Transformer: fixed weights, no speculative decoding, no early stopping, and no multimodal encoder. Once that path is clear, we can locate its state in vLLM and SGLang.

Four inputs and three outputs: how many forwards?

In one lineFour inputs and three outputs take one prefill and two decodes. Less queueing brings the first token sooner but does not shorten later gaps.

The prompt is A B C D; the answer will be E F G. Start with an empty cache and process the prompt in one pass. Read across each row: which positions has the model processed, and which token has it just picked but not yet fed back in?

Phase Input this step Cached positions New output
Prefill A B C D KV for A B C D E
First decode E KV for A B C D E F
Second decode F KV for A B C D E F G

Each new output is sampled from the last position's logits in that forward. KV means the keys and values retained by attention layers. The table lists their token positions rather than expanding every layer's tensors.

If the request ends after G, G needs no further forward. Sampling E and computing E's KV happen at different times. The output at D produces E's token ID; feeding E into the next forward creates E's own keys and values.

This example therefore takes one prefill and two decode forwards, not three decode forwards simply because there are three output tokens. These are not kernel counts: each forward contains many operations, and prefill can itself be chunked.

Now put the same request on a clock. Suppose queueing takes 40 ms, prefill takes 60 ms, and each subsequent decode takes 20 ms. Ignore tokenization, transport, and other overheads for this calculation:

Time since submission Work just completed What the user sees
40 ms Queueing ends; computation starts No output yet
100 ms Prefill finishes; E is sampled First token
120 ms E is processed; F is sampled Second token
140 ms F is processed; G is sampled; request ends Third token

Time to first token (TTFT) is 100 ms, both subsequent output intervals are 20 ms, and end-to-end latency is 140 ms. Shorter queueing can make E appear sooner without shortening the wait between F and G. These hypothetical timings distinguish metrics; they are not engine measurements.

flowchart TD
    P[Known input A B C D] --> F[Prefill: process four positions]
    F --> K[Cache KV for A B C D]
    F --> E[Sample E]
    K --> D[Decode: input E and read historical KV]
    E --> D
    D --> U[Append KV for E]
    D --> O[Sample F, then continue or stop]

Why is parallel prefill not looking into the future?

In one lineParallel prefill only computes known positions; the causal mask still hides the future. The cache skips re-projection, but each query still reads all history.

All four input tokens are already given. Each layer can compute their projections together while a causal mask restricts attention: A cannot attend to B, and B cannot attend to C. Parallel computation over known positions does not grant access to future positions.

Count the visible query–key pairs for one head. Prefill has four queries and four keys, but not all 16 pairs are allowed. A sees only A, B sees A and B, and so on: the lower triangle has $1+2+3+4=10$ pairs. Feed E back in and only E needs a new query. It sees A through E, or five pairs.

Without the KV cache, running a full forward over A through E would give $1+2+3+4+5=15$ allowed pairs. The last row is the same five pairs either way. Caching saves recomputing the first four rows; it doesn't shorten E's context. These are logical pairs, not a GPU instruction count or a prediction of a threefold speedup. The last row computes:

$$ o_t=\operatorname{softmax}\!\left(\frac{q_tK_{\leq t}^{\mathsf T}}{\sqrt{d}}\right)V_{\leq t}. $$

Historical keys and values need not be projected again, but the new query still attends to the relevant historical keys and combines their values. KV caching avoids recomputation; it does not eliminate long-context attention reads. Sliding windows, sparse attention, and compressed states change which history is accessed and need separate analysis.

In training, the recorded answer is usually known too. A causal mask allows predictions at several answer positions in one forward, followed by label shifting and loss masking. This computes probabilities given prefixes; it is not free-running generation in a single forward. See the KV cache derivation for more detail.

Why can the bottleneck change on the same GPUs?

In one linePrefill runs many positions at once and is often compute-bound; small-batch decode mostly reads weights and KV, so it is often bandwidth-bound.

Prefill processes many positions together, often producing larger matrix multiplications in linear layers. Small-batch decode advances only a few tokens per request while potentially reading substantial weights and historical KV. Prefill is therefore often better able to use compute capacity; decode can be limited by memory bandwidth or small-operation launch overhead.

That is a starting hypothesis, not a rule. Long-context attention, communication, MoE routing, a large decode batch, and CPU scheduling can change the bottleneck. Use this lower-bound intuition to ask what a step waits for, then profile it:

$$ T_{\mathrm{step}}\gtrsim\max\!\left(\frac{F}{P_{\mathrm{eff}}},\frac{M}{B_{\mathrm{eff}}}\right). $$

F is the operation count, M the bytes moved, and the denominators are effective compute and bandwidth. Communication, launches, dependencies, and queues add costs. Substituting advertised hardware peaks does not produce a measured latency.

Arithmetic intensity says which side of the roofline each phase falls on. Prefill handles hundreds or thousands of positions at once, so linear layers do hundreds of operations per byte of weights read; small-batch decode does only one or two. The numbers are in the decode roofline example, and how these two times decide how many requests one GPU can take is in capacity.

For a KV-only capacity example, take 32 layers, 8 KV heads, head dimension 128, and 2 bytes per element. Across all layers, one token requires:

$$ 2\times32\times8\times128\times2=131072\ \text{bytes}=128\ \text{KiB}. $$

Caching 4096 tokens requires 512 MiB; 16 requests of that cached length require 8 GiB. This excludes weights, block waste, and workspace, and assumes no cross-request sharing or device sharding. It is whole-model logical KV, not necessarily a per-GPU allocation. Layer types and implementation also matter. Changing the KV-head count changes this capacity: an architectural choice such as GQA directly affects serving concurrency.

A training step looks like prefill

In training the whole sequence is known, so a forward pass is like one prefill over the whole batch: every position goes through the matrix multiplies together, with no KV cache and no token-by-token decode loop. The backward pass adds about twice the forward arithmetic, roughly 6 operations per parameter per token in total; see the pretraining pipeline. Each GPU still processes many thousands of tokens at a time, which puts the matrix multiplies far to the right of the ridge point, so training is judged by how much of peak compute it uses, its MFU. Elementwise operations and communication between GPUs still slow it down, and MFU is often only 30–50%. Training memory goes to activations, gradients and optimizer state rather than a KV cache; see precision and memory.

RL training is the exception: a rollout has to generate answers first, and that is decode. This is why RL systems often hand generation to an inference engine and keep only the forward and backward passes on the training side; see RL systems.

The scheduler sees unfinished work, not just sentences

In one lineOne step can advance an ongoing answer by 1 position and a new prompt's prefill by 7. Same batch, different work.

Suppose a step can compute 8 new token positions. Request A has just arrived with a 12-token prompt; B is already generating and needs one position advanced. One legal plan assigns B one position and A seven prefill positions. A then has five prompt positions left, while B can continue generating next round.

A cannot start answering after those seven positions: its full prompt has not been processed. It retains intermediate KV and continues with the remaining input next round. B already has its full prefix, so it can sample a new output now. Sharing a batch does not mean doing the same kind of work.

This is an illustrative schedule, not a policy any framework promises to use. Three constraints matter together:

  • Compute budget: how many new positions fit, not merely how many requests.
  • Cache budget: where those positions can be written; admitting more work without space may require preemption or waiting.
  • Latency goals: a large uninterrupted prefill can stall ongoing output, while very small chunks add scheduling and execution overhead.

Continuous batching lets finished requests leave and new requests join. Chunked prefill splits a long input across iterations. They are related, not identical switches. P–D disaggregation is another choice: separate resource pools add KV transfer and two queues. See inference deployment layouts.

Locate the work in vLLM and SGLang

In one linevLLM schedules by tokens computed and still to go, without needing separate prefill and decode loops. Identical text does not prove reusable cache.

These links pin snapshots from 2026-10-30. The table follows ordinary generation; VLMs, speculative decoding, and specialized hardware backends add branches.

Action to trace vLLM SGLang
Collect state and advance an iteration EngineCore.step event_loop_normal, with a separate overlap path
Assign work to requests Scheduler.schedule get_next_batch_to_run
Turn the plan into model execution V2 execute_model Worker conversion from ScheduleBatch to ForwardBatch
Find reusable prefixes KVCacheManager.get_computed_blocks RadixCache.match_prefix

vLLM's scheduler tracks computed tokens and work still to advance; it does not require two entirely separate prefill and decode scheduling loops. EngineCore passes a plan to the executor, and the runner manages device-side state. The plan is not already the full collection of GPU input tensors.

SGLang's normal loop receives requests, chooses a batch, executes it, and processes results. ScheduleBatch has extend/decode preparation paths; the worker constructs ForwardBatch and calls the model runner. Overlap mode changes CPU/GPU overlap, so the normal-loop sequence is not a literal timing diagram for every configuration.

Cache lookup also has boundaries. This vLLM snapshot retains the need to recompute the final position for next-token logits; block alignment can enlarge that recomputation. SGLang's radix lookup considers an extra_key namespace as well as token IDs. Identical text is not sufficient evidence of reusable state, and radix trees are not its only cache backend. The original PagedAttention and SGLang papers explain the early designs; pinned source governs the behavior discussed here.

Which optimizations can affect learning?

In one lineBatch size, output limits, temperature and weight refreshes change more than speed: RL must also check rollout age, truncation and the logprobs' distribution.

A serving endpoint cares about answer quality, time to first token, and output intervals. An RL generation backend must also deliver samples whose training meaning is recoverable.

Change First serving-side effect Additional training check
Larger batches Potentially higher throughput, potentially more waiting Policy age and whether faster-finishing samples are preferentially consumed
Lower output limits Requests finish sooner Truncation rates, length distribution, and changed rewards
Different temperature or top-p Different diversity and success rates Which sampling distribution the logprob represents, and whether the objective matches it
Refreshed generation weights A new model begins answering Invalidated KV, in-flight requests, and version boundaries

“The algorithm is unchanged” is not a substitute for verification. Even when the intended computation is equivalent, floating-point ordering can change samples near a decision boundary. Compare logits or logprobs on fixed inputs first, then generation statistics and downstream evaluation, rather than requiring every random answer to match exactly.

For example, reducing an output limit from 256 to 64 may reduce mean completion time. But an answer that needed token 65 is now truncated. If that becomes a low-reward training rollout, the feedback has changed too. Separate “generated less content” from “generated the same content faster.”

Verify a small instance

In one lineThe experiment matches full attention against incremental KV and breaks once the future is visible. Compare real schedules on the same length mix.

Run the small experiment: it compares full causal attention with incremental KV computation, then deliberately allows future attention to demonstrate a mismatch. It also checks chunked execution over the same positions and the KV capacity arithmetic.

For a real engine, fix weights, tokenizer, input tokens, positions, precision, and sampling settings. Record cold/warm cache state, queue time, time to first token, output intervals, and complete request latency. Equal request counts do not imply equal token work; preserve the input/output length distribution when comparing schedules.

To see how this generation path feeds RL training, follow an update through NeMo RL.