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

Method _load_zero_checkpoint

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

Source from the content-addressed store, hash-verified

4992 return load_path, client_state
4993
4994 def _load_zero_checkpoint(self, load_dir, tag, load_optimizer_states=True):
4995
4996 load_serial = None
4997 dp_size_changed = (not self.load_universal_checkpoint()
4998 and self.seq_dp_world_size != self.loaded_checkpoint_dp_world_size)
4999 # Elastic ZeRO-1/2 checkpoints can repartition fp32 weights; the optimizer rejects rigid ones.
5000 if dp_size_changed and (load_optimizer_states or not isinstance(self.optimizer, DeepSpeedZeroOptimizer)):
5001 raise ZeRORuntimeException("The checkpoint being loaded used a DP " \
5002 f"world size of {self.loaded_checkpoint_dp_world_size} but the " \
5003 f"current world size is {self.seq_dp_world_size}. Automatic adjustment " \
5004 "of ZeRO checkpoint partitioning with a new world size is only supported for " \
5005 "fp32 weights of elastic ZeRO stage 1/2 checkpoints loaded without optimizer states. " \
5006 "Convert the checkpoint with ds_to_universal.py and load it with " \
5007 "checkpoint.load_universal=true.")
5008
5009 universal = self.load_universal_checkpoint()
5010 pipeline_loading = self._config.zero_config.pipeline_loading_checkpoint
5011 if pipeline_loading and not universal:
5012 checkpoint_exists = any(
5013 self._get_all_zero_checkpoint_names(load_dir, tag, bf16_mode) is not None
5014 for bf16_mode in [self.bfloat16_enabled(), not self.bfloat16_enabled()])
5015 if not checkpoint_exists:
5016 raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. "
5017 "Saved fp32 weights are required for this checkpoint load.")
5018
5019 # When use loading checkpoint serial, checkpoint loading start from local rank 0,
5020 # all other local rank would be paused, waiting for its rank-1 peer ready and its notification.
5021 if pipeline_loading:
5022 assert self.zero_optimization_stage(
5023 ) == ZeroStageEnum.weights, "Only stage3 support for pipeline checkpoint loading"
5024 load_serial = torch.zeros(1).to(self.device)
5025 if dist.get_local_rank() != 0:
5026 dist.recv(tensor=load_serial, src=dist.get_rank() - 1)
5027
5028 if universal:
5029 zero_sd_list = None
5030 checkpoint_folder = f'{os.path.join(load_dir, tag)}'
5031 else:
5032 checkpoint_folder = None
5033 zero_sd_list = self._get_all_zero_checkpoints(load_dir, tag)
5034 if zero_sd_list is None:
5035 raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. "
5036 "Saved fp32 weights are required for this checkpoint load.")
5037
5038 param_shapes = self._get_zero_param_shapes()
5039 self.optimizer.load_state_dict(state_dict_list=zero_sd_list,
5040 load_optimizer_states=load_optimizer_states,
5041 load_from_fp32_weights=True,
5042 checkpoint_folder=checkpoint_folder,
5043 load_serial=load_serial,
5044 param_shapes=param_shapes)
5045
5046 if self.load_universal_checkpoint():
5047 logger.info(f'loaded universal zero checkpoints from {checkpoint_folder} for rank {self.global_rank}')
5048 else:
5049 logger.info(f"loading {len(zero_sd_list)} zero partition checkpoints for rank {self.global_rank}")
5050 return True
5051

Calls 11

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