Skip to content

Fix oversized zero_to_fp32 shards for ZeRO-1/2 checkpoints - #9040

Closed
Hang-get wants to merge 1 commit into
deepspeedai:masterfrom
Hang-get:fix-zero-to-fp32-shard-storage
Closed

Hang-get wants to merge 1 commit into
deepspeedai:masterfrom
Hang-get:fix-zero-to-fp32-shard-storage

Conversation

@Hang-get

@Hang-get Hang-get commented Nov 9, 2026

Copy link
Copy Markdown

For ZeRO-1/2 checkpoints, a sharded fp32 export saved with torch.save writes the whole fp32 parameter buffer into every shard file. This is what python zero_to_fp32.py <checkpoint> <output> does by default once the fp32 weights exceed the 5 GB shard size (roughly 1.25B parameters), and zero_to_torch.py --dtype fp32 takes the same path. Since every shard holds the whole buffer, a 7B model exported with the default settings would take about 6 × 28 GB of disk instead of 28 GB.

Cause

_zero2_merge_trainable_params concatenates the rank partitions of each parameter group into one fp32 vector and returns every parameter as a narrow().view() of it. to_torch_tensor keeps those views (contiguous() and .to(torch.float32) return the same tensor), and torch.save serializes the entire storage behind a view, not only the viewed elements. Each shard file therefore contains the complete flat buffer of every parameter group it touches. Loading still returns the right values, so the problem only shows up as output size and export time.

Change

When the export is sharded and saved with torch.save, tensors whose storage is larger than the tensor itself are cloned before saving. Tied parameters still share one copy. Single-file exports are unchanged (the buffer is written once there), and safetensors (which writes only each tensor's bytes) and fp16/bf16 exports (the cast already creates new tensors) were already correct and are not touched.

The cost is up to one shard of extra memory while that shard is written; the existing code already holds the full fp32 vector. Building ZeRO-1/2 parameters lazily, as the ZeRO-3 path does, would also avoid that full vector, but it is a larger change to the reconstruction code, so this PR keeps to the fix.

Measurements

CPU only: Intel Xeon Platinum 8352Y (one host, 128 cores), no GPU, Python 3.11.18, torch 2.10.0+cpu, DeepSpeed master at a4c989b with the CPU accelerator (gloo).

A ZeRO-2 checkpoint of an 18-layer nn.Linear(4096, 4096) model (302M parameters, 1.21 GB in fp32, world size 1) was exported with the zero_to_fp32.py CLI and --max_shard_size 250MB, which gives six shards, the same count a 7B model gets with the default 5 GB. Three runs each, a fresh process per run:

shard files output size wall time (median of 3) peak RSS
master 6 7.25 GB 11.0 s 2.53 GiB
this PR 6 1.21 GB 5.6 s 2.72 GiB

All 36 tensors in the two exports are bitwise identical.

Smaller checks: ZeRO-1 and ZeRO-2 checkpoints of an 8 × Linear(768, 768) model exported with 2 MB shards (nine files) took 170.1 MB on master for an 18.9 MB model and take 18.9 MB with this change. On master, the same checkpoint exported to safetensors or to bf16 was already the right size. A ZeRO-2 checkpoint with tied weights (the ModelWithSharedWeights layout from the test file, 100 KB shards) goes from 808,632 to 406,891 bytes, and the tied pair still loads as a single tensor.

Tests

The new TestCheckpointConvertShardSize saves ZeRO-1 and ZeRO-2 checkpoints, exports them with 20 KB shards, and checks that the shard files add up to less than 1.5× the index's total_size and that every tensor matches get_fp32_state_dict_from_zero_checkpoint. On master both cases fail at the size assertion; with this change they pass.

cd tests
pytest --forked unit/checkpoint/test_convert_checkpoint.py
# 19 passed
pytest --forked unit/checkpoint/test_shared_weights.py unit/utils/test_get_optim_files.py unit/runtime/zero/test_zero_numel_param_shape.py
# 19 passed, 3 skipped (fp16 cases on CPU)
pre-commit run --files deepspeed/utils/zero_to_fp32.py tests/unit/checkpoint/test_convert_checkpoint.py
# passed

These ran on the CPU environment above with pytest 8.3.5 (the range pinned in requirements-dev.txt). The existing TestCheckpointConvertBuffers already exercises the sharded pickle path for ZeRO-2 with world sizes 1 and 2 and still passes.

Coverage

  • ZeRO stages 1 and 2 are affected; both were reproduced and are tested. The ZeRO-3 export was checked: each parameter already has its own storage, and the output size is unchanged.
  • Only fp32 export is affected; fp16/bf16 exports were checked and are unchanged. The checkpoints came from fp32 runs on the CPU accelerator. Checkpoints from fp16/bf16 training, offload, and pipeline or tensor parallelism were not tested.
  • No GPU was available. The change only touches the offline converter, which runs on the CPU.

Duplicate check

I searched open and closed issues and PRs for zero_to_fp32 together with shard, size, storage, disk, view and max_shard_size, and found no report of this. The open PRs that touch zero_to_fp32.py deal with other problems: #8376 (tied weights with safetensors), #8378 (frozen parameter dtype), #8681 (bf16 stage-0 checkpoints), #8901 (legacy serialization) and #8954 (stale files in the output directory). ds_to_universal.py already clones narrowed slices before saving them, so it does not have this problem.

AI assistance (Cursor) was used for this change.

For ZeRO-1/2 checkpoints, zero_to_fp32 rebuilds each parameter as a view
into one concatenated fp32 buffer per parameter group. When the export is
sharded and written with torch.save (the default for zero_to_fp32.py and
for zero_to_torch.py --dtype fp32), torch.save stores the whole storage
behind every view, so each shard file contains the entire flat buffer. A
six-shard export of a 1.2 GB model wrote 7.2 GB.

Before torch.save, copy tensors that view a larger storage when the export
is sharded. Tied parameters keep sharing one copy. Single-file exports are
unchanged, and safetensors and fp16/bf16 exports were already correct.

AI assistance (Cursor) was used for this change.

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: YangHaoooooo <233765393+Hang-get@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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-09T06:34:11.366266Z b5474a7 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 14, 2026
@Hang-get Hang-get closed this Nov 19, 2026
@Hang-get
Hang-get deleted the fix-zero-to-fp32-shard-storage branch November 19, 2026 08:37
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.

1 participant