How the Megatron prefill/decode engine fleet now orders refit, shutdown and startup

PR #5024 (4 of 6, reviewed against #5023) · reviewed at ea1a6e22, re-checked at 9df434a7 (the reviewed files are unchanged since d2e22762) · review still pending

Before
PR 3 suspended every engine at once, stopped them in index order at shutdown, and built them one at a time.
This PR
Suspends prefill, then decode. Shutdown stops decode first and reports each stopped decode engine to prefill before stopping it. Builds up to 8 engines at once.
Status
Open — two parts need changes in this PR. The report step at shutdown can stall teardown for hours and frees nothing (C3). Code for a refit wait that a later commit removed is still there (C4). At 1,000 engines, startup and shutdown still go one engine (or 8 engines) at a time; see the last section (review comments C9–C11).

Two pages on this PR, one question each. This one: what the PR changes compared with PR 3.

Background. A prefill engine reads the prompt and keeps its KV blocks pinned until a decode engine has pulled them. A refit suspends every engine, copies the new weights in, and resumes them. Engines run the persist KV mode, so suspend and resume leave the KV memory, the block allocator and the pinned blocks alone. Dotted words have a short explanation: hover or tap them.

Example fleet: 1 prefill engine P0 and 2 decode engines D0, D1. Each row is one operation in MegatronDynamoGeneration, PR 3 on the left, this PR on the right. Boxes in one column run at the same time; arrows mean "then".

PR 3 (base) PR 4 (this PR) Refit: suspend P0 D0 D1 one wave P0 D0 D1 prefill, then decode no wait for pinned blocks Refit: resume P0 D0 D1 one wave P0 D0 D1 one wave, unchanged Shutdown P0 D0 D1 index order, then services D0 D1 report D0, D1 ≤610 s each P0 C3: frees nothing, can stall teardown Startup: build P0 D0 D1 one at a time P0 D0 D1 ThreadPoolExecutor rounds of up to 8 engines registered in launch order (refit rank order) prefill decode new: run at once problem (review comment) unchanged

The same change as pseudocode

Refit suspend · driver process

# driver, MegatronDynamoGeneration.suspend_for_refit
call_engines(all engines, suspend)                     # PR 3: one wave
call_engines(prefill, suspend)                         # each wave: Ray calls in parallel
call_engines(decode, suspend)                          # no wait for pinned blocks
on failure: resume every engine the suspend reached       # then re-raise

Shutdown · driver process

# driver, MegatronDynamoGeneration.shutdown
for engine in engines: stop(engine)                       # PR 3: index order
owners = read handoff owner from each decode rank 0      # ≤600 s
for d in decode: stop(d)                                  # one at a time
for owner in owners:                                      # C3: one Ray call per owner
    prefill0.confirm_decode_terminated(owner)             # ≤ 600 + 10 s each
for p in prefill: stop(p)                                 # frees every pinned block anyway
stop frontend, NATS, etcd

Real code: PR 3 shutdown, PR 4 shutdown, _confirm_decode_terminated.

The 600 s is today's default dynamo_cfg.control_timeout_s. C1 removes that key (following #4801 C9), so the caller's timeout or a fixed shutdown constant takes its place. The problem does not depend on the number: each report still waits its full timeout, one after another.

🔴 review comment C3 — dynamo_generation.py:1381

Left behind: a pause for a wait that is gone

An earlier commit in this PR paused prefill, waited for decode to pull every pinned block, then suspended. A later commit (ef48e301) removed the wait, because with a deep decode backlog the pins did not drain for over an hour. The worker pieces stayed: pause_for_refit, a paused flag read in four places, and pinned_handoff_block_count. Only tests call them, here and in PRs 5 and 6. The class docstring, the PR title and the PR body still describe the wait.

The fix changed after the review of PR 2. #4912 C7 asks PR 2 to rewrite the worker's _sleep/_wake: every wait for an engine state gets a timeout, and wake looks at the engine's real state instead of a saved flag. So C4 asks this PR to take PR 2's version of those four functions as it is, not to restore the older untimed one or keep this PR's own change to when the flags are set.

🔴 review comment C4 — megatron_worker.py:2492 C5 — dynamo_generation.py:307 C6 — PR body

Will this hold up at 1,000 engines?

Take 500 prefill and 500 decode engines. vLLM, plain Megatron and TRT-LLM's own prefill/decode mode on main each keep every engine's workers in one worker group, so each step is one wave: send the call to every worker, then wait once (run_all_workers_single_data, TRT-LLM P/D wake and sleep). A wave costs as long as its slowest engine, however many engines there are. This PR keeps one wave for the weight transfer, the KV rebuild and the resume: _call_engines sends every call before it waits on any. Three steps don't:

main (vLLM, Megatron, TRT-LLM) this PR · 500 prefill + 500 decode Refit: suspend 1 wave · slowest engine 500 prefill 500 decode 2 waves, no need Refit: resume 1 wave · slowest engine 1 wave · slowest engine same as main Startup: build 1 wave · slowest engine reserve GPUs + read node IP: 1,000 times, one by one 125 rounds of 8; each round waits for its slowest engine Shutdown 1 wave · slowest engine 1,000 stops one by one, plus one report per decode engine (C3) prefill decode one wave: send to all, wait once one at a time, or a wait with no reason
StepThis PRCost at 1,000 enginesAsk (review comment)
Suspenda prefill wave, then a decode wave (L911-L923)slowest prefill + slowest decode: up to 2 waves, not a cascadeOne wave. The order only existed so prefill could pause while decode drained the pins (PR body, "Refit ordering"), and ef48e301 removed that wait. The PR's own resume docstring says no order is needed in persist mode, and the two facts it uses hold on the way down too: a suspended prefill engine still serves pulls, and a handoff to a sleeping decode engine is queued.
StartupGPU reservation and a node-IP lookup per engine, one by one (L1197); then rounds of 8 (L1246)The PR's own comment: 64 engines one at a time took about 2 h, about 110 s each. 1,000 engines = 125 rounds × at least 110 s ≈ 3.8 h, more whenever one engine in a round is slow.Reserve and start every engine at once, as RayWorkerGroup does for its workers, then wait once. If a limit is needed, use a pool that starts the next engine as soon as any one finishes, not rounds.
Shutdownone engine at a time: decode (L1409), one report per decode engine, then prefill (L1424)1,000 stops back to back; a hung endpoint adds up to 600 s eachAfter C3 deletes the report, no role order is left to keep: stop every engine at once, as VllmGeneration.shutdown does (one cleanup call per worker, at most 30 s, then kill). On main, worker_group.shutdown stops a whole group in one wave.

🔴 review comment C9 — suspend in one wave C10 — build at once C11 — stop every engine at once

So what. The refit order is safe: upstream Megatron-LM keeps a suspended prefill engine serving pulls and releases in persist mode, and records the weight version per token. For the same reason it doesn't need to be an order at all: suspend can be one wave, like resume. The PR should drop the shutdown report step (C3) and the leftover pause code (C4), and fix the docs (C5, C6). At 1,000 engines the real cost is startup (hours, in rounds of 8) and shutdown (one engine at a time); startup should be one wave, and so should shutdown, as on main.