Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 7f17bbf

Browse files
Anthony Chenfacebook-github-bot
authored andcommitted
expose use_orig_params to d2go config
Summary: Pull Request resolved: #582 Expose use_orig_params for FSDP constructor to d2go config. Read more about it in the docstring of torch.distributed.fsdp.fully_sharded_data_parallel. use_orig_params=False (default) uses FlatParameters to store flattened parameters, which saves memory by avoiding fragmentation. However, use_orig_params=True is essential for models that are partly frozen. This is because FlatParameters can only accept uniform requries_grad across the whole model Reviewed By: wat3rBro Differential Revision: D46917757 fbshipit-source-id: 12ebe83e6de456e37d89eaf8b257f23925a6786d
1 parent 60b6995 commit 7f17bbf

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

d2go/trainer/fsdp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def add_fsdp_configs(_C: CN):
3737
# Configs for fully sharded data parallel (fsdp)
3838
# Check out https://pytorch.org/docs/stable/fsdp.html
3939
# and docstring of torch.distributed.fsdp.fully_sharded_data_parallel
40-
# See docstring of CpuOffload and BackwardPrefetch in torch.distributed.fsdp.fully_sharded_data_parallel
4140
_C.FSDP.CPU_OFFLOAD = False
4241
_C.FSDP.BACKWARD_PREFETCH = True
42+
_C.FSDP.USE_ORIG_PARAMS = False
4343
# Find autowrap policy at D2GO_WRAP_POLICY_REGISTRY, or use '' to disable autowrap
4444
_C.FSDP.AUTO_WRAP_POLICY = "never_wrap_policy"
4545
_C.FSDP.AUTO_WRAP_MIN_PARAMS = int(1e4)
@@ -176,6 +176,7 @@ def build_fsdp(
176176
state_dict_rank0_only: bool = True,
177177
ignored_modules: Optional[nn.Module] = None,
178178
forward_prefetch: bool = False,
179+
use_orig_params: bool = False,
179180
device_id: Optional[int] = None,
180181
):
181182
if sharding_algorithm == ShardingAlgorithm.SHARD_GRAD_OP:
@@ -227,6 +228,7 @@ def build_fsdp(
227228
"backward_prefetch": backward_prefetch,
228229
"ignored_modules": ignored_modules,
229230
"forward_prefetch": forward_prefetch,
231+
"use_orig_params": use_orig_params,
230232
"device_id": torch.cuda.current_device() if not device_id else device_id,
231233
}
232234
# default to using use_local_state_dict if state_dict_type is None
@@ -304,6 +306,7 @@ def apply(self, model: nn.Module) -> FSDPWrapper:
304306
state_dict_rank0_only=self.cfg.FSDP.STATE_DICT_RANK0_ONLY,
305307
ignored_modules=ignored_modules,
306308
forward_prefetch=forward_prefetch,
309+
use_orig_params=self.cfg.FSDP.USE_ORIG_PARAMS,
307310
device_id=torch.cuda.current_device(),
308311
)
309312
return wrapped_model

0 commit comments

Comments
 (0)