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

Method save_16bit_model

deepspeed/runtime/engine.py:5798–5847  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

5796 return self.save_16bit_model(save_dir, save_filename)
5797
5798 def save_16bit_model(self, save_dir, save_filename="pytorch_model.bin", exclude_frozen_parameters=False):
5799 """
5800 Save 16bit model weights
5801
5802 This method saves the 16bit model weights at the desired destination.
5803
5804 Arguments:
5805 save_dir: Required. Directory for saving the model
5806 save_filename: Optional. Filename to save to. Defaults to ``pytorch_model.bin``
5807 exclude_frozen_parameters: Optional. Exclude frozen parameters from checkpointed state.
5808
5809 Returns:
5810 ``True`` when a model has been saved, ``False`` otherwise. It will not be saved if
5811 stage3_gather_16bit_weights_on_model_save is ``False``.
5812
5813 Important: all processes must call this method and not just the process with rank 0. It is
5814 because the processes need to work in sync to gather the weights. This method will hang
5815 waiting to synchronize with other processes if it's called just for the process with rank 0.
5816
5817 """
5818
5819 path = os.path.join(save_dir, save_filename)
5820
5821 if self.zero_optimization_partition_weights():
5822 self._raise_if_autoep_zero3_consolidated_export("save_16bit_model")
5823 if self.zero_gather_16bit_weights_on_model_save():
5824 # consolidation is expensive in time and memory and therefore isn't a default
5825 state_dict = self._zero3_consolidated_16bit_state_dict(
5826 exclude_frozen_parameters=exclude_frozen_parameters)
5827 else:
5828 # the model will be bogus if not consolidated so don't confuse the user by saving it
5829 logger.info(
5830 f"Did not save the model {path} because stage3_gather_16bit_weights_on_model_save is False")
5831 return False
5832 else:
5833 state_dict = self.module_state_dict(exclude_frozen_parameters=exclude_frozen_parameters)
5834
5835 tag = f"global_step{self.global_steps}"
5836 tag = str(tag)
5837 commit_info = CheckpointCommitInfo(tag=tag, save_dir=save_dir, save_latest=False)
5838 self.checkpoint_engine.create(commit_info)
5839
5840 if dist.get_rank() == 0:
5841 self.checkpoint_engine.makedirs(save_dir, exist_ok=True)
5842 logger.info(f"Saving model weights to {path}, tag: {tag}")
5843 self.checkpoint_engine.save(state_dict, path)
5844
5845 self.checkpoint_engine.commit(commit_info)
5846
5847 return True
5848
5849 def empty_partition_cache(self):
5850 """

Callers 2

save_fp16_modelMethod · 0.95

Calls 11

module_state_dictMethod · 0.95
makedirsMethod · 0.80
createMethod · 0.45
get_rankMethod · 0.45
saveMethod · 0.45
commitMethod · 0.45

Tested by 1