MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / load_checkpoint

Method load_checkpoint

deepspeed/runtime/engine.py:4603–4716  ·  view source on GitHub ↗

Load training checkpoint Arguments: load_dir: Required. Directory to load the checkpoint from tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file load_module_strict: Optional.

(self,
                        load_dir,
                        tag=None,
                        load_module_strict=True,
                        load_optimizer_states=True,
                        load_lr_scheduler_states=True,
                        load_module_only=False,
                        custom_load_fn=None)

Source from the content-addressed store, hash-verified

4601 return ckpt_files
4602
4603 def load_checkpoint(self,
4604 load_dir,
4605 tag=None,
4606 load_module_strict=True,
4607 load_optimizer_states=True,
4608 load_lr_scheduler_states=True,
4609 load_module_only=False,
4610 custom_load_fn=None):
4611 """
4612 Load training checkpoint
4613
4614 Arguments:
4615 load_dir: Required. Directory to load the checkpoint from
4616 tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file
4617 load_module_strict: Optional. Boolean to strictly enforce that the keys in state_dict of module and checkpoint match.
4618 load_optimizer_states: Optional. Boolean to load the training optimizer states from Checkpoint. Ex. ADAM's momentum and variance.
4619 When ``False``, ZeRO still restores the saved fp32 master weights but starts with fresh optimizer states.
4620 load_lr_scheduler_states: Optional. Boolean to add the learning rate scheduler states from Checkpoint.
4621 load_module_only: Optional. Boolean to load only the model weights from the checkpoint. Ex. warmstarting.
4622 ZeRO stages 1 and 2 rebuild fp32 master weights from the loaded model weights; universal checkpoints
4623 first load those model weights from their fp32 copies. ZeRO stage 3 restores fp32 weights because its
4624 model checkpoint does not contain the partitioned model weights.
4625 NVMe optimizer offload requires the same DP world size, parameter/subgroup layout, and swap storage
4626 configuration, and does not support custom_load_fn. Existing optimizer states are preserved.
4627 custom_load_fn: Optional. Custom model load function.
4628
4629 Returns:
4630 A tuple of ``load_path`` and ``client_state``.
4631 *``load_path``: Path of the loaded checkpoint. ``None`` if loading the checkpoint failed.
4632 *``client_state``: State dictionary used for loading required training states in the client code.
4633
4634 Important: under ZeRO3, one cannot load checkpoint with ``engine.load_checkpoint()`` right
4635 after ``engine.save_checkpoint()``. It is because ``engine.module`` is partitioned, and
4636 ``load_checkpoint()`` wants a pristine model. If insisting to do so, please reinitialize engine
4637 before ``load_checkpoint()``.
4638
4639 """
4640
4641 if tag is None:
4642 latest_tag = "latest_universal" if self.load_universal_checkpoint() else "latest"
4643 latest_path = os.path.join(load_dir, latest_tag)
4644 if os.path.isfile(latest_path):
4645 with open(latest_path, "r") as fd:
4646 tag = fd.read().strip()
4647 else:
4648 if self.load_universal_checkpoint():
4649 raise ValueError(f'Invalid for universal checkpoint: {latest_path} does not exist')
4650 else:
4651 logger.warning(
4652 f"Unable to find latest file at {latest_path}, if trying to load latest "
4653 "checkpoint please ensure this file exists or pass an explicit checkpoint tag when loading a checkpoint."
4654 )
4655 return None, None
4656
4657 if load_module_only and self.zero_nvme_offload_optimizer() and custom_load_fn is not None:
4658 raise ZeRORuntimeException("NVMe module-only checkpoint load does not support custom_load_fn")
4659
4660 if self._optimizer_has_ckpt_event_prologue():