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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
import math
from typing import NamedTuple, Literal, Callable
from functools import partial
import torch
from torch.optim.lr_scheduler import LambdaLR
from diffusers.optimization import get_scheduler as get_scheduler_, get_cosine_with_hard_restarts_schedule_with_warmup
class OneCyclePhase(NamedTuple):
step_min: int
step_max: int
min: float
max: float
func: Callable[[float], float]
def warmup_linear(progress: float):
return progress
def warmup_cos(exp: int, progress: float):
lr = 0.5 * (1.0 + math.cos(math.pi * (1 + progress)))
lr = lr ** (exp - (exp - 1) * progress)
return lr
def anneal_linear(progress: float):
return 1 - progress
def anneal_half_cos(exp: int, progress: float):
lr = 1.0 + math.cos(math.pi * (0.5 + 0.5 * progress))
lr = lr ** (exp - (exp - 1) * progress)
return lr
def anneal_cos(exp: int, progress: float):
lr = 0.5 * (1.0 + math.cos(math.pi * progress))
lr = lr ** (exp - (exp - 1) * progress)
return lr
def get_one_cycle_schedule(
optimizer: torch.optim.Optimizer,
num_training_steps: int,
warmup: Literal["cos", "linear"] = "cos",
annealing: Literal["cos", "half_cos", "linear"] = "cos",
warmup_exp: int = 1,
annealing_exp: int = 1,
min_lr: float = 0.04,
mid_point: float = 0.3,
last_epoch: int = -1
):
if warmup == "linear":
warmup_func = warmup_linear
else:
warmup_func = partial(warmup_cos, warmup_exp)
if annealing == "linear":
anneal_func = anneal_linear
elif annealing == "half_cos":
anneal_func = partial(anneal_half_cos, annealing_exp)
else:
anneal_func = partial(anneal_cos, annealing_exp)
thresh_up = int(num_training_steps * min(mid_point, 0.5))
if annealing == "linear":
thresh_down = thresh_up * 2
phases = [
OneCyclePhase(0, thresh_up, min_lr, 1, warmup_func),
OneCyclePhase(thresh_up, thresh_down, min_lr, 1, anneal_func),
OneCyclePhase(thresh_down, num_training_steps, 0, min_lr, anneal_func),
]
else:
phases = [
OneCyclePhase(0, thresh_up, min_lr, 1, warmup_func),
OneCyclePhase(thresh_up, num_training_steps, 0, 1, anneal_func),
]
def lr_lambda(current_step: int):
phase = [p for p in phases if current_step >= p.step_min][-1]
return phase.min + phase.func((current_step - phase.step_min) / (phase.step_max - phase.step_min)) * (phase.max - phase.min)
return LambdaLR(optimizer, lr_lambda, last_epoch)
def get_scheduler(
id: str,
optimizer: torch.optim.Optimizer,
num_training_steps_per_epoch: int,
gradient_accumulation_steps: int = 1,
min_lr: float = 0.04,
warmup_func: Literal["cos", "linear"] = "cos",
annealing_func: Literal["cos", "half_cos", "linear"] = "cos",
warmup_exp: int = 1,
annealing_exp: int = 1,
cycles: int = 1,
train_epochs: int = 100,
warmup_epochs: int = 10,
):
num_training_steps_per_epoch = math.ceil(
num_training_steps_per_epoch / gradient_accumulation_steps
) * gradient_accumulation_steps
num_training_steps = train_epochs * num_training_steps_per_epoch
num_warmup_steps = warmup_epochs * num_training_steps_per_epoch
if id == "one_cycle":
lr_scheduler = get_one_cycle_schedule(
optimizer=optimizer,
num_training_steps=num_training_steps,
warmup=warmup_func,
annealing=annealing_func,
warmup_exp=warmup_exp,
annealing_exp=annealing_exp,
min_lr=min_lr,
)
elif id == "cosine_with_restarts":
if cycles is None:
cycles = math.ceil(math.sqrt(((num_training_steps - num_warmup_steps) / num_training_steps_per_epoch)))
lr_scheduler = get_cosine_with_hard_restarts_schedule_with_warmup(
optimizer=optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=num_training_steps,
num_cycles=cycles,
)
else:
lr_scheduler = get_scheduler_(
id,
optimizer=optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=num_training_steps,
)
return lr_scheduler
|