One chat path for NeMo-Gym through Dynamo: where the chat template, the prefix splice and the TQ writes should live

Design proposal for #4979, first written at a1659e9d; updated for #4979 at a2c74e6, which has merged #4905. Links are pinned to a2c74e6; links to deleted code use a1659e9d. Dynamo 1.5.1, vLLM 0.29.0.

Nine pages on this stack (#4905, #4979 and #5115), one question each. This one: the design asked for, and how much of it was built.

Before
At a1659e9d, Dynamo's Rust frontend rendered Gym's chat calls, and the engine re-ran a copy of the capture steps on tokens alone. (#4905's rollouts are not affected: they send token ids to /v1/completions, which Dynamo passes through without a template.)
This PR
Commits 790feb77, 0b7894a4 and 43efaba3 build the proposal: a launcher starts Dynamo's frontend with NeMo RL's chat_engine_factory; our chat engine renders with the checkpoint's own template and processor, reads the TQ prefix and splices; the engine does the one TQ write; Dynamo's Rust renderer is refused.
Status
Mostly built, at a2c74e6. Three differences from the proposal: (1) pixels travel pickled inside the call, not through Dynamo's shared-memory / NIXL sender; (2) only the vLLM halves are shared, Megatron's capture classes were not generalized; (3) the native chat server kept its own copy of the steps after the render (see shared or copied). C27 (the design) and its parts are resolved.
Background. The Dynamo frontend takes chat calls and its KV router picks the engine. Token capture writes each call's tokens, routed experts and pixels to TQ. Dotted words have a short explanation: hover or tap them.

Each column follows one Gym call through seven steps: ① receive the call, ② apply the chat template, ③ read the earlier tokens from TQ and splice them in, ④ pick an engine, ⑤ turn images into pixels, ⑥ run the model and write the call to TQ, ⑦ reply. Dashed boxes are OS processes, and the boxes inside are the classes that run each step. Without Dynamo, all of it runs in one process per vLLM worker. Before the redesign, #4979 moved ② into Dynamo's Rust code and ran a copy of ③ after routing. The proposal, now built, keeps Dynamo's frontend and router, but runs our own Python inside the frontend process, through Dynamo's chat_engine_factory hook.

Three setups, one Gym call

Native vLLM + Gym the standard to match Gym process POST /v1/chat/completions, ng_capture #4979 before the redesign a1659e9d: Rust renders, engine copies steps Gym process POST /v1/chat/completions, ng_capture Proposed, now built a2c74e6: our Python in Dynamo's frontend Gym process POST /v1/chat/completions, ng_capture ④ Gym's own client picks 1 of N worker servers vLLM worker process (Ray actor) × N FastAPI route (HTTP thread) ① receives the chat call ⑦ returns JSON: text + commit coords create_chat_completion · L799 NeMoRLOnlineRenderer.preprocess_chat ② vLLM renderer, checkpoint template ⑤ HF processor → pixels (this process) ③ decide_capture_input, then splice (asks the worker for the TQ prefix) preprocess_chat · chat_server.py L285 VllmAsyncGenerationWorkerImpl ③ _resolve_admission_prefix → TQ read ⑥ _begin_/_finish_request_capture → ONE TQ write (TQTokenSink) _finish_request_capture · L1046 same process: no network hop, pixels handed over in memory vLLM AsyncLLM → GPU ⑥ ids · logprobs · routed experts come back into this process ⑦ reply TQ (data plane storage) read by: ③ worker, in this process written by: ⑥ worker, in this process R W Dynamo spreads calls over N frontends Dynamo frontend process × N Rust HTTP server ① receives the chat call ⑦ returns JSON: text + coords Rust preprocessor (dynamo-renderer) ② minijinja template + Rust tokenizer 1 unexpanded token per image no processor code runs Rust KV router (same process) ④ picks 1 of M engines by cache overlap, on the Rust-rendered ids Dynamo request plane (TCP): token ids + annotations + image URLs vLLM worker process (Ray actor) × M Dynamo Worker (Rust, inside the actor) receives the call; streams the reply NativeVllmEngine._generate_gym ③ TQ read + splice: COPIED steps, no candidate step, EOS-only cut ⑤ HF processor again, from image URLs ⑥ begin capture … _stage_gym_call → ONE TQ write (TQTokenSink) _generate_gym · L492 vLLM AsyncLLM → GPU ⑥ routed experts stay in this process ⑦ reply TQ (data plane storage) read by: ③ _generate_gym, worker process written by: ⑥ _stage_gym_call, worker process R W Dynamo spreads calls over N frontends Dynamo frontend process × N Rust HTTP server ① receives the chat call ⑦ returns JSON: text + coords OUR chat engine (Python, same process) set up through chat_engine_factory ② FrontendRenderer: the shared render ⑤ HF processor → pixels, ONCE ③ decide_capture_input + splice TQ read via TQTokenSource ⑦ turns engine output into the reply ChatEngineFactory · chat_engine.py L448 Rust KV router (routed_engine) ④ picks 1 of M engines on the final, spliced ids request plane (TCP): ids + capture plan pixels: pickled in the call (differs) vLLM worker process (Ray actor) × M Dynamo Worker (Rust, inside the actor) receives the call; streams the reply VllmAsyncGenerationWorkerImpl same class as native, wrapped by DynamoVllmAdapter · adapter.py L196 ⑥ begin capture → generate → ONE TQ write vLLM AsyncLLM → GPU ⑥ routed experts stay in this process ⑦ reply TQ (data plane storage) read by: ③ chat engine, frontend process written by: ⑥ adapter, worker process R W same as native new piece differs from native (risk) dashed = one OS process reply path TQ read TQ write Circled numbers are the 7 steps of one Gym call. ①–⑦ appear in the class that runs them; Gym itself does ④ only in the native setup.

Where each piece lives, as built. The Dynamo frontend is one process. Its HTTP server and KV router are Rust, and our chat engine is a Python object in that same process: Dynamo's Rust code calls into it. It renders in a pool of helper processes (render_pool.py) with the native server's render code, so ② and ⑤ match native. The TQ read for ③ happens in the chat engine, through a read-only TQTokenSource (_CaptureSource). It hands the spliced ids to the KV router in the same process (④). The call then crosses the network to one vLLM worker, a Ray actor process. There, Dynamo's Rust Worker calls DynamoVllmAdapter, which wraps vLLM's AsyncLLM in that same process. It begins capture, runs the model and does the one TQ write (⑥), using the same worker methods as the native server. The reply goes back with only the text and where the call was stored, so routed experts never leave the worker.

What each step means, and where pixels and TQ go

TermWhat it is
Chat template + processorTurns chat messages into token ids. The template (Jinja text from the checkpoint) lays out the turns. The processor (Python from the checkpoint, can be custom) turns each image or video into pixel values and into the right number of image tokens.
Prefix spliceOn turn 2+, the earlier turns are swapped for the exact tokens the model produced, so training sees what the model really generated, not a re-render. splice_prefix_tokens.
TQ prefix readFetches those earlier tokens from TQ, using the chain of earlier calls Gym names.
Capture decisionNot "capture on or off". With forest training, Gym says "this call may continue an earlier one" (mode="candidate"). decide_capture_input checks the messages and decides: continue from the earlier tokens, or start fresh. It needs the messages, so it must run where the messages are: in the frontend.
Capture planThe small record the frontend attaches for the engine (GymCallPlan): the decided capture context, how long the earlier part is, and where the splice cut. No token arrays: the earlier tokens are already the start of the prompt.

The proposal, step by step: what was built

Where it landed: RL only, in #4979. No Gym change: Gym already sends the chat call and the ng_capture context to the frontend. No Dynamo change: the hook exists in 1.5.1, but it is marked experimental.

  1. Move the chat server out of the worker. Built in 790feb77: the classes that lived in _setup_vllm_openai_api_server are now chat_server.py.
  2. Split the steps into "before the engine" and "after the engine". Partly. 0b7894a4 shares the render and capture decision (_render_chat_call) and the turn-end ids. Two differences: Megatron's TQMegatronPromptPreparer and TQMegatronTokenStager (L124, L443) were not generalized, and the native server kept its own copy of the steps after the render (chat_server.py L339-L436) next to Dynamo's.
  3. Our chat engine for the frontend. Built in 43efaba3: a launcher starts Dynamo's frontend with ChatEngineFactory.
  4. Slim the engine adapter. Built. DynamoVllmAdapter on the DynamoEngineAdapter base runs only the "after" half (L263-L404). The old _generate_gym copy is gone.
  5. Refuse the Rust renderer. Built (see above).

✅ all resolved: C27 design C26 forest training C28 Rust renderer C29 logprobs C30 mixed media C31 config messages #4905 C61 groundwork · open: #5119 Megatron forest training

So what. #4979 now keeps everything Dynamo is for (N frontends, KV routing) and what the native path gets right: the checkpoint's own template and processor, one decision and one splice, and routed experts that never cross HTTP. What is left: pixels ride inside the call instead of shared memory or NIXL, which costs bandwidth on large images and video; Megatron does not share the halves yet; and the steps after the render still exist twice, so a change to one (such as #4506's prefix recovery) has to be made to both. The hook is still marked experimental in Dynamo.