mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-30 22:24:21 +00:00
[misc] update pre-commit and run all files (#4752)
* [misc] update pre-commit * [misc] run pre-commit * [misc] remove useless configuration files * [misc] ignore cuda for clang-format
This commit is contained in:
@@ -21,16 +21,17 @@ import math
|
||||
class AnnealingLR(object):
|
||||
"""Anneals the learning rate."""
|
||||
|
||||
def __init__(self,
|
||||
optimizer,
|
||||
max_lr,
|
||||
min_lr,
|
||||
warmup_steps,
|
||||
decay_steps,
|
||||
decay_style,
|
||||
use_checkpoint_lr_scheduler=True,
|
||||
override_lr_scheduler=False):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
optimizer,
|
||||
max_lr,
|
||||
min_lr,
|
||||
warmup_steps,
|
||||
decay_steps,
|
||||
decay_style,
|
||||
use_checkpoint_lr_scheduler=True,
|
||||
override_lr_scheduler=False,
|
||||
):
|
||||
# Class values.
|
||||
self.optimizer = optimizer
|
||||
|
||||
@@ -50,23 +51,21 @@ class AnnealingLR(object):
|
||||
self.override_lr_scheduler = override_lr_scheduler
|
||||
self.use_checkpoint_lr_scheduler = use_checkpoint_lr_scheduler
|
||||
if self.override_lr_scheduler:
|
||||
assert not self.use_checkpoint_lr_scheduler, 'both override and '\
|
||||
'use-checkpoint are set.'
|
||||
assert not self.use_checkpoint_lr_scheduler, "both override and " "use-checkpoint are set."
|
||||
|
||||
# Set the learning rate
|
||||
self.step(0)
|
||||
|
||||
def get_lr(self):
|
||||
"""Learning rate decay functions from:
|
||||
https://openreview.net/pdf?id=BJYwwY9ll pg. 4"""
|
||||
https://openreview.net/pdf?id=BJYwwY9ll pg. 4"""
|
||||
|
||||
# Use linear warmup for the initial part.
|
||||
if self.warmup_steps > 0 and self.num_steps <= self.warmup_steps:
|
||||
return self.max_lr * float(self.num_steps) / \
|
||||
float(self.warmup_steps)
|
||||
return self.max_lr * float(self.num_steps) / float(self.warmup_steps)
|
||||
|
||||
# If the learning rate is constant, just return the initial value.
|
||||
if self.decay_style == 'constant':
|
||||
if self.decay_style == "constant":
|
||||
return self.max_lr
|
||||
|
||||
# For any steps larger than `self.decay_steps`, use `self.min_lr`.
|
||||
@@ -81,13 +80,12 @@ class AnnealingLR(object):
|
||||
assert decay_ratio <= 1.0
|
||||
delta_lr = self.max_lr - self.min_lr
|
||||
|
||||
if self.decay_style == 'linear':
|
||||
coeff = (1.0 - decay_ratio)
|
||||
elif self.decay_style == 'cosine':
|
||||
if self.decay_style == "linear":
|
||||
coeff = 1.0 - decay_ratio
|
||||
elif self.decay_style == "cosine":
|
||||
coeff = 0.5 * (math.cos(math.pi * decay_ratio) + 1.0)
|
||||
else:
|
||||
raise Exception('{} decay style is not supported.'.format(
|
||||
self.decay_style))
|
||||
raise Exception("{} decay style is not supported.".format(self.decay_style))
|
||||
|
||||
return self.min_lr + coeff * delta_lr
|
||||
|
||||
@@ -96,16 +94,16 @@ class AnnealingLR(object):
|
||||
self.num_steps += increment
|
||||
new_lr = self.get_lr()
|
||||
for group in self.optimizer.param_groups:
|
||||
group['lr'] = new_lr
|
||||
group["lr"] = new_lr
|
||||
|
||||
def state_dict(self):
|
||||
state_dict = {
|
||||
'max_lr': self.max_lr,
|
||||
'warmup_steps': self.warmup_steps,
|
||||
'num_steps': self.num_steps,
|
||||
'decay_style': self.decay_style,
|
||||
'decay_steps': self.decay_steps,
|
||||
'min_lr': self.min_lr
|
||||
"max_lr": self.max_lr,
|
||||
"warmup_steps": self.warmup_steps,
|
||||
"num_steps": self.num_steps,
|
||||
"decay_style": self.decay_style,
|
||||
"decay_steps": self.decay_steps,
|
||||
"min_lr": self.min_lr,
|
||||
}
|
||||
return state_dict
|
||||
|
||||
@@ -116,43 +114,35 @@ class AnnealingLR(object):
|
||||
return cls_value
|
||||
|
||||
if not self.use_checkpoint_lr_scheduler:
|
||||
assert cls_value == sd_value, \
|
||||
f'AnnealingLR: class input value {cls_value} and checkpoint' \
|
||||
f'value {sd_value} for {name} do not match'
|
||||
assert cls_value == sd_value, (
|
||||
f"AnnealingLR: class input value {cls_value} and checkpoint" f"value {sd_value} for {name} do not match"
|
||||
)
|
||||
return sd_value
|
||||
|
||||
def load_state_dict(self, sd):
|
||||
|
||||
if 'start_lr' in sd:
|
||||
max_lr_ = sd['start_lr']
|
||||
if "start_lr" in sd:
|
||||
max_lr_ = sd["start_lr"]
|
||||
else:
|
||||
max_lr_ = sd['max_lr']
|
||||
self.max_lr = self._check_and_set(self.max_lr, max_lr_,
|
||||
'learning rate')
|
||||
max_lr_ = sd["max_lr"]
|
||||
self.max_lr = self._check_and_set(self.max_lr, max_lr_, "learning rate")
|
||||
|
||||
self.min_lr = self._check_and_set(self.min_lr, sd['min_lr'],
|
||||
'minimum learning rate')
|
||||
self.min_lr = self._check_and_set(self.min_lr, sd["min_lr"], "minimum learning rate")
|
||||
|
||||
if 'warmup_iter' in sd:
|
||||
warmup_steps_ = sd['warmup_iter']
|
||||
if "warmup_iter" in sd:
|
||||
warmup_steps_ = sd["warmup_iter"]
|
||||
else:
|
||||
warmup_steps_ = sd['warmup_steps']
|
||||
self.warmup_steps = self._check_and_set(self.warmup_steps,
|
||||
warmup_steps_,
|
||||
'warmup iterations')
|
||||
warmup_steps_ = sd["warmup_steps"]
|
||||
self.warmup_steps = self._check_and_set(self.warmup_steps, warmup_steps_, "warmup iterations")
|
||||
|
||||
if 'end_iter' in sd:
|
||||
decay_steps_ = sd['end_iter']
|
||||
if "end_iter" in sd:
|
||||
decay_steps_ = sd["end_iter"]
|
||||
else:
|
||||
decay_steps_ = sd['decay_steps']
|
||||
self.decay_steps = self._check_and_set(self.decay_steps, decay_steps_,
|
||||
'total number of iterations')
|
||||
self.decay_style = self._check_and_set(self.decay_style,
|
||||
sd['decay_style'],
|
||||
'decay style')
|
||||
decay_steps_ = sd["decay_steps"]
|
||||
self.decay_steps = self._check_and_set(self.decay_steps, decay_steps_, "total number of iterations")
|
||||
self.decay_style = self._check_and_set(self.decay_style, sd["decay_style"], "decay style")
|
||||
|
||||
if 'num_iters' in sd:
|
||||
num_steps = sd['num_iters']
|
||||
if "num_iters" in sd:
|
||||
num_steps = sd["num_iters"]
|
||||
else:
|
||||
num_steps = sd['num_steps']
|
||||
num_steps = sd["num_steps"]
|
||||
self.step(increment=num_steps)
|
||||
|
Reference in New Issue
Block a user