Skip to content

Preserve non-floating buffers in ZeRO checkpoint conversion - #8515

Merged
sfc-gh-truwase merged 2 commits into
deepspeedai:masterfrom
aprylewu:fix/checkpoint-nonfloating-buffers
Oct 10, 2026
Merged

sfc-gh-truwase merged 2 commits into
deepspeedai:masterfrom
aprylewu:fix/checkpoint-nonfloating-buffers

Conversation

@aprylewu

@aprylewu aprylewu commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

ZeRO checkpoint reconstruction currently converts every persistent buffer to fp32. This silently changes integer values before load_state_dict() gets a chance to restore their dtype. For example, an int64 buffer containing 16777217 becomes 16777216; reloading a small model that uses those offsets changes its output from 3.98585796 to 1.99292898. The newer dtype-selectable exporter also casts all buffers while planning and materializing shards, so fixing reconstruction alone is insufficient.

Restrict precision conversion to floating-point tensors. Integer, boolean, and other non-floating buffers retain their dtype and values during reconstruction and export, while floating buffers still recover to fp32 and follow the requested export dtype. The same dtype selection is used for empty shard-planning tensors and materialized output, keeping shard sizes accurate. Shared tensor aliases remain preserved.

The regression tests save actual ZeRO-2 and ZeRO-3 checkpoints after a small SGD training step, reconstruct and export them, reload the model, and compare its output. They cover int64 offsets above the fp32 exact-integer range, boolean masks, fp16 source buffers, fp32/fp16/bf16 exports, and sharded-file metadata. The expected precision-converted model comes from PyTorch's Module.to(dtype).float() behavior.

The existing conversion integration test also now uses tmp_path: its previous tmpdir object returned a legacy py.path stat wrapper that raises AttributeError: st_st_size at the existing .stat().st_size assertions. I reproduced that failure with the unmodified converter before updating the fixture.

Validation on an Apple M3 Max, 48 GiB RAM, macOS 27, Python 3.12.9, PyTorch 2.8.0:

  • Entire tests/unit/checkpoint/test_convert_checkpoint.py: 17 passed. The new integration cases each execute with one and two real CPU/gloo ranks, for both ZeRO stages and all three output dtypes.
  • The same final tests with the original converter: 12 failed, 5 passed; failures are incorrect non-floating dtypes and changed model outputs.
  • All applicable pre-commit run --files deepspeed/utils/zero_to_fp32.py tests/unit/checkpoint/test_convert_checkpoint.py hooks passed; git diff --check passed.

No CUDA/GPU training, model-convergence run, or safetensors serialization test was performed. The tests use real DeepSpeed initialization, training, checkpoint saving, reconstruction, and PyTorch serialization; no model weights, external services, or compiled DeepSpeed kernels are needed for these CPU cases.

Developed with OpenAI Codex assistance and independently reviewed by another coding agent and myself manually. All reported checks were run locally.

Signed-off-by: Mingyang Wu <[email protected]>

Assisted-by: OpenAI Codex

@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: 1fa0ea4d3d

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

# recover just the buffers while restoring them to fp32 if they were saved in fp16
buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
buffers = {
k: v.float() if v.is_floating_point() else v

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 Add the required sign-off to this commit

This is a non-merge commit, but its commit message has no Signed-off-by trailer, so it does not satisfy the repository's mandatory commit policy. Recreate the commit with --signoff using the configured Git name and email.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

@aprylewu aprylewu Sep 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The current head (1fa0ea4) already includes a Signed-off-by trailer, and the DCO check is passing. Here are the commit and DCO result. This appears to be a false positive.

@aprylewu

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing and approving this!

@aprylewu

Copy link
Copy Markdown
Contributor Author

Since this is approved, mind merging when you get a chance? 🙂

@sfc-gh-truwase
sfc-gh-truwase added this pull request to the merge queue Oct 10, 2026
Merged via the queue into deepspeedai:master with commit a20acd1 Oct 10, 2026
15 checks passed
@aprylewu
aprylewu deleted the fix/checkpoint-nonfloating-buffers branch October 15, 2026 23:58
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Oct 28, 2026
… states (deepspeedai#8885)

## Summary

Describes, as affine maps in global ranks, where a live ZeRO engine
stores each element of a parameter and its optimizer states, so
`plan_transfer` can read from or write into training storage directly
(e.g. training → rollout weight sync) without gathering full parameters
first.

- `zero_affine.py`
- `zero_param_storage_affine_map`: low-precision replicas under
ZeRO-0/1/2 (DP or expert-DP group), `ds_tensor` partitions under ZeRO-3,
padding excluded.
- `zero_optimizer_storage_affine_maps`: fp32 master + optimizer states
(ZeRO-1/2, bf16 optimizer, ZeRO-3).
- `zero_param_storage_shard` / `zero_optimizer_storage_shard`: flat
views of ZeRO's own buffers matching the maps.
- `autotp_storage_affine_maps`: AutoTP layout × ZeRO storage, one
all-gather over the TP group.
- `affine.py`: `nest_affine_maps` composes a layout map (TP/EP shard
placement) with per-shard storage maps. A process's stored run of an N-D
shard is described as at most `2·ndim−1` N-D boxes in the parameter's
own shape, so plans stay small (row-parallel shards are not cut per
row).
- `affine_transfer.py`: `plan_transfer` accepts an N-D piece in a 1-D
(flat) shard only when the piece is dense there; every other rank
mismatch is still refused.
- `affine_ir_spec.md`: phase-2 section on layout vs storage maps.
- Fix `test_convert_checkpoint` `stat().size` → `st_size` (from deepspeedai#8515).

No change to ZeRO, AutoTP or the executor. Offloaded optimizer states
raise `NotImplementedError`; MiCS / hpZ are not covered.

## Tests

- `tests/unit/checkpoint/test_zero_affine.py`,
`test_affine_transfer_plan.py`: ZeRO-0/1/2/3 param + optimizer maps
checked against ZeRO's own buffers (bitwise rebuild), AutoTP nest, N-D
box count, flat-shard acceptance/refusal.
  - ≤2 ranks: Intel Arc B70 (XPU) ×2, pass. >2 ranks: CPU (gloo), pass.
- End-to-end (deepspeedai/DeepSpeedExamples#1021): Qwen3-8B, 8× NVIDIA
L20, AutoTP-2 training engine → AutoTP-4 rollout engine via
`autotp_storage_affine_maps` + `plan_transfer` + executor (deepspeedai#8809),
ZeRO-2 and ZeRO-3, weights bitwise equal after every sync. Plans are
built once and reused.

| Qwen3-8B, 8×L20 | ZeRO-2 gather | ZeRO-2 affine `lowest` | ZeRO-2
affine `balanced` | ZeRO-3 gather | ZeRO-3 affine |
|---|---|---|---|---|---|
| maps + plan (once) | - | 0.63 + 0.57 s | 0.70 + 0.70 s | - | 0.82 +
0.60 s |
| first sync | 1.01 s | 1.16 s | 0.48 s | 1.71 s | 1.18 s |
| repeated syncs | 0.78-0.82 s | 1.05-1.35 s | 0.49-0.55 s | 1.22-1.28 s
| 0.45-0.74 s |
| device memory added | 3.48 GB | 0.30 GB | 0.16 GB | 3.48 GB | 0.66 GB
|
| sent / local copy | - | 35.2 / 5.0 GB | 15.1 / 25.1 GB | - | 34.0 /
6.3 GB |
| executor rounds | - | 96 | 41 | - | 32 |

Each map has 3,192 pieces on both sides; before N-D boxes (one piece per
stored row) the same ZeRO-3 maps had 592k source / 2.36M target pieces
and planning took 207 s. Under ZeRO-2 every DP rank holds a replica:
`lowest` is the planner's default holder choice, `balanced` is a
`replica_selector` in the example (local copy first, else the
least-loaded holder).

---------

Signed-off-by: Jin, Youzhi <[email protected]>
Co-authored-by: Copilot <[email protected]>
@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 14, 2026
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.

2 participants