-
Notifications
You must be signed in to change notification settings - Fork 820
Support row-only MXFP8 distributed master-weight casts #3488
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
xiuhu17
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
xiuhu17:fix-mxfp8-rowwise-master-cast
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.
+225
−41
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,132 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # See LICENSE for license information. | ||
|
|
||
| """Run with pytest on one GPU or torchrun -m pytest on multiple GPUs.""" | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
| import torch | ||
| import transformer_engine.pytorch as te | ||
| import transformer_engine_torch as tex | ||
| from transformer_engine.pytorch.tensor.utils import ( | ||
| cast_master_weights_to_fp8, | ||
| quantize_master_weights, | ||
| ) | ||
|
|
||
| available, reason = te.is_mxfp8_available(return_reason=True) | ||
| pytestmark = pytest.mark.skipif(not available, reason=reason) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def group(): | ||
| owned = not torch.distributed.is_initialized() | ||
| if owned: | ||
| torch.cuda.set_device(int(os.getenv("LOCAL_RANK", "0"))) | ||
| if "RANK" in os.environ: | ||
| torch.distributed.init_process_group("nccl") | ||
| else: | ||
| torch.distributed.init_process_group( | ||
| "nccl", store=torch.distributed.HashStore(), rank=0, world_size=1 | ||
| ) | ||
| yield torch.distributed.group.WORLD | ||
| if owned: | ||
| torch.distributed.destroy_process_group() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mixed", [False, True]) | ||
| @pytest.mark.parametrize("fragments", [False, True]) | ||
| def test_rowwise_master_cast(group, monkeypatch, mixed, fragments): | ||
| rank = torch.distributed.get_rank(group) | ||
| world = torch.distributed.get_world_size(group) | ||
| shape = (160, 96) # Both scale padding and non-tile-aligned logical shapes. | ||
| full = torch.linspace(-4, 7, 160 * 96, device="cuda").reshape(shape) | ||
| quantizers = [te.MXFP8Quantizer(te.DType.kFloat8E4M3, rowwise=True, columnwise=False)] | ||
| if mixed: | ||
| quantizers.append(te.MXFP8Quantizer(te.DType.kFloat8E4M3, rowwise=True, columnwise=True)) | ||
| weights = [q(full.to(torch.bfloat16)) for q in quantizers] | ||
| pointers = [(w._rowwise_data.data_ptr(), w._rowwise_scale_inv.data_ptr()) for w in weights] | ||
| if world == 1: | ||
| lo, hi = 0, full.numel() | ||
| elif rank == 0: | ||
| lo, hi = 0, 17 # Split inside a 32-value block. | ||
| elif rank == 1: | ||
| lo, hi = 17, full.numel() | ||
| else: | ||
| lo, hi = 0, 0 # No master shard on tail ranks. | ||
| real_reduce = torch.distributed.all_reduce | ||
| reduced_sizes = [] | ||
|
|
||
| def record_reduce(tensor, *args, **kwargs): | ||
| reduced_sizes.append(tensor.numel()) | ||
| return real_reduce(tensor, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr(torch.distributed, "all_reduce", record_reduce) | ||
| for step in range(2): | ||
| master = full + step * 0.25 | ||
| shard = master.flatten()[lo:hi] if lo < hi else None | ||
| outputs = [ | ||
| ( | ||
| torch.empty(hi - lo, dtype=torch.uint8, device="cuda"), | ||
| torch.empty(hi - lo, dtype=torch.uint8, device="cuda") if i else None, | ||
| ) | ||
| for i in range(len(weights)) | ||
| ] | ||
| reduced_sizes.clear() | ||
| cast = quantize_master_weights if step == 0 else cast_master_weights_to_fp8 | ||
| cast( | ||
| weights, | ||
| [shard] * len(weights), | ||
| [lo if shard is not None else None] * len(weights), | ||
| group, | ||
| fsdp_shard_model_weights=outputs if fragments else None, | ||
| ) | ||
| expected_amax = sum(w._rowwise_scale_inv.numel() for w in weights) | ||
| if mixed: | ||
| expected_amax += weights[1]._columnwise_scale_inv.numel() | ||
| assert reduced_sizes == [expected_amax] | ||
| for idx, (weight, quantizer) in enumerate(zip(weights, quantizers)): | ||
| expected = quantizer(master.to(torch.bfloat16)) | ||
| for direction in ["rowwise", "columnwise"] if idx else ["rowwise"]: | ||
| data = getattr(weight, f"_{direction}_data") | ||
| actual = torch.zeros_like(data).flatten() | ||
| if lo < hi: | ||
| src = ( | ||
| outputs[idx][direction == "columnwise"] | ||
| if fragments | ||
| else data.flatten()[lo:hi] | ||
| ) | ||
| actual[lo:hi].copy_(src) | ||
| real_reduce(actual, op=torch.distributed.ReduceOp.MAX, group=group) | ||
| torch.testing.assert_close( | ||
| actual.view(shape), getattr(expected, f"_{direction}_data"), rtol=0, atol=0 | ||
| ) | ||
| scale = getattr(weight, f"_{direction}_scale_inv") | ||
| ref = getattr(expected, f"_{direction}_scale_inv") | ||
| rows, cols = (160, 3) if direction == "rowwise" else (5, 96) | ||
| torch.testing.assert_close(scale[:rows, :cols], ref[:rows, :cols], rtol=0, atol=0) | ||
| assert pointers[idx] == ( | ||
| weight._rowwise_data.data_ptr(), | ||
| weight._rowwise_scale_inv.data_ptr(), | ||
| ) | ||
| assert weights[0]._columnwise_data is None | ||
| assert weights[0]._columnwise_scale_inv is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("offset,length", [(0, 0), (31, 3), (1, 4094)]) | ||
| def test_rowwise_partial_kernels(offset, length): | ||
| inp = torch.randn(length, device="cuda", dtype=torch.bfloat16) | ||
| row = torch.zeros((128, 4), device="cuda", dtype=inp.dtype) | ||
| ref = torch.zeros_like(row) | ||
| col = torch.zeros((4, 128), device="cuda", dtype=inp.dtype) | ||
| # Empty view may still have backing storage. Omission is shape-based. | ||
| omitted = col.flatten()[:0].view(0, 0) | ||
| tex.mxfp8_scaling_compute_partial_amax(inp, row, omitted, 64, 64, offset) | ||
| tex.mxfp8_scaling_compute_partial_amax(inp, ref, col, 64, 64, offset) | ||
| torch.testing.assert_close(row, ref, rtol=0, atol=0) | ||
| scales = torch.full((128, 4), 127, dtype=torch.uint8, device="cuda") | ||
| out = torch.empty(length, dtype=torch.uint8, device="cuda") | ||
| tex.mxfp8_scaling_partial_cast( | ||
| inp, out, out[:0], scales, scales.flatten()[:0].view(0, 0), 64, 64, offset | ||
| ) | ||
| torch.testing.assert_close(out, inp.to(torch.float8_e4m3fn).view(torch.uint8), rtol=0, atol=0) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The L0 MXFP8 suite collects this test using single-process pytest, while the L1 distributed suite does not select or launch it. As a result, the rank-partitioned branches—including the unaligned shard boundary, empty tail ranks, and mixed row-only/bidirectional layout—are not exercised in repository CI. This is non-blocking, but future regressions in the distributed behavior changed here could go undetected. Please register this test with the distributed suite or add equivalent self-launched multi-rank coverage.
Knowledge Base Used: Verification and CI matrix
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!