-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add fully_shard_optimizer for mixed-precision FSDP #5411
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 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5b8177f
Implement experimental fully_shard_optimizer hook adapter
wujingyue a74ec3f
Refine experimental fully_shard_optimizer adapter
wujingyue f1a5934
Refine experimental FSDP optimizer behavior tests
wujingyue 3dce5f4
Document fully_shard_optimizer adapter alternatives
wujingyue a211993
Fix fully_shard_optimizer lint formatting
wujingyue 98615f2
Document optimizer precision alternatives
wujingyue e283ece
Move optimizer failure test
wujingyue 8fc3947
Rename Adam optimizer failure test
wujingyue 239fdde
Add FusedAdam MFSDP optimizer test
wujingyue 71a0866
Test MFSDP optimizer grad dtype mismatch
wujingyue dfbce46
Remove custom MFSDP optimizer test
wujingyue 542c84a
Relax MFSDP optimizer dtype assertion
wujingyue 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
108 changes: 108 additions & 0 deletions
108
megatron/core/distributed/fsdp/src/megatron_fsdp/experimental/optimizer.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,108 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Optimizer adapter for the minimal Megatron-FSDP path.""" | ||
|
|
||
| from typing import Any, NamedTuple | ||
|
|
||
| import torch | ||
| from torch import nn | ||
|
|
||
| from .parameter_group import contained_in_parameter_group | ||
|
|
||
|
|
||
| def fully_shard_optimizer(optimizer: torch.optim.Optimizer) -> None: | ||
| """Attach FSDP-aware step hooks to an optimizer instance. | ||
|
|
||
| The adapted optimizer preserves its existing parameter groups and only adds | ||
| temporary gradient casting around optimizer steps for FSDP sharded | ||
| parameters whose data dtype differs from their grad dtype. | ||
|
|
||
| Alternatives considered: | ||
| - Monkey-patching optimizer methods directly on the instance. This is | ||
| more invasive and harder to compose than hooks. | ||
| - Generating an FSDP-specific subclass per ``torch.optim.Optimizer``. | ||
| This adds extra class-generation machinery, but would let us | ||
| instrument ``zero_grad`` and ``__init__`` as well as ``step`` if needed. | ||
| - Casting from ``main_grad.dtype`` to ``main_weight.dtype`` after the | ||
| last microbatch and casting back before the first microbatch. This | ||
| should be done from a root post-backward callback if needed later, so | ||
| users do not need to call ``fully_shard_optimizer`` on an existing | ||
| ``torch.optim.Optimizer``. | ||
| - Letting the user set ``main_weight`` and ``main_grad`` to the same | ||
| dtype. This is enough for an FSDP2 drop-in replacement path and lets | ||
| optimizers stay unaware of FSDP precision handling. | ||
|
|
||
| Args: | ||
| optimizer: Optimizer instance to adapt in place. | ||
| """ | ||
|
|
||
| class CastedGrad(NamedTuple): | ||
| """Original grad tensor temporarily replaced during an optimizer step.""" | ||
|
|
||
| parameter: nn.Parameter | ||
| original_grad: torch.Tensor | ||
|
|
||
| def set_grad(parameter: nn.Parameter, grad: torch.Tensor) -> None: | ||
| """Install a grad with matching grad_dtype on a sharded parameter.""" | ||
| # Clear the existing grad before switching grad_dtype; the sharded | ||
| # parameter cannot advertise a new grad dtype while the old grad | ||
| # object with the previous dtype is still attached. | ||
| parameter.grad = None | ||
| parameter.grad_dtype = grad.dtype | ||
| parameter.grad = grad | ||
|
|
||
| casted_grads: list[CastedGrad] = [] | ||
|
|
||
| def step_pre_hook( | ||
| hooked_optimizer: torch.optim.Optimizer, args: tuple[Any, ...], kwargs: dict[str, Any] | ||
| ) -> None: | ||
| closure = kwargs.get("closure") | ||
| if closure is None and len(args) > 1: | ||
| closure = args[1] | ||
| if closure is not None: | ||
| # Step hooks run outside the base optimizer step, but closures run inside it. | ||
| # We need to cast grads after the closure materializes them and before the | ||
| # optimizer consumes them, which this hook-only adapter cannot intercept. | ||
| raise NotImplementedError( | ||
| "fully_shard_optimizer does not support optimizer.step closures." | ||
| ) | ||
| assert not casted_grads | ||
| for group in hooked_optimizer.param_groups: | ||
| for parameter in group["params"]: | ||
| if not isinstance(parameter, nn.Parameter): | ||
| raise TypeError( | ||
| "fully_shard_optimizer expected optimizer param groups to contain " | ||
| f"nn.Parameter values, got {type(parameter)!r}." | ||
| ) | ||
| if not contained_in_parameter_group(parameter): | ||
| continue | ||
| if parameter.grad is None: | ||
| continue | ||
| if parameter.grad.dtype == parameter.dtype: | ||
| continue | ||
|
|
||
| casted_grads.append(CastedGrad(parameter, parameter.grad)) | ||
| set_grad(parameter, parameter.grad.to(dtype=parameter.dtype)) | ||
|
|
||
| def step_post_hook( | ||
| hooked_optimizer: torch.optim.Optimizer, args: tuple[Any, ...], kwargs: dict[str, Any] | ||
| ) -> None: | ||
| del hooked_optimizer, args, kwargs | ||
| for parameter, original_grad in casted_grads: | ||
| set_grad(parameter, original_grad) | ||
| casted_grads.clear() | ||
|
|
||
| optimizer.register_step_pre_hook(step_pre_hook) | ||
| optimizer.register_step_post_hook(step_post_hook) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
|
|
||
| """Unit tests for Megatron-FSDP optimizer behavior.""" | ||
|
|
||
| import pytest | ||
| import torch | ||
| from torch import nn | ||
| from torch.distributed.device_mesh import init_device_mesh | ||
| from transformer_engine.pytorch.optimizers import FusedAdam | ||
|
|
||
| from megatron.core.distributed.fsdp.src.megatron_fsdp.experimental import ( | ||
| Flat, | ||
| Placements, | ||
| fully_shard, | ||
| ) | ||
| from megatron.core.distributed.fsdp.src.megatron_fsdp.mixed_precision import MixedPrecisionPolicy | ||
|
|
||
|
|
||
| class TinyModel(nn.Module): | ||
| """Small model with two separately shardable units.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.fc1 = nn.Linear(8, 16) | ||
| self.relu = nn.ReLU() | ||
| self.fc2 = nn.Linear(16, 4) | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| """Run the tiny model.""" | ||
| return self.fc2(self.relu(self.fc1(x))) | ||
|
|
||
|
|
||
| def _flat_placements() -> Placements: | ||
| return Placements(dp_axes=[0], parameter=[Flat()], gradient=[Flat()], optimizer=[Flat()]) | ||
|
|
||
|
|
||
| def test_adam_without_adapter_raises_precision_error(distributed_setup): | ||
| """Raw Adam should fail on mixed-precision FSDP parameters without the adapter.""" | ||
| world_size = distributed_setup.world_size | ||
| device = distributed_setup.device | ||
| mesh = init_device_mesh(device.type, (world_size,)) | ||
| torch.manual_seed(2026) | ||
| model = TinyModel().to(device=device, dtype=torch.bfloat16) | ||
| fully_shard(model.fc1, mesh=mesh, placements=_flat_placements()) | ||
| fully_shard(model.fc2, mesh=mesh, placements=_flat_placements()) | ||
| optimizer = torch.optim.Adam(model.parameters(), lr=0.01) | ||
|
|
||
| x = torch.randn(6, 8, device=device, dtype=torch.bfloat16) | ||
| optimizer.zero_grad(set_to_none=True) | ||
| loss = model(x).sum() | ||
| loss.backward() | ||
|
|
||
| with pytest.raises(RuntimeError, match="same device and the same dtype"): | ||
| optimizer.step() | ||
|
|
||
|
|
||
| def test_fused_adam_without_adapter_accepts_mismatched_grads(distributed_setup): | ||
| """TE FusedAdam should handle mixed-precision FSDP grads without the adapter.""" | ||
| world_size = distributed_setup.world_size | ||
| device = distributed_setup.device | ||
|
|
||
| mesh = init_device_mesh(device.type, (world_size,)) | ||
| torch.manual_seed(2026) | ||
| model = TinyModel().to(device=device, dtype=torch.bfloat16) | ||
| # These are the defaults, but spell them out so the test clearly exercises | ||
| # mismatched parameter and gradient precision. | ||
| mixed_precision_policy = MixedPrecisionPolicy( | ||
| main_params_dtype=torch.float32, main_grads_dtype=torch.bfloat16 | ||
| ) | ||
| fully_shard( | ||
| model.fc1, | ||
| mesh=mesh, | ||
| placements=_flat_placements(), | ||
| mixed_precision_policy=mixed_precision_policy, | ||
| ) | ||
| fully_shard( | ||
| model.fc2, | ||
| mesh=mesh, | ||
| placements=_flat_placements(), | ||
| mixed_precision_policy=mixed_precision_policy, | ||
| ) | ||
| optimizer = FusedAdam(model.parameters(), lr=0.01) | ||
|
|
||
| x = torch.randn(6, 8, device=device, dtype=torch.bfloat16) | ||
| optimizer.zero_grad(set_to_none=True) | ||
| loss = model(x).sum() | ||
| loss.backward() | ||
|
|
||
| for parameter in model.parameters(): | ||
| assert parameter.grad is not None | ||
| assert parameter.dtype != parameter.grad.dtype | ||
|
|
||
| params_before_step = [parameter.detach().clone() for parameter in model.parameters()] | ||
| optimizer.step() | ||
|
|
||
| assert any( | ||
| not torch.equal(parameter_before, parameter.detach()) | ||
| for parameter_before, parameter in zip(params_before_step, model.parameters()) | ||
| ) |
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.