Skip to content

refactor(megatron): serve P/D engines through DynamoMegatronAdapter (proposal for #5023 C1) - #58

Draft
terrykong wants to merge 3 commits into
terryk/pr5023-basefrom
terryk/pr5023-c1-dynamo-megatron-adapter
Draft

terrykong wants to merge 3 commits into
terryk/pr5023-basefrom
terryk/pr5023-c1-dynamo-megatron-adapter

Conversation

@terrykong

Copy link
Copy Markdown
Owner

Proposal, not for merge. This shows the design asked for in review comment C1 on NVIDIA-NeMo#5023, applied on top of NVIDIA-NeMo#5023's head (c396aafa), so it can be compared with the PR as code instead of as a description.

What it changes

Today rank 0 of each Megatron prefill/decode engine hosts Megatron-LM's own Dynamo adapter (MegatronLLMEngine) in external launch mode. That adapter talks to the engine through its coordinator, gets "ready" and KV events over a ZMQ event reporter, and needs an event port and a system port picked from [4000, 4100).

This branch replaces it with NeMo RL's own DynamoMegatronAdapter, a sibling of main's DynamoVllmAdapter, in three commits:

  1. feat(dynamo): add prefill/decode hooks to DynamoEngineAdapter — three optional hooks on the shared base: serve_prefill, a handoff argument on serve_rollout, and release_handoff. The adapter's disaggregation_mode picks the handler: a prefill engine returns its handoff as disaggregated_params, and a decode engine reads it from prefill_result and releases it once. start_dynamo_endpoint registers that mode under the component the caller names. vLLM engines stay aggregated; DynamoVllmAdapter only accepts the new argument.
  2. feat(megatron): add DynamoMegatronAdapter over the native worker — the adapter. It runs on rank 0, on the worker's inference loop, and calls the engine through the worker's own InferenceClient, the one generate_async uses. It does not subclass MegatronLLMEngine.
  3. refactor(megatron): serve engines through DynamoMegatronAdapter — MegatronDynamoGenerationMixin.start_dynamo_endpoint builds the adapter and calls main's start_dynamo_endpoint with the runtime's engine_env as is (DYN_SYSTEM_PORT=0, so the OS picks it). Removed: the Megatron-LM Config / MegatronLLMEngine / Worker block, attach_dynamo_events and the event reporter, the event and system port picking, and the DEFAULT_DYNAMO_SYSTEM_PORT_RANGE_* constants with their port-map line. With no ports to share, start_endpoints starts every engine at once. dynamo_worker_mixin.py goes from 415 to 228 lines.

What is real and what is a sketch

Adapter method State How
start real InferenceEngineCapabilities.from_engine on the built engine, as Megatron-LM's adapter registers capacity
serve_rollout (no handoff) real the worker's own _build_sampling_params + _generate_with_persistent_engine, the step generate_async runs per sample
serve_rollout (decode, with handoff) real after the claim add_request_with_kv_handoff_streaming(prompt, params, kv_meta, block_ids); on failure it waits for abort_request_and_wait before marking the handoff safe to release
serve_prefill real add_request_with_id with do_kv_handoff=True, num_tokens_to_generate=0; returns the pinned blocks plus a release block in Megatron-LM's own format. A cancelled prefill frees its blocks with InferenceClient.release_handoff once it finishes
release_handoff stub must send RELEASE_KV to the prefill engine's coordinator. Megatron-LM has this only as a private method, MegatronLLMEngine._release_handoff_from_meta_async (llm_engine.py:708)
claim before decode reads (_claim) stub must send CLAIM_KV with this engine's owner. Only private in Megatron-LM: MegatronLLMEngine._claim_remote_handoff (llm_engine.py:609, wire steps in _remote_handoff_command, :618)
abort real cancels the request's task; the native step aborts its Megatron request
kv_event_sources real prefill engines with prefix caching return a PushSource; the engine's dynamo_helper KV listener publishes in-process. Decode returns none

Both stubs need the same upstream change: move the claim/release wire code out of MegatronLLMEngine into functions (for example beside confirm_terminated in handoff_recovery.py), so this adapter can call them without copying the protocol.

Behavior differences from NVIDIA-NeMo#5023 to decide on

  • When prefill KV is freed. The base releases a handoff after serve_rollout returns, so prefill holds the blocks for the whole decode. Megatron-LM's adapter releases at the first decoded chunk. A backend can release earlier and make the later call do nothing; I left that out to keep the hook simple.
  • No release retry loop. Megatron-LM's decode adapter retries failed releases every 5 s (_recover_handoffs). Not ported here.
  • Endpoint types. Main's start_dynamo_endpoint registers chat,completions; feat(megatron): host Megatron-LM's Dynamo adapter on each engine's rank zero NVIDIA-NeMo/RL#5023 registered completions only. A chat call reaches the Megatron adapter and is refused with a clear error until feat(megatron): serve NeMo-Gym through the Dynamo frontend's chat engine NVIDIA-NeMo/RL#5097 adds the Gym handler.
  • Health probes. Megatron-LM answers decode health probes with an engine heartbeat. The shared base has no probe handling, same as vLLM on main.
  • Routed experts are not returned, because the native Megatron generate_async does not return them either.
  • leader_worker_idx is the engine's index in the fleet; it only feeds per-worker metrics.

How to read it

Commit by commit. Commit 1 is the shared interface, commit 2 is the only Megatron-specific code, and commit 3 is mostly deletions. The design picture is at https://terrykong.github.io/gh-pages-poc/terryk/pr-4801-pd-one-interface.html.

Tested

CPU only, on macOS, in a hand-built venv:

  • test_dynamo_engine_adapter.py (new, 9 passed), test_dynamo_megatron_adapter.py (new, 11 passed), test_megatron_dynamo_worker_mixin.py (12 passed), test_megatron_dynamo_generation.py (43 passed): 75 passed together.
  • ai-dynamo has no macOS wheel, so these ran against a small local stand-in for the ai-dynamo 1.8 classes they import (not committed). Megatron-LM's moe/ops/paged_stash needs triton, which was also stubbed locally. CI runs them against the real packages.
  • Two mutations were checked: dropping the cancelled-prefill release, and dropping do_kv_handoff=True. Each made one adapter test fail.
  • Not run: test_dynamo_vllm_adapter.py (needs vLLM), pyrefly, and anything on a GPU. ruff 0.9.9 check and format pass on every changed file.

🤖 Generated with Claude Code

A backend that splits prefill and decode across engines now has three
optional hooks on the shared adapter base:

- serve_prefill: a prefill engine computes the prompt's KV, keeps it,
  and returns where it is. The base sends that to Dynamo as
  disaggregated_params on a "length" chunk with no tokens.
- serve_rollout(handoff=...): a decode engine gets the prefill's
  disaggregated_params and reads the KV from there.
- release_handoff: called once per handoff after serve_rollout returns
  or raises, so the prefill engine can free the KV.

The adapter's disaggregation_mode picks the handler, and
start_dynamo_endpoint registers the engine as that mode under the
component the caller names. Aggregated engines (vLLM today) are
unchanged; the vLLM adapter only accepts the new argument.

Signed-off-by: Terry Kong <[email protected]>
The Megatron backend of DynamoEngineAdapter. It runs on rank 0 of a
prefill or decode engine, on the worker's inference loop, and calls the
engine through the worker's own InferenceClient:

- start: registers the engine's KV capacity from
  InferenceEngineCapabilities.from_engine.
- serve_rollout: runs the same SamplingParams and generate step that
  generate_async uses. With a handoff, it reads the prefill's KV with
  add_request_with_kv_handoff_streaming.
- serve_prefill: runs prefill with do_kv_handoff=True and returns the
  pinned blocks plus the release address, in Megatron-LM's format.
- abort: cancels the request's task; the native step aborts the
  Megatron request.
- kv_event_sources: prefill engines with prefix caching forward the
  engine's KV events to Dynamo in-process.

Claiming and releasing a handoff on the prefill coordinator are left as
NotImplementedError: Megatron-LM has that code only as private methods
of its own adapter (llm_engine.py:609 and :708).

Signed-off-by: Terry Kong <[email protected]>
Rank 0 of each prefill/decode engine now builds DynamoMegatronAdapter
and registers it with engine_adapter.start_dynamo_endpoint, the same
call the vLLM worker uses, under the component for its role.

Because the adapter calls the engine in-process, these go away:
- Megatron-LM's Config / MegatronLLMEngine / Worker setup and its
  external launch mode,
- attach_dynamo_events and the engine event reporter over ZMQ,
- the event port and the system port picked from [4000, 4100), with the
  DEFAULT_DYNAMO_SYSTEM_PORT_RANGE_* constants. The runtime's
  engine_env keeps DYN_SYSTEM_PORT=0, so the OS picks it.

With no ports to share, start_endpoints starts every engine at once and
still names the engine that failed. Tests drive the real adapter and
start_dynamo_endpoint with only Dynamo's Worker faked.

Signed-off-by: Terry Kong <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant