Prefill: First Token and Cache Fill | Generated by AI

Home 2026.08

Yes, you’re basically right — but let me be precise, because “prefill” in this file refers to a phase of request processing, and filling the KV cache is only part of what it does.

What prefill means here

In LLM serving, a request goes through two phases:

1. Prefill (context.is_prefill_prefill_forward)

2. Decode (_decode_forward)

The key lines

# forward():
if has_cache and context.slot_mapping is not None:
    store_kvcache(k, v, k_cache, v_cache, context.slot_mapping)  # ← fill cache

if context.is_prefill:
    return self._prefill_forward(...)   # prompt processed at once → 1st token
else:
    return self._decode_forward(...)    # 1 new token, attends to cached K/V

So your intuition, corrected

Interesting detail in this file: there’s also prefix caching support — in _prefill_forward, when block_tables is set and the cache already has content (_gather_kv_from_cache), a new request can reuse cached K/V from a previously-seen prefix instead of recomputing it. In that case prefill only computes K/V for the new suffix tokens and attends over the gathered cached prefix.


Generated by AI. Curating and sharing still takes effort. If you find it useful, feel free to donate. WeChat: @lzwjavaWeChat QR · X: @lzwjava · Say hi 👋

Back Donate