-
Notifications
You must be signed in to change notification settings - Fork 803
[PyTorch] Fix MXFP8 master weight cast when a rank owns no shard. #3348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rapatel
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
rapatel:ripatel/handle_empty_shards
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
tests/pytorch/mxfp8/test_mxfp8_master_weight_empty_shard.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| import transformer_engine.pytorch as te | ||
| from transformer_engine.pytorch import MXFP8Quantizer | ||
| from transformer_engine.pytorch.tensor.utils import quantize_master_weights | ||
|
|
||
| recipe_available, reason_for_no_recipe = te.is_mxfp8_available(return_reason=True) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def single_rank_group(): | ||
| # Only tear down a group this fixture owns; another test may have set one up. | ||
| created = not torch.distributed.is_initialized() | ||
| if created: | ||
| torch.cuda.set_device(0) | ||
| torch.distributed.init_process_group( | ||
| backend="nccl", store=torch.distributed.HashStore(), rank=0, world_size=1 | ||
| ) | ||
| try: | ||
| yield torch.distributed.GroupMember.WORLD | ||
| finally: | ||
| if created: | ||
| torch.distributed.destroy_process_group() | ||
|
|
||
|
|
||
| def _make_weight(dtype): | ||
| quantizer = MXFP8Quantizer(fp8_dtype=te.DType.kFloat8E4M3, rowwise=True, columnwise=True) | ||
| weight = quantizer.make_empty((128, 128), dtype=dtype, device="cuda") | ||
| quantizer.update_quantized(torch.randn(128, 128, dtype=dtype, device="cuda"), weight) | ||
| return weight | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not recipe_available, reason=reason_for_no_recipe) | ||
| @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float32]) | ||
| def test_empty_master_shard_agrees_with_populated_rank(monkeypatch, single_rank_group, dtype): | ||
| """A rank owning no shard must reduce the same amax dtype as one that owns data. | ||
|
|
||
| Wide FSDP sharding pads the parameter bucket, so the tail ranks can end up with an | ||
| empty shard of every weight. Those ranks still join the amax all-reduce. | ||
| """ | ||
| amax_dtypes = [] | ||
| real_all_reduce = torch.distributed.all_reduce | ||
|
|
||
| def spy(tensor, *args, **kwargs): | ||
| amax_dtypes.append(tensor.dtype) | ||
| return real_all_reduce(tensor, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr(torch.distributed, "all_reduce", spy) | ||
|
|
||
| populated = _make_weight(dtype) | ||
| master = torch.randn(populated.numel(), dtype=torch.float32, device="cuda") | ||
| quantize_master_weights([populated], [master], [0], group=single_rank_group) | ||
|
|
||
| # Used to raise UnboundLocalError instead of reaching the all-reduce. | ||
| quantize_master_weights([_make_weight(dtype)], [None], [None], group=single_rank_group) | ||
|
|
||
| assert len(amax_dtypes) == 2 | ||
| assert amax_dtypes[0] == amax_dtypes[1] | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.