Repository navigation
ZeRO-3: reuse the weight partition as the master copy when it already has the master dtype - #8842
Draft
pengdurice wants to merge 1 commit into
Draft
pengdurice wants to merge 1 commit into
pengdurice wants to merge 1 commit into
Conversation
… has the master dtype Signed-off-by: pengdurice <pengduhit@gmail.com>
This branch has not been deployed
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.
ZeRO-3: reuse the weight partition as the master copy when it already has the master dtype
Summary
DeepSpeedZeroOptimizer_Stage3._create_fp32_partitionsreuses a sub-group's weight partition as its master copy whenthe two would be identical (same dtype, on the device): under torch autocast with an fp32 model, and with bf16 master
weights. The check compares
tensor.device == self.device, whereself.deviceis a string such as"cuda:0"(
get_accelerator().current_device_name()). Atorch.devicenever equals a string, so the branch never runs andZeRO-3 keeps a second copy of every partition: +4 bytes per parameter for an fp32 model under autocast, +2 with bf16
master weights. The fix compares with
torch.device(self.device).Duplicate check
Searched open and recent pull requests on 2026-10-15 ("master weights", "fp32 partition", "autocast", "stage3
device"): none touches this comparison.
The change
deepspeed/runtime/zero/stage3.py, one line:The branch it enables already exists (the code comment above it explains it); offload, NVMe and the other
branches are unchanged.
Measured
MiniMax-M3 built from its config (random weights), ZeRO-3, bf16 weights with bf16 master weights and Adam states, no
CPU offload. The fix was tested in a DeepSpeed study tree before this branch existed.
Runs with
"bf16": {"enabled": true}and fp32 master weights are not affected: there the partitions are bf16 andthe master copy fp32, so the branch does not apply with or without the fix.
Tests
TestZero3AutocastSharesMasterWeights(2 ranks,tests/unit/v1/zero/test_zero.py): torch autocast (bf16) without abf16 / fp16 section, so the model weights stay fp32; ZeRO-3 without offload. Every master partition must be the
weight partition itself (
master is model_partition), then 2 training steps run.Hardware: 1 node of 8 H200 (driver 580.126.09), torch 2.8.0+cu126, pytest 9.1.1.
pre-commit run --filespasses on both changed files.Compatibility
torch.device(name)accepts every accelerator's device name; not runAI assistance
This change, its test and this description were prepared with AI assistance (Claude).