Skip to content

ZeRO-3: fix offload_states() with frozen parameters and stale states after reload_states() - #9015

Open
xylian86 wants to merge 1 commit into
deepspeedai:masterfrom
xylian86:xinyu/zero3-offload-states-frozen-params
Open

xylian86 wants to merge 1 commit into
deepspeedai:masterfrom
xylian86:xinyu/zero3-offload-states-frozen-params

Conversation

@xylian86

@xylian86 xylian86 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

ZeRO-3 offload_states() / reload_states(), which free device memory between training phases (e.g. for RL rollouts), have two bugs:

  1. offload_states() maps every module parameter onto lp_param_buffer, in module order. That buffer holds only the partitions of the parameters the optimizer trains, in optimizer order, so any frozen parameter makes it raise RuntimeError: start (...) + length (...) exceeds dimension size (...). reload_states() already maps the buffer in optimizer order.
  2. After offload_states(pin_memory=True), a later offload_states(pin_memory=False) puts the current values into new pageable copies, but reload_states() still restores the fp32 masters, bf16 partitions and bf16 gradient buffer from the pinned buffers of the first call. Training then silently goes on from the older values.

Fixes

Before → after, 2 GH200, ZeRO-3, bf16, FusedAdam:

  1. A frozen layer: offload_states() raises → it offloads every state, the frozen layer's partitions too; after reload_states() every partition and the forward output are bit-identical to before.
  2. offload_states(pin_memory=True), a training step, offload_states(pin_memory=False), reload_states(): the fp32 masters and bf16 parameters were back at the step before (Adam, lr 1e-2: up to 1.0e-2 off) → bit-identical.

…an unpinned offload

offload_states() mapped every module parameter onto lp_param_buffer in module
order, but the buffer holds only the partitions of the parameters the
optimizer trains, in optimizer order. Any frozen parameter made it raise
"start + length exceeds dimension size". It now maps the buffer in optimizer
order, as reload_states() does, and moves the partitions of the other
parameters on their own.

reload_states() restored the fp32 master partitions, the bf16 partitions and
the bf16 gradient buffer from the pinned buffers of an earlier offload with
pin_memory=True, also after an offload with pin_memory=False had put the
current values elsewhere, so training went on from older values. It now
reloads each state from where the last offload put it, as ZeRO-1/2 does.

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

chatgpt-codex-connector Bot commented Nov 4, 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-04T20:21:42.372432Z 9ee4c17 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: 9ee4c17018

ℹ️ 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 on lines +3949 to +3950
for param in self._get_untrained_parameters():
partition = param.ds_tensor

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 Offload all frozen low-precision storage

When frozen ZeRO-3 weights use zero_hpz_partition_size > 1 or zero_quantized_nontrainable_weights, moving only param.ds_tensor leaves param.ds_secondary_tensor and/or param.ds_tensor.ds_quant_scale on the accelerator. Frozen hpZ secondary tensors deliberately persist (only trained fp16_groups are invalidated) and can be larger than the primary shard, so offload_states(include=[lp_params]) returns while significant model state is still resident and memory-sensitive callers can still OOM; these auxiliary tensors need to be moved or invalidated on offload and restored or recreated on reload.

AGENTS.md reference: AGENTS.md:L38-L38

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The offload path unnecessarily allocates and copies a full CPU partition before overwriting it, risking host-memory exhaustion for large frozen layers; the destination should be allocated directly with pin_empty_like().

1 open issue
What changed in this PR

This PR fixes ZeRO-3 state offloading between training phases, handling frozen parameters and preventing stale values from being restored when switching from pinned to unpinned offloads.

Changes:

  • Map trained parameter partitions in optimizer order and offload untrained partitions separately.
  • Reload states from their current storage rather than previously cached pinned buffers.
  • Add two-rank regression tests for frozen parameters and pinned-to-unpinned offload cycles.
File Description
tests/​unit/​v1/​zero/​test_offload_states.py Adds regression tests for partition restoration and stale-state prevention.
deepspeed/​runtime/​zero/​stage3.py Corrects partition mapping, handles untrained partitions, and reloads current state values.

Review effort: Balanced

Comment on lines +3953 to +3954
self.untrained_lp_param_pin_buffers[param.ds_id] = get_accelerator().pin_memory(
torch.empty_like(partition, device=device))
@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator

@xylian86 thanks for the PR. Please address the feedback items

@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

This branch has not been deployed

No deployments
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.

4 participants