-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[DEV] fix(megatron-fsdp): reduce padding for grouped expert weights #5013
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6e7030
avoid oversized CSF for grouped expert weights
xuwchen ded15ce
split MFSDP grouped expert buckets to reduce padding
xuwchen 3f5ccca
add unit tests for MFSDP grouped expert bucket split
xuwchen b3afa96
Merge branch 'dev' into mfsdp_grouped_expert_padding_dev
yaox12 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
86 changes: 86 additions & 0 deletions
86
tests/unit_tests/distributed/megatron_fsdp/test_mfsdp_param_and_grad_buffer.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,86 @@ | ||
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
|
||
| import math | ||
|
|
||
| import torch | ||
|
|
||
| from megatron.core.distributed.fsdp.src.megatron_fsdp.param_and_grad_buffer import ( | ||
| BucketingPolicy, | ||
| _get_parameter_groups, | ||
| ) | ||
|
|
||
|
|
||
| class _ExpertTestModule(torch.nn.Module): | ||
| """ | ||
| Mock module whose params are routed under `.experts.` to trigger | ||
| is_expert_param=True. The outer `layer` attribute puts a dot before | ||
| `experts` in the parameter path (e.g. `layer.experts.linear_fc1`). | ||
| """ | ||
|
|
||
| def __init__(self, shapes): | ||
| super().__init__() | ||
| self.layer = torch.nn.Module() | ||
| self.layer.experts = torch.nn.ParameterDict( | ||
| {name: torch.nn.Parameter(torch.empty(shape)) for name, shape in shapes.items()} | ||
| ) | ||
|
|
||
|
|
||
| def _get_bucket_signatures(module): | ||
| bucket_groups, _, _ = _get_parameter_groups( | ||
| module, BucketingPolicy(suggested_bucket_size=None), meta_device_init_fp8_params={} | ||
| ) | ||
| param_to_name = {param: name for name, param in module.named_parameters()} | ||
| return [ | ||
| { | ||
| "chunk_size_factor": group.chunk_size_factor, | ||
| "params": [(param_to_name[param], tuple(param.shape)) for param in group.params], | ||
| } | ||
| for group in bucket_groups | ||
| ] | ||
|
|
||
|
|
||
| def test_grouped_expert_weights_split_when_chunk_size_factors_differ(): | ||
| """Grouped expert weights with mismatched chunk size factors get routed to separate buckets.""" | ||
| num_local_experts = 4 | ||
| hidden_size = 12 | ||
| moe_ffn_hidden_size = 8 | ||
| shapes = { | ||
| "linear_fc1": (num_local_experts, 2 * moe_ffn_hidden_size, hidden_size), | ||
| "linear_fc2": (num_local_experts, hidden_size, moe_ffn_hidden_size), | ||
| } | ||
| module = _ExpertTestModule(shapes) | ||
|
|
||
| assert _get_bucket_signatures(module) == [ | ||
| { | ||
| "chunk_size_factor": torch.Size(shapes["linear_fc1"])[1:].numel(), | ||
| "params": [("layer.experts.linear_fc1", shapes["linear_fc1"])], | ||
| }, | ||
| { | ||
| "chunk_size_factor": torch.Size(shapes["linear_fc2"])[1:].numel(), | ||
| "params": [("layer.experts.linear_fc2", shapes["linear_fc2"])], | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def test_per_expert_2d_weights_merge_via_lcm(): | ||
| """Per-expert 2D weights merge into a single bucket via LCM chunk size factor.""" | ||
| hidden_size = 12 | ||
| moe_ffn_hidden_size = 8 | ||
| shapes = { | ||
| "linear_fc1": (2 * moe_ffn_hidden_size, hidden_size), | ||
| "linear_fc2": (hidden_size, moe_ffn_hidden_size), | ||
| } | ||
| module = _ExpertTestModule(shapes) | ||
|
|
||
| assert _get_bucket_signatures(module) == [ | ||
| { | ||
| "chunk_size_factor": math.lcm( | ||
| torch.Size(shapes["linear_fc1"])[1:].numel(), | ||
| torch.Size(shapes["linear_fc2"])[1:].numel(), | ||
| ), | ||
| "params": [ | ||
| ("layer.experts.linear_fc1", shapes["linear_fc1"]), | ||
| ("layer.experts.linear_fc2", shapes["linear_fc2"]), | ||
| ], | ||
| } | ||
| ] |
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.
Add large expert param in "remaining params" so that it will have a standalone bucket.
Uh oh!
There was an error while loading. Please reload this page.
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.
Added a unit test in
tests/unit_tests/distributed/megatron_fsdp/test_mfsdp_param_and_grad_buffer.py.