Skip to content

23.4 Chunked prefill (mixed prefill + decode batches)

The continuous batching scheduler prefills a prompt whole, in one step, and a prompt longer than the budget may only run in a step of its own. Two costs follow. While a 2000-token prompt runs, every decoding request waits for that step, so its time per output token (TPOT) spikes. And a long prompt can wait a long time for a step of its own while others decode. Chunked prefill splits the prompt across steps and mixes the pieces with decodes, so both waits are bounded.

Concepts from other sections (7)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

The solution is small, because the scheduler already has the seam it plugs into. It builds on the scheduler and its PrefillPolicy from the previous section and on the runner’s ForwardBatch from the Candle model runner. There is no Rust trait contract file yet, so the interface below pins the API; the configuration knob is prefill_chunk in the [engine] table of config/runtime.schema.json. The code is rust/crates/tl-engine/src/chunk.rs: the Chunked prefill policy, plan (one step’s mixed batch for the runner and the rows to sample), and split.

Learning objectives

  • Define TTFT and TPOT and explain how chunking trades a little of the first for a bound on the second.
  • Implement the Chunked policy and explain where the “decodes first” rule actually lives.
  • Build a mixed batch with one sequence per decode and per chunk, sampling only rows that predict an unknown token.
  • Argue why chunking changes when K and V are computed but not their values.

The quantities involved:

SymbolMeaningType
β\betatoken budget per step (max_batch_tokens)integer
κ\kappachunk size (prefill_chunk)integer
mmtokens of a prompt still to prefillinteger
dddecodes in the stepinteger
τ(tokens)\tau(\text{tokens})time of one step, roughly linear in its tokensseconds

Time to first token (TTFT) is the time from arrival to the first generated token; for a prompt it needs every chunk done. Time per output token (TPOT) is the time between a running request’s tokens, which is one step. A step’s time grows with the tokens it processes, so TPOT is about τ(d+prefill tokens in the step)\tau(d + \text{prefill tokens in the step}).

Chunking fixes the step’s size. Each step takes the dd decodes first, then gives each prefilling request min⁡(m,κ,budget left)\min(m, \kappa, \text{budget left}) tokens, so TPOT is bounded by τ(β)\tau(\beta) whatever prompts arrive. A prompt of mm tokens needs about ⌈m/min⁡(κ,β−d)⌉\lceil m / \min(\kappa, \beta - d) \rceil steps to its first token instead of one big step, so TTFT for long prompts rises a little while TPOT for everyone else stops spiking. The right κ\kappa is measured, not derived: Serving metrics and tracing exports TTFT and TPOT histograms, and the load generator drives the engine so you can read them.

Splitting a prompt into chunks is safe only if the K and V of each position, and the final logits, agree with one whole prefill. Absolute positions (pos =ps+i= p_s + i, so RoPE turns by the same angle) and writing each chunk’s K and V to the same blocks preserve the cached values. Candle can use different floating-point reduction paths for different tensor shapes, so chunked and whole logits are compared within a small tolerance rather than bit for bit.

Once the scheduler has granted chunks, plan(scheduler, schedule_output) builds what the runner takes:

  • one ForwardSeq per decode: the newest token, at position nr−1n_r - 1;
  • one ForwardSeq per chunk: tokens start..start + len at start;
  • the rows to sample: every decode, and each chunk whose last flag says it reaches the end of the sequence.

A middle chunk’s logits predict a token the prompt already has, so they are computed and thrown away. The policy itself is one line: Chunked { chunk }.chunk_len(m, budget, _) = min(m, chunk, budget).

Take budget β=8\beta = 8, κ=4\kappa = 4, and blocks of 4. A has a 2-token prompt and max_tokens 3; B has a 10-token prompt and max_tokens 1; both arrive at step 1. The schedule, step by step:

StepDecodesChunksTokensSampled
1noneA 0..2 (last), B 0..46A
2AB 4..85A
3AB 8..10 (last)3A, B

B’s first token comes at step 3, and no step exceeds 8 tokens. Without chunks (WholePrompt), B (10 > 8) cannot share a step with A’s decodes, so it waits until A finishes and then runs alone in a 10-token step 4.

split(10, 4) gives the boundaries a prompt alone in the engine would use: (0,4),(4,4),(8,2)(0, 4), (4, 4), (8, 2), every token once, in order.

The whole interface is four items:

rust/crates/tl-engine/src/chunk.rs
pub struct Chunked { pub chunk: usize }
impl PrefillPolicy for Chunked { fn chunk_len(&self, remaining: usize, budget: usize, alone: bool) -> usize; }
pub struct StepPlan<'a> { pub batch: ForwardBatch<'a>, pub sample: Vec<(usize, RequestId)> } // (logits row, request)
pub fn plan<'a, B: BlockSpace>(s: &'a Scheduler<B>, out: &ScheduleOutput) -> StepPlan<'a>;
pub fn split(n: usize, chunk: usize) -> Vec<(usize, usize)>; // a chunk of 0 means 1

You turn chunking on with Scheduler::new(cfg, blocks).with_prefill_policy(Box::new(Chunked { chunk })); the tokio server does so when prefill_chunk > 0.

Try to predict each step before reading it.

  1. A step’s time grows with its tokens, so bounding TPOT means bounding the tokens in every step: the budget β\beta must hold for prefill tokens too, not just decodes.
  2. The scheduler already has a seam for “how much of this prompt now”: PrefillPolicy::chunk_len(remaining, budget, alone). Chunking is therefore a new policy, not a new scheduler: take min⁡(m,κ,budget)\min(m, \kappa, \text{budget}).
  3. Decodes are planned first by the scheduler, so whatever the policy grants comes out of what decodes left. That is the “decodes first, then chunks” rule with no extra code.
  4. The runner already takes a batch of (tokens, start, blocks) sequences, so a mixed batch is just more of them: one per decode (one token at n−1n - 1), one per chunk (its slice at start). No new kernel is needed.
  5. Only rows whose logits predict an unknown token may be sampled: every decode, and a chunk that reaches the end of the sequence. A middle chunk’s last logits predict a prompt token that already exists.
  6. Chunking is correct only because positions are absolute and KV goes to the same blocks, so the last chunk sees exactly the keys a whole prefill would have written.

The policy is one expression. chunk.max(1) guards a misconfigured chunk of 0 (which would otherwise grant nothing forever), and alone is ignored: unlike WholePrompt, a chunk never needs a step of its own because it always fits the budget.

impl PrefillPolicy for Chunked {
fn chunk_len(&self, remaining: usize, budget: usize, _alone: bool) -> usize {
remaining.min(self.chunk.max(1)).min(budget) // min(m, kappa, budget left after decodes)
}
}

In the worked example at step 1, A’s 2-token prompt gets min⁡(2,4,8)=2\min(2, 4, 8) = 2 (and is last), then B gets min⁡(10,4,6)=4\min(10, 4, 6) = 4.

Rows are numbered in the order sequences are pushed: decodes first, then chunks. sample records (row, request) for exactly the rows whose logits are used. Each ForwardSeq borrows its tokens and block table from the scheduler, so building the batch copies nothing.

pub fn plan<'a, B: BlockSpace>(s: &'a Scheduler<B>, out: &ScheduleOutput) -> StepPlan<'a> {
let mut p = StepPlan::default();
for &id in &out.decode {
let Some(toks) = s.tokens(id) else { continue };
let n = toks.len();
p.sample.push((p.batch.seqs.len(), id)); // every decode is sampled
p.batch.seqs.push(ForwardSeq {
tokens: &toks[n - 1..], // the newest token only
start: n - 1, // at position n - 1, not n
blocks: s.blocks().block_table(id),
});
}
for c in &out.prefill {
let Some(toks) = s.tokens(c.id) else { continue };
if c.last {
p.sample.push((p.batch.seqs.len(), c.id)); // only a chunk that ends the sequence
}
p.batch.seqs.push(ForwardSeq {
tokens: &toks[c.start..c.start + c.len], // this chunk's slice
start: c.start, // absolute start: RoPE and q_offset follow
blocks: s.blocks().block_table(c.id),
});
}
p
}

The engine then runs ModelRunner::forward(&plan.batch), samples logits.row(row) for each (row, id) in plan.sample, and passes the tokens to Scheduler::on_step, which ignores any request whose sequence is not fully computed.

split returns the boundaries a lone prompt would use. step_by(c) gives the starts, and c.min(n - s) shortens the last chunk so it never runs past the prompt.

pub fn split(n: usize, chunk: usize) -> Vec<(usize, usize)> {
let c = chunk.max(1); // a chunk of 0 means 1
(0..n).step_by(c).map(|s| (s, c.min(n - s))).collect() // (start, len); last one short
}

split(10, 4) is [(0, 4), (4, 4), (8, 2)].

The full solution is chunk.rs; the phases that call the policy are in sched.rs.

plan is O(sequences in the step)O(\text{sequences in the step}) and allocates only the two small vectors. Chunking adds no forward work; it only spreads it over more steps. The real cost is that a prompt of mm tokens takes about ⌈m/min⁡(κ,β−d)⌉\lceil m / \min(\kappa, \beta - d) \rceil steps to its first token.

Why a policy object instead of chunk logic inside the scheduler? The scheduler’s phases, budget, and preemption stay unchanged, chunking becomes a three-line plug-in, and WholePrompt remains available for comparison. Why decodes first, rather than a fair share of the budget between prefill and decode? Decodes are the latency users already see (TPOT), and putting them first guarantees they never wait on a prompt. vLLM v1 unifies the two under one budget without phases, which is more flexible but harder to reason about. Why not sample middle chunks and discard the token in the scheduler? It would work, but it spends sampler work and RNG draws on tokens that are thrown away, and a stray draw would desynchronize seeded streams. Not sampling them is both cheaper and simpler.

One tradeoff is accepted: chunked and whole prefills agree within floating-point tolerance, not bit for bit, because Candle may pick different reduction paths for different shapes. Greedy continuations are preserved on the tested model, but a near-tie could in principle flip.

A correct implementation handles these cases:

CaseKind of checkChecksWhy it matters downstream
the hand-worked planunitthe worked example, step by step, through the schedulerthe worked example
the same workload without chunksunitB waits for step 4 and a 10-token stepthe problem chunking solves
split boundariesunit(0,4),(4,4),(8,2)(0,4), (4,4), (8,2) and the edge caseschunk boundaries cover each token once
plan samples only sequence endsunitdecode at n−1n - 1, chunk tokens and start, only the right rows sampledthe engine samples only real next tokens
the token budget is never exceededpropertyrandom prompts up to 4x the budget: every step within β\beta, every output exactthe TPOT bound holds
a long prompt progresses beside decodespropertya 40-token prompt starts within 4 steps beside 3 decodes; without chunks it waits about 38why chunking exists
chunked equals whole prefilldifferentialchunk sizes 1, 3, 7, 16 give logits within tolerance and preserve greedy continuationschunking never changes answers

The mistakes here are mostly off-by-ones in positions or a missing min:

PitfallSymptomHow the solution avoids it
A chunk that ignores the budget leftsteps exceed max_batch_tokens; TPOT spikes return.min(budget) in chunk_len
A chunk that ignores prefill_chunklong prompts still run whole.min(self.chunk.max(1))
Sampling a middle chunk’s logitsextra tokens appear in the answer, or the scheduler must guard against themplan pushes to sample only when c.last
Placing a decode token at position nneach decode reads and writes one slot offstart: n - 1
A last chunk of full size past the promptthe plan reads past the promptsplit uses c.min(n - s)
Chunk-relative positions or q_offset 0chunked output differs from whole outputstart: c.start, absolute, passed through to RoPE and attention in the runner

Chunked prefill depends on the scheduler, which consults PrefillPolicy in phases 2 and 3 and marks sampled rows with Chunk.last, and on the runner’s Candle-backed attention, absolute positions, and Rust KV cache, whose chunk invariance makes chunking output-preserving. Going forward, the tokio server runs plan every step and reads prefill_chunk from runtime.toml, and Serving metrics and tracing exports TTFT and TPOT, which is how prefill_chunk is chosen for a given machine.

Production systems go further:

This solutionProduction equivalentWhat it addsWhere to look
one fixed chunk sizeSarathi-Serve’s stall-free batchingchunk size from a TPOT targetSarathi-Serve
decodes first, then chunksvLLM v1’s unified schedulerone token budget across prefill and decode, no phasesvllm/v1/core/sched/scheduler.py
mixed batches on one enginedisaggregated prefillprefill and decode on different machinesDistServe, Disaggregated prefill and decode
  • chunked prefill: prefilling a prompt over several steps in pieces of at most prefill_chunk tokens, mixed with decodes.
  • TTFT (time to first token): time from a request’s arrival to its first generated token.
  • TPOT (time per output token): time between successive tokens of a running request, about one step.
  • mixed batch: one forward step containing both decode sequences and prefill chunks.
  • last chunk: the chunk that reaches the end of a sequence, the only chunk whose logits are sampled.
  • A long prompt prefilled in one step makes that step slow and every running request waits for it; in chunks of at most prefill_chunk tokens it shares each step with the decodes.
  • Every step stays within max_batch_tokens: decodes first, then prefill chunks in what is left.
  • A mixed batch is one sequence per decode and one per chunk; only decodes and chunks that end their sequence are sampled.
  • Chunking changes when K and V are computed, never their values: the logits after the last chunk agree with one whole prefill within floating-point tolerance.
  1. Why does plan place a decode at position n−1n - 1 and not nn?
    AnswerThe newest token, at index $n - 1$, has not been fed yet; feeding it writes its K and V at slot $n - 1$ and produces the logits for token $n$. Placing it at $n$ would write one slot off and rotate it by the wrong RoPE angle.
  2. Why is a middle chunk’s row not sampled?
    AnswerIts last logits predict the next prompt token, which is already known. Sampling it would add a wrong token to the answer and consume a random draw.
  3. Where does the “decodes first” rule live, given that Chunked only computes a minimum?
    AnswerIn the scheduler's phase order: decodes take their tokens from the budget in phase 1, so the `budget` that phases 2 and 3 pass to `chunk_len` is what decodes left.
  4. Why does chunking not change the answer?
    AnswerEach chunk is fed at its absolute start, so RoPE angles and causal visibility are those of a whole prefill, and its K and V go into the same blocks and slots. The last chunk then attends over the same keys and values a whole prefill would have computed.
  5. In the worked example, why does step 2 process 5 tokens?
    AnswerA decodes (1 token) and B receives $\min(6, 4, 7) = 4$ prompt tokens (positions 4..8), for $1 + 4 = 5$.