← Back to Pull requests

Honor explicit dtype in ZeRO tensor constructors #8959

Open
unknown opened this pull request with 604 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 30, 2026 15:28 UTC
Author

Summary

Fixes #8948.

Honor an explicit tensor-constructor dtype inside deepspeed.zero.Init, instead of unconditionally converting floating-point results to the context's dtype.

The unconditional conversion can round an explicitly fp32 torch.arange to bf16 before a model computes RoPE inverse frequencies. Converting the resulting buffer back to fp32 cannot recover the lost precision.

Changes

  • Preserve a non-None explicit dtype in both the wrapped torch factory functions and the torch.Tensor.__new__ hook.

  • Keep the existing context-dtype conversion when dtype is omitted or None. Device override and parameter-partitioning behavior are unchanged.

  • Document constructor dtype precedence in the zero.Init docstring.

  • Add a regression test comparing RoPE-style frequencies against a plain PyTorch fp32 reference. It also checks explicit fp32 torch.Tensor construction and the existing implicit bf16 behavior for torch.Tensor and torch.zeros.

  • The registration hook that records the Init dtype for a parameter a parent adds to a partitioned child is probed with getattr and skipped when the API is absent (torch < 2.5), so Init(dtype=...) does not crash on older PyTorch; such parameters keep the pre-hook behavior.

  • The bf16 regression tests are gated on is_bf16_supported(), and test_explicit_fp32_param_trains is renamed to test_fp32_ctor_param_trains_under_bf16_config and given an explicit SGD config, since the old name over-promised and the module-level Adam config requires an op build.
  • This intentionally changes dtype precedence for explicitly typed constructor calls. Such tensors may use more initialization memory than before when their requested dtype is wider than the context dtype. This is not a change to the engine's subsequent model/buffer conversion policy.

    Validation

    Validated commit: 7122900a4404f8197fce0eb8908cb5d5554084fe.

    Environment: CPU backend on a host with 2 Intel Xeon Platinum 8457C CPUs; Python 3.12.3; PyTorch 2.14.0.dev20260804+xpu. No GPU was used; accelerator/driver version is not applicable to these CPU runs.

    Latest head (7cdf75be0) additionally validated on CPU (DS_ACCELERATOR=cpu, torch 2.13.0, 8x NVIDIA H20 host, GPUs idle): tests/unit/runtime/zero/test_zero_context.py + tests/unit/runtime/zero/test_zero_linear_direct_init.py 21 passed, 1 skipped; tests/unit/v1/half_precision/test_keep_in_fp32_modules.py 50 passed, 7 skipped. The parametrized test_added_child_bias_trains[True] covers deepspeed.zero.Init(module=model, ...) run before deepspeed.initialize and fails fast with the dtype mismatch without the fix.

    DS_ACCELERATOR=cpu pytest -q 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
    # All applicable hooks passed
    

    The previously missed torch.Tensor(4, dtype=torch.float32) path was reproduced on the CPU backend: it returned bf16 before the second constructor-hook fix. The regression now checks that it returns fp32.

    Compatibility and remaining validation

    | Area | Coverage |
    |---|---|
    | ZeRO Init, bf16 context, explicit fp32 and implicit constructor dtypes | Regression passed on CPU |
    | Existing ZeRO context/sharding/gather tests | Entire test file passed on CPU, including its single- and two-rank cases |
    | fp16/fp32 context targets for the new regression | Not separately exercised |
    | GPU backends, including NVIDIA L20 | Not run |
    | CPU/NVMe offload and tensor/pipeline/sequence/expert parallel combinations | Not specifically validated |
    | Full Transformers RoPE reproduction and end-to-end training convergence | Not run |
    | Throughput and peak memory | Not measured; no performance or convergence claim |

    Sibling-path inspection covered both ZeRO constructor hooks. ZeRO-1/2 optimizer paths do not define these hooks. The independent deepspeed.OnDevice constructor wrappers retain their existing conversion policy and are outside this PR's ZeRO Init scope.

    Duplicate check

    Checked the issue discussion and open PR searches for 8948 in:body and zero.Init explicit dtype. No other PR addressing this constructor-dtype fix was found. Related #8843 preserves model-listed fp32 parameters; it does not fix precision lost while explicitly typed tensors are constructed.

    AI assistance

    The implementation, regression test, and this description were prepared with AI assistance.

    Sibling-path check (AGENTS.md)

    ZeRO-3 _post_init_method also casts floating-point params to the Init dtype at partition time; this PR's _post_init_method change is consistent with it. ZeRO-1/2 optimizer paths do not define constructor hooks. fp16 vs bf16 optimizer wrappers are unaffected — they operate on already-constructed params.

    Diff: 604 lines (+453 / -151) across 7 files Reviewer: jinyouzhi Review duration: 45158m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.57) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 604 lines diff across 7 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.383
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #8959"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57.1% (MEDIUM) · Higher = larger blast radius
    57.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 31.6% · Safe to merge
    31.6%