Who answers a Dynamo request inside the engine: vLLM today, Megatron in this PR, and Megatron after C1

Stack: #4801 → #4912 → #5023 → #5024 → #5025 → #5097 · main at 5e271174, this PR at c396aafa, the C1 draft at d40ae141

vLLM today
On main, each vLLM engine answers Dynamo through NeMo-RL's own adapter, which calls the native vLLM worker. Token capture, routed experts and generation cuts all go through that worker.
This PR
Each Megatron engine answers through Megatron-LM's adapter instead. It reaches the engine through the engine's coordinator, over sockets, and #5097 later has to turn routed experts and generation cuts off on that path.
After C1
Proposed, not in the PR. A NeMo-RL DynamoMegatronAdapter on the same base class as vLLM, calling the native Megatron worker. A draft shows it as real code: terrykong/RL#58.

Three pages for this PR. The ★ page links every page in the stack.

Background. Dynamo's frontend sends each request to a Worker running inside an engine process. The Worker hands it to an adapter, the class that knows how to run a request on that engine. On main, NeMo-RL writes that class once (DynamoEngineAdapter) and vLLM fills in four methods. In prefill/decode serving, one engine reads the prompt and hands its KV cache to a second engine that writes the answer; the handoff is the step this stack adds. Dotted words have a short explanation: hover or tap them.

One request, three ways

Same example in all three columns: one completion request, prompt token ids [1, 2, 3], 2 new tokens. The Megatron columns use one prefill and one decode engine.

vLLM on main Megatron in this PR Megatron after C1 (draft) Dynamo frontend {prompt:[1,2,3], max_tokens:2} frontend: prefill leg, then decode leg {prompt:[1,2,3], max_tokens:2} frontend: prefill leg, then decode leg {prompt:[1,2,3], max_tokens:2} vLLM worker process (Ray actor, GPU) Megatron rank 0 process (Ray actor, GPU) Megatron rank 0 process (Ray actor, GPU) DynamoVllmAdapter (NeMo-RL) on DynamoEngineAdapter MegatronLLMEngine (Megatron-LM) its own class, not NeMo-RL's base DynamoMegatronAdapter (NeMo-RL) on DynamoEngineAdapter, like vLLM serve_rollout(req) ZMQ socket to the coordinator serve_prefill / serve_rollout native vLLM worker generate([1,2,3]) → [8, 2] capture, routed experts, cuts: yes engine coordinator → engine ranks InferenceClient request → [8, 2] routed experts, cuts: refused (#5097) native Megatron worker prefill: pin KV → handoff; decode → [8, 2] capture, cuts: as native Megatron registration and ports start_dynamo_endpoint(adapter) DYN_SYSTEM_PORT "0" (OS picks) no event port one step, on the actor's loop registration and ports start_dynamo_endpoint + attach DYN_SYSTEM_PORT 4001 (picked, C5) event port 4000 (picked, C5) two steps: start, then "ready" event registration and ports main's start_dynamo_endpoint(adapter) DYN_SYSTEM_PORT "0" (OS picks) no event port one step, as vLLM DynamoEngineAdapter (main) — what a backend fills in start · serve_rollout · abort · kv_event_sources + serve_prefill · serve_rollout(handoff=) · release_handoff ← new optional P/D hooks (C1) vLLM fills the first four today; Megatron fills all of them after C1; vLLM can add the P/D hooks later proposed in C1 what C1 and C5 ask to replace output [8, 2] is the same in all three; the path to it differs
vLLM on mainMegatron in this PRMegatron after C1
Who owns the adapter classNeMo-RLMegatron-LM; #5097 subclasses itNeMo-RL, same base as vLLM
How a request reaches the enginedirect call into the workerthrough the coordinator, over socketsdirect call into the worker
Steps to register12 (start, then attach + "ready")1
Ports NeMo-RL picksnone2 per engine, picked then bound later (C5)none
Capture, routed experts, cutsnative workeroutside the native worker; routed experts and cuts refused in #5097native worker, so the same as Megatron without Dynamo (which does not return routed experts today either)
Where P/D handoff lives(no P/D on main)inside Megatron-LM's adapterthree hooks on the shared base; Megatron-LM's KV functions called from them

The change, as pseudocode

Rank 0 serves the engine — this PR

# Ray call on rank 0; the adapter runs as a task on the engine's inference loop
def start_dynamo_endpoint(namespace, engine_env, …):
    event, system = pick free ports in [4000, 4100)   # released, bound later (C5)
    os.environ |= engine_env + {DYN_SYSTEM_PORT: system}
    cfg = Config(external mode, role, event port, …)
    run on inference loop: Worker(MegatronLLMEngine(cfg)).run()
# second Ray call, every rank
def attach_dynamo_events(addr):
    reporter.connect(addr); reporter.send("ready", coordinator address)

After C1 (draft)

# Ray call on rank 0; same function vLLM registers through
def start_dynamo_endpoint(namespace, engine_env, …):
    event, system = pick free ports in [4000, 4100)
    cfg = Config(external mode, role, event port, …)
    adapter = DynamoMegatronAdapter(self, role)      # calls this worker directly
    engine_adapter.start_dynamo_endpoint(adapter, engine_env)  # DYN_SYSTEM_PORT stays "0"
def attach_dynamo_events(addr): …                     # gone: no event socket

Real code: this PR's start_dynamo_endpoint; main's start_dynamo_endpoint; the draft's diff.

What the review asks, and what the draft shows

C1 asks for the right-hand column: one adapter shape for every backend, so Megatron runs capture and generation cuts through its own worker the way vLLM does, and the later PRs shrink (#5025 uses main's rollout_client, #5097 drops its two refusals). C5 asks that the two picked ports not come along. The draft PR is there so the difference is visible as code, not to be merged.

What in the draft is real and what is not: start, serve_rollout (plain and decode-with-handoff), serve_prefill, abort and kv_event_sources call real Megatron code. Two steps are stubs, release_handoff and the decode-side claim, because Megatron-LM keeps the RELEASE_KV / CLAIM_KV messages in private methods of MegatronLLMEngine; the draft names them, and they need one small upstream change that turns them into functions. The CPU unit tests pass (75) against stand-ins for the Linux-only Dynamo package; nothing ran on a GPU.

C1 · dynamo_worker_mixin.py:327 C5 · dynamo_worker_mixin.py:264 draft · terrykong/RL#58 design · one P/D interface