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

Method save_checkpoint

deepspeed/runtime/engine.py:4975–5072  ·  view source on GitHub ↗

Save training checkpoint Arguments: save_dir: Required. Directory for saving the checkpoint tag: Optional. Checkpoint tag used as a unique identifier for the checkpoint, global step is used if not provided. Tag name must be the same across all ranks.

(self, save_dir, tag=None, client_state={}, save_latest=True, exclude_frozen_parameters=False)

Source from the content-addressed store, hash-verified

4973 logger.warning(msg)
4974
4975 def save_checkpoint(self, save_dir, tag=None, client_state={}, save_latest=True, exclude_frozen_parameters=False):
4976 """Save training checkpoint
4977
4978 Arguments:
4979 save_dir: Required. Directory for saving the checkpoint
4980 tag: Optional. Checkpoint tag used as a unique identifier for the checkpoint, global step is
4981 used if not provided. Tag name must be the same across all ranks.
4982 client_state: Optional. State dictionary used for saving required training states in the client code.
4983 save_latest: Optional. Save a file 'latest' pointing to the latest saved checkpoint.
4984 exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state.
4985 Important: all processes must call this method and not just the process with rank 0. It is
4986 because each process needs to save its master weights and scheduler+optimizer states. This
4987 method will hang waiting to synchronize with other processes if it's called just for the
4988 process with rank 0.
4989
4990 """
4991 if not save_dir:
4992 raise ValueError(f"save_dir must be a non-empty string, got {save_dir!r}")
4993
4994 if self._optimizer_has_ckpt_event_prologue():
4995 # Custom preparation for checkpoint save, if applicable
4996 self.optimizer.checkpoint_event_prologue()
4997
4998 rank = self.local_rank if self.use_node_local_storage() else self.global_rank
4999
5000 # This is to make sure the checkpoint names are created without collision
5001 # There seems to be issue creating them in parallel
5002
5003 # Ensure save_dir directory exists
5004 if rank == 0:
5005 self.checkpoint_engine.makedirs(save_dir, exist_ok=True)
5006 dist.barrier()
5007
5008 if tag is None:
5009 tag = f"global_step{self.global_steps}"
5010
5011 # Ensure tag is a string
5012 tag = str(tag)
5013 commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=save_latest)
5014
5015 self.checkpoint_engine.create(commit_info)
5016
5017 # Ensure checkpoint tag is consistent across ranks
5018 self._checkpoint_tag_validation(tag)
5019
5020 if self.has_moe_layers:
5021 self.save_non_zero_checkpoint = False
5022 self._create_checkpoint_file(save_dir, tag, False)
5023 self._save_moe_checkpoint(save_dir,
5024 tag,
5025 client_state=client_state,
5026 exclude_frozen_parameters=exclude_frozen_parameters)
5027
5028 # We distribute the task of saving layer checkpoint files among
5029 # data parallel instances, so all procs should call _save_checkpoint.
5030 # All procs then call module_state_dict(), but only procs of data
5031 # parallel rank 0 save the general model params.
5032 if not self.has_moe_layers: