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

Method _load_zero_checkpoint

deepspeed/runtime/engine.py:4817–4873  ·  view source on GitHub ↗
(self, load_dir, tag, load_optimizer_states=True)

Source from the content-addressed store, hash-verified

4815 return load_path, client_state
4816
4817 def _load_zero_checkpoint(self, load_dir, tag, load_optimizer_states=True):
4818
4819 load_serial = None
4820 dp_size_changed = (not self.load_universal_checkpoint()
4821 and self.seq_dp_world_size != self.loaded_checkpoint_dp_world_size)
4822 # Elastic ZeRO-1/2 checkpoints can repartition fp32 weights; the optimizer rejects rigid ones.
4823 if dp_size_changed and (load_optimizer_states or not isinstance(self.optimizer, DeepSpeedZeroOptimizer)):
4824 raise ZeRORuntimeException("The checkpoint being loaded used a DP " \
4825 f"world size of {self.loaded_checkpoint_dp_world_size} but the " \
4826 f"current world size is {self.seq_dp_world_size}. Automatic adjustment " \
4827 "of ZeRO checkpoint partitioning with a new world size is only supported for " \
4828 "fp32 weights of elastic ZeRO stage 1/2 checkpoints loaded without optimizer states. " \
4829 "Convert the checkpoint with ds_to_universal.py and load it with " \
4830 "checkpoint.load_universal=true.")
4831
4832 universal = self.load_universal_checkpoint()
4833 pipeline_loading = self._config.zero_config.pipeline_loading_checkpoint
4834 if pipeline_loading and not universal:
4835 checkpoint_exists = any(
4836 self._get_all_zero_checkpoint_names(load_dir, tag, bf16_mode) is not None
4837 for bf16_mode in [self.bfloat16_enabled(), not self.bfloat16_enabled()])
4838 if not checkpoint_exists:
4839 raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. "
4840 "Saved fp32 weights are required for this checkpoint load.")
4841
4842 # When use loading checkpoint serial, checkpoint loading start from local rank 0,
4843 # all other local rank would be paused, waiting for its rank-1 peer ready and its notification.
4844 if pipeline_loading:
4845 assert self.zero_optimization_stage(
4846 ) == ZeroStageEnum.weights, "Only stage3 support for pipeline checkpoint loading"
4847 load_serial = torch.zeros(1).to(self.device)
4848 if dist.get_local_rank() != 0:
4849 dist.recv(tensor=load_serial, src=dist.get_rank() - 1)
4850
4851 if universal:
4852 zero_sd_list = None
4853 checkpoint_folder = f'{os.path.join(load_dir, tag)}'
4854 else:
4855 checkpoint_folder = None
4856 zero_sd_list = self._get_all_zero_checkpoints(load_dir, tag)
4857 if zero_sd_list is None:
4858 raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. "
4859 "Saved fp32 weights are required for this checkpoint load.")
4860
4861 param_shapes = self._get_zero_param_shapes()
4862 self.optimizer.load_state_dict(state_dict_list=zero_sd_list,
4863 load_optimizer_states=load_optimizer_states,
4864 load_from_fp32_weights=True,
4865 checkpoint_folder=checkpoint_folder,
4866 load_serial=load_serial,
4867 param_shapes=param_shapes)
4868
4869 if self.load_universal_checkpoint():
4870 logger.info(f'loaded universal zero checkpoints from {checkpoint_folder} for rank {self.global_rank}')
4871 else:
4872 logger.info(f"loading {len(zero_sd_list)} zero partition checkpoints for rank {self.global_rank}")
4873 return True
4874

Calls 11

bfloat16_enabledMethod · 0.95
toMethod · 0.45
recvMethod · 0.45
get_rankMethod · 0.45
load_state_dictMethod · 0.45