Skip to content
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

[Bugs] Disable grad scaler in AmpOptimWrapper when dtype is bf16 #1553

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mmengine/optim/optimizer/amp_optimizer_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@ def __init__(self,
else:
scaler_type = GradScaler

enable_loss_scaler = dtype != torch.bfloat16

if loss_scale == 'dynamic':
# If loss_scale is a string, it must be 'dynamic', then dynamic
# loss scaling will be used.
self.loss_scaler = scaler_type()
self.loss_scaler = scaler_type(enabled=enable_loss_scaler)
elif isinstance(loss_scale, float):
# Static loss scaling
self._scale_update_param = loss_scale
self.loss_scaler = scaler_type(init_scale=loss_scale)
self.loss_scaler = scaler_type(
init_scale=loss_scale, enabled=enable_loss_scaler)
elif isinstance(loss_scale, dict):
# More specific configuration.
loss_scale['enabled'] = loss_scale.pop('enabled',
True) and enable_loss_scaler
self.loss_scaler = scaler_type(**loss_scale)
else:
raise TypeError('loss_scale must be of type float, dict, or '
Expand Down
Loading