Skip to content

feat(megatron): dynamo switch and per-role engine layouts - #4801

Merged
terrykong merged 23 commits into
mainfrom
laurad/pd-sc-1-plan-seam-config
Nov 25, 2026
Merged

terrykong merged 23 commits into
mainfrom
laurad/pd-sc-1-plan-seam-config

Conversation

@lauradang

@lauradang lauradang commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

First of six PRs that serve NeMo RL's Megatron inference engines as separate prefill and decode workers behind a Dynamo frontend, under the SingleController. A prefill engine reads the prompt and builds its KV cache. A decode engine takes that cache over NIXL and writes the answer. The trainer pushes new weights into both after every step (a refit).

This PR is config, validation and layout arithmetic only. Nothing starts yet, and refit is unchanged.

Building blocks

What it adds

  • Switch. policy.generation.backend: megatron + dynamo: true, the same switch vLLM uses. The replica shape comes from the backend-neutral policy.generation.disaggregation block.
  • dynamo_cfg. Megatron reads the same DynamoCfg block as vLLM (dynamo/config.py): frontend router args and timeouts, every field with a default.
  • Per-role engine layouts. mcore_generation_config.prefill_engine / decode_engine (EngineLayoutOverrides: TP and PP) let the two roles use different widths, the way trtllm_cfg keeps its own prefill and decode engines. Prefill (compute-bound) and decode (memory-bound) often want different widths; the KV handoff reshards between them. merged_inference_megatron_cfg gains an overrides= argument and drops the two role keys, so they never reach a model's megatron_cfg.
  • One place for engine widths. engine_width(role) = TP × PP of the layout the role's dedicated inference policy is built with. It refuses an expert-parallel grid that does not fit one engine, instead of failing at model build. replica_width builds on it.
  • Shared layout helpers. New disagg_layout.py (resolve_disagg_layout, replica_width_from, plan_engines, DISAGG_BACKENDS) holds what the disaggregated backends share, with the same names as the TRT-LLM layout module in feat: vLLM prefill/decode disaggregation for GRPO rollouts #4920.
  • Placement. Behind Dynamo, MegatronGeneration.nvlink_domain_span is the widest engine: the router pairs prefill and decode engines across the whole fleet, so only one engine's ranks need NVLink.
  • Engine role key. policy.generation._dynamo_role is not a user key: a later PR in the stack sets prefill or decode on each engine's own copy of the config, as TrtllmGeneration injects trtllm_cfg._engine_roles. Validation refuses it in a user's config.
  • Guards.
    • dynamo: true with a backend other than vllm or megatron is rejected.
    • The legacy GRPO entrypoint refuses Megatron + Dynamo (only the SingleController builds these engines).
    • validate_megatron_dynamo_generation lists the shapes the engines cannot serve: no disaggregation, fewer than one prefill or decode engine, more than one frontend or frontend tokenization, colocation, refit_transport set, quantization, prefix caching off, KV mode other than persist, the Megatron HTTP server on.
    • Without dynamo: true, MegatronGeneration refuses disaggregation.enabled and the per-role layouts, which only Dynamo engines read.
    • MegatronGeneration.validate_settings runs the checks above and then refuses dynamo: true outright, since nothing builds the engines yet. PR 5 removes that refusal.

Diagrams come from the design doc "Megatron Prefill/Decode behind Dynamo in NeMo RL".

Stack

main ← PR 1 (#4801, this) ← PR 2 (#4912) ← PR 3 (#5023) ← PR 4 (#5024) ← PR 5 (#5025) ← PR 6 (#5097)

Each PR is based on the one before it, so review each against its own base. PRs 1–4 change nothing at runtime on their own; PR 5 turns the feature on; PR 6 adds NeMo-Gym serving.

Tests

test_megatron_dynamo_config.py (switch, per-role widths, expert grid, NVLink span, rejected shapes, refusals without Dynamo), test_setup.py (replica sizing → inference segment size), test_grpo.py (legacy entrypoint refuses).

Issues

None

Convergence / perf / functionality proof

N/A: config, validation and layout arithmetic only. dynamo: true is still refused, so nothing new runs. GPU runs come with PR 5, which turns the feature on.

Needs upstream changes

None for this PR. The stack as a whole needs a Megatron-LM pin with the Dynamo integration before PR 5 can run.

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@lauradang
lauradang force-pushed the laurad/pd-sc-1-plan-seam-config branch 4 times, most recently from 61594f7 to 8ef2983 Compare October 20, 2026 21:11
@lauradang
lauradang marked this pull request as ready for review October 20, 2026 21:27

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all 12 changed files and the directly affected configuration, setup, runtime, and refit contracts. The preparatory engine seam is appropriately separated from the deferred Megatron fleet implementation. No blocking correctness findings; one nonblocking type-check coverage issue is noted inline.

Validation was static only: tests and linters were not executed, and no delegated review was performed.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@lauradang
lauradang force-pushed the laurad/pd-sc-1-plan-seam-config branch from 8ef2983 to ed14111 Compare October 26, 2026 03:25
@lauradang lauradang changed the title feat(dynamo): engine switch, Megatron engine config, and the engine seam feat(dynamo): engine switch, per-role layouts, and refit membership for Megatron engines Oct 26, 2026
@lauradang
lauradang force-pushed the laurad/pd-sc-1-plan-seam-config branch from ed14111 to e6d6957 Compare October 26, 2026 04:42
@copy-pr-bot

copy-pr-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@lauradang lauradang changed the title feat(dynamo): engine switch, per-role layouts, and refit membership for Megatron engines feat(dynamo): engine switch, per-worker layouts, and refit membership for Megatron engines Oct 26, 2026
@lauradang
lauradang marked this pull request as draft November 2, 2026 17:30
@copy-pr-bot

copy-pr-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

…bership shard widths

Megatron prefill/decode engines served through a managed Dynamo frontend are
switched on with policy.generation.dynamo=true on a backend=megatron config,
the way vLLM engines will be. A new megatron.dynamo_config module validates
the dynamo_cfg block (frontend args, timeouts, per-role prefill_engine /
decode_engine layout overrides), sizes each role's engine from the layout its
dedicated inference policy is built with (including the expert-parallel grid
check), and tiles replicas prefill-first.

RefitMembership gains per-shard widths with cumulative rank prefixes so a
prefill shard at TP 4 can sit next to decode shards at TP 2 in one refit
communicator; the vLLM and Megatron refit paths index workers through
worker_offset. The legacy GRPO entrypoint refuses the switch; the
SingleController sizes inference segments by the replica width.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang
lauradang changed the base branch from laurad/pd-sc-base-4773 to main November 4, 2026 02:59
@lauradang
lauradang force-pushed the laurad/pd-sc-1-plan-seam-config branch from e72303f to 6593448 Compare November 4, 2026 03:00
@lauradang lauradang changed the title feat(dynamo): engine switch, per-worker layouts, and refit membership for Megatron engines feat(megatron): dynamo switch, per-role engine layouts, and refit membership shard widths Nov 4, 2026
@lauradang lauradang self-assigned this Nov 4, 2026
@lauradang
lauradang marked this pull request as ready for review November 4, 2026 03:04

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up complete; no eligible findings remain.

The earlier type-check finding no longer applies: the reported module is absent from the current Dynamo directory. The current worker-pool implementation is included in the type-check roots.

I inspected all 13 changed files across three delegated topics and checked the affected setup and refit contracts. The previously reviewed source tree was unavailable, so I couldn't establish which changes were introduced since the previous review. This approval is limited to the follow-up scope, not a fresh full review.

Validation was static only; tests and linters weren't executed.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a findings-only review. Request a full review with /review reset=true.

@lauradang lauradang left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting the stack so cleanly — the arithmetic held up under review. The refit offsets were hand-checked with widths (4, 2, 2) and a dead lower shard. The expert-grid check matches Megatron-LM's initialize_model_parallel divisibility rule at the pinned commit. merged_inference_megatron_cfg is unchanged for existing callers.

Four inline items. The one that matters is the first: until PR 5 lands, the SingleController silently accepts dynamo: true.

Generated by Claude Code

Comment thread nemo_rl/models/generation/__init__.py Resolved
lauradang and others added 2 commits November 3, 2026 19:28
…layouts

- MegatronGeneration.validate_settings checks a dynamo config with
  validate_megatron_dynamo_generation and then refuses it, so the
  SingleController no longer trains plain aggregated engines with the
  switch on.
- Per-role TP/PP overrides must be positive.
- Type-check megatron/dynamo_config.py.
- Test the non-megatron backend guard in resolve_generation_class.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
A Megatron engine behind Dynamo learns whether it is a prefill or decode
engine from policy.generation._dynamo_role, which the engine fleet sets on
each engine's own copy of the config (as TrtllmGeneration injects
trtllm_cfg._engine_roles). The worker can then build the handoff engine for
that role without a separate pre-build attach call. Validation refuses the key
in a user's config.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@github-actions github-actions Bot added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Nov 23, 2026
@github-actions

Copy link
Copy Markdown

CI label: CI:Lfast, set automatically (default for code and main CI changes).

  • Removed CI:L0 added by lauradang: only repo maintainers can change the CI label or skip CI.
    If this label is wrong, ask a maintainer to set the right one.

terrykong
terrykong previously approved these changes Nov 23, 2026
@terrykong
terrykong enabled stacked auto-merge (squash) November 23, 2026 22:50
@terrykong
terrykong disabled stacked auto-merge November 24, 2026 00:37

@jthomson04 jthomson04 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 0db391a for the Dynamo side. The switch, the validation rules and the frontend args line up with the pinned Dynamo nightly and with Megatron-LM's integration at the pin (NIXL only, persist KV mode, one replica per engine). Two should-fix items inline; the third is a question. I haven't repeated Terry's threads.

…k by engine

- Megatron behind Dynamo validates dynamo_cfg with the vLLM DynamoCfg;
  MegatronDynamoCfg, megatron_dynamo_cfg and DynamoFrontendArgsBase are gone.
- backend megatron with disaggregation.enabled and dynamo off is refused
  instead of silently serving aggregated engines.
- nvlink_domain_span behind Dynamo is the widest engine, not the replica:
  the router pairs prefill and decode engines across the whole fleet, so
  the KV handoff is not kept inside a replica.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 65b2e6b

@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 709918f

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 2d6450e

- MegatronGeneration.validate_settings refuses mcore_generation_config
  prefill_engine / decode_engine when dynamo is off; they were dropped
  without an error and the run used the shared layout.
- dynamo/config.py module docstring covers Megatron as well as vLLM.
- grpo.py guard comment names the side effects it actually precedes.
- Tests: decode-wider nvlink_domain_span case; zero_prefill,
  frontend_tokenize and real_quant refusal cases; role layouts
  without Dynamo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang lauradang changed the title feat(megatron): dynamo switch, per-role engine layouts, refit shard widths feat(megatron): dynamo switch and per-role engine layouts Nov 24, 2026
@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 6320c18

The per-role engine layout check indexed
policy_config["generation"]["mcore_generation_config"], which pyrefly
rejects because GenerationConfig has no such key. Reuse the mcore_cfg
local that validate_settings already reads through MCoreGenerationConfig.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 9c549f8

@lauradang lauradang left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review at 9c549f8. Thanks to terrykong and jthomson04: all 11 of their threads still hold at this head (C4–C15 and the three Dynamo threads). Only terrykong's C11 shared-module request is partly open; see C30 and C31.

Findings:

  1. (C29) Distillation now accepts megatron with dynamo=true and ignores the Dynamo settings. Share the GRPO refusal with distillation.
  2. (C30) Delete the unused DISAGG_BACKENDS.
  3. (C31) Make TRT-LLM use the shared layout helpers; restore the engine-count check first.
  4. (C32) Delete Megatron's replica_width, which only tests call.
  5. (C33) Drop the Dynamo check in resolve_generation_class that can never fire.

Checked and fine: the engine-width expert-grid rule matches Megatron-LM's initialize_model_parallel; the prefix-caching and persist requirements match Megatron-LM's disaggregation code; all 44 new tests pass on CPU; ruff is clean.

nemo-rl-review: kind=self harness=claude-code/2.1.329 model=claude-opus-5-5 effort=medium review-pr-team=v3 reviewed=9c549f8

Comment thread nemo_rl/models/generation/__init__.py Resolved
Comment thread nemo_rl/models/generation/__init__.py Resolved
lauradang and others added 2 commits November 24, 2026 15:33
- Add reject_megatron_dynamo_outside_single_controller in
  megatron/dynamo_config.py: for backend megatron it raises
  NotImplementedError when dynamo is on and ValueError when
  prefill_engine / decode_engine are set without dynamo. Call it from
  grpo.setup (replacing the inline check) and distillation.setup, so
  distillation no longer trains plain Megatron for a Dynamo config.
- Drop the unread DISAGG_BACKENDS constant from disagg_layout.py.
- Drop the duplicate dynamo guard in resolve_generation_class; the
  backend refusal test now targets configure_generation_config, the
  copy that runs.
- Drop the test-only Megatron replica_width helper and its asserts.
- Give each build_clusters Megatron Dynamo case an inference node count
  whole replicas fill.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
TRT-LLM kept its own copies of the layout helpers that disagg_layout.py
now holds, so main had two resolve_disagg_layout functions and two
engine planners.

- trtllm/config.py drops _as_dict and resolve_disagg_layout and uses
  the shared ones; replica_width sums through replica_width_from.
- TrtllmGeneration._plan_engines calls disagg_layout.plan_engines, so
  its layout errors are now ValueError instead of AssertionError.
- plan_engines refuses a replica without at least one prefill and one
  decode engine, the check TRT-LLM's planner had: without it 0/0 hit
  ZeroDivisionError and 0/1 planned decode-only replicas.
- grpo.py, nccl_reshard_utils.py and the TRT-LLM tests import
  resolve_disagg_layout from disagg_layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Laura Dang <[email protected]>
@lauradang

Copy link
Copy Markdown
Contributor Author

/ok to test 45337a5

@terrykong
terrykong enabled stacked auto-merge (squash) November 25, 2026 00:19
@terrykong
terrykong merged commit 9676b83 into main Nov 25, 2026
100 of 107 checks passed
@terrykong
terrykong deleted the laurad/pd-sc-1-plan-seam-config branch November 25, 2026 03:26
shikicloud added a commit to shikicloud/RL that referenced this pull request Nov 26, 2026
main d040a22 brings NVIDIA-NeMo#4801 (Megatron prefill/decode behind Dynamo), which adds
its own disagg_layout.py with a subset of this branch's helpers, plus NVIDIA-NeMo#5267
(prefix counts per HTTP frontend) and NVIDIA-NeMo#5158 (multi-harness).

- disagg_layout.py: this branch's module, with main's replica_width_from and
  main's plan_engines (ValueError instead of assert, role-count check).
  validate_disagg_entrypoint lets megatron + dynamo through to its own checks.
- trtllm/config.py, trtllm_generation.py, grpo.py: keep the shared-module
  wrappers of this branch.
- vllm_generation.py: fan the per-frontend prefix counts out per engine.
- nemo_gym.py: pass both forward_session_id_as_conversation_id and
  multi_harness.

Signed-off-by: Shiki Wu <[email protected]>
shikicloud added a commit to shikicloud/RL that referenced this pull request Nov 27, 2026
DisaggConfig takes num_prefill_engines / num_decode_engines as positive
integers on this branch, so a zero count is refused when the layout block
is read, before the Megatron Dynamo check and plan_engines see it. The
three cases from NVIDIA-NeMo#4801 matched the later messages.

Signed-off-by: Shiki Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants