-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
221 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,36 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
import torch | ||
|
||
from .schedulers import ConstantSchedulerWithLinearWarmup, LambdaWarmupScheduler, LinearSchedulerWithLinearWarmup | ||
|
||
|
||
class WarmupLRScheduler(torch.optim.lr_scheduler.LambdaLR, ABC): | ||
class WarmupLRScheduler(LambdaWarmupScheduler, torch.optim.lr_scheduler.LambdaLR): | ||
def __init__( | ||
self, | ||
optimizer: torch.optim.Optimizer, | ||
num_warmup_steps: int, | ||
num_training_steps: int, | ||
*args, | ||
verbose: bool = False, | ||
**kwargs, | ||
) -> None: | ||
last_epoch = -1 | ||
self.interval = "step" | ||
self.num_warmup_steps = num_warmup_steps | ||
self.num_training_steps = num_training_steps | ||
super().__init__(optimizer, self.lr_lambda, last_epoch, verbose) | ||
|
||
@abstractmethod | ||
def lr_lambda(self, current_step: int) -> float: | ||
... | ||
|
||
|
||
class LinearLRSchedulerWithLinearWarmup(WarmupLRScheduler): | ||
def lr_lambda(self, current_step: int) -> float: | ||
if current_step < self.num_warmup_steps: | ||
return current_step / self.num_warmup_steps | ||
return max( | ||
0.0, | ||
(self.num_training_steps - current_step) / (self.num_training_steps - self.num_warmup_steps), | ||
super().__init__( | ||
*args, | ||
optimizer=optimizer, | ||
lr_lambda=self.value_lambda, | ||
num_warmup_steps=num_warmup_steps, | ||
last_epoch=last_epoch, | ||
verbose=verbose, | ||
**kwargs, | ||
) | ||
|
||
|
||
class ConstantLRSchedulerWithLinearWarmup(WarmupLRScheduler): | ||
def lr_lambda(self, current_step: int) -> float: | ||
if current_step < self.num_warmup_steps: | ||
return current_step / self.num_warmup_steps | ||
return 1.0 | ||
class LinearLRSchedulerWithLinearWarmup(WarmupLRScheduler, LinearSchedulerWithLinearWarmup): | ||
pass | ||
|
||
|
||
class ConstantLRSchedulerWithLinearWarmup(WarmupLRScheduler, ConstantSchedulerWithLinearWarmup): | ||
pass | ||
|
||
|
||
LR_SCHEDULERS = [LinearLRSchedulerWithLinearWarmup, ConstantLRSchedulerWithLinearWarmup] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.