-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathscheduler.py
More file actions
56 lines (47 loc) · 1.98 KB
/
scheduler.py
File metadata and controls
56 lines (47 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) NXAI GmbH and its affiliates 2024
# Author: Maximilian Beck
# Licensed under the Apache License, Version 2.0
# Adapted from xlstm/experiments/lr_scheduler.py
# Source: https://github.com/NX-AI/xlstm
import math
from abc import abstractmethod
from torch.optim import lr_scheduler
class BaseLRScheduler(lr_scheduler._LRScheduler):
def __init__(self, optimizer, last_epoch=-1):
super().__init__(optimizer, last_epoch)
@abstractmethod
def get_lr(self):
"""Returns the current learning rate for each parameter group."""
raise NotImplementedError
@abstractmethod
def reinitialize(self, **kwargs) -> None:
"""Reinitializes the learning rate scheduler."""
raise NotImplementedError
class LinearWarmupCosineAnnealingLR(BaseLRScheduler):
def __init__(self, optimizer, warmup_steps, decay_until_step, max_lr, min_lr, last_epoch=-1):
self.optimizer = optimizer
self.warmup_steps = warmup_steps
self.decay_until_step = decay_until_step
self.min_lr = min_lr
self.max_lr = max_lr
super().__init__(optimizer, last_epoch)
@staticmethod
def compute_lr(step, warmup_steps, decay_until_step, max_lr, min_lr):
if step < warmup_steps:
return max_lr * step / warmup_steps
if step > decay_until_step:
return min_lr
if warmup_steps <= step < decay_until_step:
decay_ratio = (step - warmup_steps) / (decay_until_step - warmup_steps)
assert 0.0 <= decay_ratio <= 1.0
coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
return min_lr + coeff * (max_lr - min_lr)
else:
return min_lr
def get_lr(self):
"""Returns the current learning rate for each parameter group."""
step = self.last_epoch
return (
self.compute_lr(step, self.warmup_steps, self.decay_until_step, self.max_lr, self.min_lr)
for _ in self.optimizer.param_groups
)