Skip to content

ZeRO-3: honor stage3_max_reuse_distance for forward-gathered parameters - #9003

Open
YidaWeng wants to merge 4 commits into
deepspeedai:masterfrom
YidaWeng:fix/zero3-forward-param-release
Open

YidaWeng wants to merge 4 commits into
deepspeedai:masterfrom
YidaWeng:fix/zero3-forward-param-release

Conversation

@YidaWeng

@YidaWeng YidaWeng commented Nov 4, 2026 •

Copy link
Copy Markdown

Summary

Forward-gathered parameters were not released when stage3_max_reuse_distance was set to a small value. The prefetcher skips parameters that are already available and records the step at which they will be needed next; the release path then retained those parameters until that step regardless of the configured reuse distance. Because the prefetcher can scan far enough ahead to see the backward reuse of parameters gathered in the forward pass, the parameters stayed resident until backward and accumulated across layers (see #7670).

The reuse-distance scan already keeps parameters that are reused within the configured window and releases the rest, so the extra prefetch-skip retention is removed in favor of that scan for non-offloaded parameters. stage3_max_reuse_distance: 0 now releases parameters as soon as each module's forward completes and re-gathers them during backward, trading communication for lower forward memory. The trace scan also uses each future module's own leaf-module flag when collecting its parameters.

The prefetch-skip retention is deliberately kept for NVMe-offloaded parameters: releasing one also returns its swap-in buffer, and a later on-demand fetch can then fail with no available buffer. This preserves the previous NVMe queue/buffer ordering while keeping aggressive release for parameters that stay on the accelerator.

The default (1e9) behavior is unchanged: parameters are still kept resident through backward.

Testing

  • New tests/unit/runtime/zero/test_zero3_forward_parameter_release.py covers aggressive release with prefetch enabled (fails before this change), default retention, release with prefetch disabled, and an NVMe-offload training regression test (skipped when async-io is unavailable).
  • Existing ZeRO-3 tests such as test_zero_leaf_module.py, test_zero_allreduce_fetch_params.py, test_zero_context.py, and test_zeropp.py pass on CPU.
  • Manually validated with torchrun --nproc_per_node=2.
  • pre-commit passes (flake8 run separately under Python 3.12).

Fixes #7670

The parameter coordinator scans ahead during prefetch and records the
step at which each parameter is needed. Parameters that are already
available are skipped by the prefetch, and the release path retained them
until that recorded step regardless of stage3_max_reuse_distance. Since
the prefetcher can scan far enough ahead to see the backward reuse of the
parameters gathered in the forward pass, every forward-gathered parameter
stayed resident until backward even when the user configured an
aggressive release distance.

Rely on the reuse-distance scan to decide which parameters to retain. It
already keeps parameters reused within the configured window and releases
the rest, so setting stage3_max_reuse_distance to 0 now releases gathered
parameters as soon as each module's forward completes and re-gathers them
during backward. Also use the future module's own leaf-module flag while
scanning the trace so that parameters of leaf modules are counted
correctly.

Add regression tests covering aggressive release with prefetch enabled,
the default retention behavior, and release without prefetching, and
document the memory/communication tradeoff of a zero reuse distance.

Signed-off-by: YidaWeng <84164537+YidaWeng@users.noreply.github.com>

@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: d21ce033e0

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

@chatgpt-codex-connector

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-04T03:56:52.193705Z d21ce03 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.

The release path now honors stage3_max_reuse_distance for forward-gathered
parameters. NVMe parameters need the previous skip-based retention because
releasing one also returns its swap-in buffer, and a later on-demand fetch
can then fail with no buffer available. Retain skipped NVMe params while
leaving the aggressive release behavior for non-offloaded parameters, and
add an NVMe offload regression test.

Signed-off-by: YidaWeng <84164537+YidaWeng@users.noreply.github.com>
@gpu-test-labeler gpu-test-labeler Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026

This branch has not been deployed

No deployments
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.

[BUG] ZeRO-3 with HuggingFace Transformers: Gathered Parameters in Forward Pass Not Released Promptly

2 participants