Step-by-Step vLLM Serving Loop Tour | Generated by AI

Home 2026.08

Here’s my recommended breakpoint tour. This codebase is a minimal vLLM clone, so the goal is to watch the serving loop: how a request flows through scheduling → KV cache allocation → model forward → token append, and how prefill and decode interleave (continuous batching).

Best run via the experiment that already drives the loop step-by-step (not the one-shot run_inference.py):

python3.12 experiments/04_scheduler_watch.py

It prints a scheduler trace with patched logging, which is the perfect scaffold for breakpoints.


The tour (in this order)

1. The outer loop — see the whole pipeline first

2. The scheduler — the “continuous batching aha”

3. KV cache block management — the PagedAttention core

4. Tensor prep — the trickiest bookkeeping

5. The moment KV is actually written/read

6. Closing the loop


Watch expressions worth having at stops 2–3: seq.num_cached_tokens, seq.num_scheduled_tokens, seq.num_tokens, seq.block_table, len(self.block_manager.free_block_ids), and at stop 4: seq.block_table[i] * self.block_size + offset.

If you want an IDE-friendly path, .idea/ is present (PyCharm), so you can set these as breakpoints in the GUI; for a quick CLI check, insert breakpoint() calls and step with python3.12 experiments/04_scheduler_watch.py. Note: pdb/stepping works fine since experiments/04 drives the step loop on the main thread (the multiprocessing in ModelRunner.__init__ is only for TP>1).


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