Repository navigation
Conversation
…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>
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: 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".
| for param in self._get_untrained_parameters(): | ||
| partition = param.ds_tensor |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
🟡 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
| self.untrained_lp_param_pin_buffers[param.ds_id] = get_accelerator().pin_memory( | ||
| torch.empty_like(partition, device=device)) |
|
@xylian86 thanks for the PR. Please address the feedback items |

Summary
ZeRO-3
offload_states()/reload_states(), which free device memory between training phases (e.g. for RL rollouts), have two bugs:offload_states()maps every module parameter ontolp_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 raiseRuntimeError: start (...) + length (...) exceeds dimension size (...).reload_states()already maps the buffer in optimizer order.offload_states(pin_memory=True), a lateroffload_states(pin_memory=False)puts the current values into new pageable copies, butreload_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:
offload_states()raises → it offloads every state, the frozen layer's partitions too; afterreload_states()every partition and the forward output are bit-identical to before.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.