forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.py
More file actions
307 lines (248 loc) Β· 11.8 KB
/
Copy pathoptimization.py
File metadata and controls
307 lines (248 loc) Β· 11.8 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import math
from paddle.optimizer.lr import LambdaDecay, LRScheduler
__all__ = [
"LinearDecayWithWarmup",
"ConstScheduleWithWarmup",
"CosineDecayWithWarmup",
"PolyDecayWithWarmup",
"CosineAnnealingWithWarmupDecay",
"LinearAnnealingWithWarmupDecay",
]
def is_integer(number):
if sys.version > "3":
return isinstance(number, int)
return isinstance(number, (int, long))
class CosineAnnealingWithWarmupDecay(LRScheduler):
def __init__(self, max_lr, min_lr, warmup_step, decay_step, last_epoch=-1, verbose=False):
self.decay_step = decay_step
self.warmup_step = warmup_step
self.max_lr = max_lr
self.min_lr = min_lr
super(CosineAnnealingWithWarmupDecay, self).__init__(max_lr, last_epoch, verbose)
def get_lr(self):
if self.warmup_step > 0 and self.last_epoch <= self.warmup_step:
return float(self.max_lr) * (self.last_epoch) / self.warmup_step
if self.last_epoch > self.decay_step:
return self.min_lr
num_step_ = self.last_epoch - self.warmup_step
decay_step_ = self.decay_step - self.warmup_step
decay_ratio = float(num_step_) / float(decay_step_)
coeff = 0.5 * (math.cos(math.pi * decay_ratio) + 1.0)
return self.min_lr + coeff * (self.max_lr - self.min_lr)
class LinearAnnealingWithWarmupDecay(LRScheduler):
def __init__(self, max_lr, min_lr, warmup_step, decay_step, last_epoch=-1, verbose=False):
self.decay_step = decay_step
self.warmup_step = warmup_step
self.max_lr = max_lr
self.min_lr = min_lr
super(LinearAnnealingWithWarmupDecay, self).__init__(max_lr, last_epoch, verbose)
def get_lr(self):
if self.warmup_step > 0 and self.last_epoch <= self.warmup_step:
return float(self.max_lr) * (self.last_epoch) / self.warmup_step
if self.last_epoch > self.decay_step:
return self.min_lr
num_step_ = self.last_epoch - self.warmup_step
decay_step_ = self.decay_step - self.warmup_step
decay_ratio = float(num_step_) / float(decay_step_)
coeff = 1.0 - decay_ratio
return self.min_lr + coeff * (self.max_lr - self.min_lr)
class LinearDecayWithWarmup(LambdaDecay):
"""
Creates a learning rate scheduler, which increases learning rate linearly
from 0 to given `learning_rate`, after this warmup period learning rate
would be decreased linearly from the base learning rate to 0.
Args:
learning_rate (float):
The base learning rate. It is a python float number.
total_steps (int):
The number of training steps.
warmup (int or float):
If int, it means the number of steps for warmup. If float, it means
the proportion of warmup in total training steps.
last_epoch (int, optional):
The index of last epoch. It can be set to restart training. If
None, it means initial learning rate.
Defaults to -1.
verbose (bool, optional):
If True, prints a message to stdout for each update.
Defaults to False.
Examples:
.. code-block:: python
from paddlenlp.transformers import LinearDecayWithWarmup
lr, warmup_steps, max_steps = 0.1, 100, 1000
lr_scheduler = LinearDecayWithWarmup(lr, max_steps, warmup_steps)
"""
def __init__(self, learning_rate, total_steps, warmup, last_epoch=-1, verbose=False):
warmup_steps = warmup if is_integer(warmup) else int(math.floor(warmup * total_steps))
def lr_lambda(current_step):
if current_step < warmup_steps:
return float(current_step) / float(max(1, warmup_steps))
return max(0.0, float(total_steps - current_step) / float(max(1, total_steps - warmup_steps)))
super(LinearDecayWithWarmup, self).__init__(learning_rate, lr_lambda, last_epoch, verbose)
class ConstScheduleWithWarmup(LambdaDecay):
"""
Creates a learning rate scheduler, which increases learning rate linearly
from 0 to given `learning_rate` during warmup periods and keeps learning
rate a constant after that.
Args:
learning_rate (float):
The base learning rate. It is a python float number.
warmup (int or float):
If int, it means the number of steps for warmup. If float, it means
the proportion of warmup in total training steps.
total_steps (int, optional):
The number of training steps. If `warmup` is a float number,
`total_steps` must be provided.
Defaults to None.
last_epoch (int, optional):
The index of last epoch. It can be set to restart training. If
None, it means initial learning rate.
Defaults to -1.
Examples:
.. code-block:: python
from paddlenlp.transformers import ConstScheduleWithWarmup
lr, warmup_steps = 0.1, 100
lr_scheduler = ConstScheduleWithWarmup(lr, warmup_steps)
"""
def __init__(self, learning_rate, warmup, total_steps=None, last_epoch=-1, verbose=False):
warmup_steps = warmup if is_integer(warmup) else int(math.floor(warmup * total_steps))
if is_integer(warmup):
warmup_steps = warmup
elif total_steps:
warmup_steps = int(math.floor(warmup * total_steps))
else:
raise ValueError(
"Please provide total steps if `warmup` is a float number , or provide integer for argument `warmup`."
)
def lr_lambda(current_step):
if current_step < warmup_steps:
return float(current_step) / float(max(1.0, warmup_steps))
return 1.0
super(ConstScheduleWithWarmup, self).__init__(learning_rate, lr_lambda, last_epoch, verbose)
class CosineDecayWithWarmup(LambdaDecay):
"""
Creates a learning rate scheduler, which increases learning rate linearly
from 0 to given `learning_rate`, after this warmup period learning rate
would be decreased following the values of the cosine function. If
`with_hard_restarts` is True, the cosine function could have serveral hard
restarts.
Args:
learning_rate (float):
The base learning rate. It is a python float number.
total_steps (int):
The number of training steps.
warmup (int or float):
If int, it means the number of steps for warmup. If float, it means
the proportion of warmup in total training steps.
with_hard_restarts (bool):
Whether cosine function has several hard restarts.
Defaults to False.
num_cycles (int or float, optional):
If `with_hard_restarts` is False, it means the number of waves in
cosine scheduler and should be an integer number and defaults to 1.
If `with_hard_restarts` is True, it means the number of hard
restarts to use and should be a float number and defaults to be 0.5.
Defaults to None.
last_epoch (int, optional):
The index of last epoch. It can be set to restart training. If
None, it means initial learning rate.
Defaults to -1.
Examples:
.. code-block:: python
from paddlenlp.transformers import CosineDecayWithWarmup
lr, warmup_steps, max_steps = 0.1, 100, 1000
lr_scheduler = CosineDecayWithWarmup(lr, max_steps, warmup_steps)
"""
def __init__(
self,
learning_rate,
total_steps,
warmup,
with_hard_restarts=False,
num_cycles=None,
last_epoch=-1,
verbose=False,
):
warmup_steps = warmup if is_integer(warmup) else int(math.floor(warmup * total_steps))
# Input check
if num_cycles is not None:
assert (
not with_hard_restarts
and isinstance(num_cycles, int)
or with_hard_restarts
and isinstance(num_cycles, float)
), "`num_circles` should be an integer while `with_hard_restarts` is False, an float while `with_hard_restarts` is True."
else:
num_cycles = 1 if not with_hard_restarts else 0.5
def lr_lambda(current_step):
if current_step < warmup_steps:
return float(current_step) / float(max(1, warmup_steps))
progress = float(current_step - warmup_steps) / float(max(1, total_steps - warmup_steps))
if with_hard_restarts:
if progress >= 1.0:
return 0.0
return max(0.0, 0.5 * (1.0 + math.cos(math.pi * ((float(num_cycles) * progress) % 1.0))))
return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress)))
super(CosineDecayWithWarmup, self).__init__(learning_rate, lr_lambda, last_epoch, verbose)
class PolyDecayWithWarmup(LambdaDecay):
"""
Creates a learning rate scheduler, which increases learning rate linearly
from 0 to given `lr_init`, after this warmup period learning rate would
be decreased as a polynomial decay from the base learning rate to the end
learning rate `lr_end`.
Args:
learning_rate (float):
The base learning rate. It is a python float number.
total_steps (int):
The number of training steps.
warmup (int or float):
If int, it means the number of steps for warmup. If float, it means
the proportion of warmup in total training steps.
lr_end (float, optional):
The end learning rate.
Defaults to 1e-7.
power (float, optional):
Power factor.
Defaults to 1.0.
last_epoch (int, optional):
The index of last epoch. It can be set to restart training. If
None, it means initial learning rate.
Defaults to -1.
Examples:
.. code-block:: python
from paddlenlp.transformers import PolyDecayWithWarmup
lr, lr_end, warmup_steps, max_steps = 0.1, 1e-6, 100, 1000
lr_scheduler = PolyDecayWithWarmup(lr, max_steps, warmup_steps, lr_end)
"""
def __init__(self, learning_rate, total_steps, warmup, lr_end=1e-7, power=1.0, last_epoch=-1, verbose=False):
lr_init = learning_rate
assert (
lr_init > lr_end
), f"`lr_end` must be be smaller than `learning_rate`. But `lr_end` is {lr_end} while `learning_rate` is {lr_init}."
warmup_steps = warmup if is_integer(warmup) else int(math.floor(warmup * total_steps))
def lr_lambda(current_step):
if current_step < warmup_steps:
return float(current_step) / float(max(1, warmup_steps))
elif current_step > total_steps:
return lr_end / lr_init # it multiplies by lr_init equals to lr_end
else:
lr_range = lr_init - lr_end
decay_steps = total_steps - warmup_steps
pct_remaining = 1 - (current_step - warmup_steps) / decay_steps
decay = lr_range * pct_remaining**power + lr_end
return decay / lr_init # it multiplies by lr_init equals to decay
super(PolyDecayWithWarmup, self).__init__(lr_init, lr_lambda, last_epoch, verbose)