| 4897 | return load_path, client_state |
| 4898 | |
| 4899 | def _load_zero_checkpoint(self, load_dir, tag, load_optimizer_states=True): |
| 4900 | |
| 4901 | load_serial = None |
| 4902 | dp_size_changed = (not self.load_universal_checkpoint() |
| 4903 | and self.seq_dp_world_size != self.loaded_checkpoint_dp_world_size) |
| 4904 | # Elastic ZeRO-1/2 checkpoints can repartition fp32 weights; the optimizer rejects rigid ones. |
| 4905 | if dp_size_changed and (load_optimizer_states or not isinstance(self.optimizer, DeepSpeedZeroOptimizer)): |
| 4906 | raise ZeRORuntimeException("The checkpoint being loaded used a DP " \ |
| 4907 | f"world size of {self.loaded_checkpoint_dp_world_size} but the " \ |
| 4908 | f"current world size is {self.seq_dp_world_size}. Automatic adjustment " \ |
| 4909 | "of ZeRO checkpoint partitioning with a new world size is only supported for " \ |
| 4910 | "fp32 weights of elastic ZeRO stage 1/2 checkpoints loaded without optimizer states. " \ |
| 4911 | "Convert the checkpoint with ds_to_universal.py and load it with " \ |
| 4912 | "checkpoint.load_universal=true.") |
| 4913 | |
| 4914 | universal = self.load_universal_checkpoint() |
| 4915 | pipeline_loading = self._config.zero_config.pipeline_loading_checkpoint |
| 4916 | if pipeline_loading and not universal: |
| 4917 | checkpoint_exists = any( |
| 4918 | self._get_all_zero_checkpoint_names(load_dir, tag, bf16_mode) is not None |
| 4919 | for bf16_mode in [self.bfloat16_enabled(), not self.bfloat16_enabled()]) |
| 4920 | if not checkpoint_exists: |
| 4921 | raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. " |
| 4922 | "Saved fp32 weights are required for this checkpoint load.") |
| 4923 | |
| 4924 | # When use loading checkpoint serial, checkpoint loading start from local rank 0, |
| 4925 | # all other local rank would be paused, waiting for its rank-1 peer ready and its notification. |
| 4926 | if pipeline_loading: |
| 4927 | assert self.zero_optimization_stage( |
| 4928 | ) == ZeroStageEnum.weights, "Only stage3 support for pipeline checkpoint loading" |
| 4929 | load_serial = torch.zeros(1).to(self.device) |
| 4930 | if dist.get_local_rank() != 0: |
| 4931 | dist.recv(tensor=load_serial, src=dist.get_rank() - 1) |
| 4932 | |
| 4933 | if universal: |
| 4934 | zero_sd_list = None |
| 4935 | checkpoint_folder = f'{os.path.join(load_dir, tag)}' |
| 4936 | else: |
| 4937 | checkpoint_folder = None |
| 4938 | zero_sd_list = self._get_all_zero_checkpoints(load_dir, tag) |
| 4939 | if zero_sd_list is None: |
| 4940 | raise ZeRORuntimeException(f"Unable to load ZeRO checkpoint from {os.path.join(load_dir, tag)}. " |
| 4941 | "Saved fp32 weights are required for this checkpoint load.") |
| 4942 | |
| 4943 | param_shapes = self._get_zero_param_shapes() |
| 4944 | self.optimizer.load_state_dict(state_dict_list=zero_sd_list, |
| 4945 | load_optimizer_states=load_optimizer_states, |
| 4946 | load_from_fp32_weights=True, |
| 4947 | checkpoint_folder=checkpoint_folder, |
| 4948 | load_serial=load_serial, |
| 4949 | param_shapes=param_shapes) |
| 4950 | |
| 4951 | if self.load_universal_checkpoint(): |
| 4952 | logger.info(f'loaded universal zero checkpoints from {checkpoint_folder} for rank {self.global_rank}') |
| 4953 | else: |
| 4954 | logger.info(f"loading {len(zero_sd_list)} zero partition checkpoints for rank {self.global_rank}") |
| 4955 | return True |
| 4956 | |