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

Method load_checkpoint

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

4202 return ckpt_files
4203
4204 def load_checkpoint(self,
4205 load_dir,
4206 tag=None,
4207 load_module_strict=True,
4208 load_optimizer_states=True,
4209 load_lr_scheduler_states=True,
4210 load_module_only=False,
4211 custom_load_fn=None):
4212 """
4213 Load training checkpoint
4214
4215 Arguments:
4216 load_dir: Required. Directory to load the checkpoint from
4217 tag: Checkpoint tag used as a unique identifier for checkpoint, if not provided will attempt to load tag in 'latest' file
4218 load_module_strict: Optional. Boolean to strictly enforce that the keys in state_dict of module and checkpoint match.
4219 load_optimizer_states: Optional. Boolean to load the training optimizer states from Checkpoint. Ex. ADAM's momentum and variance
4220 load_lr_scheduler_states: Optional. Boolean to add the learning rate scheduler states from Checkpoint.
4221 load_module_only: Optional. Boolean to load only the model weights from the checkpoint. Ex. warmstarting.
4222 custom_load_fn: Optional. Custom model load function.
4223
4224 Returns:
4225 A tuple of ``load_path`` and ``client_state``.
4226 *``load_path``: Path of the loaded checkpoint. ``None`` if loading the checkpoint failed.
4227 *``client_state``: State dictionary used for loading required training states in the client code.
4228
4229 Important: under ZeRO3, one cannot load checkpoint with ``engine.load_checkpoint()`` right
4230 after ``engine.save_checkpoint()``. It is because ``engine.module`` is partitioned, and
4231 ``load_checkpoint()`` wants a pristine model. If insisting to do so, please reinitialize engine
4232 before ``load_checkpoint()``.
4233
4234 """
4235
4236 if tag is None:
4237 latest_tag = "latest_universal" if self.load_universal_checkpoint() else "latest"
4238 latest_path = os.path.join(load_dir, latest_tag)
4239 if os.path.isfile(latest_path):
4240 with open(latest_path, "r") as fd:
4241 tag = fd.read().strip()
4242 else:
4243 if self.load_universal_checkpoint():
4244 raise ValueError(f'Invalid for universal checkpoint: {latest_path} does not exist')
4245 else:
4246 logger.warning(
4247 f"Unable to find latest file at {latest_path}, if trying to load latest "
4248 "checkpoint please ensure this file exists or pass an explicit checkpoint tag when loading a checkpoint."
4249 )
4250 return None, None
4251
4252 if self._optimizer_has_ckpt_event_prologue():
4253 # Prepare for checkpoint load by ensuring all parameters are partitioned
4254 self.optimizer.checkpoint_event_prologue()
4255
4256 load_path, client_states = self._load_checkpoint(load_dir,
4257 tag,
4258 load_module_strict=load_module_strict,
4259 load_optimizer_states=load_optimizer_states,
4260 load_lr_scheduler_states=load_lr_scheduler_states,
4261 load_module_only=load_module_only,