Skip to content

Improve overflow handling in ZeRO - #6976

Merged
loadams merged 99 commits into
masterfrom
olruwase/ds_5241
Jun 9, 2025
Merged

loadams merged 99 commits into
masterfrom
olruwase/ds_5241

Conversation

@tjruwase

@tjruwase tjruwase commented Jan 28, 2025 •

Copy link
Copy Markdown
Contributor

Fix #5241: Improve overflow handling

  • ZeRO 1
  • ZeRO 2
  • ZeRO 3
  • BF16Optimizer

Enable pydantic configuration for mixed precision

  • bf16
  • fp16

@tjruwase

Copy link
Copy Markdown
Contributor Author

@delock, @inkcherry, can you please help investigate the failing xpu-max1100 CI? Thanks!

Comment thread deepspeed/runtime/hybrid_engine.py Resolved
@delock

delock commented Feb 5, 2025

Copy link
Copy Markdown
Collaborator

@delock, @inkcherry, can you please help investigate the failing xpu-max1100 CI? Thanks!

@tjruwase thanks! Our engineer is looking into it.

@sayakpaul

Copy link
Copy Markdown

Any ETA on this for merge?

@tjruwase

tjruwase commented Jun 6, 2025

Copy link
Copy Markdown
Contributor Author

Any ETA on this for merge?
Since CI looks to now be fine, this should be merged by 06/13/25. Thanks for the patience.

@loadams
loadams enabled auto-merge (squash) June 9, 2025 16:39
@loadams
loadams merged commit e440506 into master Jun 9, 2025
@loadams
loadams deleted the olruwase/ds_5241 branch June 9, 2025 17:30
deepcharm pushed a commit to deepcharm/DeepSpeed that referenced this pull request Jun 16, 2025
Fix deepspeedai#5241: Improve overflow handling
- [x] ZeRO 1
- [x] ZeRO 2
- [ ] ZeRO 3
- [ ] BF16Optimizer

Enable pydantic configuration for mixed precision
- [x] bf16
- [x] fp16

---------

Signed-off-by: Olatunji Ruwase <[email protected]>
Signed-off-by: Fabien Dupont <[email protected]>
Signed-off-by: Logan Adams <[email protected]>
Signed-off-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Xinyu Lian <[email protected]>
Co-authored-by: loadams <[email protected]>
Co-authored-by: Omar Elayan <[email protected]>
Co-authored-by: Fabio Geraci <[email protected]>
Co-authored-by: Sam Foreman <[email protected]>
Co-authored-by: Fabien Dupont <[email protected]>
Co-authored-by: Liangliang Ma <[email protected]>
Co-authored-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Olatunji Ruwase <[email protected]>
Signed-off-by: Max Kovalenko <[email protected]>
Antlera pushed a commit to Antlera/DeepSpeed that referenced this pull request Jun 27, 2025
Fix deepspeedai#5241: Improve overflow handling 
- [x] ZeRO 1
- [x] ZeRO 2
- [ ] ZeRO 3
- [ ] BF16Optimizer

Enable pydantic configuration for mixed precision
- [x] bf16
- [x] fp16

---------

Signed-off-by: Olatunji Ruwase <[email protected]>
Signed-off-by: Fabien Dupont <[email protected]>
Signed-off-by: Logan Adams <[email protected]>
Signed-off-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Xinyu Lian <[email protected]>
Co-authored-by: loadams <[email protected]>
Co-authored-by: Omar Elayan <[email protected]>
Co-authored-by: Fabio Geraci <[email protected]>
Co-authored-by: Sam Foreman <[email protected]>
Co-authored-by: Fabien Dupont <[email protected]>
Co-authored-by: Liangliang Ma <[email protected]>
Co-authored-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Olatunji Ruwase <[email protected]>
mauryaavinash95 pushed a commit to DataStates/DeepSpeed that referenced this pull request Oct 4, 2025
Fix deepspeedai#5241: Improve overflow handling 
- [x] ZeRO 1
- [x] ZeRO 2
- [ ] ZeRO 3
- [ ] BF16Optimizer

Enable pydantic configuration for mixed precision
- [x] bf16
- [x] fp16

---------

Signed-off-by: Olatunji Ruwase <[email protected]>
Signed-off-by: Fabien Dupont <[email protected]>
Signed-off-by: Logan Adams <[email protected]>
Signed-off-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Xinyu Lian <[email protected]>
Co-authored-by: loadams <[email protected]>
Co-authored-by: Omar Elayan <[email protected]>
Co-authored-by: Fabio Geraci <[email protected]>
Co-authored-by: Sam Foreman <[email protected]>
Co-authored-by: Fabien Dupont <[email protected]>
Co-authored-by: Liangliang Ma <[email protected]>
Co-authored-by: inkcherry <[email protected]>
Co-authored-by: Logan Adams <[email protected]>
Co-authored-by: Olatunji Ruwase <[email protected]>
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Jun 26, 2026
…ai#8035)

## Summary

Flip `DeepSpeedBF16Config.check_grad_overflow` default from `False` to
`True`, so bf16 users get the same gradient-overflow protection that
fp16 users already get by default.

## Motivation

The bf16 documentation states bf16 "does not require loss scaling"
(deepspeed.ai/docs/config-json/), but this overstates the safety
guarantee for the bf16 + ZeRO-2 (non-offload) partition-flat gradient
accumulation path. We reproduced a deterministic catastrophic NaN under
a small set of training conditions:

- ZeRO-2 (non-offload) + bf16
- Mixture-of-Transformers (modality-specific transformer branches)
- Heterogeneous per-sample loss masks (e.g. 50% action-invalid samples
in robotics VLA training)

Under these conditions, a single bf16 element in
`engine.optimizer.averaged_gradients[i]` overflows to `+inf`. The
downstream `Adam.step` then computes `inf / sqrt(inf) = NaN` in a fused
kernel, which simultaneously corrupts the partition slice's `exp_avg`,
`exp_avg_sq`, and fp32 master weights. The next forward pass propagates
NaN through every layer; the training run is dead with no useful
diagnostic. Reproduced consistently in DeepSpeed 0.16.9 - 0.17.1 at step
~22 with our internal repro.

The infrastructure to detect and skip such steps was correctly added by
deepspeedai#6976 (`check_grad_overflow` option,
`DeepSpeedZeroOptimizer.check_overflow` method, and step-skip logic at
`stage_1_and_2.step()` lines ~2128-2143). However the default was set to
`False` for bf16, so users hitting this condition do not receive the
protection.

## Change

Single line: `check_grad_overflow: bool = False` ->
`check_grad_overflow: bool = True` in `DeepSpeedBF16Config`. Updated
docstring + bf16 example block accordingly.

## Backward compatibility

Users who have benchmarked the check as too expensive AND have
separately confirmed their bf16 path cannot overflow can opt out by
setting:

\`\`\`json
"bf16": {
  "enabled": true,
  "check_grad_overflow": false
}
\`\`\`

The runtime cost is one isfinite-style scan over the gradient partition
per optimizer step (already implemented in
`DeepSpeedZeroOptimizer.check_overflow`); typically under 1% of step
wallclock.

## Related

- Issue deepspeedai#5242 (open) - bf16+ZeRO-2 NaN on real training runs
(Baichuan2-7B + 8x A800)
- PR deepspeedai#6976 - introduced `check_grad_overflow` option and underlying skip
logic

## Test plan

- [x] Reproducer (private repo): with default `False`, run dies at step
~22; with `True`, training survives via DeepSpeed's existing skip-step
path.
- [x] Existing CI should pass unchanged; this PR only changes a default
value in `precision_config.py`.

Signed-off-by: Yongzhe Wang <[email protected]>
nathon-lee pushed a commit to nathon-lee/DeepSpeed_woo that referenced this pull request Jul 1, 2026
…ai#8035)

## Summary

Flip `DeepSpeedBF16Config.check_grad_overflow` default from `False` to
`True`, so bf16 users get the same gradient-overflow protection that
fp16 users already get by default.

## Motivation

The bf16 documentation states bf16 "does not require loss scaling"
(deepspeed.ai/docs/config-json/), but this overstates the safety
guarantee for the bf16 + ZeRO-2 (non-offload) partition-flat gradient
accumulation path. We reproduced a deterministic catastrophic NaN under
a small set of training conditions:

- ZeRO-2 (non-offload) + bf16
- Mixture-of-Transformers (modality-specific transformer branches)
- Heterogeneous per-sample loss masks (e.g. 50% action-invalid samples
in robotics VLA training)

Under these conditions, a single bf16 element in
`engine.optimizer.averaged_gradients[i]` overflows to `+inf`. The
downstream `Adam.step` then computes `inf / sqrt(inf) = NaN` in a fused
kernel, which simultaneously corrupts the partition slice's `exp_avg`,
`exp_avg_sq`, and fp32 master weights. The next forward pass propagates
NaN through every layer; the training run is dead with no useful
diagnostic. Reproduced consistently in DeepSpeed 0.16.9 - 0.17.1 at step
~22 with our internal repro.

The infrastructure to detect and skip such steps was correctly added by
deepspeedai#6976 (`check_grad_overflow` option,
`DeepSpeedZeroOptimizer.check_overflow` method, and step-skip logic at
`stage_1_and_2.step()` lines ~2128-2143). However the default was set to
`False` for bf16, so users hitting this condition do not receive the
protection.

## Change

Single line: `check_grad_overflow: bool = False` ->
`check_grad_overflow: bool = True` in `DeepSpeedBF16Config`. Updated
docstring + bf16 example block accordingly.

## Backward compatibility

Users who have benchmarked the check as too expensive AND have
separately confirmed their bf16 path cannot overflow can opt out by
setting:

\`\`\`json
"bf16": {
  "enabled": true,
  "check_grad_overflow": false
}
\`\`\`

The runtime cost is one isfinite-style scan over the gradient partition
per optimizer step (already implemented in
`DeepSpeedZeroOptimizer.check_overflow`); typically under 1% of step
wallclock.

## Related

- Issue deepspeedai#5242 (open) - bf16+ZeRO-2 NaN on real training runs
(Baichuan2-7B + 8x A800)
- PR deepspeedai#6976 - introduced `check_grad_overflow` option and underlying skip
logic

## Test plan

- [x] Reproducer (private repo): with default `False`, run dies at step
~22; with `True`, training survives via DeepSpeed's existing skip-step
path.
- [x] Existing CI should pass unchanged; this PR only changes a default
value in `precision_config.py`.

Signed-off-by: Yongzhe Wang <[email protected]>
Signed-off-by: nathon-lee <[email protected]>
nathon-lee pushed a commit to nathon-lee/DeepSpeed_woo that referenced this pull request Jul 1, 2026
…ai#8035)

## Summary

Flip `DeepSpeedBF16Config.check_grad_overflow` default from `False` to
`True`, so bf16 users get the same gradient-overflow protection that
fp16 users already get by default.

## Motivation

The bf16 documentation states bf16 "does not require loss scaling"
(deepspeed.ai/docs/config-json/), but this overstates the safety
guarantee for the bf16 + ZeRO-2 (non-offload) partition-flat gradient
accumulation path. We reproduced a deterministic catastrophic NaN under
a small set of training conditions:

- ZeRO-2 (non-offload) + bf16
- Mixture-of-Transformers (modality-specific transformer branches)
- Heterogeneous per-sample loss masks (e.g. 50% action-invalid samples
in robotics VLA training)

Under these conditions, a single bf16 element in
`engine.optimizer.averaged_gradients[i]` overflows to `+inf`. The
downstream `Adam.step` then computes `inf / sqrt(inf) = NaN` in a fused
kernel, which simultaneously corrupts the partition slice's `exp_avg`,
`exp_avg_sq`, and fp32 master weights. The next forward pass propagates
NaN through every layer; the training run is dead with no useful
diagnostic. Reproduced consistently in DeepSpeed 0.16.9 - 0.17.1 at step
~22 with our internal repro.

The infrastructure to detect and skip such steps was correctly added by
deepspeedai#6976 (`check_grad_overflow` option,
`DeepSpeedZeroOptimizer.check_overflow` method, and step-skip logic at
`stage_1_and_2.step()` lines ~2128-2143). However the default was set to
`False` for bf16, so users hitting this condition do not receive the
protection.

## Change

Single line: `check_grad_overflow: bool = False` ->
`check_grad_overflow: bool = True` in `DeepSpeedBF16Config`. Updated
docstring + bf16 example block accordingly.

## Backward compatibility

Users who have benchmarked the check as too expensive AND have
separately confirmed their bf16 path cannot overflow can opt out by
setting:

\`\`\`json
"bf16": {
  "enabled": true,
  "check_grad_overflow": false
}
\`\`\`

The runtime cost is one isfinite-style scan over the gradient partition
per optimizer step (already implemented in
`DeepSpeedZeroOptimizer.check_overflow`); typically under 1% of step
wallclock.

## Related

- Issue deepspeedai#5242 (open) - bf16+ZeRO-2 NaN on real training runs
(Baichuan2-7B + 8x A800)
- PR deepspeedai#6976 - introduced `check_grad_overflow` option and underlying skip
logic

## Test plan

- [x] Reproducer (private repo): with default `False`, run dies at step
~22; with `True`, training survives via DeepSpeed's existing skip-step
path.
- [x] Existing CI should pass unchanged; this PR only changes a default
value in `precision_config.py`.

Signed-off-by: Yongzhe Wang <[email protected]>
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.

[BUG] Zero2 offload overflow