core.distributed.fsdp.src.megatron_fsdp.experimental.optimizer#
Optimizer adapter for the minimal Megatron-FSDP path.
Module Contents#
Functions#
Attach FSDP-aware step hooks to an optimizer instance. |
API#
- core.distributed.fsdp.src.megatron_fsdp.experimental.optimizer.fully_shard_optimizer(
- optimizer: torch.optim.Optimizer,
- *,
- precision_aware: bool = False,
Attach FSDP-aware step hooks to an optimizer instance.
The adapted optimizer preserves its existing parameter groups, temporarily casts gradients around optimizer steps for FSDP sharded parameters whose data dtype differs from their grad dtype unless the optimizer is precision aware, and refreshes compute weights after each optimizer step.
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 instrumentzero_gradand__init__as well asstepif needed. - Casting frommain_grad.dtypetomain_weight.dtypeafter 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 callfully_shard_optimizeron an existingtorch.optim.Optimizer. - Letting the user setmain_weightandmain_gradto the same dtype. This is enough for an FSDP2 drop-in replacement path and lets optimizers stay unaware of FSDP precision handling.- Parameters:
optimizer – Optimizer instance to adapt in place.
precision_aware – Whether the optimizer accepts FSDP’s mixed-precision gradients without temporary casting.