Skip to content

Consolidate the naming of named_parameter and state_dict for CheckpointWrapper #80089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/distributed/fsdp/test_checkpoint_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ def check_fn(l):
self.assertTrue(param.requires_grad)
self.assertFalse(param.grad is None)

def test_fqn(self):
lin = nn.Linear(10, 10, bias=False)
lin = checkpoint_wrapper(lin)
state_dict = lin.state_dict()
for fqn, _ in lin.named_parameters():
self.assertTrue(fqn in state_dict, msg=f"{fqn} not in state_dict.")

if __name__ == "__main__":
run_tests()
14 changes: 13 additions & 1 deletion torch/distributed/algorithms/_checkpoint/checkpoint_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torch.distributed.utils import _replace_by_prefix
from torch.distributed.fsdp.wrap import _recursive_wrap, lambda_auto_wrap_policy
import torch.nn as nn
from typing import Dict, Any
from typing import Any, Dict, Iterator, Tuple
from functools import partial

_CHECKPOINT_PREFIX = "_checkpoint_wrapped_module"
Expand Down Expand Up @@ -61,6 +61,18 @@ def forward(self, *args, **kwargs):
**kwargs,
)

def named_parameters(
self,
*args,
**kwargs,
) -> Iterator[Tuple[str, torch.nn.Parameter]]:
"""
Overrides :meth:`named_parameters()` to intercept parameter names and
remove all occurrences of _CHECKPOINT_PREFIX.
"""
for param_name, param in super().named_parameters(*args, **kwargs):
yield param_name.replace(f"{_CHECKPOINT_PREFIX}.", ""), param

@staticmethod
def _post_state_dict_hook(
module: nn.Module,
Expand Down