-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix-FP16-ZeRO-Stage-0-loss-Scaling-In-engine.backward() #8393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3325,6 +3325,16 @@ def backward(self, loss, retain_graph=False, scale_wrt_gas=True): | |
| # TODO: handle these scaling with direct calls to loss.backward() | ||
| if isinstance(self.optimizer, ZeROOptimizer): | ||
| loss = self.optimizer.scale_if_loss(loss) | ||
| elif self.fp16_enabled() and not self.zero_optimization(): | ||
| # fp16 + ZeRO stage 0: FP16_Optimizer.step() always divides gradients | ||
| # by cur_scale, so loss must be pre-scaled before backward. Calling | ||
| # loss.backward() directly makes effective gradient updates ~cur_scale | ||
| # times too small, stalling training. Route through | ||
| # FP16_Optimizer.backward() which applies the scaling correctly. | ||
| self.optimizer.backward(loss, **backward_kwargs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| self._backward_epilogue() | ||
| self._running_engine_backward = False | ||
| return gas_scaled_loss | ||
|
Comment on lines
+3334
to
+3337
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When callers enable Useful? React with 👍 / 👎. |
||
| elif self.torch_autocast_z0_gradscaler: | ||
| loss = self.torch_autocast_z0_gradscaler.scale(loss) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a single-parent, non-merge commit, but its commit message has no
Signed-off-bytrailer, so it does not meet the repository's mandatory commit requirement and should be recreated with--signoff.AGENTS.md reference: AGENTS.md:L8-L8
Useful? React with 👍 / 👎.