Skip to content

Refit: local plan building, node-add cache key, and NIXL backend#5872

Merged
wdykas merged 3 commits into
NVIDIA:mainfrom
wdykas:wdykas/local-refit-plan-nixl
Jul 24, 2026
Merged

Refit: local plan building, node-add cache key, and NIXL backend#5872
wdykas merged 3 commits into
NVIDIA:mainfrom
wdykas:wdykas/local-refit-plan-nixl

Conversation

@wdykas

@wdykas wdykas commented Jul 18, 2026

Copy link
Copy Markdown
Contributor
  1. Local deterministic global planning instead of rank 0 building and sending to everyone.
  2. We are adding nixl backend as a communication backend.

The point of these changes is that this would allow us to hopefully be able to handle arbitrary nodes coming in and out of the training and inference groups without resetting the job.

  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@wdykas
wdykas requested review from a team as code owners July 18, 2026 21:57
@copy-pr-bot

copy-pr-bot Bot commented Jul 18, 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.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft July 18, 2026 21:57
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@wdykas

wdykas commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 14b97e0

@wdykas

wdykas commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

/claude review

@claude claude Bot 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.

Light review — the refactor looks correct and self-consistent (clean rename with no lingering build_centralized_reshard_plan references, valid _PlanCacheKey.world_size addition, and deterministic ordering that holds because gathered_pairs is identical across ranks and Python dicts preserve insertion order).

One non-blocking suggestion on test coverage: this PR adds a substantial NIXL backend plus a centralized→local plan-building refactor, but no new unit tests. The newly-extracted pure functions are testable without GPUs or a live process group and would directly guard the PR's core correctness claim — that sender and receiver independently derive matching task_ids. A cheap CPU-only test in tests/unit_tests/resharding/test_planner.py could construct synthetic (src_meta, dst_meta) rosters and assert:

  • build_plan_from_rosters(rosters, my_global_rank=A) send ops and build_plan_from_rosters(rosters, my_global_rank=B) recv ops share the same task_id for each transfer (the determinism guarantee);
  • adding a rank to the rosters and replaying keeps existing task_ids stable (the node-add / membership-growth path).

Everything else LGTM.

Replace the centralized (rank-0 gather/build/scatter) reshard planner with local
plan building: every rank all-gathers parameter metadata and replays one
deterministic schedule (_iter_global_transfer_ops), keeping only its own
send/recv ops. The iteration order and monotonic task_id depend only on the
gathered metadata, so a send op and its matching recv op agree on task_id with
no central authority, dropping the rank-0 bottleneck and the scatter. The gather
and the build are split (index_metadata_rosters + build_plan_from_rosters) so the
build can also run off rosters assembled another way, and the schedule iterates
sorted roster ranks so a growing rank set rebuilds without renumbering existing
task_ids. CPU-only unit tests in test_planner.py cover the cross-rank task_id
determinism and the node-add stability.

Include world_size in the reshard plan cache key so growing the destination pool
(adding inference nodes) rebuilds the plan instead of reusing a stale one.

Add NixlCopyService: a cross-cluster, non-collocated backend over NIXL (UCX/RDMA)
that keeps refit off the torch process group. Agent metadata, registrations, and
a {task_id: (addr,len,dev)} map of the recv buffers are exchanged once in a cached
handshake; every refit after that is pure NIXL and sender-driven. A receiver
signals each source that its buffers are free, the source waits for that, syncs
its weights, and issues one WRITE per receiver; the WRITE carries a notification
so the receiver knows its weights landed. These ready/data notifications order
producer and consumer per refit with no barrier and no per-refit collective. The
service also augments a host-only UCX_TLS (e.g. tcp) with cuda_copy,cuda_ipc so
GPU tensors transfer instead of segfaulting.

Membership can also grow without a collective: local_roster_entry registers a
node's recv buffers and returns its (agent metadata, recv descriptors) entry, and
update_membership installs an orchestrator-relayed roster on every rank, adding
new peers point-to-point. Notifications are tagged with an orchestrator-provided
membership epoch so a node that joins later stays in sync.

Signed-off-by: William Dykas <wdykas@nvidia.com>
@wdykas
wdykas force-pushed the wdykas/local-refit-plan-nixl branch from 14b97e0 to 6792b29 Compare July 21, 2026 14:06

# Name by group rank, which is unique across the (possibly cross-world)
# group. dist.get_rank() would collide: separate worlds each have a rank 0.
self.agent_name = agent_name or f"refit-nixl-rank-{self.rank}"

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.

this is specific to Nemo-rl which has two different worlds and the ranks can conflict

Comment thread megatron/core/resharding/copy_services/nixl_copy_service.py
descs = {task_id: self._mem_desc(t) for task_id, t in items}
return (self.agent_name, self.agent.get_agent_metadata(), descs)

def update_membership(self, roster: Dict[int, tuple], epoch: int) -> None:

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.

This is for node joining which can implemented later

@sidsingh-nvidia sidsingh-nvidia Jul 23, 2026

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.

Should we check-in this code for when we plan to implement elasticity, then? Feels odd to have untested code in main.

@nvcsathe

Copy link
Copy Markdown

LGTM

@wdykas
wdykas requested a review from sidsingh-nvidia July 22, 2026 15:29
Comment thread megatron/core/resharding/copy_services/nixl_copy_service.py Outdated
Comment thread megatron/core/resharding/copy_services/nixl_copy_service.py
@sidsingh-nvidia

Copy link
Copy Markdown
Contributor

/claude strict-review

@sidsingh-nvidia

sidsingh-nvidia commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

The point of these changes is that this would allow us to hopefully be able to handle arbitrary nodes coming in and out of the training and inference groups

Is there a way to test this? And would this elasticity currently be limited to DP in inference?

@wdykas

wdykas commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test d6fb491

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Jul 23, 2026
@wdykas
wdykas enabled auto-merge July 23, 2026 16:30
@wdykas
wdykas added this pull request to the merge queue Jul 23, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30034797201

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 23, 2026
@wdykas
wdykas added this pull request to the merge queue Jul 23, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30049102792

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30051556418

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30052010057

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30052267431

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30052578747

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30055536833

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 24, 2026
@wdykas
wdykas added this pull request to the merge queue Jul 24, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30116069434

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30117537409

Merged via the queue into NVIDIA:main with commit 6f0db6f Jul 24, 2026
90 of 93 checks passed
@wdykas
wdykas deleted the wdykas/local-refit-plan-nixl branch July 24, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants