Skip to content

Commit 349e8c9

Browse files
yh0903Copilot
andcommitted
Reject unsupported BF16 norm fast path
Fail fast when ZeRO-1 selects the dedicated BF16 optimizer with FP32 gradient accumulation, where disabling norm computation is not implemented. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: yh0903 <helloyu0903@gmail.com>
1 parent fb02ab0 commit 349e8c9

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

deepspeed/runtime/engine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,9 @@ def _do_optimizer_sanity_check(self, basic_optimizer):
20682068
logger.warning("**** You are using ZeRO with an untested optimizer, proceed with caution *****")
20692069
if model_dtype == torch.bfloat16 and grad_accum_dtype == torch.float32 and self.zero_optimization_stage(
20702070
) == 1 and not self.zero_cpu_offload():
2071+
if not self.zero_compute_grad_norm():
2072+
raise ValueError("zero_optimization.compute_grad_norm=false does not support ZeRO Stage 1 with "
2073+
"BF16 parameters and FP32 gradient accumulation")
20712074
return BFLOAT16
20722075
return ZERO_OPTIMIZATION
20732076
elif amp_enabled:

docs/_pages/config-json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Enabling and configuring ZeRO memory optimizations
516516

517517
| Description | Default |
518518
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
519-
| Compute and retain the global gradient norm during ZeRO Stage 1/2 optimizer steps. Set to `false` only with a GPU optimizer, without ZenFlow or gradient clipping, and when callers do not use `get_global_grad_norm()`; finite/overflow checking is unchanged. | `true` |
519+
| Compute and retain the global gradient norm during ZeRO Stage 1/2 optimizer steps. Set to `false` only with a GPU optimizer, without ZenFlow, gradient clipping, or ZeRO Stage 1 BF16 parameters with FP32 gradient accumulation, and when callers do not use `get_global_grad_norm()`; finite/overflow checking is unchanged. | `true` |
520520

521521
<i>**overlap_comm**</i>: [boolean]
522522

tests/unit/runtime/zero/test_zero1_optimizer_fastpath.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ def test_fast_path_matches_default_update(self, stage):
8585
torch.manual_seed(123)
8686
baseline_model = SimpleModel(hidden_dim=4)
8787
fast_model = copy.deepcopy(baseline_model)
88-
baseline_engine, baseline_optimizer, _, _ = deepspeed.initialize(
89-
model=baseline_model,
90-
model_parameters=baseline_model.parameters(),
91-
config=_config(compute_grad_norm=True, stage=stage))
92-
fast_engine, fast_optimizer, _, _ = deepspeed.initialize(
93-
model=fast_model,
94-
model_parameters=fast_model.parameters(),
95-
config=_config(compute_grad_norm=False, stage=stage))
88+
baseline_engine, baseline_optimizer, _, _ = deepspeed.initialize(model=baseline_model,
89+
model_parameters=baseline_model.parameters(),
90+
config=_config(compute_grad_norm=True,
91+
stage=stage))
92+
fast_engine, fast_optimizer, _, _ = deepspeed.initialize(model=fast_model,
93+
model_parameters=fast_model.parameters(),
94+
config=_config(compute_grad_norm=False, stage=stage))
9695
inputs = torch.randn(1, 4, device=baseline_engine.device, dtype=torch.bfloat16)
9796
targets = torch.randn(1, 4, device=baseline_engine.device, dtype=torch.bfloat16)
9897

@@ -167,3 +166,11 @@ def test_zenflow_rejects_disabled_norm(self):
167166
deepspeed.initialize(model=model,
168167
model_parameters=model.parameters(),
169168
config=_config(compute_grad_norm=False, zenflow={}))
169+
170+
def test_fp32_gradient_accumulation_rejects_disabled_norm(self):
171+
model = SimpleModel(hidden_dim=4)
172+
config = _config(compute_grad_norm=False)
173+
config["data_types"] = {"grad_accum_dtype": "fp32"}
174+
175+
with pytest.raises(ValueError, match="BF16 parameters and FP32 gradient accumulation"):
176+
deepspeed.initialize(model=model, model_parameters=model.parameters(), config=config)

0 commit comments

Comments
 (0)