Repository navigation
Conversation
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>
Codex Review Summary: ✅ CompletedThis 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: 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: |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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>
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.pyreturn a state dict that silently lacks every frozen weight.load_state_dicton it then fails with missing keys. Stage 2 and 3 are not affected.Cause
The engine only writes
frozen_param_shapes/frozen_param_fragmentswhenzero_optimization_partition_gradients()is true, so ZeRO-1 checkpoints have no frozen metadata.parse_model_statesonly 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_statestakes 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 (soexclude_frozen_parametersstill 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 checksexclude_frozen_parameters=Truedrops 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.test_convert_checkpoint.pywith the change: 12 passed, 7 skipped (the world-size-2 cases need 2 devices).yapf --diffandflake8on 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.