How a Megatron prefill/decode engine registers with the Dynamo frontend, next to how a vLLM engine does

Stack: #4801 → #4912 → #5023 → #5024 → #5025 → #5097 · this page: part 3 of 6, reviewed against #4912 · code links pinned at 868942a0; the review comments are on head c396aafa, which adds merges from main, including the move to Dynamo 1.8

Before
The Megatron prefill and decode engines were built and refit, but nothing registered them with a Dynamo frontend. No etcd, NATS or frontend ran for them.
This PR
The driver starts etcd, NATS and the frontend. At the first refit, rank 0 of each engine runs Megatron-LM's own Dynamo adapter inside its process, and the driver waits until the frontend lists both the prefill and the decode engine.
Status
New path, still switched off. Review open. Startup still refuses backend: megatron with dynamo: true; part 5 (#5025) turns it on. The review asks for one design change (C1: build the adapter on the same base as vLLM) and three smaller fixes (links below).

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

Background. Dynamo needs three services: etcd (where engines list themselves), NATS (events) and the frontend (HTTP). Each engine runs a Dynamo Worker wrapped around an adapter that knows how to talk to that engine. In prefill/decode serving, one engine reads the prompt (prefill) and hands its KV cache to a second engine that writes the answer (decode). Dotted words have a short explanation: hover or tap them.

Two ways an engine joins the frontend

vLLM already does this: once the engines load, the driver asks each data-parallel leader to start its endpoint, which wraps the engine in NeMo-RL's own adapter. A Megatron engine has no weights until the first refit, so this PR starts its endpoint in the first resume_after_refit. It also uses Megatron-LM's adapter, MegatronLLMEngine, which does not hold the engine. It waits for a "ready" message with the address of the engine's coordinator, then sends requests there. Example below: vLLM with 3 data-parallel leaders, and Megatron with 1 prefill engine (2 GPUs) and 1 decode engine (4 GPUs).

vLLM + dynamo (already on main) Megatron prefill/decode + dynamo (this PR) driver process driver process (or the controller's pickled copy, for ② and ④) ① _start_dynamo_services → ManagedDynamoRuntime starts etcd, NATS, frontend then engines load their weights ① __init__ → ManagedDynamoRuntime (same class) starts etcd, NATS, frontend engines built with no weights: nothing registers ② after load: _register_dynamo_engines → 3 DP leaders: start_dynamo_endpoint_async( namespace, engine_env) ② first resume_after_refit → start_endpoints per engine: rank 0 start_dynamo_endpoint(…) then every rank attach_dynamo_events(addr) ④ wait_for_engines(3) until /health lists {"backend": 3} + model ④ wait_for_engines({"prefill": 1, "backend": 1}) same call as vLLM; the copy runs its inner poll Ray call: namespace, engine_env Ray call to rank 0 → returns {event_port: 4000, system_port: 4001} vLLM worker (Ray actor, GPU) × 3 DP leaders engine rank 0 (Ray actor, GPU) × 2: prefill, decode ③ Worker(DynamoVllmAdapter).run() adapter: NeMo-RL's own runs on the actor's loop, calls AsyncLLM directly component "backend" (DYNAMO_COMPONENT) endpoint "generate" (DYNAMO_ENDPOINT) planes engine_env["DYN_…"] (indexed) DYN_SYSTEM_PORT "0" (OS picks) registers right away: the engine already serves endpoint_types "chat,completions" ③ Worker(MegatronLLMEngine).run() — new adapter: Megatron-LM's; task on the inference loop waits for ZMQ "ready" on :4000, then talks to the engine's coordinator through InferenceClient component "prefill" | "backend" (by role) endpoint "generate" (literal) planes .get("DYN_…", default) DYN_SYSTEM_PORT "4001" (picked here) endpoint_types "completions" registers in etcd registers in etcd (after "ready") etcd + NATS + Dynamo frontend (processes the driver started in ①; one set per run) /health instances: [{component:"backend", endpoint:"generate", instance_id}, … ×3] /health instances: [{component:"prefill", …}, {component:"backend", …}] ← Megatron new in this PR ③ right: C1 asks to replace this adapter ①–④ match across the two columns; ① and ④ use the same code

Inside ③ on the right, the second call in ② is attach_dynamo_events: rank 0 connects an event reporter to port 4000 and sends ready {coordinator_address}. Only after that does the adapter register the engine.

Where they become one. Both use the same ManagedDynamoRuntime.start for the services, the same wait_for_engines on the driver. That method is now a thin wrapper: it checks that etcd and NATS are still running, then calls wait_for_engine_registrations, the poll of each frontend's /health and /v1/models. This PR moved that poll out of the class for one reason: on the single controller, the first resume after a refit can also come from the controller's pickled copy, which owns no etcd or NATS process, so it can only run the poll. vLLM registers at setup on the driver, so it never needs that second entry point. They also share the same Dynamo Rust Worker. What still differs after that: the adapter class (NeMo-RL's vs. Megatron-LM's), when it starts (after load vs. at the first refit), how it reaches the engine (direct call vs. the coordinator), the component names, and how the plane and port settings are read.

QuestionvLLMMegatron (this PR)
Who starts the endpoint?The driver, right after the engines load.Whoever makes the first resume_after_refit call: the driver at setup, or the controller's copy. A repeat call returns the first call's ports (L237-L246).
What crosses into the engine actor?namespace, engine_envThose, plus the model name, a start timeout and the ports other engines on the node took; back come the event and system ports and, for decode, a handoff owner id.
What tells the driver it failed?dynamo_endpoint_error on each leader, polled during the wait.The same, through check_alive, polled during the wait. A stopped endpoint cannot start again in that process.
Which adapter?NeMo-RL's DynamoEngineAdapter, filled in by DynamoVllmAdapter.Megatron-LM's own MegatronLLMEngine. The review (C1) asks for a NeMo-RL DynamoMegatronAdapter on the same base as vLLM instead, so both backends share one adapter shape.

The change, as pseudocode

Controller: the first resume after a refit, before

# driver or controller copy; one call per training step
def resume_after_refit():
    errors = restore_engines()   # every engine resumes, bounded by a deadline
    if errors: raise

After

# driver or controller copy; one call per training step
def resume_after_refit():
    errors = restore_engines()          # every engine resumes, bounded by a deadline
    if errors: raise
    if fleet_ready: return                    # only the first resume does the rest
    for engine in engines:                    # in order, one deadline for all
        meta = engine.rank0.start_dynamo_endpoint(…)  # {event_port: 4000, system_port: 4001}
        engine.all_ranks.attach_dynamo_events(meta.addr) # rank 0 sends "ready"
    wait({"prefill": 1, "backend": 1}, check_alive)
    fleet_ready = True

Engine rank 0: start_dynamo_endpoint (new)

# Ray call on rank 0; the adapter then runs as a task on the engine's inference loop
def start_dynamo_endpoint(namespace, engine_env, …):
    if endpoint stopped: raise                 # one Worker per process
    if started: return same ports
    event, system = free ports in [4000, 4100)
    os.environ |= engine_env + {DYN_SYSTEM_PORT: system}
    cfg = Config(external mode, role, component, …)  # C1: replace with a NeMo-RL adapter
    run on inference loop: Worker(MegatronLLMEngine(cfg)).run()

Real code: _await_dynamo_fleet_ready, start_endpoints, start_dynamo_endpoint, _report_dynamo_ready.

What the review asks

The shared parts hold up: the services are one class, the wait is one function both sides call (the controller's copy too), and the endpoint lifecycle has CPU tests. The main ask is the adapter in ③ (C1): build a NeMo-RL DynamoMegatronAdapter on main's DynamoEngineAdapter, as vLLM does, instead of hosting Megatron-LM's MegatronLLMEngine. The design and what the later PRs then do are on the one P/D interface page. The other three are small: the controller test's fake worker accepts any argument name (C2), after #4801, Megatron's config should not carry render_workers or session_affinity_ttl_secs (they do nothing until #5097 turns chat on), so each backend passes them to the runtime instead (C3), and two docstrings are stale (C4).

C1 · dynamo_worker_mixin.py:327 C2 · test_megatron_dynamo_generation.py:213 C3 · dynamo_config.py:78 C4 · dynamo_generation.py:1019