Refit, token capture and the prefix splice: does the Dynamo path reuse them or copy them?

#4979 at a2c74e6, which has merged #4905 (at c6a4c66b). All links are pinned to a2c74e6; links to deleted code use the SHA it lived at. First written against #4905 at 44671d3b and #4979 at c4fe2bd5.

Before
At c4fe2bd5, #4979's Dynamo engine re-ran its own copy of the native chat server's capture steps on token ids alone. The copy skipped the forest-training "candidate" step, so those calls failed. It called the prefix splice with different inputs (explained below), and it never ran the retained-media check.
This PR
Three commits (790feb77, 0b7894a4, 43efaba3) moved the render, the capture decision, the TQ read and the splice into NeMo RL's own chat engine inside Dynamo's frontend. It runs the native render code and passes the splice the same inputs. The engine side keeps only the steps after the render.
Status
Fixed in #4979: forest training, the splice inputs and the media check. Still open: four pieces are written twice, and one check runs only on the native path. A parity test sends 24 calls through both paths and checks they match.

Nine pages on this stack (#4905, #4979 and #5115), one question each. This one: does the Dynamo path reuse NCCL refit, TQ token capture and the prefix splice, or copy them?

What both paths share now, and what is still written twice

Gym's chat calls reach vLLM one of two ways. Natively, each vLLM worker runs its own chat server (chat_server.py). Through Dynamo, NeMo RL's chat engine runs in Dynamo's frontend process, and an engine adapter runs inside the vLLM worker. Token capture and the prefix splice run on both. Dotted words have a short explanation: hover or tap them.

PieceShared or copied at a2c74e6
NCCL refitShared. Dynamo serves the worker's own vLLM engine, so refit is the worker's update_weights_from_collective_async. #4905 deleted the old Dynamo refit.
Rollouts (token ids in)Shared. The adapter calls the worker's _serve_rollout, the same function the Ray path uses.
Capture coreShared. Begin, media snapshot, TQ write and abort are the worker's own methods (vllm_worker_async.py L809-L1135). So are the TQ prefix read, splice_prefix_tokens, the turn-end ids, the candidate decision, the chat render, the on-policy sampling check and vLLM's tool and reasoning parsers.
Post-render step orderWritten twice. TQ read → splice → fit max_tokens → media snapshot → move image placeholders → begin capture: native chat_server.py L339-L436; Dynamo chat_engine.py L286-L306, L361-L383 and adapter.py L319-L360.
The staged messageWritten twice. Native builds it from vLLM's reply (L597-L621); the adapter builds it by hand (_stage L487-L520).
SamplingParamsCopied from vLLM's chat server into adapter._generate L406-L445.
Errors and abortsMapped by hand on each side: native routes L799-L948; Dynamo chat_engine.py L231-L255, adapter.py L263-L289, L396-L404. Dynamo never records the context_length abort reason; an overflow there is recorded as engine_error.
"top_logprobs must be set"Native only (L539-L544). Through Dynamo the check never runs, and the parity test always sets top_logprobs, so it does not notice.

One Gym call, step by step: native server vs Dynamo

Each row is one step. The middle column is the shared code both sides call. The example is turn 2 of a chat on a checkpoint whose turns end on <|im_end|> (151645) but whose tokenizer's EOS is <|endoftext|> (151643), the shape the parity test builds for its turn_end_not_eos case.

Native chat server chat_server.py, in the vLLM worker Shared code — one copy called by both sides Dynamo path chat_engine.py (frontend) + adapter.py (worker) 1 · Render, decide a Gym candidate preprocess_chat → _render_chat_call · L320 → token_in, history […1001, 151645] _render_chat_call chat_server.py · L202-L279 checkpoint template, candidate check 1 · Render, decide a Gym candidate render pool → FrontendRenderer · L479 → token_in, history […1001, 151645] Steps 2–6: the helpers are shared, but each side writes the order that calls them 2 · Read earlier turns from TQ host._resolve_admission_prefix · L347 → prefix […220, 17, 151645] resolve_admission_prefix tq_token_sink.py · L804 staging chain → cached TQ read 2 · Read earlier turns from TQ _capture.resolve_prefix · L369 → prefix […220, 17, 151645] 3 · Splice in the model's own tokens splice_prefix_tokens(...) · L381 turn ends {151643, 151645} → cut found splice_prefix_tokens, turn_end_token_ids openai_server_utils.py · L74, L30 same render, history and turn-end ids 3 · Splice in the model's own tokens chat_engine._splice · L376 turn ends {151643, 151645} → cut found 4 · Fit max_tokens to the window _clamp_max_tokens · L393 on the spliced prompt clamp_max_tokens chat_server.py · L166 prompt + output ≤ max_model_len 4 · Fit max_tokens to the window clamp_max_tokens(...) · L300 on the spliced prompt 5 · Images: snapshot, move placeholders media · L400, remap · L412 same process as the render _capture_request_media + remap vllm_worker_async.py · L868 remap_multimodal_placeholders · utils.py L163 5 · Images: snapshot, move placeholders adapter.py · media L335, remap L344 in the worker, after the network hop 6 · Open the capture call _begin_request_capture · L424 admission = token_in _begin_request_capture vllm_worker_async.py · L827 → Gym begin_call 6 · Open the capture call adapter.py · L354 a leftover candidate is refused (L311) 7 · AsyncLLM.generate in the vLLM worker: the same engine object for both, so the same weights and the same refit 8 · Build vLLM SamplingParams vLLM's OpenAIServingChat · L838 vLLM's own chat server does it no shared NeMo RL code vLLM's server code, copied 8 · Build vLLM SamplingParams adapter._generate · L406-L445 copied from vLLM's chat server 9 · Stage tokens + logprobs to TQ attach_* · L597-L621 message built from vLLM's reply _finish_request_capture vllm_worker_async.py · L1046 one TQ write 9 · Stage tokens + logprobs to TQ adapter._stage · L487-L520 builds the same message by hand 10 · Map errors, abort capture route · L799-L948 reasons: context_length, engine_error… _abort_request_capture vllm_worker_async.py · L1131 statuses and reasons are not shared 10 · Map errors, abort capture chat_engine L231 · adapter L263, L396 never records context_length 11 · Refuse logprobs without top_logprobs chat_server.py · L539-L544 → 400 no shared code 11 · Not checked the call goes on to the engine one copy, same inputs on both sides written twice: can drift call order written twice only one side has it Token ids: 151645 = <|im_end|> (ends each turn), 151643 = this tokenizer's EOS, [220, 17] = the model's turn-1 answer, 1001 = the template's re-render of it.

"Same function, different inputs": what it meant, and how it was fixed

The earlier version of this page said the splice on the two paths was "same function, different inputs". It meant this. At c4fe2bd5 both paths called splice_prefix_tokens. It counts how many turn-end tokens the old history holds (N), then cuts the new prompt at its N-th turn end and puts the model's own tokens before the cut. The native server called it at vllm_worker_async.py L1432, the Dynamo engine at native_engine.py L559. Only the model's own tokens from TQ were the same on both. The other three inputs came from different places:

ArgumentNative at c4fe2bd5Dynamo at c4fe2bd5
eos_token_id
which ids end a turn
configured stop_token_ids + the generation config's EOS list + the tokenizer's EOS (L1188-L1194, L1439)not passed, so the tokenizer's EOS only
template_prefix_token_ids
where to count N
the history re-rendered up to the last assistant message (L1416)None: the engine had no messages, so N was counted in the model's own prefix, plus one EOS if it did not end on one (L163-L168)
template_token_ids
the new prompt
vLLM's renderer with the checkpoint's templateDynamo's Rust renderer in the frontend, then vLLM's processor (L741-L748)

The turn-end ids are the input that broke. Same turn-2 call as the figure, run through the old function (shortened to the tokens that matter):

Native: turn ends = {151643, 151645}Dynamo: turn ends = {151643}
model's prefix from TQ[user … 151645, assistant 220, 17, 151645], same on both
N, the turn ends to skip2 (in the history render)1 (no 151643 in the prefix, plus the one it adds)
the new prompt holds151645 three times; the 2nd closes turn 1no 151643 at all: Qwen's template never writes it
result[… 220, 17, 151645, user 21, 22, 151645, assistant]AssertionError: EOS token #1 not found: the call fails

So every turn-2 call with a prefix failed through Dynamo on such a model, while the native path served it. gpt-oss has the same shape: a tool-call turn ends on <|call|>, which is not its tokenizer's EOS. That was review comment C16. The other two inputs did not break this example, but they could make the two paths cut in different places: counting in the model's prefix goes wrong if the model wrote an EOS id as ordinary text, and the Rust renderer is a different program from the checkpoint's own template.

Fixed at a2c74e6, in #4979, RL only. 0b7894a4 added turn_end_token_ids (stop ids + generation-config EOS + tokenizer EOS) and made eos_token_id required. 43efaba3 moved Dynamo's splice into the frontend, where the render pool runs the same _render_chat_call as native, so both get the same new prompt and the same history render. 53d7c9df deleted the None mode. The turn_end_not_eos parity case now checks the prompt matches.

@@ Dynamo's splice call · native_engine.py L559-L564 at c4fe2bd5 → chat_engine.py L376-L382 at a2c74e6
         splice = splice_prefix_tokens(
-            tokenizer=self._llm.tokenizer,
-            model_prefix_token_ids=prefix_token_ids,
-            template_prefix_token_ids=None,
-            template_token_ids=template_token_ids,
+            tokenizer=None,
+            model_prefix_token_ids=prefix,
+            template_prefix_token_ids=rendered.history_token_ids,
+            template_token_ids=rendered.template_token_ids,
+            eos_token_id=turn_end_token_ids,
         )

history_token_ids and template_token_ids come from _render_chat_call, and turn_end_token_ids from the render pool, built with the same two functions the native server uses at L386. One difference is left: Dynamo passes tokenizer=None, which only drops the decoded text from the error message.

✅ review comment C16 — resolved C26 forest training — resolved

So what. The splice and the capture core are now one copy, so the bug above cannot come back on one path only. The risk that is left is drift in what is still written twice: the order of steps 2–6, the staged message, the error mapping, and the native-only top_logprobs check. The next change to land there is #4506 (prefix recovery). It adds restore steps between the TQ read and the splice, but only in the native chat path (vllm_worker_async.py L1568-L1600 at 7d75696). Moving steps 2–6 into one shared method before #4506 lands would give it one place to go.