Skip to content

Commit fa1e49f

Browse files
committed
Rename submodule unshard helper arg to pass_direction Literal[forward, backward].
1 parent a7fd186 commit fa1e49f

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class FullyShardedDataParallel(_BaseDataParallel):
9494

9595
@staticmethod
9696
def _fine_grained_recurse_module_types(
97-
config: TransformerConfig,
98-
ddp_config: DistributedDataParallelConfig,
97+
config: TransformerConfig, ddp_config: DistributedDataParallelConfig
9998
) -> Tuple[Type[nn.Module], ...]:
10099
"""Container module classes that need ``parameters(recurse=True)`` for fine-grained hooks."""
101100
if (

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from contextlib import contextmanager
1919
from enum import Enum, auto
2020
from functools import partial
21-
from typing import Any, Dict, List, Optional, Tuple, Type
21+
from typing import Any, Dict, List, Literal, Optional, Tuple, Type
2222

2323
import torch
2424
import torch.nn as nn
@@ -563,14 +563,14 @@ def _register_fsdp_hooks(self, root_module):
563563
fsdp_unit_modules = self.fsdp_unit_modules
564564

565565
def _param_list_for_submodule_unshard(
566-
module: nn.Module, *, for_backward: bool
566+
module: nn.Module, pass_direction: Literal["forward", "backward"]
567567
) -> List[nn.Parameter]:
568568
"""Build the parameter list for fine-grained or FSDP-unit unshard hooks."""
569569
if isinstance(module, tuple(fsdp_unit_modules)):
570570
return list(module.parameters())
571571
fine_grained_enabled = (
572572
self.enable_fine_grained_param_gather_backward_hook
573-
if for_backward
573+
if pass_direction == "backward"
574574
else self.enable_fine_grained_param_gather_hook
575575
)
576576
if (
@@ -775,7 +775,7 @@ def _pre_forward_param_unshard(module: nn.Module, *unused):
775775
else:
776776
module._training_state = TrainingState.FORWARD
777777

778-
param_list = _param_list_for_submodule_unshard(module, for_backward=False)
778+
param_list = _param_list_for_submodule_unshard(module, "forward")
779779

780780
# All-gather the parameters before the forward pass.
781781
self.all_gather_and_wait_parameters_ready(
@@ -891,7 +891,7 @@ def _pre_backward_param_unshard(module: nn.Module, *unused):
891891
for sub_module in module.modules():
892892
sub_module._training_state = TrainingState.PRE_BACKWARD
893893

894-
param_list = _param_list_for_submodule_unshard(module, for_backward=True)
894+
param_list = _param_list_for_submodule_unshard(module, "backward")
895895

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

0 commit comments

Comments
 (0)