Skip to content

Keep fp32 masters on ZeRO-1 and ZeRO-2 when a resume skips optimizer state - #8891

Merged
sfc-gh-truwase merged 7 commits into
deepspeedai:masterfrom
sfc-gh-truwase:load-fp32-masters-without-optimizer-state
Oct 27, 2026
Merged

sfc-gh-truwase merged 7 commits into
deepspeedai:masterfrom
sfc-gh-truwase:load-fp32-masters-without-optimizer-state

Conversation

@sfc-gh-truwase

@sfc-gh-truwase sfc-gh-truwase commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • load_optimizer_states=False used to skip the ZeRO shard, so a fresh optimizer rebuilt the fp32 master weights from the bf16/fp16 parameters and dropped the precision saved in SINGLE_PARTITION_OF_FP32_GROUPS or universal fp32.pt.
  • DeepSpeedEngine.load_checkpoint no longer accepts load_fp32_weights. zero_optimization.load_from_fp32_weights is deprecated and ignored. A full ZeRO load always restores the saved fp32 masters. load_optimizer_states=False with load_module_only=False restores those masters and starts Adam with fresh moments. load_module_only=True rebuilds ZeRO-1 and ZeRO-2 masters from the loaded model weights. Universal module-only loads read those model weights from fp32.pt first. ZeRO-3 still reads its fp32 partition, because the model checkpoint does not hold the partitioned weights.
  • A data-parallel size change that requests optimizer state, and a rigid ZeRO-1/2 checkpoint, raise 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.
  • A legacy checkpoint with no *_optim_states.pt files, and a universal folder with no fp32.pt, raise ZeRORuntimeException on any load that needs the masters. ZeRO-1/2 load_module_only=True still succeeds without the shard. A parameter_alignment mismatch raises on a full load and on a masters-only load. Module-only remains the warm start for that mismatch.
  • Pytest 8 registers each DistributedFixture subclass, so a test skips when the machine has fewer GPUs than the fixture world size.
  • Further ZeRO-3 and dedicated ZeRO-1 BF16 work is in Restore fp32 masters for ZeRO-3 and the ZeRO-1 BF16 optimizer #8892, stacked on this branch. Follow-ups that do not fail the current unit tests, including ZeRO-3 NVMe 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_weights is ignored. When a universal load has load_optimizer_states=False, load_hp_checkpoint_state_from_checkpoint_dir reads fp32.pt and returns. It does not open optimizer_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."]
Loading

A parameter_alignment mismatch 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, and TestStage12ParamAlignment on 4× NVIDIA H200 (driver 580.159.03), torch 2.9.1+cu130, Python 3.12.12, pytest 8.4.1, commit 894a2b72: 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 raises ZeRORuntimeException, 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_tp2 on the same machine: 2 passed.
  • The eight elastic DP cases and both ZeRO++ cases skip when one device is visible: 4 required, 1 available.
  • Still open: fp16, MoE, and NVMe offload, including ZeRO-3 load_module_only=True with 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T21:29:21.730043Z 945aa1c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread deepspeed/runtime/engine.py Outdated
"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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread deepspeed/runtime/engine.py Outdated
# 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment thread deepspeed/runtime/zero/stage3.py Outdated
Comment on lines +3588 to +3589
if not load_from_fp32_weights:
self.refresh_fp32_params()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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]>
@sfc-gh-truwase
sfc-gh-truwase force-pushed the load-fp32-masters-without-optimizer-state branch from 945aa1c to f9958d4 Compare October 22, 2026 21:38
@sfc-gh-truwase
sfc-gh-truwase requested a review from jeffra October 22, 2026 21:39
@sfc-gh-truwase sfc-gh-truwase changed the title Keep fp32 masters when a resume skips optimizer state Keep fp32 masters on ZeRO-1 and ZeRO-2 when a resume skips optimizer state Oct 22, 2026
@sfc-gh-truwase
sfc-gh-truwase requested a review from stas00 October 22, 2026 21:54
Comment thread deepspeed/runtime/zero/config.py Outdated
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does "one load" mean?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't understand this sentence: None uses this value.

Comment thread deepspeed/runtime/base_optimizer.py Outdated
Comment on lines +368 to +376
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

@stas00 stas00 Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

sfc-gh-truwase and others added 2 commits October 23, 2026 14:11
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]>
sfc-gh-truwase and others added 2 commits October 24, 2026 13:47
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]>
@sfc-gh-truwase
sfc-gh-truwase added this pull request to the merge queue Oct 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 26, 2026
@sfc-gh-truwase
sfc-gh-truwase added this pull request to the merge queue Oct 27, 2026
Merged via the queue into deepspeedai:master with commit 2dd22dc Oct 27, 2026
15 checks passed
@sfc-gh-truwase
sfc-gh-truwase deleted the load-fp32-masters-without-optimizer-state branch October 27, 2026 14:52
Levius-Fubuki added a commit to Levius-Fubuki/DeepSpeed that referenced this pull request Oct 29, 2026
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]>
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 6, 2026
…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]>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants