Skip to content

Commit 97ff650

Browse files
committed
Update on "[FSDP][optim_state_dict][2/N] Add _get_fqn_to_fsdp_param_info to map from original FQN to flat_param"
**Motivation:** Add a helper to map from the FQN to the corresponding flat_param. The helper will directly get flat_param from fsdp_state and flat_handler as flat_param is not registered to the module if `use_orig_params` is True. [ghstack-poisoned]
2 parents ec6526f + 73b2d96 commit 97ff650

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

torch/distributed/fsdp/_optim_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,11 +1293,12 @@ def _optim_state_dict(
12931293

12941294
def _get_fqn_to_fsdp_param_info(model: nn.Module) -> Dict[str, FSDPParamInfo]:
12951295
"""
1296-
Construct the maaping from a param's fqn to its corresponding FSDPParamInfo
1297-
if the param is managed by FSDP. FlatParam only stores the first FQN of a
1298-
shared parameter. So the keys in the mapping are guranteed to map to unique
1299-
parameters.
1296+
Construct the mapping from a param's fqn to its corresponding ``FSDPParamInfo``
1297+
if the param is managed by FSDP. ``FlatParameter._fqns`` only stores the first
1298+
FQN of a shared parameter. So the keys in the mapping are guaranteed to map
1299+
to unique parameters.
13001300
"""
1301+
13011302
def module_fn(module, prefix, fqn_to_param_info):
13021303
# TODO: make it work with composable API.
13031304
if not isinstance(module, fsdp_file.FullyShardedDataParallel):
@@ -1319,6 +1320,9 @@ def return_fn(fqn_to_param_info):
13191320
return fqn_to_param_info
13201321

13211322
fqn_to_param_info: Dict[str, FSDPParamInfo] = {}
1323+
# FlatParameter._fqns stores the local fqn, starting from the root of the
1324+
# FSDP. Using _apply_to_modules() with model (may not be the FSDP root
1325+
# module) allows us to construct the global fqn.
13221326
return _apply_to_modules(
13231327
model,
13241328
module_fn,

0 commit comments

Comments
 (0)