Skip to content

Avoid extra MFSDP v2 model-weight sync memcpy - #5834

Merged
ericharper merged 1 commit into
NVIDIA:mainfrom
wujingyue:cast
Jul 17, 2026
Merged

Avoid extra MFSDP v2 model-weight sync memcpy#5834
ericharper merged 1 commit into
NVIDIA:mainfrom
wujingyue:cast

Conversation

@wujingyue

@wujingyue wujingyue commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Avoid an extra device-to-device memcpy when MFSDP v2 syncs optimizer weights into compute weights and the placements already match.
  • Cast main_weight directly into the persistent model_weight buffer for the same-placement path.
  • Keep cast-before-redistribute for different placements so cross-rank communication uses the lower-precision compute dtype instead of the higher-precision optimizer dtype.

Root cause

sync_model_weight_from_main_weight() always cast main_weight into a temporary compute-dtype DBuffer, then called redistribute(..., out=model_weight). When main_weight and model_weight already had the same placements, redistribute only copied the temporary buffer into model_weight, producing an avoidable D2D memcpy after the cast.

Implementation

This adds an out= path to DBuffer.cast and uses it from MFSDP v2 model-weight sync. The DBuffer helper change is an implementation detail for writing the cast result directly into existing storage.

Validation

  • UV_NO_SYNC=1 CUDA_VISIBLE_DEVICES=0,1 uv run python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/mfsdp_v2/test_dbuffer.py
    • 19 passed, 6 skipped
  • UV_NO_SYNC=1 CUDA_VISIBLE_DEVICES=0,1 uv run python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/mfsdp_v2/test_fully_shard.py
    • 14 passed
  • git diff --check
    • passed

@copy-pr-bot

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

@wujingyue
wujingyue force-pushed the cast branch 3 times, most recently from 1081a42 to 1c948b3 Compare July 16, 2026 01:25
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@wujingyue
wujingyue force-pushed the cast branch 2 times, most recently from 44885b9 to 85cbc0c Compare July 16, 2026 01:32
@wujingyue wujingyue changed the title Add DBuffer cast output support Avoid extra MFSDP v2 model-weight sync memcpy Jul 16, 2026
@wujingyue
wujingyue marked this pull request as ready for review July 16, 2026 01:33
@wujingyue
wujingyue requested review from a team as code owners July 16, 2026 01:33
@wujingyue

Copy link
Copy Markdown
Contributor Author

/ok to test 85cbc0c

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jul 17, 2026
@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 17, 2026
@ericharper
ericharper enabled auto-merge July 17, 2026 18:03
@ericharper
ericharper added this pull request to the merge queue Jul 17, 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/29602452616

Merged via the queue into NVIDIA:main with commit 4a1f743 Jul 17, 2026
189 of 191 checks passed
chochowski pushed a commit to chochowski/Megatron-LM that referenced this pull request Jul 20, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: mchochowski <mchochowski@nvidia.com>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>
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: low Run functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants