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

Method load_checkpoint

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

4475 return ckpt_files
4476
4477 def load_checkpoint(self,
4478 load_dir,
4479 tag=None,
4480 load_module_strict=True,
4481 load_optimizer_states=True,
4482 load_lr_scheduler_states=True,
4483 load_module_only=False,
4484 custom_load_fn=None):
4485 """
4486 Load training checkpoint
4487
4488 Arguments:
4489 load_dir: Required. Directory to load the checkpoint from
4490 tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file
4491 load_module_strict: Optional. Boolean to strictly enforce that the keys in state_dict of module and checkpoint match.
4492 load_optimizer_states: Optional. Boolean to load the training optimizer states from Checkpoint. Ex. ADAM's momentum and variance.
4493 When ``False``, ZeRO still restores the saved fp32 master weights but starts with fresh optimizer states.
4494 load_lr_scheduler_states: Optional. Boolean to add the learning rate scheduler states from Checkpoint.
4495 load_module_only: Optional. Boolean to load only the model weights from the checkpoint. Ex. warmstarting.
4496 ZeRO stages 1 and 2 rebuild fp32 master weights from the loaded model weights; universal checkpoints
4497 first load those model weights from their fp32 copies. ZeRO stage 3 restores fp32 weights because its
4498 model checkpoint does not contain the partitioned model weights.
4499 custom_load_fn: Optional. Custom model load function.
4500
4501 Returns:
4502 A tuple of ``load_path`` and ``client_state``.
4503 *``load_path``: Path of the loaded checkpoint. ``None`` if loading the checkpoint failed.
4504 *``client_state``: State dictionary used for loading required training states in the client code.
4505
4506 Important: under ZeRO3, one cannot load checkpoint with ``engine.load_checkpoint()`` right
4507 after ``engine.save_checkpoint()``. It is because ``engine.module`` is partitioned, and
4508 ``load_checkpoint()`` wants a pristine model. If insisting to do so, please reinitialize engine
4509 before ``load_checkpoint()``.
4510
4511 """
4512
4513 if tag is None:
4514 latest_tag = "latest_universal" if self.load_universal_checkpoint() else "latest"
4515 latest_path = os.path.join(load_dir, latest_tag)
4516 if os.path.isfile(latest_path):
4517 with open(latest_path, "r") as fd:
4518 tag = fd.read().strip()
4519 else:
4520 if self.load_universal_checkpoint():
4521 raise ValueError(f'Invalid for universal checkpoint: {latest_path} does not exist')
4522 else:
4523 logger.warning(
4524 f"Unable to find latest file at {latest_path}, if trying to load latest "
4525 "checkpoint please ensure this file exists or pass an explicit checkpoint tag when loading a checkpoint."
4526 )
4527 return None, None
4528
4529 if self._optimizer_has_ckpt_event_prologue():
4530 # Prepare for checkpoint load by ensuring all parameters are partitioned
4531 self.optimizer.checkpoint_event_prologue()
4532
4533 load_path, client_states = self._load_checkpoint(load_dir,
4534 tag,