Where a Gym call reads and writes TQ: vLLM, Megatron, and Megatron behind Dynamo

PR #5097 (6 of 6 in the Megatron prefill/decode stack) · main a723b597, #5097 head 751efd79

Before
vLLM runs every Gym call through three shared steps — prepare, begin, finalize — on all its paths. Aggregated Megatron has its own two-hook copy.
This PR
Adds token capture for Megatron engines behind Dynamo: prepare is shared with vLLM (in the frontend); finalize is a second Megatron copy, stage_completed, on the decode engine.
Status
Open — two asks staged on #5097: write routed experts (C2), and use the native failure path so a staging error cannot turn a finished call into a 400 (C3). Nothing else needs to change for the text-only calls #5097 serves.

Four pages on #5097, one question each; the first chip is an earlier PR in the stack. This one: does Megatron run a Gym call through the same steps as vLLM?

Background. A Gym call is one chat turn from an agent. TQ holds what the model actually saw and wrote. Each call does at most one read (the earlier turns' exact token ids, the prefix) and one write (this call's tokens). Behind Dynamo, the KV router needs final token ids, so the read moves to the frontend. Dotted words: hover or tap.

Four paths, the same three steps

vLLM, native + Gymmain vLLM behind Dynamomain Megatron, aggregatedmain Megatron behind Dynamo, P/D#5097 vLLM worker process (Ray actor) ① prepareprepare_prompt R TQ: prefix [1,5,6,2] splice + clamp → ids ② beginstart_capture cut restore · media copy begin_call (before gen) stamps the weight version ③ generate (vLLM engine) ④ finalizesave_capture W TQ: ids, logprobs, routes, media, top-k Dynamo frontend process ① preparesame function R TQ: prefix [1,5,6,2] ids → KV router ids + extra_args vLLM worker process ② beginstart_capture cut restore · media copy begin_call (before gen) ③ generate ④ finalizesave_capture W TQ: ids, logprobs, routes, media, top-k Megatron engine, rank 0 (Ray actor) ① prepareprompt_preparer R TQ: prefix [1,5,6,2] own splice function ③ generate (MLM engine) ②+④ stagepayload_stager begin_call (after gen) weight version from policy_epoch W TQ: ids, logprobs, routes, media on error: answer, no coords Dynamo frontend process ① preparegym_prompt.py R TQ: prefix [1,5,6,2] same as vLLM → ids ids + extra_args prefill engine, rank 0 fills KV; skips capture KV handoff decode engine, rank 0 ③ generate ②+④ copystage_completed begin_call (after gen) W TQ: ids, logprobs ✕ no routes (C2) ✕ admission error → 400, tokens thrown away (C3) media, top-k, cuts: refused one shared function, used by every vLLM path (and by #5097's prepare) Megatron's own copy of the step (on main) second Megatron copy added by #5097, with the two gaps marked ✕ R TQ / W TQ= the one TQ read and the one TQ write per call Dashed boxes = one OS process. Underlined names link to the code. Same example on every path: turn 2 of a Gym call; TQ holds turn 1's exact ids [1,5,6,2]; the prompt sent to the engine starts with them. ② and ④ are one box on Megatron because begin_call runs at stage time, after generation, on both Megatron paths.
StepSame on all four?What differs, and does it matter today
① prepare (TQ read)vLLM ×2 and #5097: yes, one function. Aggregated Megatron: its own copy.Aggregated Megatron splices with replace_prefix_tokens, not the shared splice_prefix_tokens. Already on main; not this stack.
② begin (before generation)vLLM: before. Both Megatron paths: after, inside staging.The weight version comes out the same (taken from policy_epoch). What vLLM does before generation — cut restore, media copy, top-k — #5097 refuses (here). So no ask now; it matters when a later PR lifts those refusals.
④ finalize (TQ write)No. #5097 adds a second Megatron copy.It writes no routed experts (C2), and its failure handling differs from native: an admission error escapes to the adapter's except ValueError and becomes a 400 (C3).

The ask: one Megatron staging core — proposed, not in the PR

Nothing below is implemented. Both Megatron paths should finalize through one function, so the Dynamo path gets routes and the native failure rule for free.

Decode engine rank 0, after generation (proposed)

# token_capture.py, called by native stage() and by the Dynamo adapter
def _stage_call(admission, *, prompt_ids, gen_ids, logprobs, policy_epoch, routes, media):
    call = None
    try:
        call = begin_call(CaptureAdmission.model_validate(admission),
                          weight_version=_weight_version(policy_epoch))   # inside the try now
        return complete_call(call, prompt_ids, gen_ids, logprobs,
                             extras=routed_experts(routes),        # C2: no more extras=None
                             attachments=media)
    except Exception:                                              # C3: as native stage()
        return fail_call(call) if call else None                 # None → answer the call, no coords

💬 review comment C2 — token_capture.py:571 C3 — token_capture.py:598

So what. For text-only Gym calls, Megatron behind Dynamo already reads TQ the same way vLLM does, and its "begin after generation" matches aggregated Megatron. The real gap is the write: a second copy that drops routes and fails differently. One shared Megatron staging function fixes both. Moving all three steps into one module shared with vLLM is the longer-term goal, and fits the refit and worker cleanup tracked in #5413.