Repository navigation
Conversation
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]>
Open
3 of 4 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sDynamoVllmAdapter, in three commits:feat(dynamo): add prefill/decode hooks to DynamoEngineAdapter— three optional hooks on the shared base:serve_prefill, ahandoffargument onserve_rollout, andrelease_handoff. The adapter'sdisaggregation_modepicks the handler: a prefill engine returns its handoff asdisaggregated_params, and a decode engine reads it fromprefill_resultand releases it once.start_dynamo_endpointregisters that mode under the component the caller names. vLLM engines stay aggregated;DynamoVllmAdapteronly accepts the new argument.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 ownInferenceClient, the onegenerate_asyncuses. It does not subclassMegatronLLMEngine.refactor(megatron): serve engines through DynamoMegatronAdapter—MegatronDynamoGenerationMixin.start_dynamo_endpointbuilds the adapter and calls main'sstart_dynamo_endpointwith the runtime'sengine_envas is (DYN_SYSTEM_PORT=0, so the OS picks it). Removed: the Megatron-LMConfig/MegatronLLMEngine/Workerblock,attach_dynamo_eventsand the event reporter, the event and system port picking, and theDEFAULT_DYNAMO_SYSTEM_PORT_RANGE_*constants with their port-map line. With no ports to share,start_endpointsstarts every engine at once.dynamo_worker_mixin.pygoes from 415 to 228 lines.What is real and what is a sketch
startInferenceEngineCapabilities.from_engineon the built engine, as Megatron-LM's adapter registers capacityserve_rollout(no handoff)_build_sampling_params+_generate_with_persistent_engine, the stepgenerate_asyncruns per sampleserve_rollout(decode, with handoff)add_request_with_kv_handoff_streaming(prompt, params, kv_meta, block_ids); on failure it waits forabort_request_and_waitbefore marking the handoff safe to releaseserve_prefilladd_request_with_idwithdo_kv_handoff=True,num_tokens_to_generate=0; returns the pinned blocks plus areleaseblock in Megatron-LM's own format. A cancelled prefill frees its blocks withInferenceClient.release_handoffonce it finishesrelease_handoffRELEASE_KVto 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)CLAIM_KVwith this engine's owner. Only private in Megatron-LM:MegatronLLMEngine._claim_remote_handoff(llm_engine.py:609, wire steps in_remote_handoff_command,:618)abortkv_event_sourcesPushSource; the engine'sdynamo_helperKV listener publishes in-process. Decode returns noneBoth stubs need the same upstream change: move the claim/release wire code out of
MegatronLLMEngineinto functions (for example besideconfirm_terminatedinhandoff_recovery.py), so this adapter can call them without copying the protocol.Behavior differences from NVIDIA-NeMo#5023 to decide on
serve_rolloutreturns, 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._recover_handoffs). Not ported here.start_dynamo_endpointregisterschat,completions; feat(megatron): host Megatron-LM's Dynamo adapter on each engine's rank zero NVIDIA-NeMo/RL#5023 registeredcompletionsonly. 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.generate_asyncdoes not return them either.leader_worker_idxis 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.moe/ops/paged_stashneeds triton, which was also stubbed locally. CI runs them against the real packages.do_kv_handoff=True. Each made one adapter test fail.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