Skip to content

ZeRO-3 partial offload: fix LR schedules and checkpoints of the device sub-groups - #8973

Merged
xylian86 merged 7 commits into
deepspeedai:masterfrom
xylian86:xinyu/zero3-partial-offload-optimizer
Nov 20, 2026
Merged

xylian86 merged 7 commits into
deepspeedai:masterfrom
xylian86:xinyu/zero3-partial-offload-optimizer

Conversation

@xylian86

@xylian86 xylian86 commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

With offload_optimizer.ratio < 1, ZeRO-3 keeps some sub-groups on the device and steps them with a separate torch
optimizer (backup_optimizer). That optimizer has several bugs that change the training result without an error.
SuperOffload uses the same optimizer for its device sub-groups; the rest of SuperOffload is fixed in #8972.

Fixes

  1. The device sub-groups ignored LR schedules and set_lr(), and changes of betas, eps and weight_decay. Ina
    first step at lr 0, the weights moved by 3.0e-3. The backup optimizer now copies these values before each step.
  2. Checkpoints had no Adam state for the device sub-groups (a resume was 4.9e-3 off after one step). It is now saved
    with the state of the CPU sub-groups: a resume is bit-identical, and a checkpoint loads with another ratio.
  3. After a checkpoint load, every device step also updated the CPU sub-groups of param groups 1 and up, because their
    dicts were shared with the main optimizer (3.0e-3 extra per step at lr 3e-3). Each param group of the backup
    optimizer now has its own dict.
  4. safe_get_full_optimizer_state and safe_set_full_optimizer_state raised KeyError for the device sub-groups, and
    ds_to_universal raised KeyError: 0. Both work now.
  5. With adam_w_mode: false, the device sub-groups used decoupled weight decay (2.4e-2 off after 4 steps); they now use
    torch.optim.Adam (L2). bias_correction: false with ratio < 1 now raises a ValueError.

… sub-groups and checkpoint their Adam state

With offload_optimizer.ratio < 1, the sub-groups kept on the device are stepped by a separate torch optimizer.

- Give each of its param groups its own dict and copy lr, betas, eps and weight_decay from self.optimizer's group
  before each device step (ZeRO-3 and SuperOffload). LR schedules ignored the first group, and the shared dicts of the
  other groups kept every CPU partition after a checkpoint load, so device steps also updated CPU sub-groups.
- Save the device sub-groups' Adam state with the other sub-groups' in optimizer_state_dict['state'], and move it to
  the backup optimizer on load and on universal load, so that checkpoints do not depend on the ratio and
  ds_to_universal finds every sub-group.
- Read the state of a device sub-group from the backup optimizer in safe_get/set_full_optimizer_state.
- Use torch Adam (L2) when adam_w_mode is off, and reject bias_correction=False.

Signed-off-by: Xinyu Lian <lian7@illinois.edu>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 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-11-01T06:40:35.578678Z afded17 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: afded1772a

ℹ️ 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/zero/stage3.py Resolved
…ram dtype

torch Adam keeps the moments of the device sub-groups in the param dtype and fails on fp32 moments for bf16 params
("expected dtype float for `end`"). DeepSpeedCPUAdam keeps fp32 moments for bf16 params (fp32_optimizer_states), so a
load that keeps them in fp32 would pass fp32 moments to the backup optimizer. Cast them to the param dtype when they
move there. Test: resume with bf16 master weights at ratio 0.5.

Signed-off-by: Xinyu Lian <lian7@illinois.edu>
@tohtana tohtana added needs-gpu-test GPU testing is recommended to validate the changes in this PR. and removed needs-gpu-test GPU testing is recommended to validate the changes in this PR. labels Nov 10, 2026
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 17, 2026
…time (deepspeedai#8844)

# Universal Checkpointing: read AutoEP ZeRO-3 optimizer files one at a
time

## Problem

`ds_to_universal` converts a ZeRO-3 AutoEP checkpoint in two parts. The
dense parameters go through the usual
extract-and-merge path, which reads one optimizer file per worker and
writes fragments to `tmp/`. The routed experts
go through `_consolidate_zero3_autoep_expert_states`, which first loads
**every** rank's optimizer file into one dict
(`optim_states_by_rank`) and then keeps a copy of every expert fragment
in memory until all of them are merged.

Together those files hold every optimizer state of the model: fp32 (or
bf16) master weights plus both Adam moments.
For a large MoE model that is more than one host has:

| Model | Parameters | Optimizer state, fp32 master + fp32 Adam (12
B/param) | bf16 master + bf16 Adam (6 B/param) |
|---|---|---|---|
| MiniMax-M3 | 426.2B | 4.65 TiB | 2.33 TiB |
| GLM-5.2 | 743.4B | 8.11 TiB | 4.06 TiB |

The H200 nodes this was found on have 2 TiB of host memory, so the
conversion cannot finish for either model.

## Change

`_consolidate_zero3_autoep_expert_states` reads one rank's optimizer
file at a time:

- Each expert fragment it finds is written to
`<output>/tmp/autoep_expert_fragments/` (one file per parameter,
  optimizer state, EP rank and partition rank).
- Every reference into the rank's optimizer state, including the loop
variables, is dropped before the next file is
  read.
- The merge then loads, for one parameter and one state at a time, only
that parameter's fragments, and deletes each
fragment file once it is read, so the disk does not hold every fragment
and every universal file at the same time.

Peak host memory becomes one optimizer file plus one full expert tensor,
instead of all optimizer files.

The checks are unchanged: missing ranks, duplicate or missing
expert-data-parallel partitions, shape mismatches and
placement disagreements raise the same errors. The temporary fragments
live under `<output>/tmp`, which the converter
already uses for the dense fragments and removes at the end.

## Peak host memory

Measured with a probe on one checkpoint: a 4-layer mock MoE model (64
experts, hidden 1024, expert intermediate
4096: 3.22B expert parameters) trained one step and saved with ZeRO-3
and AutoEP EP 4 on 8 GPUs (bf16, Adam), giving
8 optimizer files of 36.19 GiB in total. Each version ran only
`_consolidate_zero3_autoep_expert_states`, in its own
process; peak resident memory is `ru_maxrss` (0.4 GiB of it is the
imports).

| | Peak host memory | Time | Output |
|---|---|---|---|
| master 3fe2d95 | 109.20 GiB | 93.6 s | |
| this change | 13.13 GiB | 203.6 s | all 36 universal files (fp32,
exp_avg, exp_avg_sq of each expert weight) bitwise equal to master's |

Master holds about 3 times the optimizer files at once; with this change
the peak is about one optimizer file plus one
parameter's tensors, and it does not grow with the number of ranks. The
cost is time: the fragments are written to
and read back from `<output>/tmp`, 2.2x slower here on a shared Lustre
file system. For MiniMax-M3 and GLM-5.2 the
old path cannot finish at all on a 2 TiB host.

Hardware: 1 node of 8 NVIDIA H100 80GB (driver 595.112.07), torch
2.8.0+cu126. Probe script:
`ucp_expert_memory_probe.py` (modes `save`, `convert`, `compare`).

## Tests

On master 3fe2d95 with this change, 1 node of 8 NVIDIA H100 80GB
(driver 595.112.07), torch 2.8.0+cu126, pytest 8.3.5:

| Test | Result |
|---|---|
| `tests/unit/checkpoint/test_autoep_affine_universal.py` (calls the
function directly) and `tests/unit/v1/moe/test_autoep_checkpoint.py`
(AutoEP ZeRO-3 save, convert, load at other layouts) | 69 cases: 67
passed, 2 skipped (ZeRO-1/2 universal-checkpoint cases that run only
with `DEEPSPEED_RUN_H100_TESTS=1`; this change is on the ZeRO-3 path) |

`pre-commit run --files` passes on the changed file.

## Duplicate check

Searched open pull requests on 2026-11-13 ("ds_to_universal", "universal
checkpoint AutoEP"). deepspeedai#9069 and deepspeedai#8973 change
how ZeRO-3 loads universal checkpoints and partial-offload checkpoints,
not `ds_to_universal.py`. deepspeedai#8848 (mine) also
edits `_consolidate_zero3_autoep_expert_states` (keys by tensor-parallel
and data-parallel rank); whichever of the two
merges second needs a small rebase.

## Compatibility

| Cell | Validated |
|---|---|
| ZeRO-3 + AutoEP checkpoint conversion, save / convert / load at other
layouts | yes (tests) |
| Dense (non-AutoEP) ZeRO-3 conversion | path unchanged |
| ZeRO-1 / ZeRO-2 conversion | path unchanged |

## AI assistance

This change, its tests and this description were prepared with AI
assistance (Claude).

---------

Signed-off-by: pengdurice <pengduhit@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Xinyu Lian <lian7@illinois.edu>
…eckpoint load; skip tests the device cannot run

A checkpoint saved with bias_correction=False restores it to self.optimizer's param groups, past the check in
__init__, so the CPU sub-groups would step without bias correction and the device ones (torch Adam) with it.
_sync_backup_optimizer_group now rejects it before a device sub-group steps.

With the CPU accelerator no sub-group is kept on another device, so test_first_step_at_zero_lr skips there.
The tests train in bf16, so the module skips on devices without it.

Signed-off-by: Xinyu Lian <lian7@illinois.edu>
@xylian86
xylian86 enabled auto-merge November 19, 2026 21:18
…t with the CPU accelerator

With the CPU accelerator no sub-group is kept on another device, so no step syncs the backup optimizer and the
test's pytest.raises fails; the failing DistributedTest worker then hung cpu-torch-latest until its 6-hour timeout.

Signed-off-by: Xinyu Lian <lian7@illinois.edu>
@xylian86
xylian86 added this pull request to the merge queue Nov 20, 2026
Merged via the queue into deepspeedai:master with commit 25eaac4 Nov 20, 2026
19 checks passed
@xylian86
xylian86 deleted the xinyu/zero3-partial-offload-optimizer branch November 20, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants