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

feat: min-max constrain of OptimManager's loss scale #129

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
feat: min-max constrain of optimmanager's loss scale
Achazwl committed Jul 24, 2023

Verified

This commit was signed with the committer’s verified signature.
jalaziz Jameel Al-Aziz
commit d8e443a82d871e8ffe1152c2eee516607c21e667
8 changes: 6 additions & 2 deletions bmtrain/optim/optim_manager.py
Original file line number Diff line number Diff line change
@@ -50,6 +50,8 @@ def __init__(self,
loss_scale : Optional[float] = None,
loss_scale_factor : float = 2,
loss_scale_steps : int = 1024,
min_loss_scale = 1,
max_loss_scale = float("inf"),
):
if loss_scale is not None:
self.loss_scale = loss_scale
@@ -60,6 +62,8 @@ def __init__(self,
self.steps_since_last_scale = 0
self.loss_scale_factor = loss_scale_factor if loss_scale_factor > 1 else 1 / loss_scale_factor
self.loss_scale_steps = loss_scale_steps
self.min_loss_scale = 1
self.max_loss_scale = max_loss_scale

self.optimizers = []
self.lr_schedulers = []
@@ -112,7 +116,7 @@ def step(self):
This function can also handle gradient overflow by reducing the loss scale when it occurs.
"""
if self.loss_scale_enabled and self.loss_scale > 1:
if self.loss_scale_enabled and self.loss_scale > self.min_loss_scale:
has_overflow = False
for optimizer in self.optimizers:
try:
@@ -140,7 +144,7 @@ def step(self):
if self.loss_scale_enabled:
self.steps_since_last_scale += 1

if self.steps_since_last_scale >= self.loss_scale_steps:
if self.steps_since_last_scale >= self.loss_scale_steps and self.loss_scale < self.max_loss_scale:
self._justify_scale(self.loss_scale * self.loss_scale_factor)

current_stream = torch.cuda.current_stream()