Skip to content

Recover frozen parameters in zero_to_fp32 for ZeRO-1 checkpoints - #9138

Open
sohailshk wants to merge 2 commits into
deepspeedai:masterfrom
sohailshk:fix/zero-to-fp32-stage1-frozen
Open

sohailshk wants to merge 2 commits into
deepspeedai:masterfrom
sohailshk:fix/zero-to-fp32-stage1-frozen

Conversation

@sohailshk

Copy link
Copy Markdown

Fixes #5439

What breaks

With ZeRO stage 1 and any parameter that has requires_grad=False (LoRA/adapters, frozen embeddings), get_fp32_state_dict_from_zero_checkpoint / zero_to_fp32.py return a state dict that silently lacks every frozen weight. load_state_dict on it then fails with missing keys. Stage 2 and 3 are not affected.

Cause

The engine only writes frozen_param_shapes/frozen_param_fragments when zero_optimization_partition_gradients() is true, so ZeRO-1 checkpoints have no frozen metadata. parse_model_states only recovers buffers from the stored module state, and the optimizer shards hold only trainable parameters.

Fix

ZeRO-1 does not partition parameters, so the frozen weights are stored whole in the module state of the model-states file. When no frozen metadata was saved, parse_model_states takes the non-empty tensors in that module state that are not buffers, not optimizer-owned parameters and not tied aliases, and feeds them through the existing frozen-parameter path (so exclude_frozen_parameters still works). Empty tensors are skipped because ZeRO-3 module states only hold placeholders; ZeRO-3 checkpoints without frozen metadata (written before #3205) therefore convert exactly as before.

I fixed the converter rather than the engine condition (which came in with #3205 without a recorded reason) because this also recovers ZeRO-1 checkpoints that already exist, and saving frozen fragments in ZeRO-1 would store a second copy of every frozen weight next to the module state. The checkpoint format and the engine are unchanged.

Test

TestCheckpointConvertFrozenParams::test_frozen_params_are_recovered[1|2] (stage 1 and 2): trains one step, saves, converts, compares every key and value with the module state dict, and checks exclude_frozen_parameters=True drops only the frozen keys.

Commands (torch 2.15.0 CPU, 1 process, DS_ACCELERATOR=cpu DS_BUILD_OPS=0):

  • pytest tests/unit/checkpoint/test_convert_checkpoint.py -k FrozenParams: master 1 failed (stage 1) / 1 passed; with the change 2 passed.
  • whole test_convert_checkpoint.py with the change: 12 passed, 7 skipped (the world-size-2 cases need 2 devices).
  • yapf --diff and flake8 on both touched files: clean.

Not duplicating

Open PRs editing zero_to_fp32.py (#6655, #8376, #8378, #8530, #8681, #8901, #8954, #9084) don't touch frozen-parameter recovery; #5439 has no linked PR.

Compatibility matrix

Validated: ZeRO-1 and ZeRO-2, fp32, 1 rank, CPU. Not validated: ZeRO-3, multi-rank, bf16/fp16 on accelerators, offload, MoE/pipeline checkpoints. Checkpoints that carry frozen_param_shapes (ZeRO-2/3 saved by current versions) take the unchanged path.

The implementation and tests were written with AI assistance.

ZeRO-1 checkpoints do not record frozen parameters separately (that is
only done when gradients are partitioned), so the fp32 state dict built
from them silently lacked every frozen weight. Parameters are not
partitioned in ZeRO-1, so they are stored whole in the module state;
take them from there when no frozen metadata was saved.

Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T20:33:31.030102Z 6604093 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.

@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 20, 2026

@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: 6604093ba7

ℹ️ 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".

# handle shared params
shared_params = [[k, v] for k, v in state_dict["shared_params"].items()]

if frozen_param_shapes is None:

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 Limit fallback recovery to ZeRO-1

When converting a ZeRO-3 checkpoint whose frozen metadata is None (for example, one saved with exclude_frozen_parameters=True or an older pre-metadata checkpoint), this branch also runs because the stage has not been checked yet. A non-buffer tensor emitted by a custom module state_dict is then treated as a full frozen fragment, but _zero3_merge_frozen_params assumes flattened per-rank shards; for a normal 2-D tensor its narrow(0, ..., shape.numel()) either exceeds dimension 0 or produces the wrong element count for view(shape), so a checkpoint that previously converted now raises. Restrict this inference to ZeRO-1, or pass the optimizer stage into model-state parsing.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This was a real problem. It reproduces with a hand-built ZeRO-3-style checkpoint (one rank, frozen_param_shapes and frozen_param_fragments both None, and a non-buffer 2x3 tensor named extra_state next to an empty-placeholder weight in the module state). With the first version of this PR, get_fp32_state_dict_from_zero_checkpoint fails in _zero3_merge_frozen_params with start (0) + length (6) exceeds dimension size (2). On master the same checkpoint converts, and extra_state is simply left out.

The stage is only known after the optimizer states are parsed, so the fix is there rather than in parse_model_states. Frozen parameters inferred from the module state are now marked as inferred, and _get_fp32_state_dict_from_zero_checkpoint drops them when the optimizer states report ZeRO-3. The ZeRO-3 path then behaves as on master. ZeRO-1/2 and ZeRO-3 checkpoints that carry real frozen metadata are unchanged.

I added test_zero3_extra_module_tensor_is_not_treated_as_frozen_param with that hand-built checkpoint.

Checks run (CPU, DS_ACCELERATOR=cpu, pytest tests/unit/checkpoint/test_convert_checkpoint.py): the new test fails without the fix (1 failed, the error above) and passes with it; the whole file gives 13 passed, 7 skipped.

The module-state fallback for frozen parameters assumes whole tensors, which
only holds for unpartitioned ZeRO-1/2 module states. A ZeRO-3 model state
without frozen metadata (exclude_frozen_parameters=True, or an older
checkpoint) can still carry other non-buffer tensors, for example from a
custom state_dict. Those were read as flattened per-rank frozen fragments and
made the conversion raise. Drop the inferred frozen parameters once the
optimizer states report ZeRO-3.

Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 21, 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

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.

Why not save frozen params unless: self.zero_optimization_stage() >= ZeroStageEnum.gradients?

1 participant