ZeRO0 does not handle BF16 gradients properly #5154
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The combination of BF16 and ZeRO0 (no ZeRO optimization) has some issues with handling gradients.
BF16_Optimizer
seems to be designed to accumulate gradients in FP32, but this doesn't match other parts.BF16_Optimizer
. However, the engine performs allreduce on BF16 gradients after that. As the result, gradients are not properly reduced.BF16_Optimizer
'sbackward()
. This clears BF16 gradients. So it doesn't work when the gradient accumulation step > 1.There are two possible approaches to resolve this issue:
This PR takes the first approach to resolve the issue. ZeRO 1/2/3 follows the first approach though the second one has the advantage in terms the precision for gradient accumulation.