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

Method save_checkpoint

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

4680 logger.warning(msg)
4681
4682 def save_checkpoint(self, save_dir, tag=None, client_state={}, save_latest=True, exclude_frozen_parameters=False):
4683 """Save training checkpoint
4684
4685 Arguments:
4686 save_dir: Required. Directory for saving the checkpoint
4687 tag: Optional. Checkpoint tag used as a unique identifier for the checkpoint, global step is
4688 used if not provided. Tag name must be the same across all ranks.
4689 client_state: Optional. State dictionary used for saving required training states in the client code.
4690 save_latest: Optional. Save a file 'latest' pointing to the latest saved checkpoint.
4691 exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state.
4692 Important: all processes must call this method and not just the process with rank 0. It is
4693 because each process needs to save its master weights and scheduler+optimizer states. This
4694 method will hang waiting to synchronize with other processes if it's called just for the
4695 process with rank 0.
4696
4697 """
4698 if not save_dir:
4699 raise ValueError(f"save_dir must be a non-empty string, got {save_dir!r}")
4700
4701 if self._optimizer_has_ckpt_event_prologue():
4702 # Custom preparation for checkpoint save, if applicable
4703 self.optimizer.checkpoint_event_prologue()
4704
4705 rank = self.local_rank if self.use_node_local_storage() else self.global_rank
4706
4707 # This is to make sure the checkpoint names are created without collision
4708 # There seems to be issue creating them in parallel
4709
4710 # Ensure save_dir directory exists
4711 if rank == 0:
4712 self.checkpoint_engine.makedirs(save_dir, exist_ok=True)
4713 dist.barrier()
4714
4715 if tag is None:
4716 tag = f"global_step{self.global_steps}"
4717
4718 # Ensure tag is a string
4719 tag = str(tag)
4720 commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=save_latest)
4721
4722 self.checkpoint_engine.create(commit_info)
4723
4724 # Ensure checkpoint tag is consistent across ranks
4725 self._checkpoint_tag_validation(tag)
4726
4727 if self.has_moe_layers:
4728 self.save_non_zero_checkpoint = False
4729 self._create_checkpoint_file(save_dir, tag, False)
4730 self._save_moe_checkpoint(save_dir,
4731 tag,
4732 client_state=client_state,
4733 exclude_frozen_parameters=exclude_frozen_parameters)
4734
4735 # We distribute the task of saving layer checkpoint files among
4736 # data parallel instances, so all procs should call _save_checkpoint.
4737 # All procs then call module_state_dict(), but only procs of data
4738 # parallel rank 0 save the general model params.
4739 if not self.has_moe_layers: