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

Method load_checkpoint

deepspeed/runtime/engine.py:4559–4660  ·  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

4557 return ckpt_files
4558
4559 def load_checkpoint(self,
4560 load_dir,
4561 tag=None,
4562 load_module_strict=True,
4563 load_optimizer_states=True,
4564 load_lr_scheduler_states=True,
4565 load_module_only=False,
4566 custom_load_fn=None):
4567 """
4568 Load training checkpoint
4569
4570 Arguments:
4571 load_dir: Required. Directory to load the checkpoint from
4572 tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file
4573 load_module_strict: Optional. Boolean to strictly enforce that the keys in state_dict of module and checkpoint match.
4574 load_optimizer_states: Optional. Boolean to load the training optimizer states from Checkpoint. Ex. ADAM's momentum and variance.
4575 When ``False``, ZeRO still restores the saved fp32 master weights but starts with fresh optimizer states.
4576 load_lr_scheduler_states: Optional. Boolean to add the learning rate scheduler states from Checkpoint.
4577 load_module_only: Optional. Boolean to load only the model weights from the checkpoint. Ex. warmstarting.
4578 ZeRO stages 1 and 2 rebuild fp32 master weights from the loaded model weights; universal checkpoints
4579 first load those model weights from their fp32 copies. ZeRO stage 3 restores fp32 weights because its
4580 model checkpoint does not contain the partitioned model weights.
4581 custom_load_fn: Optional. Custom model load function.
4582
4583 Returns:
4584 A tuple of ``load_path`` and ``client_state``.
4585 *``load_path``: Path of the loaded checkpoint. ``None`` if loading the checkpoint failed.
4586 *``client_state``: State dictionary used for loading required training states in the client code.
4587
4588 Important: under ZeRO3, one cannot load checkpoint with ``engine.load_checkpoint()`` right
4589 after ``engine.save_checkpoint()``. It is because ``engine.module`` is partitioned, and
4590 ``load_checkpoint()`` wants a pristine model. If insisting to do so, please reinitialize engine
4591 before ``load_checkpoint()``.
4592
4593 """
4594
4595 if tag is None:
4596 latest_tag = "latest_universal" if self.load_universal_checkpoint() else "latest"
4597 latest_path = os.path.join(load_dir, latest_tag)
4598 if os.path.isfile(latest_path):
4599 with open(latest_path, "r") as fd:
4600 tag = fd.read().strip()
4601 else:
4602 if self.load_universal_checkpoint():
4603 raise ValueError(f'Invalid for universal checkpoint: {latest_path} does not exist')
4604 else:
4605 logger.warning(
4606 f"Unable to find latest file at {latest_path}, if trying to load latest "
4607 "checkpoint please ensure this file exists or pass an explicit checkpoint tag when loading a checkpoint."
4608 )
4609 return None, None
4610
4611 if self._optimizer_has_ckpt_event_prologue():
4612 # Prepare for checkpoint load by ensuring all parameters are partitioned
4613 self.optimizer.checkpoint_event_prologue()
4614
4615 load_path, client_states = self._load_checkpoint(load_dir,
4616 tag,