Stack: #4801 → #4912 → #5023 → #5024 → #5025 → #5097 · main at 5e271174, this PR at 88fe34ee (the C1 draft is on c396aafa), #5097 at 751efd79, the C1 draft at d40ae141 · Megatron-LM pin e0a4bfed, Dynamo 1.8.0.dev20261117, vLLM 0.33.0
MegatronPolicyWorker: #4912 removed the separate Dynamo worker class.Three pages for this PR. The ★ page links every page in the stack.
How to read the figure. This is a composition diagram, the containment half of a UML class diagram. A dashed box is a process: purple for a Ray actor, gray for a plain OS process. A solid box inside it is one object. A line field ──▶ Type means "this object holds a pointer to a Type in that field" (has-a). A hollow triangle ▷ Base means "this class is a subclass of Base" (is-a). The colored chip at a box's top right says which repo the class's code lives in: pink NeMo-RL, yellow Megatron-LM, gray Dynamo, brown vLLM. Column D is the author's counterproposal from the C1 thread: an outlined MLM change badge marks a Megatron-LM change it needs (proposed, not in Megatron-LM yet), a struck-through row is what that change would remove, and the box with a dotted gray border is what it leaves for a follow-up PR (tap it for pseudocode). Every name is a permalink. Each process row starts at the same height in every column, but the boxes inside do not line up, so the fill does the comparing: green means the same class is also in another column, hatched green means another column has a different class in the same role, and a plain box with a dark border exists only in this column. A teal dash-dot inner line with a ↻ #5097 badge marks a class that #5097 moves out of dynamo/vllm/ and reshapes; hover or tap it to see how, as before → after pseudocode. The border color is a separate signal: what the review asks to change, and every ask names the PR its comment is on (#5023 C1 is for this PR; #4912 C10 and #5097 C5 are for the PRs below and above). An outlined chip such as #5025 or #5097 marks a box or line that a PR above this one adds; the Show: up to #5023 button fades them so you see this PR alone. A box with a dashed inner panel shows the one object whose class a later PR swaps (is-a, not a second object).
main · dynamo/vllm/chat_engine.py
# frontend event loop, one Gym chat call async def _serve(body): params = on_policy_sampling(…) # vLLM helper, called directly rendered = await pool.render(body) # render process plan = await prepare_prompt(rendered, …) # from vllm/prompt_and_capture sampling_options = {…} # built inline reply = await call_engine(plan, …)
#5097 · dynamo/chat_engine.py
# frontend event loop, one Gym chat call async def _serve(body): params = on_policy_sampling(…) params = backend.checked_sampling(…) # VllmChatBackend | MegatronChatBackend rendered = await pool.render(body) plan = await backend.prepare_prompt(rendered, …) # splice moved to gym_prompt.py sampling, output = backend.engine_options(body, rendered) reply = await call_engine(plan, …)
Real code: ChatBackend protocol; load_chat_backend picks the module from _CHAT_BACKEND_MODULES; VllmChatBackend, MegatronChatBackend.
main · dynamo/vllm/render_pool.py
# each render process, at start def _init_worker(settings): _WORKER = _Worker(spec, model_dir) # vLLM objects, in this file class _Worker: render(), respond()
#5097 · dynamo/render_pool.py
# frontend: one pool per model pool = RenderPool(workers, renderer=backend.renderer) # "module:Class" # each render process, at start def _init_worker(settings, renderer): worker_cls = _renderer_class(renderer) # import by name _WORKER = worker_cls(spec, model_dir) class _Worker: … # moved and renamed VllmRenderer in dynamo/vllm/chat_backend.py # was _Worker MegatronRenderer in dynamo/megatron/chat_backend.py # new
Both follow the RenderWorker protocol: render, respond, info. Named by VllmChatBackend.renderer and MegatronChatBackend.renderer.
#5023 · megatron/dynamo_worker_mixin.py
# rank 0, on the inference loop engine = MegatronLLMEngine(config, model) # Megatron-LM's class; rollouts only await Worker(engine, worker_config).run()
#5097 · same line, plus dynamo/megatron/adapter.py
# rank 0, on the inference loop engine = MegatronLLMEngine(config, model) engine = NemoRLMegatronLLMEngine(config, model, host=self) # one object, a subclass class NemoRLMegatronLLMEngine(MegatronLLMEngine): start(): super() + chat settings in runtime_data # frontend renders with them generate(): Gym call? else super().generate() # rollouts unchanged read ForwardedCall from extra_args; gym_sampling_request() # sampling from the rendered chat _serve_gym_call() → host.stage_dynamo_gym_call # stage the captured call to TQ _stream_chunks(): wrap with _FinalTee # overrides a private MLM method
Why it exists: Megatron-LM's adapter serves token-id rollouts only, and a Gym chat call needs NeMo-RL steps it cannot know. Under #5023 C1 the adapter is NeMo-RL's own class that already holds the worker, so #5097 adds these as a Gym entry in _handlers() and chat settings in start(), as DynamoVllmAdapter does (#5097 C5).
main · dynamo/vllm/chat_engine.py
# frontend, once at launch CHAT_PROCESSOR = "…dynamo.vllm.chat_engine:provider" provider() → ChatEngineFactory(options): __init__: _capture, _early_pool
#5097 · dynamo/chat_engine.py
# frontend, once at launch CHAT_PROCESSOR = "…dynamo.chat_engine:provider" # moved up one folder provider() → ChatEngineFactory(options): # unchanged __init__: _capture, _early_pool # unchanged _backend = load_chat_backend(options.chat_backend) # "vllm" | "megatron"
The factory still owns the TQ reader and the early render pool; the only new field is the backend.
serve() slot on the base class (proposed, not written)main · dynamo/engine_adapter.py
# engine actor, inference loop class DynamoEngineAdapter(LLMEngine): async def generate(request, context): handler = pick by annotation # rollout | Gym call | generation cut async for chunk in handler(…): yield chunk serve_rollout(…) # one finished result per call
column C (C1 draft) fills the slots itself
# DynamoMegatronAdapter in terrykong/RL#58 serve_rollout, serve_prefill, release_handoff # NeMo-RL re-does Megatron-LM's handoff
column D follow-up · from the C1 reply
# engine actor, inference loop class DynamoEngineAdapter(LLMEngine): async def generate(request, context): handler = pick by annotation rollout = parse_annotation(request) async for chunk in self.serve(request, rollout, context): yield chunk # base packs engine_data.rollout async def serve(request, rollout, context): … # default: today's slots, so vLLM is unchanged class DynamoMegatronAdapter(DynamoEngineAdapter): _mlm: MegatronLLMEngine async def serve(request, rollout, context): async for chunk in self._mlm.generate(request, context): yield chunk # Megatron-LM owns the handoff
Source: the author's reply on C1. Nothing here is written yet; the author would do it in a follow-up PR, and the staged reply (C6, not published) asks for it in this PR.
Scroll sideways on a phone. Not drawn: etcd and NATS (two plain processes the runtime starts in the driver), and the other model-parallel ranks, which hold an engine and an inference client but no Dynamo objects.
What #5097 refactors in the frontend (the ↻ boxes; same classes, new shape)
| before (main) | after (#5097) | why |
|---|---|---|
NemoRLChatEngine, ChatEngineFactory in dynamo/vllm/chat_engine.py; _serve calls on_policy_sampling and prepare_prompt directly |
Same classes in dynamo/chat_engine.py; _serve calls backend.checked_sampling, backend.prepare_prompt, backend.engine_options |
One chat engine serves vLLM and Megatron; the engine-specific steps sit behind ChatBackend. |
RenderPool in dynamo/vllm/render_pool.py builds _Worker (vLLM objects) |
RenderPool in dynamo/render_pool.py imports a RenderWorker by name: VllmRenderer or MegatronRenderer |
The same pool runs Megatron's renderer. |
prepare_prompt, PreparedPrompt in vllm/prompt_and_capture.py |
prepare_prompt, PreparedPrompt in gym_prompt.py; vLLM keeps a thin wrapper that adds its max-token clamp |
The prefix splice is not vLLM-specific. |
| CHAT_PROCESSOR points at the vLLM module | CHAT_PROCESSOR points at the shared module; ChatProcessorOptions.chat_backend names the backend | The frontend launch is the same for both engines. |
One rollout, from the driver to the engine. "Dynamo-only" is code that exists only for the Dynamo path. "Shared with native" is code that native generation runs too, so it gets token capture, refit and routed experts for free.
| layer | A · vLLM main | B · #5023 (+#5097) | C · after C1 | D · author's proposal | kind |
|---|---|---|---|---|---|
| â‘ HTTP client in the driver | post_rollout | generate_through_frontend (#5025; #5023 has none) | generate_through_frontend (#5025) | generate_through_frontend (#5025) | Dynamo-only |
| â‘¡ Dynamo frontend (Rust): HTTP + KV router | 1 engine | prefill engine, then decode engine | prefill engine, then decode engine | prefill engine, then decode engine | Dynamo-only |
③ Dynamo Worker in the engine actor | ✓ | ✓ (rank 0) | ✓ (rank 0) | ✓ (rank 0) | Dynamo-only |
â‘£ adapter (the LLMEngine) | DynamoVllmAdapter | MegatronLLMEngine; from #5097 the subclass NemoRLMegatronLLMEngine | DynamoMegatronAdapter | now: MegatronLLMEngine plus 4 Megatron-LM hooks; follow-up: a DynamoMegatronAdapter that holds it | Dynamo-only |
| ⑤ worker method native generation runs | _serve_rollout | skipped | _generate_with_persistent_engine | skipped (Megatron-LM's generate runs the request) | shared with native |
| client to the engine | none (AsyncLLM is in the actor) | InferenceClient #2 Dynamo-only | inference_client (the worker's) | InferenceClient #2 Dynamo-only | shared with native |
| â‘¥ engine's own extra process | vLLM engine core | coordinator | coordinator | coordinator | shared with native |
| side channel | none | EngineEventReporter → ZMQ → EngineEventReceiver, 2 picked ports | none | as B today; none once attached mode lands (a proposed Megatron-LM change) | Dynamo-only |
| total between caller and engine | 6 (4 Dynamo-only) | 6 (5 Dynamo-only, + a socket pair) | 7 (4 Dynamo-only) | 6 today, same as B (+ the socket pair); no socket pair after attached mode; 7 after the follow-up |
C has one more layer than A or B, the native worker step. That layer is why C1 exists: it is how vLLM gets capture, routed experts and generation cuts without writing them again. D today is B. Its proposed Megatron-LM changes drop only the socket pair; its follow-up adds a wrapper layer, not the native step.
C vs D, in one line each. Both end with a DynamoMegatronAdapter under DynamoEngineAdapter. C fills the base class's existing slots (serve_prefill, serve_rollout, release_handoff), so NeMo-RL re-does Megatron-LM's handoff code. D adds one serve slot and hands the whole request to Megatron-LM's generate. What is open is timing (this PR or a follow-up) and the four Megatron-LM changes D needs. The staged reply (C6, not published yet) asks for D's follow-up shape in this PR.
| comment | fix it in | what changes in the picture |
|---|---|---|
| #5023 C1 | this PR | In the engine actor, Megatron-LM's MegatronLLMEngine is replaced by a NeMo-RL DynamoMegatronAdapter on vLLM's base class. It holds the worker (_host), like DynamoVllmAdapter, and calls the native step. The second InferenceClient and the event socket pair go away. Draft: terrykong/RL#58. |
| author's reply on C1 | this PR + a follow-up | Column D. Keep Megatron-LM's MegatronLLMEngine; add a result hook, a sampling hook, routes in the handoff and an attached mode to Megatron-LM; later give DynamoEngineAdapter a serve slot so a thin DynamoMegatronAdapter can hold Megatron-LM's adapter. Declines the C1 draft because it copies Megatron-LM's handoff protocol (serve_prefill, _decode_from, _publish_kv_event, about 70 lines of private HandoffChannel code) and does not unblock routed experts or generation cuts, which native Megatron refuses too. |
| #5023 C5 | this PR | The two ports picked in start_dynamo_endpoint go away. With no event socket there is no event port, and DYN_SYSTEM_PORT stays "0" as main sets it. Still open at 88fe34ee: both ports are picked early, and the DYN_SYSTEM_PORT override is at L335. The author's plan removes them with attached mode in Megatron-LM, later. |
| #5023 C3 | this PR | No longer needed. Main now gives Megatron the shared DynamoCfg (read here), which has both render_workers and session_affinity_ttl_secs. serves_chat=False stays. A one-line reply saying so is staged. |
| #5097 C5 | #5097, above | NemoRLMegatronLLMEngine (a subclass of Megatron-LM's class that overrides its private _stream_chunks) folds into C1's adapter: a Gym-call entry in _handlers() and the chat settings in start(), the way vLLM's adapter does it. |
| #4912 C10 | #4912, below | Done. The engine actor is the native MegatronPolicyWorker; MegatronDynamoGenerationMixin sits on MegatronPolicyWorkerImpl, and MegatronDynamoPolicyWorker is gone. #4912 merged that way, and it reached this PR with the merge of main at 4965ccf. The C1 draft is older (c396aafa) and still has the old class. |
| #5023 C2 · #5023 C4 | this PR | Fixed. No class changes: a test fake's signature (C2, 46f214f) and two stale docstrings (C4, 8014411). Both threads resolved. |
What the author already changed (checked against the code at 88fe34ee)
| comment | status | where |
|---|---|---|
| #4912 C10 one worker class | accepted | #4912 merged with the mixin on the native worker (75d9ebc); reached this PR via the merge of main (4965ccf) |
| #5023 C2 test fake signature | fixed | 46f214f; thread resolved |
| #5023 C4 stale docstrings | fixed | 8014411; thread resolved |
| #5023 C3 frontend config fields | no longer needed | main's shared DynamoCfg has both fields; thread open until the staged reply is published |
| #5023 C5 picked ports | open | no reply, no code change; ports picked at L319-L335. The author's plan: attached mode in Megatron-LM, later |
| #5023 C1 NeMo-RL adapter | declined for now | her reply: keep Megatron-LM's adapter now, four Megatron-LM changes and a serve slot later (column D) |
NemoRLChatEngine holds a ChatBackend (VllmChatBackend or MegatronChatBackend), and each render process holds a RenderWorker (VllmRenderer or MegatronRenderer). Same shape, one class per engine. Tokenization happens here for both._host) and calls its methods. Megatron-LM's adapter holds no worker. So it opens its own InferenceClient to a coordinator the worker already has a client for, and gets KV events and "ready" through a ZMQ push/pull pair between two threads of the same process. That pair is the only reason for the two picked ports (C5).NemoRLMegatronLLMEngine exists, and why C1 removes it: at #5023 rank 0 builds Megatron-LM's own MegatronLLMEngine, which serves token-id rollouts only. A Gym chat call needs NeMo-RL steps Megatron-LM cannot know: read the forwarded call from extra_args, rebuild sampling from the rendered chat, stage the captured call to TQ through the worker, and add chat settings to runtime_data. So #5097 changes that one line to build a subclass instead: still one object. Under #5023 C1 the adapter is NeMo-RL's own class and already holds the worker, so #5097 can add those steps as a handler, as vLLM does, with no subclass.NemoRLMegatronLLMEngine subclasses Megatron-LM's class and overrides its private _stream_chunks, catching the final record through a context variable (_FinalTee). It breaks quietly if Megatron-LM renames that method. #5097 C5 asks to fold it into C1's adapter.MegatronDynamoPolicyWorker was a second Ray actor class with its own name. #4912 C10 asked to fold it into the native worker, #4912 merged that way (75d9ebc), and it reached this PR when the author merged main (4965ccf). Now the engine actor is the native MegatronPolicyWorker, and MegatronDynamoGenerationMixin sits on MegatronPolicyWorkerImpl and falls back to plain Megatron when the config has no role.build_sampling_params), routes in the handoff, and an attached mode. Until then, B's second client and its missing native step stay. D's gain over C: no NeMo-RL copy of Megatron-LM's handoff code.DynamoEngineAdapter → worker method → engine. What is left: Megatron's prefill/decode handoff (serve_prefill, release_handoff), which needs Megatron-LM to make its private release and claim calls public (example). The extra coordinator process is Megatron's own and stays in native generation too.The shape test: in a good design, swapping the engine changes only the boxes that touch the engine. After #5097 that holds for the frontend. In this PR it does not hold for the engine actor, where four objects (an adapter, a client, a reporter and a receiver) differ from vLLM's one. C1 brings it back to one. D brings it back to one too, but only after the follow-up and the four Megatron-LM changes.
C1 · dynamo_worker_mixin.py:327 C3 · dynamo_config.py:78 C5 · dynamo_worker_mixin.py:264 #5097 C5 · adapter.py #4912 C10 · megatron_policy_worker.py draft · terrykong/RL#58 author's reply · C1 thread