|
18 | 18 | from contextlib import contextmanager |
19 | 19 | from enum import Enum, auto |
20 | 20 | 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 |
22 | 22 |
|
23 | 23 | import torch |
24 | 24 | import torch.nn as nn |
@@ -563,14 +563,14 @@ def _register_fsdp_hooks(self, root_module): |
563 | 563 | fsdp_unit_modules = self.fsdp_unit_modules |
564 | 564 |
|
565 | 565 | def _param_list_for_submodule_unshard( |
566 | | - module: nn.Module, *, for_backward: bool |
| 566 | + module: nn.Module, pass_direction: Literal["forward", "backward"] |
567 | 567 | ) -> List[nn.Parameter]: |
568 | 568 | """Build the parameter list for fine-grained or FSDP-unit unshard hooks.""" |
569 | 569 | if isinstance(module, tuple(fsdp_unit_modules)): |
570 | 570 | return list(module.parameters()) |
571 | 571 | fine_grained_enabled = ( |
572 | 572 | self.enable_fine_grained_param_gather_backward_hook |
573 | | - if for_backward |
| 573 | + if pass_direction == "backward" |
574 | 574 | else self.enable_fine_grained_param_gather_hook |
575 | 575 | ) |
576 | 576 | if ( |
@@ -775,7 +775,7 @@ def _pre_forward_param_unshard(module: nn.Module, *unused): |
775 | 775 | else: |
776 | 776 | module._training_state = TrainingState.FORWARD |
777 | 777 |
|
778 | | - param_list = _param_list_for_submodule_unshard(module, for_backward=False) |
| 778 | + param_list = _param_list_for_submodule_unshard(module, "forward") |
779 | 779 |
|
780 | 780 | # All-gather the parameters before the forward pass. |
781 | 781 | self.all_gather_and_wait_parameters_ready( |
@@ -891,7 +891,7 @@ def _pre_backward_param_unshard(module: nn.Module, *unused): |
891 | 891 | for sub_module in module.modules(): |
892 | 892 | sub_module._training_state = TrainingState.PRE_BACKWARD |
893 | 893 |
|
894 | | - param_list = _param_list_for_submodule_unshard(module, for_backward=True) |
| 894 | + param_list = _param_list_for_submodule_unshard(module, "backward") |
895 | 895 |
|
896 | 896 | # All-gather / unshard the module parameters before the backward pass. |
897 | 897 | self.all_gather_and_wait_parameters_ready( |
|
0 commit comments