14/30
Part 4 of 7 · Long promptsLearn and do

Chunked prefill rides along with the decode steps

Cut the prompt into chunks, and prefill one chunk in each decode step. The step still reads the weights once, so the chunk's arithmetic rides along on a read the decoders were paying for anyway. Sarathi-Serve, the paper behind the idea, calls the result stall-free schedules, and vLLM ships it as chunked prefill.

import math


def step_with_chunk(batch: int, context: int, chunk: int) -> float:
    """One step that decodes batch sequences and prefills chunk prompt tokens."""
    memory_ms = (weights_gb + batch * kv_per_token * context / 1e9) / 3.35
    math_ms = 2 * params * (batch + chunk) / (989e12 * 0.5) * 1000
    return max(memory_ms, math_ms)


for chunk in (256, 512, 2048):
    ms = step_with_chunk(64, 2000, chunk)
    steps = math.ceil(8000 / chunk)
    print(f"chunks of {chunk:4,}: {steps:2} steps of {ms:4.1f} ms, the new prompt waits {steps * ms:3.0f} ms")
Predict

With chunks of 512 tokens, what do the 64 decoding users see?

Pick one to see what it prints.
Pick an answer above to see the rest