Skip to content

Add end-to-end BF16 stage 1 tests for zero and invalid gradient norms - #8614

Open
vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix-bf16-zero-norm-assert-20260920
Open

vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix-bf16-zero-norm-assert-20260920

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Adds two end-to-end BF16 ZeRO stage 1 tests, built through deepspeed.initialize: an all-zero-gradient step (fully masked batch) runs and reports norm 0, and an inf loss skips the step (skipped_steps == 1) with parameters unchanged.

Not a duplicate: #8638 already changed the behavior this PR first targeted, so the source change is dropped. Its only BF16 skip test, test_bf16_optimizer_reports_invalid_step_and_recovers, mocks the norm; nothing covers these two cases on a real engine. No other open PR adds them.

Tests (CPU only, current master):

  • pytest tests/unit/runtime/half_precision/test_bf16_grad_norm_guard.py: 2 passed. Both fail if assert all_groups_norm > 0. is restored.
  • pytest tests/unit/v1/zero/test_invalid_grad_norm.py: 17 passed, 1 skipped (needs 2 GPUs).

GPU/NPU and the compatibility matrix were not run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The training-loop behavior needs integration coverage using an initialized BF16 optimizer rather than a private-state stand-in.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Improves BF16 gradient-norm handling by allowing valid zero norms while rejecting the -1 non-finite sentinel with an actionable error.

Changes:

  • Replaces the optimization-removable assertion with an explicit runtime check.
  • Adds focused tests for zero, non-finite, and ordinary gradient norms.
File Description
deepspeed/​runtime/​bf16_optimizer.py Validates gradient norms before clipping.
tests/​unit/​runtime/​half_precision/​test_bf16_grad_norm_guard.py Tests gradient-norm guard behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +39 to +42
class _Bf16Step:
"""The parts of BF16_Optimizer.step that reaching the guard needs, and nothing else."""

step = BF16_Optimizer.step

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2541d80: the stand-in is replaced by an engine test with a real BF16_Optimizer (ZeRO 1, bf16, fp32 grad accumulation), covering the zero-gradient step and the non-finite error.

Run a ZeRO stage 1 bf16 engine through deepspeed.initialize for two cases:
an all-zero-gradient step (norm 0) still runs, and an inf loss skips the
step (skipped_steps == 1) and leaves the parameters unchanged.

Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
@vineethsaivs
vineethsaivs force-pushed the fix-bf16-zero-norm-assert-20260920 branch from 2541d80 to 9284f23 Compare October 22, 2026 15:33
@vineethsaivs vineethsaivs changed the title Say why a bf16 gradient norm cannot be clipped from Add end-to-end BF16 stage 1 tests for zero and invalid gradient norms Oct 22, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026

This branch has not been deployed

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

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants