Save 16bit model weights This method saves the 16bit model weights at the desired destination. Arguments: save_dir: Required. Directory for saving the model save_filename: Optional. Filename to save to. Defaults to ``pytorch_model.bin``
(self, save_dir, save_filename="pytorch_model.bin", exclude_frozen_parameters=False)
| 5892 | return self.save_16bit_model(save_dir, save_filename) |
| 5893 | |
| 5894 | def save_16bit_model(self, save_dir, save_filename="pytorch_model.bin", exclude_frozen_parameters=False): |
| 5895 | """ |
| 5896 | Save 16bit model weights |
| 5897 | |
| 5898 | This method saves the 16bit model weights at the desired destination. |
| 5899 | |
| 5900 | Arguments: |
| 5901 | save_dir: Required. Directory for saving the model |
| 5902 | save_filename: Optional. Filename to save to. Defaults to ``pytorch_model.bin`` |
| 5903 | exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state. |
| 5904 | |
| 5905 | Returns: |
| 5906 | ``True`` when a model has been saved, ``False`` otherwise. It will not be saved if |
| 5907 | stage3_gather_16bit_weights_on_model_save is ``False``. |
| 5908 | |
| 5909 | Important: all processes must call this method and not just the process with rank 0. It is |
| 5910 | because the processes need to work in sync to gather the weights. This method will hang |
| 5911 | waiting to synchronize with other processes if it's called just for the process with rank 0. |
| 5912 | |
| 5913 | """ |
| 5914 | |
| 5915 | path = os.path.join(save_dir, save_filename) |
| 5916 | |
| 5917 | if self.zero_optimization_partition_weights(): |
| 5918 | self._raise_if_autoep_zero3_consolidated_export("save_16bit_model") |
| 5919 | if self.zero_gather_16bit_weights_on_model_save(): |
| 5920 | # consolidation is expensive in time and memory and therefore isn't a default |
| 5921 | state_dict = self._zero3_consolidated_16bit_state_dict( |
| 5922 | exclude_frozen_parameters=exclude_frozen_parameters) |
| 5923 | else: |
| 5924 | # the model will be bogus if not consolidated so don't confuse the user by saving it |
| 5925 | logger.info( |
| 5926 | f"Did not save the model {path} because stage3_gather_16bit_weights_on_model_save is False") |
| 5927 | return False |
| 5928 | else: |
| 5929 | state_dict = self.module_state_dict(exclude_frozen_parameters=exclude_frozen_parameters) |
| 5930 | |
| 5931 | tag = f"global_step{self.global_steps}" |
| 5932 | tag = str(tag) |
| 5933 | commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=False) |
| 5934 | self.checkpoint_engine.create(commit_info) |
| 5935 | |
| 5936 | if dist.get_rank() == 0: |
| 5937 | self.checkpoint_engine.makedirs(save_dir, exist_ok=True) |
| 5938 | logger.info(f"Saving model weights to {path}, tag: {tag}") |
| 5939 | self.checkpoint_engine.save(state_dict, path) |
| 5940 | |
| 5941 | self.checkpoint_engine.commit(commit_info) |
| 5942 | |
| 5943 | return True |
| 5944 | |
| 5945 | def empty_partition_cache(self): |
| 5946 | """ |