Skip to content

Fix BF16 AutoEP folding: avoid BF16 round-trip in FP32 accumulation - #8989

Closed
siddhant-rajhans wants to merge 1 commit into
deepspeedai:masterfrom
siddhant-rajhans:fix/bf16-autoep-fp32-staging
Closed

siddhant-rajhans wants to merge 1 commit into
deepspeedai:masterfrom
siddhant-rajhans:fix/bf16-autoep-fp32-staging

Conversation

@siddhant-rajhans

Copy link
Copy Markdown

Summary

_update_hp_grad in BF16_Optimizer applies AutoEP folding correction to lp.grad (BF16). The correction helper (reduce_autoep_folding_gradient) internally promotes to FP32 for the TP all-reduce, but copies the result back to the BF16 grad buffer before returning. The caller then casts this rounded BF16 value to FP32 for accumulation:

BF16 local grad -> FP32 TP reduction -> BF16 writeback -> FP32 accumulation
                                         ^^^^^^^^^^^^
                                         unnecessary precision loss

This introduces an avoidable BF16 rounding boundary between the FP32 reduction and FP32 accumulation, causing numerical divergence in router gradients, master weights, and Adam moments under AutoEP+AutoTP folding (reported as grad norm 0.78 vs 7.91 on a 65k-token sequence).

Fix

Promote lp.grad to FP32 before passing it to the folding correction, so the TP reduction operates directly on the FP32 staging tensor. The corrected FP32 result then feeds into the high-precision accumulation buffer without a BF16 round-trip.

When AutoEP folding is not active, the original cast-and-accumulate path is unchanged.

Fixes #8913

Test plan

_update_hp_grad applies AutoEP folding correction to lp.grad (BF16),
which internally promotes to FP32 for the TP all-reduce but copies the
result back to the BF16 grad buffer.  The wrapper then casts this
rounded BF16 value to FP32 for accumulation, introducing an unnecessary
precision loss between the FP32 reduction and FP32 accumulation.

Promote the gradient to FP32 before the folding correction so the TP
reduction result feeds directly into the high-precision accumulation
buffer without a BF16 round-trip.

Fixes deepspeedai#8913

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T23:10:05.719860Z dbfc252 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dbfc252252

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +519 to +522
fp32_grad = lp.grad.float()
apply_folding_correction_to_grad_buffer(self.autoep_folding_spec,
lp,
lp.grad,
fp32_grad,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add and run a regression for FP32 folding accumulation

With BF16 parameters and FP32 accumulation under TP folding, this changes the gradients used by training, but no regression test is added and the commit provides only a test plan, not an on-device result. Existing tests in test_autoep_autotp_bf16_folding_parity.py use mocks or exactly BF16-representable corrections, so they also pass with the old round-trip. Extend that suite with a TP2 training comparison whose reduction result is not BF16-representable, verify accumulated gradients and optimizer updates against an independent FP32 reference, and execute it on actual devices with hardware/results recorded. Root AGENTS.md explicitly requires this integration validation.

AGENTS.md reference: AGENTS.md:L81-L82

Useful? React with 👍 / 👎.

@0z5a 0z5a 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.

Read at dbfc2522.

This is the same fix as #8920, which has been open since Oct 27, also says Fixes #8913, and changes the same lines; the two conflict in bf16_optimizer.py. Since there is no test here, I ran #8920's test_autoep_autotp_bf16_folding_parity.py against both branches on CPU (gloo, 2 ranks):

branch result
#8920 11 passed
this PR 9 passed, 2 failed
master 3 passed, 8 failed

So the fp32 reduction itself is fixed here. The two cases that still fail are test_hp_buffer_keeps_fp32_reduction with clear_lp_grads=False. After _update_hp_grad, the parameter is marked corrected, but lp.grad still holds the local, pre-reduction gradient, because the corrected fp32 value only goes into the hp buffer. backward_epilogue uses clear_lp_grads=False by default, so after backward, anything that reads param.grad sees the uncorrected value, and consuming that gradient a second time skips the correction. #8920 copies the corrected value back into lp.grad for this reason, and only promotes when grad_acc_dtype is fp32.

Since #8920 covers the same issue and has the tests, it may be simplest to consolidate there.

f'high precision param has no gradient, lp param_id = {id(lp)} group_info = [{group_idx}][{param_idx}]'

hp_grad.data.add_(lp.grad.data.to(hp_grad.dtype).view(hp_grad.shape))
hp_grad.data.add_(fp32_grad.data.view(hp_grad.shape))

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.

The corrected value goes only into hp_grad, but apply_folding_correction_to_grad_buffer has already marked lp as corrected. With the default clear_lp_grads=False, lp.grad keeps the pre-reduction gradient under that marker. Copying fp32_grad back into lp.grad, as #8920 does, keeps the two consistent.

@siddhant-rajhans

Copy link
Copy Markdown
Author

Closing in favor of #8920, which addresses the same issue (#8913) and was filed earlier. Apologies for the duplication.

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.

[Precision] BF16 AutoEP folding rounds FP32 TP reduction before FP32 gradient accumulation

2 participants