Repository navigation
Conversation
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>
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For ZeRO-1/2 checkpoints, a sharded fp32 export saved with
torch.savewrites the whole fp32 parameter buffer into every shard file. This is whatpython zero_to_fp32.py <checkpoint> <output>does by default once the fp32 weights exceed the 5 GB shard size (roughly 1.25B parameters), andzero_to_torch.py --dtype fp32takes 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_paramsconcatenates the rank partitions of each parameter group into one fp32 vector and returns every parameter as anarrow().view()of it.to_torch_tensorkeeps those views (contiguous()and.to(torch.float32)return the same tensor), andtorch.saveserializes 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 thezero_to_fp32.pyCLI 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: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 (theModelWithSharedWeightslayout 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
TestCheckpointConvertShardSizesaves 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'stotal_sizeand that every tensor matchesget_fp32_state_dict_from_zero_checkpoint. On master both cases fail at the size assertion; with this change they pass.These ran on the CPU environment above with pytest 8.3.5 (the range pinned in requirements-dev.txt). The existing
TestCheckpointConvertBuffersalready exercises the sharded pickle path for ZeRO-2 with world sizes 1 and 2 and still passes.Coverage
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.pydeal 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.pyalready clones narrowed slices before saving them, so it does not have this problem.AI assistance (Cursor) was used for this change.