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)
| 5150 | logger.warning(msg) |
| 5151 | |
| 5152 | def save_checkpoint(self, save_dir, tag=None, client_state={}, save_latest=True, exclude_frozen_parameters=False): |
| 5153 | """Save training checkpoint |
| 5154 | |
| 5155 | Arguments: |
| 5156 | save_dir: Required. Directory for saving the checkpoint |
| 5157 | tag: Optional. Checkpoint tag used as a unique identifier for the checkpoint, global step is |
| 5158 | used if not provided. Tag name must be the same across all ranks. |
| 5159 | client_state: Optional. State dictionary used for saving required training states in the client code. |
| 5160 | save_latest: Optional. Save a file 'latest' pointing to the latest saved checkpoint. |
| 5161 | exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state. |
| 5162 | Important: all processes must call this method and not just the process with rank 0. It is |
| 5163 | because each process needs to save its master weights and scheduler+optimizer states. This |
| 5164 | method will hang waiting to synchronize with other processes if it's called just for the |
| 5165 | process with rank 0. |
| 5166 | |
| 5167 | """ |
| 5168 | if not save_dir: |
| 5169 | raise ValueError(f"save_dir must be a non-empty string, got {save_dir!r}") |
| 5170 | |
| 5171 | if self._optimizer_has_ckpt_event_prologue(): |
| 5172 | # Custom preparation for checkpoint save, if applicable |
| 5173 | self.optimizer.checkpoint_event_prologue() |
| 5174 | |
| 5175 | rank = self.local_rank if self.use_node_local_storage() else self.global_rank |
| 5176 | |
| 5177 | # This is to make sure the checkpoint names are created without collision |
| 5178 | # There seems to be issue creating them in parallel |
| 5179 | |
| 5180 | # Ensure save_dir directory exists |
| 5181 | if rank == 0: |
| 5182 | self.checkpoint_engine.makedirs(save_dir, exist_ok=True) |
| 5183 | dist.barrier() |
| 5184 | |
| 5185 | if tag is None: |
| 5186 | tag = f"global_step{self.global_steps}" |
| 5187 | |
| 5188 | # Ensure tag is a string |
| 5189 | tag = str(tag) |
| 5190 | commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=save_latest) |
| 5191 | |
| 5192 | self.checkpoint_engine.create(commit_info) |
| 5193 | |
| 5194 | # Ensure checkpoint tag is consistent across ranks |
| 5195 | self._checkpoint_tag_validation(tag) |
| 5196 | |
| 5197 | if self.has_moe_layers: |
| 5198 | self.save_non_zero_checkpoint = False |
| 5199 | self._create_checkpoint_file(save_dir, tag, False) |
| 5200 | self._save_moe_checkpoint(save_dir, |
| 5201 | tag, |
| 5202 | client_state=client_state, |
| 5203 | exclude_frozen_parameters=exclude_frozen_parameters) |
| 5204 | |
| 5205 | # We distribute the task of saving layer checkpoint files among |
| 5206 | # data parallel instances, so all procs should call _save_checkpoint. |
| 5207 | # All procs then call module_state_dict(), but only procs of data |
| 5208 | # parallel rank 0 save the general model params. |
| 5209 | if not self.has_moe_layers: |