Skip to content

feat(refit): support context-parallel draft weight transport - #3750

Open
seonjinn wants to merge 128 commits into
NVIDIA-NeMo:mainfrom
seonjinn:seonjinn/draft-cotrain-cp-refit-v4-20260821
Open

seonjinn wants to merge 128 commits into
NVIDIA-NeMo:mainfrom
seonjinn:seonjinn/draft-cotrain-cp-refit-v4-20260821

Conversation

@seonjinn

@seonjinn seonjinn commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Adds pipeline-parallel-aware draft weight refit for Megatron draft co-training, and the context-parallel checks that nccl_reshard needs.

  • Only the last PP stage owns the draft model. During refit, the owner sends a small manifest (status, owner rank, and per-tensor name, shape, and dtype) with broadcast_obj_from_pp_rank. Then it streams the draft tensors one at a time with dist.broadcast on the PP group. The code is in nemo_rl/models/megatron/draft/refit.py.
  • Draft weights go after the policy weights in the live, metadata, and ModelOpt real-quant refit paths.
  • If the owner export fails, every rank in the PP lane raises the same coded error (EXPORTER_ERROR or MANIFEST_INVALID). The nccl_reshard misc broadcast checks each draft tensor against the prepared metadata (METADATA_DRIFT) before it sends.
  • nccl_reshard maps trainer CP replicas onto the transport mesh and checks that all ranks build the same weight manifest (assert_refit_weight_manifest_rank_agreement in nccl_reshard_utils.py).
  • Setup rejects unsupported generation topology before collective initialization.

Supported contract

  • Draft refit works through the full-param refit paths (colocated CUDA-IPC, collective broadcast, and checkpoint-engine), with Megatron PP >= 1.
  • Setup rejects policy.draft.enabled=true with refit_transport: nccl_reshard or a sparse transport. Draft refit over nccl_reshard is a follow-up.
  • With nccl_reshard, vLLM generation must use CP1.

Issues

None.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Validation

  • Head fbd587bc90 addresses the 11-18 review. It removes the packed-broadcast readiness token and the custom binary PP protocol, and it streams the draft per tensor.
  • Tests: tests/unit/models/megatron/test_draft_refit.py (PP1 stream, metadata-only, disabled draft, coded owner errors, metadata drift, and a 2-rank gloo PP stream), manifest agreement tests in tests/unit/weight_sync/test_nccl_reshard_utils.py, and a misc-broadcast draft drift test in test_megatron_worker.py.
  • Local checks: compileall and ruff (isort, lint, format) on the changed files. Unit tests did not run locally (no GPU, Linux-only lockfile). CI runs them.
  • This is a correctness feature. No throughput improvement is claimed.

Reapply the intended DFlash-only net delta from ef405c8 onto NVIDIA main 36b5999. The original signed series spans fe8a7fa..ef405c8.

Signed-off-by: seonjinn <[email protected]>
@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test 8a4c7f2

seonjinn added a commit to seonjinn/RL that referenced this pull request Nov 12, 2026
Signed-off-by: seonjinn <[email protected]>

# Conflicts:
#	nemo_rl/models/policy/workers/megatron_policy_worker.py
#	nemo_rl/weight_sync/nccl_reshard_weight_synchronizer.py
#	tests/unit/models/policy/test_megatron_worker.py
#	tests/unit/weight_sync/test_weight_synchronizer.py
@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test 440e439

@seonjinn

Copy link
Copy Markdown
Contributor Author

@jinglinglingling can you review it again ?

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test 1192c2b

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test 373c2a2

@jinglinglingling jinglinglingling 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.

Thanks for the quick turnaround. I re-checked the earlier comments at 373c2a2: the Megatron-generation token order (with the new ordering test), the refit.md wording, the new in-process and gloo tests, and the cleanups all look good, and the OCI GPU run covers the 8-rank TP2xPP2xCP2 layout.

Two things left:

  1. The TRT-LLM nccl_reshard receiver that came in with the main merge has the same token-order problem (inline).
  2. The PR conflicts with main again (prepare_nccl_reshard_refit_info in vllm_backend.py, and docs/design-docs/nccl-reshard-refit.md). Because of that, CI fails at checkout and no tests actually ran at this head. Could you resolve the conflicts and re-run CI, including codecov?

Happy to approve once those are in.

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test d5aa627

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test 0ec8492

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test d1f68b7

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test e65acdb

@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test fa45b07

seonjinn added a commit to seonjinn/RL that referenced this pull request Nov 18, 2026
Carry over the NVIDIA-NeMo#3750 review changes: required draft_weights, preflight
token before bulk reshard on Megatron and TRT-LLM receivers, and removal
of draft_metadata_only, include_draft and generation cp_size.

Signed-off-by: seonjinn <[email protected]>

@yuki-97 yuki-97 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.

Great to see draft refit become CP/PP-aware: the PR brings the draft from each lane's owner stage to every trainer rank, so the refit stream stays identical across ranks under TP, PP and CP.

Thanks for posting the OCI GB200 GPU gate (2 nodes x 4 GPUs) in the description, including the 8-rank TP2xPP2xCP2 torchrun stage; it shows clearly that the lane protocol holds on real hardware. Thanks also for the extensive self-review.

Scope: the PR diff, at fa45b07

Checked:

  • Readiness token pairs on every path: the Megatron/Automodel collective and Megatron nccl_reshard senders send it, and the vLLM, Megatron-generation and TRT-LLM receivers read it before any payload on both transports, including empty misc.
  • Runs without a draft enter no new draft collective, since _iter_draft_weights_for_refit returns first; the only added cost is one int32 token per refit.
  • Every PP-lane raise depends on values all lane ranks already agreed on, and rank-local export/allocation failures go through the consensus, so lanes fail together.
  • Every caller of _iter_params_with_optional_kv_scales passes the preflighted draft_weights (keyword-only, required), so the draft is exported once per refit before any payload.

Remaining comments are on:

  • The test-only branch in the PP-owner broadcast and the manifest-agreement helper
  • The stale reason in the lm_policy.py guard comment for draft with nccl_reshard
  • Reusing the existing PP object broadcast instead of the custom wire protocol
  • Streaming the draft like the target instead of the per-refit preflight and readiness token

nit

  • Builder patch in the manifest-mismatch test that does not reach the worker
  • Moving _collective_device and _reduce_lane_consensus into nemo_rl/distributed/collectives.py
  • Moving the manifest-agreement helper into nccl_reshard_utils.py
  • Gathering the draft refit code into a new draft/refit.py
  • Moving the tensor-manifest encoding into a new nemo_rl/distributed/tensor_manifest.py if the custom protocol stays
  • Please refresh the PR description: it still says the PR is stacked on #3744, cites older validation heads, and its Supported contract section lists train-side CP lanes as supported while setup still rejects draft with Megatron CP>1

Generated by Claude Code (#4999 version)

Comment thread nemo_rl/models/megatron/draft/utils.py Resolved
Comment thread docs/guides/refit.md Resolved
Comment thread nemo_rl/models/megatron/draft/utils.py Resolved
Comment thread nemo_rl/models/megatron/draft/utils.py Resolved
Comment thread docs/guides/refit.md Resolved
Comment thread nemo_rl/models/megatron/draft/utils.py Resolved
Address review: the PP owner sends a manifest with broadcast_obj_from_pp_rank,
then streams draft tensors one at a time on the PP group. Remove the
packed-broadcast preflight token, move the manifest rank-agreement helper to
nccl_reshard_utils and the collective helpers to collectives.py, and append
checked draft weights last in the nccl_reshard misc broadcast.

Signed-off-by: seonjinn <[email protected]>
@seonjinn

Copy link
Copy Markdown
Contributor Author

/ok to test fbd587b

copy-pr-bot Bot pushed a commit that referenced this pull request Nov 19, 2026
Take the manifest plus per-tensor PP stream from draft/refit.py and drop the readiness token, draft preflight and packed frame protocol. stream_draft_weights_for_refit gains an export_weights hook so DFlash and DSpark providers export their own weights.

Signed-off-by: seonjinn <[email protected]>

@yuki-97 yuki-97 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.

Merge blockers — please rebase on main; the PR is 36 commits behind and the "Check if PR branch is up to date" job allows at most 10.

Thanks for resolving the previous round of comments: the draft now streams from its PP owner stage like the target, and the readiness token, the per-refit preflight and the custom wire protocol are gone.

Scope: fix commits b4e5372 through fbd587b, checked against the open threads from the previous review, plus a scan of the changed files for new issues.

Checked:

  • Every previous thread held at the current head: stream_draft_weights_for_refit sends one manifest with broadcast_obj_from_pp_rank and one dist.broadcast per tensor, the draft comes last in every live and metadata producer (collective, CUDA-IPC, checkpoint-engine, both ModelOpt paths and the nccl_reshard misc stream), and the token and preflight files are byte-identical to main.
  • The WORLD manifest-agreement check is rank-stable: the bulk/misc split depends on mapping types only and is EP-reduced, non-owner shapes come from the owner's object gather, and the draft manifest is exchanged before the meta context.
  • Dropping the vLLM-side speculator rejection is safe for a static drafter: with policy.draft.enabled=false no draft.* name enters misc_meta, the manifest has no draft component and finalization skips it, the same as the collective path.
  • The second vllm_context_parallel_violations call in the synchronizer is load-bearing: distillation builds the synchronizer through the factory without check_nccl_reshard_refit_support.

Remaining comments are on:

  • A PP>1 GPU run at this head, since the new NCCL stream has only run under gloo
  • The follow-up issue for draft refit over nccl_reshard

nit

  • Two speculative_config tests that no longer check anything
  • The missing @requires_weight_folding on the new real-quant order test
  • PR title: "support context-parallel draft weight transport" names a capability setup rejects (draft with Megatron CP>1); feat(draft): stream Megatron draft weights across PP stages for refit matches the description

Generated by Claude Code

yield f"{DRAFT_REFIT_PREFIX}{name}", tensor
return

pp_group = parallel_state.get_pipeline_model_parallel_group()

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.

Following up on the previous comment (#3750 (comment)): this PP>1 stream (refit.py:133-141, one broadcast_obj_from_pp_rank manifest, then one dist.broadcast per tensor on the PP group) is new in fbd587b and has only run under gloo in the 2-process test_pp2_streams_owner_weights_to_every_lane_rank. The OCI GB200 TP2xPP2xCP2 stage posted earlier (#3750 (comment)) ran at 5b9e0dc on the protocol this commit removed, and the Megatron eagle3 nightly runs PP=1, so the NCCL path with a real Megatron PP layout has not executed at this head.

Please re-run a PP>1 draft refit on GPUs at fbd587b (the earlier torchrun stage adapted to TP2xPP2 is enough, since draft with Megatron CP>1 stays rejected) and post the result in the PR description.

ids=["eagle3", "dflash", "dspark", "dflash-k4", "model-without-method"],
)
def test_check_nccl_reshard_refit_support_rejects_draft_speculators(
def test_check_nccl_reshard_refit_support_accepts_draft_speculators(

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.

These two tests no longer check anything: this PR removed the only speculative_config read from check_nccl_reshard_refit_support (nccl_reshard_utils.py:666-965), so test_check_nccl_reshard_refit_support_accepts_draft_speculators and test_check_nccl_reshard_refit_support_accepts_non_draft_speculators only show that an ignored key is ignored. The non-draft one was the negative control for the deleted rejection, and draft with nccl_reshard is still rejected one layer up in lm_policy.py.

Suggest fix: delete these:

torch.testing.assert_close(output[0][1], torch.ones(1))


def test_real_quant_refit_yields_draft_weights_last(monkeypatch):

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.

nit: test_real_quant_refit_yields_draft_weights_last is the only _make_real_quant_worker() test without @requires_weight_folding. The helper reads MegatronQuantPolicyWorker.__ray_metadata__ (test_megatron_quant_worker.py:125), which is bound only under if _MODELOPT_AVAILABLE: (test_megatron_quant_worker.py:46-55), so without modelopt this test errors with NameError while its siblings skip.

Suggest fix:

Suggested change
def test_real_quant_refit_yields_draft_weights_last(monkeypatch):
@requires_weight_folding
def test_real_quant_refit_yields_draft_weights_last(monkeypatch):

@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

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

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.

6 participants