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)
| 5055 | logger.warning(msg) |
| 5056 | |
| 5057 | def save_checkpoint(self, save_dir, tag=None, client_state={}, save_latest=True, exclude_frozen_parameters=False): |
| 5058 | """Save training checkpoint |
| 5059 | |
| 5060 | Arguments: |
| 5061 | save_dir: Required. Directory for saving the checkpoint |
| 5062 | tag: Optional. Checkpoint tag used as a unique identifier for the checkpoint, global step is |
| 5063 | used if not provided. Tag name must be the same across all ranks. |
| 5064 | client_state: Optional. State dictionary used for saving required training states in the client code. |
| 5065 | save_latest: Optional. Save a file 'latest' pointing to the latest saved checkpoint. |
| 5066 | exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state. |
| 5067 | Important: all processes must call this method and not just the process with rank 0. It is |
| 5068 | because each process needs to save its master weights and scheduler+optimizer states. This |
| 5069 | method will hang waiting to synchronize with other processes if it's called just for the |
| 5070 | process with rank 0. |
| 5071 | |
| 5072 | """ |
| 5073 | if not save_dir: |
| 5074 | raise ValueError(f"save_dir must be a non-empty string, got {save_dir!r}") |
| 5075 | |
| 5076 | if self._optimizer_has_ckpt_event_prologue(): |
| 5077 | # Custom preparation for checkpoint save, if applicable |
| 5078 | self.optimizer.checkpoint_event_prologue() |
| 5079 | |
| 5080 | rank = self.local_rank if self.use_node_local_storage() else self.global_rank |
| 5081 | |
| 5082 | # This is to make sure the checkpoint names are created without collision |
| 5083 | # There seems to be issue creating them in parallel |
| 5084 | |
| 5085 | # Ensure save_dir directory exists |
| 5086 | if rank == 0: |
| 5087 | self.checkpoint_engine.makedirs(save_dir, exist_ok=True) |
| 5088 | dist.barrier() |
| 5089 | |
| 5090 | if tag is None: |
| 5091 | tag = f"global_step{self.global_steps}" |
| 5092 | |
| 5093 | # Ensure tag is a string |
| 5094 | tag = str(tag) |
| 5095 | commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=save_latest) |
| 5096 | |
| 5097 | self.checkpoint_engine.create(commit_info) |
| 5098 | |
| 5099 | # Ensure checkpoint tag is consistent across ranks |
| 5100 | self._checkpoint_tag_validation(tag) |
| 5101 | |
| 5102 | if self.has_moe_layers: |
| 5103 | self.save_non_zero_checkpoint = False |
| 5104 | self._create_checkpoint_file(save_dir, tag, False) |
| 5105 | self._save_moe_checkpoint(save_dir, |
| 5106 | tag, |
| 5107 | client_state=client_state, |
| 5108 | exclude_frozen_parameters=exclude_frozen_parameters) |
| 5109 | |
| 5110 | # We distribute the task of saving layer checkpoint files among |
| 5111 | # data parallel instances, so all procs should call _save_checkpoint. |
| 5112 | # All procs then call module_state_dict(), but only procs of data |
| 5113 | # parallel rank 0 save the general model params. |
| 5114 | if not self.has_moe_layers: |