Repository navigation
[UCP] Load universal checkpoints across ZeRO stages #9080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2bec476
ce758ab
ce1a806
06d874b
234e007
4aa7f4f
e488d7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| EP_IS_EXPERT_PARAM, | ||
| EP_NUM_EXPERTS, | ||
| OPTIMIZER_STATE_DICT, | ||
| PARAM_GROUPS, | ||
| FP32_FLAT_GROUPS, | ||
| PARAM, | ||
| PARTITION_COUNT, | ||
|
|
@@ -4057,6 +4058,26 @@ def _load_universal_checkpoint(self, checkpoint_folder, load_optimizer_states, l | |
| else: | ||
| self.load_module_checkpoint_state_from_checkpoint_dir_stage3(checkpoint_folder, True) | ||
|
|
||
| def _sub_groups_without_saved_moments(self, checkpoint_dir): | ||
| """The sub-groups whose parameters have no saved Adam moments. | ||
|
|
||
| ZeRO-1/2 creates Adam state on the first step, so ds_to_universal writes only fp32.pt for a checkpoint | ||
| saved before it. A sub-group with moments for some parameters but not others cannot be restored. | ||
| """ | ||
| missing = set() | ||
| for sub_group_id, fp16_group in enumerate(self.fp16_groups): | ||
| saved = [ | ||
| all( | ||
| os.path.isfile(os.path.join(checkpoint_dir, self.param_names[param], f"{key}.pt")) | ||
| for key in ("exp_avg", "exp_avg_sq")) for param in fp16_group if param in self.param_names | ||
| ] | ||
| if saved and not any(saved): | ||
| missing.add(sub_group_id) | ||
| elif not all(saved): | ||
| raise ZeRORuntimeException(f"The universal checkpoint in {checkpoint_dir} has Adam moments for only " | ||
| f"some parameters of optimizer sub-group {sub_group_id}.") | ||
| return missing | ||
|
|
||
| def load_hp_checkpoint_state_from_checkpoint_dir_stage3(self, checkpoint_dir): | ||
| """ Load optimizer and model states from the checkpoint directory. """ | ||
| checkpoint_dir = os.path.join(checkpoint_dir, "zero") | ||
|
|
@@ -4068,10 +4089,18 @@ def load_hp_checkpoint_state_from_checkpoint_dir_stage3(self, checkpoint_dir): | |
| self._load_global_state_stage3(optim_sd) | ||
|
|
||
| # Generally the step of each optimizer file should be the same, we can obtain from any parameter. | ||
| state_step = optim_sd[OPTIMIZER_STATE_DICT]['state'][0]['step'] | ||
| from deepspeed.checkpoint.universal_checkpoint import load_universal_optimizer_step | ||
| state_step = load_universal_optimizer_step(optim_sd, checkpoint_dir, self.param_names.values()) | ||
| sub_groups_without_moments = self._sub_groups_without_saved_moments(checkpoint_dir) | ||
| for key in ["fp32", "exp_avg", "exp_avg_sq"]: | ||
|
Comment on lines
+4093
to
4095
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a ZeRO-1/2 checkpoint is converted before a parameter group has ever received a gradient (for example, a checkpoint at step 0), AGENTS.md reference: AGENTS.md:L19-L19 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed: a ZeRO-1/2 checkpoint saved before the first step has no Adam state, Fixed in e488d7a. A ZeRO-3 sub-group with no saved moments now loads only its fp32 master and starts without Adam state, as a fresh engine does; a sub-group with moments for only some of its parameters is refused. |
||
| for sub_group_id, fp16_group in enumerate(self.fp16_groups): | ||
| fp32_param = self.fp32_partitioned_groups_flat[sub_group_id] | ||
| if sub_group_id in sub_groups_without_moments: | ||
| if key == "fp32": | ||
| # The source never stepped this group, so it starts without Adam state, as when fresh. | ||
| self.optimizer.state.pop(fp32_param, None) | ||
| else: | ||
| continue | ||
| key_tensor = torch.zeros_like(fp32_param) | ||
| offset = 0 | ||
| for param in fp16_group: | ||
|
|
@@ -4085,14 +4114,22 @@ def load_hp_checkpoint_state_from_checkpoint_dir_stage3(self, checkpoint_dir): | |
| offset += key_layer_state_partition.numel() | ||
| if key == "fp32": | ||
| self.fp32_partitioned_groups_flat[sub_group_id].data.copy_(key_tensor) | ||
| self.optimizer.state[fp32_param]['step'] = state_step | ||
| if state_step is not None and sub_group_id not in sub_groups_without_moments: | ||
| # Each sub-group's optimizer advances its step in place, so give it its own copy. | ||
| self.optimizer.state[fp32_param]['step'] = (state_step.clone() | ||
| if torch.is_tensor(state_step) else state_step) | ||
| else: | ||
| self.optimizer.state[fp32_param][key] = key_tensor | ||
| self._move_state_to_backup_optimizer() | ||
|
|
||
| for param_group in self.optimizer.param_groups: | ||
| # Generally, the hyperparameters of each parameter should be the same, we can obtain from any parameter. | ||
| for key, value in optim_sd[OPTIMIZER_STATE_DICT]["param_groups"][0].items(): | ||
| # A ZeRO-1/2 source has no OPTIMIZER_STATE_DICT, only the top-level PARAM_GROUPS. Both list the | ||
| # optimizer's own parameter groups in order, as self.optimizer.param_groups does. | ||
| saved_param_groups = optim_sd.get(PARAM_GROUPS) or optim_sd[OPTIMIZER_STATE_DICT][PARAM_GROUPS] | ||
| if len(saved_param_groups) != len(self.optimizer.param_groups): | ||
| raise ValueError(f"The universal checkpoint in {checkpoint_dir} has {len(saved_param_groups)} optimizer " | ||
| f"parameter groups, but this optimizer has {len(self.optimizer.param_groups)}.") | ||
| for param_group, saved_param_group in zip(self.optimizer.param_groups, saved_param_groups): | ||
| for key, value in saved_param_group.items(): | ||
| if key == 'params': | ||
| param_group['params'] = [] | ||
| else: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.