Skip to content

Reuse existing hpZeRO group in nested zero.Init contexts - #8361

Open
zupengwang wants to merge 1 commit into
deepspeedai:masterfrom
zupengwang:fix/nested-zero-init-hpz
Open

Reuse existing hpZeRO group in nested zero.Init contexts#8361
zupengwang wants to merge 1 commit into
deepspeedai:masterfrom
zupengwang:fix/nested-zero-init-hpz

Conversation

@zupengwang

Copy link
Copy Markdown
Contributor

Reuse existing hpZeRO groups in nested zero.Init contexts

Problem

Nested deepspeed.zero.Init contexts fail when zero_hpz_partition_size > 1 because each context unconditionally calls _create_zero_param_parallel_group(). The outer context has already initialized the process-wide HPZ group, so the inner context raises:

AssertionError: ZeRO parameter intra parallel group is already initialized

This occurs in real model composition flows where an outer model is constructed under ZeRO-3 and a nested from_pretrained() call enters another zero.Init context. It is reported in #4901 and reproduced again in #7066.

Fix

  • Reuse the existing ZeRO parameter parallel group when its effective size matches the requested HPZ partition size.
  • Create the group only when no group exists yet.
  • Fail early with an actionable RuntimeError when nested contexts request incompatible HPZ sizes instead of silently attaching the wrong group.

The change is local to zero.Init; it does not change public APIs or the non-HPZ path.

Test

Added a two-rank regression that nests two zero.Init contexts with ZeRO-3 + HPZ, constructs a parameterized module, and checks that the parameter is partitioned with the expected HPZ group.

Validation on a single-node RTX 3090 host, based on 87d9ecd8e0a4fd7778a58ac0f69cc85951f78ea0:

  • Baseline 2-GPU reproduction: fails on both ranks with the existing-group assertion.
  • Patched focused regression: 1 passed.
  • tests/unit/runtime/zero/test_zero_context.py: 12 passed.
  • pre-commit run --files deepspeed/runtime/zero/partition_parameters.py tests/unit/runtime/zero/test_zero_context.py: passed all hooks.
  • pip wheel --no-deps --no-build-isolation .: passed with DS_BUILD_OPS=0.
  • Fixed-seed ZeRO-3 training controls at world sizes 1, 2, and 4: nested and non-nested initialization produced identical per-step losses and final parameter sums.
  • Four-rank incompatible-size check (outer HPZ=2, inner HPZ=4): all ranks failed with the intended actionable error.

Related: #4901, #7066

Signed-off-by: Wang Zupeng <zupenwang@gmail.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: a491d71408

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

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if self.zero_param_process_group is None:
groups._create_zero_param_parallel_group(hpz_partition_size)
self.zero_param_process_group = groups._get_zero_param_intra_parallel_group()
elif dist.get_world_size(group=self.zero_param_process_group) != hpz_partition_size:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Validate HPZ group membership before reuse

When an outer Init has created world HPZ groups and an inner Init supplies a differently arranged data_parallel_group with the same cardinality—for example, outer HPZ groups [0,1]/[2,3] and inner DP groups [0,2]/[1,3]—this condition accepts reuse solely because the sizes match. The reused HPZ group then crosses the inner context's DP replicas, so secondary parameter gathers can combine values from different DP/model-parallel replicas and silently corrupt results. Verify that the existing group has the required membership within self.ds_process_group, or reject reuse, rather than comparing only its size.

Useful? React with 👍 / 👎.

self.zero_param_process_group = zero_param_parallel_group
if _ds_config is not None and _ds_config.zero_config.zero_hpz_partition_size > 1 and self.zero_param_process_group is None:
groups._create_zero_param_parallel_group(_ds_config.zero_config.zero_hpz_partition_size)
hpz_partition_size = min(_ds_config.zero_config.zero_hpz_partition_size, dist.get_world_size())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required sign-off trailer

This is a non-merge commit, but its message has no Signed-off-by: trailer. Recreate the commit with --signoff so it satisfies the repository's mandatory commit requirement.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

@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-08-30T01:12:44.597135Z a491d71 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.

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.

1 participant