Skip to content

ZeRO-3 CPU offload: fix the step() device memory regression from #8840 - #8986

Closed
xylian86 wants to merge 2 commits into
deepspeedai:masterfrom
xylian86:xinyu/zero3-master-copy-pieces
Closed

xylian86 wants to merge 2 commits into
deepspeedai:masterfrom
xylian86:xinyu/zero3-master-copy-pieces

Conversation

@xylian86

@xylian86 xylian86 commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

#8840 copies each pinned CPU master partition to the device in float32 and casts it there. It moves the whole sub-group at once, so with offload_optimizer.pin_memory: true (the default) step() needs a float32 device temporary of a whole sub-group: up to 3.73 GiB at the default sub_group_size of 1e9. The description of #8840 says the copy goes in pieces of 2**26 elements (0.25 GiB); the merged code does not. No release has #8840 yet.

This PR copies in pieces of 2**26 elements, in order on one stream, so each piece reuses the memory of the previous one. ZeRO-1/2 cast on the host into a pinned 16-bit buffer and have no device temporary; SuperOffload and ZenFlow's overlap step copy the masters their own way.

No open issue or PR covers this.

Fixes

Before → after, 1 GH200, bf16, ZeRO-3 with offload_optimizer: {device: cpu, pin_memory: true}, base 1a956270f. Device memory that step() takes on top of what is allocated before it:

  1. 1.0B parameters, default sub_group_size (one sub-group of 1007M elements): 3.75 → 0.25 GiB.
  2. 134M parameters: 0.50 → 0.25 GiB.

The step time does not change (1.0B: 0.178 → 0.178 s; copy-back 12.7 → 12.8 ms).

deepspeedai#8840 copies each pinned CPU master partition to the device in float32
and casts it there. It moved the whole sub-group at once, so step()
needed a float32 device temporary of the whole sub-group: up to 3.73
GiB at the default sub_group_size of 1e9. Its description says that the
copy goes in pieces of 2**26 elements (0.25 GiB).

The copy now goes in pieces of 2**26 elements, in order on one stream,
so each piece reuses the memory of the previous one.

Signed-off-by: Xinyu Lian <lian7@illinois.edu>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T20:58:59.536705Z 8e6295a 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.

@0z5a 0z5a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Read at 8e6295a1. This looks right to me: the pieces are views of the pinned buffer, so non_blocking still applies, and since they run in order on one stream, the allocator can reuse each piece's temporary for the next one.

Your test skips on the CPU accelerator, so I checked the chunking directly. I called _copy_pinned_master_to_device with PINNED_MASTER_COPY_NUMEL patched to 1000, a no-op event (the CPU accelerator has none) and an fp32 source of 0, 1, 999, 1000, 2000 and 2500 elements: the bf16 destination equals src.to(torch.bfloat16) every time, tails and the empty case included. I can't check the device-memory numbers here. A small test along those lines, with the constant patched down, would let the CPU job cover the tail logic too.

@pengdurice

pengdurice commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

thank you for contributing this. the #8840 assumes OOM not an issue and the chunked buffer idea is dropped. My bad of missing updating the PR description accordingly.

@pengdurice

Copy link
Copy Markdown
Contributor

LGTM, @sfc-gh-truwase , would you mind taking a look as well? ;--)

@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator

@xylian86 my concern with @pengdurice's original code is that the OOM risk is minimal to justify the extra complexity of a chunked approach. Did you encounter an OOM issue?

@xylian86

xylian86 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

@sfc-gh-truwase No, not in a real run.

  • You're right that it does not raise the allocated peak: forward/backward sets it.
  • The temporary is a whole sub-group in float32: up to 3.7 GiB per rank at the default sub_group_size.
  • But the allocator keeps it cached, so near the limit the forward/backward after step() OOMs from fragmentation.

So this PR can still let a slightly larger model fit, but I'm also fine with closing it.

@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator

@xylian86 thanks for confirming the details. I think we can close for now as there are multiple efforts to improve offloading performance :). I am very delighted by the renewed focus on offloading.

@tohtana tohtana added needs-gpu-test GPU testing is recommended to validate the changes in this PR. and removed needs-gpu-test GPU testing is recommended to validate the changes in this PR. labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants