Honor explicit dtype in ZeRO tensor constructors #8959
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
jinyouzhi
Summary
Fixes #8948.
Honor an explicit tensor-constructor
dtypeinsidedeepspeed.zero.Init, instead of unconditionally converting floating-point results to the context's dtype.The unconditional conversion can round an explicitly fp32
torch.arangeto bf16 before a model computes RoPE inverse frequencies. Converting the resulting buffer back to fp32 cannot recover the lost precision.Changes
Noneexplicitdtypein both the wrapped torch factory functions and thetorch.Tensor.__new__hook.dtypeis omitted orNone. Device override and parameter-partitioning behavior are unchanged.zero.Initdocstring.torch.Tensorconstruction and the existing implicit bf16 behavior fortorch.Tensorandtorch.zeros.getattrand skipped when the API is absent (torch < 2.5), soInit(dtype=...)does not crash on older PyTorch; such parameters keep the pre-hook behavior.is_bf16_supported(), andtest_explicit_fp32_param_trainsis renamed totest_fp32_ctor_param_trains_under_bf16_configand 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.py21 passed, 1 skipped;tests/unit/v1/half_precision/test_keep_in_fp32_modules.py50 passed, 7 skipped. The parametrizedtest_added_child_bias_trains[True]coversdeepspeed.zero.Init(module=model, ...)run beforedeepspeed.initializeand fails fast with the dtype mismatch without the fix.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.OnDeviceconstructor 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:bodyandzero.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.