Repository navigation
Fix BF16 AutoEP folding: avoid BF16 round-trip in FP32 accumulation - #8989
siddhant-rajhans wants to merge 1 commit into
Conversation
_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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| fp32_grad = lp.grad.float() | ||
| apply_folding_correction_to_grad_buffer(self.autoep_folding_spec, | ||
| lp, | ||
| lp.grad, | ||
| fp32_grad, |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
Summary
_update_hp_gradinBF16_Optimizerapplies AutoEP folding correction tolp.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: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.gradto 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
bf16_optimizer.pyauto_ep_folding.pyis unchanged; it already handles FP32 inputs correctly via its existingdtype != torch.float32branch