Skip to content

Commit a7fd186

Browse files
committed
Symmetric fine-grained FSDP unshard for TEGroupedMLP via fine_grained_recurse_module_types
1 parent 0a84107 commit a7fd186

2 files changed

Lines changed: 30 additions & 36 deletions

File tree

‎megatron/core/distributed/fsdp/mcore_fsdp_adapter.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ class FullyShardedDataParallel(_BaseDataParallel):
9393
}
9494

9595
@staticmethod
96-
def _fine_grained_pre_backward_recurse_module_types(
96+
def _fine_grained_recurse_module_types(
9797
config: TransformerConfig,
9898
ddp_config: DistributedDataParallelConfig,
9999
) -> Tuple[Type[nn.Module], ...]:
100-
"""Module classes that need ``parameters(recurse=True)`` for overlap hooks."""
100+
"""Container module classes that need ``parameters(recurse=True)`` for fine-grained hooks."""
101101
if (
102102
config.overlap_moe_expert_parallel_comm
103103
and ddp_config.data_parallel_sharding_strategy == "optim_grads_params"
@@ -220,10 +220,8 @@ def __init__(
220220
config.overlap_moe_expert_parallel_comm
221221
and ddp_config.data_parallel_sharding_strategy == "optim_grads_params"
222222
),
223-
fine_grained_pre_backward_recurse_module_types=(
224-
self._fine_grained_pre_backward_recurse_module_types(
225-
config, ddp_config
226-
)
223+
fine_grained_recurse_module_types=self._fine_grained_recurse_module_types(
224+
config, ddp_config
227225
),
228226
),
229227
)

‎megatron/core/distributed/fsdp/src/megatron_fsdp/megatron_fsdp.py‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ class MegatronFSDP(torch.nn.Module):
179179
which can improve performance when using MXFP8 parameters with activation recomputation.
180180
enable_fine_grained_param_gather_backward_hook (bool): Register pre-backward unshard hooks
181181
on each submodule (used by 1F1B EP overlap and similar schedules).
182-
fine_grained_pre_backward_recurse_module_types (Optional[Tuple[Type[nn.Module], ...]]):
183-
Module classes for which fine-grained pre-backward unshard uses
182+
fine_grained_recurse_module_types (Optional[Tuple[Type[nn.Module], ...]]):
183+
Module classes for which fine-grained pre-forward / pre-backward unshard uses
184184
``parameters(recurse=True)`` (container modules whose sharded weights live on
185185
children). Checked with :func:`isinstance`. Defaults to empty (none).
186186
report_nan_in_param_grad (bool): Whether to enable precise NaN-checking for parameter wgrad.
@@ -223,9 +223,7 @@ def __init__(
223223
disable_symmetric_registration: bool = False,
224224
enable_fine_grained_param_gather_hook: bool = False,
225225
enable_fine_grained_param_gather_backward_hook: bool = False,
226-
fine_grained_pre_backward_recurse_module_types: Optional[
227-
Tuple[Type[nn.Module], ...]
228-
] = None,
226+
fine_grained_recurse_module_types: Optional[Tuple[Type[nn.Module], ...]] = None,
229227
report_nan_in_param_grad: bool = False,
230228
):
231229
super().__init__()
@@ -281,10 +279,8 @@ def __init__(
281279
self.enable_fine_grained_param_gather_backward_hook = (
282280
enable_fine_grained_param_gather_backward_hook
283281
)
284-
recurse_types = fine_grained_pre_backward_recurse_module_types or ()
285-
self.fine_grained_pre_backward_recurse_module_types: Tuple[
286-
Type[nn.Module], ...
287-
] = recurse_types
282+
recurse_types = fine_grained_recurse_module_types or ()
283+
self.fine_grained_recurse_module_types: Tuple[Type[nn.Module], ...] = recurse_types
288284
self.report_nan_in_param_grad = report_nan_in_param_grad
289285

290286
# FSDPDistributedIndex stores the process groups and meshes used by Megatron-FSDP.
@@ -566,6 +562,25 @@ def _register_fsdp_hooks(self, root_module):
566562
"""
567563
fsdp_unit_modules = self.fsdp_unit_modules
568564

565+
def _param_list_for_submodule_unshard(
566+
module: nn.Module, *, for_backward: bool
567+
) -> List[nn.Parameter]:
568+
"""Build the parameter list for fine-grained or FSDP-unit unshard hooks."""
569+
if isinstance(module, tuple(fsdp_unit_modules)):
570+
return list(module.parameters())
571+
fine_grained_enabled = (
572+
self.enable_fine_grained_param_gather_backward_hook
573+
if for_backward
574+
else self.enable_fine_grained_param_gather_hook
575+
)
576+
if (
577+
fine_grained_enabled
578+
and self.fine_grained_recurse_module_types
579+
and isinstance(module, self.fine_grained_recurse_module_types)
580+
):
581+
return list(module.parameters(recurse=True))
582+
return list(module.parameters(recurse=False))
583+
569584
def release_module_parameters(module, bwd, lazy=False, *unused):
570585
"""
571586
Release the parameters of a given module after completing the forward
@@ -760,16 +775,7 @@ def _pre_forward_param_unshard(module: nn.Module, *unused):
760775
else:
761776
module._training_state = TrainingState.FORWARD
762777

763-
if isinstance(module, tuple(fsdp_unit_modules)):
764-
param_list = list(module.parameters())
765-
else:
766-
# All-gather the shallow parameters in every forward pass for modules
767-
# that are not FSDP units. Do not recurse unless absolutely necessary,
768-
# to allocate as little memory as possible for this forward pass.
769-
param_list = list(module.parameters(recurse=False))
770-
771-
if self.enable_fine_grained_param_gather_hook:
772-
param_list = list(module.parameters(recurse=False))
778+
param_list = _param_list_for_submodule_unshard(module, for_backward=False)
773779

774780
# All-gather the parameters before the forward pass.
775781
self.all_gather_and_wait_parameters_ready(
@@ -885,17 +891,7 @@ def _pre_backward_param_unshard(module: nn.Module, *unused):
885891
for sub_module in module.modules():
886892
sub_module._training_state = TrainingState.PRE_BACKWARD
887893

888-
if isinstance(module, tuple(fsdp_unit_modules)):
889-
param_list = list(module.parameters())
890-
elif (
891-
self.enable_fine_grained_param_gather_backward_hook
892-
and self.fine_grained_pre_backward_recurse_module_types
893-
and isinstance(module, self.fine_grained_pre_backward_recurse_module_types)
894-
):
895-
# Container modules (e.g. TEGroupedMLP): sharded weights are on children.
896-
param_list = list(module.parameters(recurse=True))
897-
else:
898-
param_list = list(module.parameters(recurse=False))
894+
param_list = _param_list_for_submodule_unshard(module, for_backward=True)
899895

900896
# All-gather / unshard the module parameters before the backward pass.
901897
self.all_gather_and_wait_parameters_ready(

0 commit comments

Comments
 (0)