Repository navigation
Keep fp32 masters on ZeRO-1 and ZeRO-2 when a resume skips optimizer state - #8891
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 945aa1c860
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| "cannot be restored and are being rebuilt from the bit16 weights, losing precision. " | ||
| "Convert the checkpoint with ds_to_universal.py and load it with " | ||
| "checkpoint.load_universal=true to keep them.") | ||
| return False |
There was a problem hiding this comment.
Check DP fallback before entering pipeline receive chain
With ZeRO-3 pipeline_loading_checkpoint=true, a non-MoE legacy masters-only resume (load_optimizer_states=False) from a different DP size deadlocks: nonzero local ranks have already blocked in the dist.recv above, while local rank 0 takes this new return and never reaches the notification send in stage3.load_state_dict. Perform the DP-size fallback before starting the serial receive chain, or explicitly release the waiting ranks.
Useful? React with 👍 / 👎.
| # load_module_only restores the model and rebuilds the masters from it. | ||
| if load_fp32_weights is None: | ||
| load_fp32_weights = self.zero_load_from_fp32_weights() | ||
| load_saved_fp32_masters = load_fp32_weights and not load_module_only |
There was a problem hiding this comment.
Honor load_fp32_weights for NVMe optimizer offload
With ZeRO-3 NVMe optimizer offload, this selection is never consumed because the normal load block is skipped by not self.zero_nvme_offload_optimizer(), after which the entire saved swap directory is copied and reset unconditionally. Consequently load_fp32_weights=False (and the documented module-only behavior) still restores checkpoint FP32 masters instead of rebuilding them from the loaded bit16 model; the NVMe path needs to apply the same master-state choice rather than always reinstating the saved swap tensors.
Useful? React with 👍 / 👎.
| if not load_from_fp32_weights: | ||
| self.refresh_fp32_params() |
There was a problem hiding this comment.
Rebuild ZeRO-3 masters before copying saved FP32 partitions
When a legacy ZeRO-3 resume loads optimizer moments but sets load_fp32_weights=False, this refresh runs only after _rigid_load_state_dict has copied checkpoint FP32_FLAT_GROUPS into both the master and bit16 partitions. It therefore rounds the saved masters back to FP32 instead of rebuilding from the model state loaded by _load_checkpoint, and can overwrite custom-loaded or otherwise divergent module values; branch before the saved-FP32 copies, as the ZeRO-1/2 sibling path does, so the new override actually selects the model weights.
AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
…state. load_optimizer_states=False skipped the ZeRO shard, so a fresh optimizer rebuilt the fp32 masters from bf16/fp16. Stage 1 and 2 now restore the saved masters without Adam moments. ZeRO-3 and the dedicated ZeRO-1 BF16 optimizer stay on the previous path. Signed-off-by: Olatunji Ruwase <[email protected]> Co-authored-by: Cursor <[email protected]>
945aa1c to
f9958d4
Compare
Signed-off-by: Olatunji Ruwase <[email protected]>
| precision loss). This can be used to initialize optimizer state even when | ||
| checkpoint is missing optimizer state. | ||
| precision loss). For ZeRO stage 1 and 2, ``DeepSpeedEngine.load_checkpoint(..., | ||
| load_fp32_weights=...)`` overrides this for one load; ``None`` uses this value. |
There was a problem hiding this comment.
what does "one load" mean?
There was a problem hiding this comment.
I also don't understand this sentence: None uses this value.
| ep_size=ep_size, | ||
| load_optimizer_states=load_optimizer_states) | ||
| if load_optimizer_states: | ||
| for key in lp._hp_mapping.get_optim_state_keys(): | ||
| opt_keys.add(key) | ||
| steps.append(step) | ||
|
|
||
| if not load_optimizer_states: | ||
| continue |
There was a problem hiding this comment.
this if and if not logic is hard to parse, if line 376 leads to ignoring everything else before it, why the previous code is even run? for some side-effect? steps and opt_keys don't get updated anyway if load_optimizer_states is False
should the whole thing above be skipped if not load_optimizer_states?
Signed-off-by: Olatunji Ruwase <[email protected]>
A full load no longer rebuilds masters from bf16, ZeRO-3 no longer gathers the whole model, and elastic ZeRO-1/2 checkpoints can repartition those masters across a new DP size when optimizer state is not loaded. Signed-off-by: Olatunji Ruwase <[email protected]> Co-authored-by: Cursor <[email protected]>
A ZeRO-1/2 load used to skip a missing fp32.pt and then copy the initialized master over the loaded weights. ZeRO-3 raised FileNotFoundError for the same gap. Signed-off-by: Olatunji Ruwase <[email protected]> Co-authored-by: Cursor <[email protected]>
Signed-off-by: Olatunji Ruwase <[email protected]> Co-authored-by: Cursor <[email protected]>
Preserve the full-state and masters-only NVMe restore branches from deepspeedai#8891 while integrating the module-only checkpoint refresh path. Signed-off-by: levius <[email protected]>
…eedai#8892) ## Summary - Follow-up to deepspeedai#8891 (merged as `2dd22dc`). The delta against `master` is ZeRO-3 and the dedicated ZeRO-1 BF16 optimizer, plus the review fixes listed below. - `load_checkpoint` still always restores saved fp32 masters. `load_optimizer_states=False` keeps those masters and starts Adam with fresh moments. `load_module_only=True` rebuilds the dedicated BF16 optimizer from the loaded bf16 weights. ZeRO-3 still reads its fp32 partition on a module-only load, because the model checkpoint does not hold the partitioned weights. - `ZeRO-3` and `BF16_Optimizer.load_state_dict(..., load_from_fp32_weights=False)` now rebuild the masters from the bit16 weights, matching ZeRO stage 1 and 2. `DeepSpeedEngine.load_checkpoint` passes true, so a normal resume keeps the saved masters. `zero_optimization.load_from_fp32_weights` stays deprecated and ignored. - Review fixes: a pipeline-serialized load now checks that ZeRO shards exist before waiting on the previous rank, so a missing shard raises instead of leaving ranks blocked in `dist.recv`. A universal load that skips optimizer state keeps `optim_fragment`, so `safe_get_full_optimizer_state` and `safe_set_full_optimizer_state` still work. A leftover debug `print` in `BF16_Optimizer` is removed. AI assistance was used to implement and test this change. This does not duplicate an open DeepSpeed PR. ## Test plan - [x] CPU, torch 2.9.1+cu130, Python 3.12.12, Intel Xeon Platinum 8488C, gloo, world size 1, `tests/unit/checkpoint/test_fp32_master_only.py` and `tests/unit/checkpoint/test_fp32_master_stage3_bf16.py`: 10 passed. Both files ran on the tree that became `a22f0cc4`. - The dedicated BF16 optimizer keeps the saved masters when optimizer state is skipped and rebuilds them for `load_module_only=True`. ZeRO-3 keeps the saved masters for both of those loads. Adam moments stay fresh. - A direct `load_state_dict(..., load_from_fp32_weights=False)` after a module-only load rebuilds the masters from the bit16 weights, for both BF16 and ZeRO-3. - Optimizer globals (`clip_grad`, `overflow`, `dynamic_loss_scale`) are not restored on a masters-only load. - A universal masters-only load into a stepped engine keeps `safe_get_full_optimizer_state` and `safe_set_full_optimizer_state` working. - A pipeline-serialized load with missing shards raises before `dist.recv`. The NVMe masters-only copy restores only the fp32 swap file. - [x] 4×H200, torch 2.9.1+cu130, Python 3.12.12, same two files on tree `a22f0cc4`: 10 passed in 52s. This includes the three data-parallel change tests (universal, legacy, elastic), which spawn 2 ranks on `cuda:0`/`cuda:1`. The deepspeedai#8891 suite there does not cover these two optimizers. Validated cells: dedicated ZeRO-1 BF16 optimizer and ZeRO-3, same data-parallel size, legacy checkpoint, bf16, no offload. ZeRO-1/2 universal masters-only load covered by the fragment test. Open cells: fp16, MoE, real NVMe offload (the NVMe test uses a stub swapper), and a data-parallel size change. --------- Signed-off-by: Olatunji Ruwase <[email protected]> Co-authored-by: Cursor <[email protected]>
Summary
load_optimizer_states=Falseused to skip the ZeRO shard, so a fresh optimizer rebuilt the fp32 master weights from the bf16/fp16 parameters and dropped the precision saved inSINGLE_PARTITION_OF_FP32_GROUPSor universalfp32.pt.DeepSpeedEngine.load_checkpointno longer acceptsload_fp32_weights.zero_optimization.load_from_fp32_weightsis deprecated and ignored. A full ZeRO load always restores the saved fp32 masters.load_optimizer_states=Falsewithload_module_only=Falserestores those masters and starts Adam with fresh moments.load_module_only=Truerebuilds ZeRO-1 and ZeRO-2 masters from the loaded model weights. Universal module-only loads read those model weights fromfp32.ptfirst. ZeRO-3 still reads its fp32 partition, because the model checkpoint does not hold the partitioned weights.ZeRORuntimeException. An elastic ZeRO-1/2 checkpoint can repartition fp32 masters when optimizer state is not loaded. Universal checkpoints keep the masters across a DP size change. ZeRO-3 still rejects a DP size change.*_optim_states.ptfiles, and a universal folder with nofp32.pt, raiseZeRORuntimeExceptionon any load that needs the masters. ZeRO-1/2load_module_only=Truestill succeeds without the shard. Aparameter_alignmentmismatch raises on a full load and on a masters-only load. Module-only remains the warm start for that mismatch.DistributedFixturesubclass, so a test skips when the machine has fewer GPUs than the fixture world size.load_module_only=True, are tracked in Follow-ups for restoring fp32 masters without optimizer state #8890.Load decision tree
zero_optimization.load_from_fp32_weightsis ignored. When a universal load hasload_optimizer_states=False,load_hp_checkpoint_state_from_checkpoint_dirreadsfp32.ptand returns. It does not openoptimizer_state.pt, and it does not fill Adam steps or moments.flowchart TD start["ZeRO checkpoint was found"] --> module{"load_module_only"} module -->|"True, legacy ZeRO-1 or ZeRO-2"| rebuild["Skip the shard. Rebuild masters from the loaded model weights. Moments stay fresh."] module -->|"True, universal or ZeRO-3"| open["Open the shard"] module -->|False| open open --> shards{"Legacy shard files present, or the checkpoint is universal"} shards -->|"Legacy files missing"| raiseMissing["ZeRORuntimeException: saved fp32 weights are required"] shards -->|present| form{"Which checkpoint"} form -->|"Legacy ZeRO-1 or ZeRO-2"| legacy12["Restore the saved fp32 masters. Load Adam moments only when requested."] form -->|"Universal ZeRO-1, ZeRO-2, or BF16"| uniMoments{"load_optimizer_states"} uniMoments -->|yes| uniFull["Read fp32.pt, the moments, and optimizer_state.pt. A missing fp32.pt raises."] uniMoments -->|no| readFp32["Read fp32.pt and return. A missing fp32.pt raises."] readFp32 --> round{"load_module_only"} round -->|no| keep["Copy the masters onto the model weights. The masters keep the saved precision. Moments stay fresh."] round -->|yes| rounded["Copy the masters onto the model weights, then rebuild the masters from those weights. Moments stay fresh."] form -->|"Legacy ZeRO-3"| z3["Copy the saved fp32 partition into the masters and then into fp16. Load moments only when requested."] form -->|"Universal ZeRO-3"| z3Moments{"load_optimizer_states"} z3Moments -->|no| z3Masters["Read fp32.pt. A missing file raises."] z3Moments -->|yes| z3Full["Read fp32.pt, exp_avg.pt, and exp_avg_sq.pt. A missing file raises."]A
parameter_alignmentmismatch raises on every legacy ZeRO-1/2 shard load. Module-only never opens that shard, so it remains the warm start.AI assistance was used to implement and test this change. This does not duplicate an open DeepSpeed PR.
Test plan
tests/unit/checkpoint/test_fp32_master_only.py,tests/unit/checkpoint/test_zero_optimizer.py, andTestStage12ParamAlignmenton 4× NVIDIA H200 (driver 580.159.03), torch 2.9.1+cu130, Python 3.12.12, pytest 8.4.1, commit894a2b72: 99 passed. Legacy and universal loads keep the saved masters and drop moments. Module-only rebuilds ZeRO-2 masters from bf16. A missing legacy shard raisesZeRORuntimeException, and module-only still loads. A legacy DP change from 2 to 1 raises. A universal DP change keeps the masters. An elastic DP change repartitions the masters and drops moments. A parameter-alignment mismatch raises for a masters-only load and a full load, and the module-only warm start matches the saved parameters. The world-size-4 elastic DP cases and both ZeRO++ loads passed.TestUnevenVocabCrossTpResume::test_resume_from_tp2on the same machine: 2 passed.4 required, 1 available.load_module_only=Truewith NVMe optimizer offload. See Follow-ups for restoring fp32 masters without optimizer state #8890.Validated cells: ZeRO stages 1, 2, and the ZeRO-3 paths in
test_zero_optimizer.py; same DP and DP 2 to 1; legacy, elastic, and universal checkpoints; bf16; no offload. Open cells: fp16, MoE, NVMe offload.