|
19 | 19 | DefaultSavePlanner,
|
20 | 20 | )
|
21 | 21 | from torch.distributed.checkpoint.planner import LoadPlanner, SavePlanner
|
| 22 | +from torch.distributed.checkpoint.state_dict import ( |
| 23 | + get_model_state_dict, |
| 24 | + set_model_state_dict, |
| 25 | +) |
22 | 26 | from torch.distributed.checkpoint.storage import StorageReader, StorageWriter
|
23 |
| - |
| 27 | +from torch.nn.parallel import DistributedDataParallel |
24 | 28 | from torchtnt.framework.callbacks._checkpoint_utils import (
|
25 | 29 | _prepare_app_state_for_checkpoint,
|
26 | 30 | _prepare_app_state_for_restore,
|
|
41 | 45 | from torchtnt.utils.checkpoint import BestCheckpointConfig, CheckpointPath
|
42 | 46 | from torchtnt.utils.optimizer import init_optim_state
|
43 | 47 | from torchtnt.utils.rank_zero_log import rank_zero_info, rank_zero_warn
|
| 48 | + |
44 | 49 | from torchtnt.utils.stateful import MultiStateful, Stateful
|
45 | 50 |
|
46 | 51 |
|
|
63 | 68 | )
|
64 | 69 |
|
65 | 70 |
|
| 71 | +class DSDModelWrapper(Stateful): |
| 72 | + """This wrapper converts state dicts to Distributed State Dicts, essentially generating |
| 73 | + state dicts as if they were created using single-device methods. This is useful for |
| 74 | + when checkpoint models might be resharded, or loaded in notebooks or otherwise non-distributed |
| 75 | + settings. |
| 76 | +
|
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, mod: torch.nn.Module) -> None: |
| 80 | + self.mod: torch.nn.Module = mod |
| 81 | + |
| 82 | + def state_dict(self) -> Dict[str, Any]: |
| 83 | + return get_model_state_dict(self.mod) |
| 84 | + |
| 85 | + def load_state_dict(self, state_dict: Dict[str, Any]) -> None: |
| 86 | + set_model_state_dict(self.mod, state_dict) |
| 87 | + |
| 88 | + |
66 | 89 | class DistributedCheckpointSaver(BaseCheckpointer):
|
67 | 90 | """
|
68 | 91 | A callback which periodically saves the application state during training using `Distributed Checkpoint <https://pytorch.org/docs/stable/distributed.checkpoint.html>`_.
|
@@ -148,6 +171,11 @@ def _checkpoint_impl(
|
148 | 171 | curr_snapshot_wait = hook == "on_train_end"
|
149 | 172 |
|
150 | 173 | app_state = _prepare_app_state_for_checkpoint(state, unit, intra_epoch)
|
| 174 | + |
| 175 | + for key, obj in app_state.items(): |
| 176 | + if isinstance(obj, DistributedDataParallel): |
| 177 | + app_state[key] = DSDModelWrapper(obj) |
| 178 | + |
151 | 179 | # TODO: evaluate whether we need to implement the equivalent of torchsnapshot.RNGState()
|
152 | 180 | if self._async_checkpoint:
|
153 | 181 | with get_timing_context(state, f"{self.__class__.__name__}.async_save"):
|
@@ -315,14 +343,17 @@ def restore(
|
315 | 343 | )
|
316 | 344 |
|
317 | 345 | # necessary for loading optimizers since states are initialized lazy
|
318 |
| - for obj in app_state.values(): |
| 346 | + for key, obj in app_state.items(): |
319 | 347 | # sometimes optimizers are actually held in a wrapper which handles calling
|
320 | 348 | # state_dict and load_state_dict, sa is the case for
|
321 | 349 | # `torchtnt.utils.prepare_module.FSDPOptimizerWrapper`, this handles that case.
|
322 | 350 | optimizer = getattr(obj, "optimizer", obj)
|
323 | 351 | if isinstance(optimizer, torch.optim.Optimizer):
|
324 | 352 | init_optim_state(optimizer)
|
325 | 353 |
|
| 354 | + if isinstance(obj, DistributedDataParallel): |
| 355 | + app_state[key] = DSDModelWrapper(obj) |
| 356 | + |
326 | 357 | try:
|
327 | 358 | dcp.load(
|
328 | 359 | {"app_state": MultiStateful(app_state)},
|
|
0 commit comments