-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedulers.py
404 lines (335 loc) · 13.1 KB
/
schedulers.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#
# Copyright (C) 2025 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
import copy
import torch
from collections.abc import Callable
from torch.optim.optimizer import Optimizer
from torch.optim.sgd import SGD
from torch.optim.lr_scheduler import (
ConstantLR,
LinearLR,
ExponentialLR,
CosineAnnealingLR,
MultiStepLR,
)
from torch.optim.lr_scheduler import _enable_get_lr_call, LRScheduler
from .optim import GreedyCoordinateGradient
from .config import SchedulerConf
class GCGScheduler(LRScheduler):
"""Base scheduler class for Greedy Coordinate Gradient optimizer.
This scheduler will round to nearest integer with a minimum value of 1.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
):
self.optimizer = optimizer
self.var_name = var_name
self.base_lrs = [
copy.deepcopy(param_group[self.var_name])
for param_group in optimizer.param_groups
]
self.last_values = self.base_lrs
self.last_epoch = 0
# Get dummy optimizer and tensor
self._dummy_param = torch.tensor([1.0], requires_grad=True)
self._dummy_optimizer = SGD([self._dummy_param], lr=1.0)
def _setup_reference_scheduler(
self,
scheduler_class: Callable[
..., ConstantLR | LinearLR | ExponentialLR | CosineAnnealingLR | MultiStepLR
],
**kwargs,
):
self._reference_scheduler = scheduler_class(self._dummy_optimizer, **kwargs)
# Synchronize reference scheduler with GCG scheduler because of the step() called in init
self._reference_scheduler.base_lrs = self.base_lrs
self._reference_scheduler.last_epoch = self.last_epoch
def get_new_values(self) -> list[int]:
self._reference_scheduler.last_epoch += 1
new_values = self._reference_scheduler._get_closed_form_lr()
return [max(round(new_value), 1) for new_value in new_values]
def get_last_lr(self) -> list[int] | list[float]: # type: ignore[override]
return self.last_values
def step(self, loss: torch.Tensor): # type: ignore[override]
self.last_epoch += 1
with _enable_get_lr_call(self):
new_values = self.get_new_values()
for idx, new_value in enumerate(new_values):
self.optimizer.param_groups[idx][self.var_name] = new_value
self.last_values = new_values
class LambdaInteger(GCGScheduler):
"""Scheduler that multiplies desired optimizer variable by a lambda.
This scheduler will round to nearest integer with a minimum value of 1.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
lr_lambda: Function that computes a multiplier given an epoch count.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
lr_lambda: Callable[[int], float],
):
super().__init__(optimizer, var_name)
self.lr_lambda = lr_lambda
def get_new_values(self) -> list[int]:
new_values = [
max(round(base_lr * self.lr_lambda(self.last_epoch)), 1)
for base_lr in self.base_lrs
]
return new_values
class ConstantInteger(GCGScheduler):
"""Scheduler that scales desired optimizer variable for a specified number of steps.
This scheduler will round to nearest integer with a minimum value of 1. See
:class:`~torch.optim.lr_scheduler.ConstantLR` for more info.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
factor: Number we multiply the parameter group variable until milestone.
total_iters: Number of steps to scale parameter group variable.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
factor: float = 1.0,
total_iters: int = 10,
):
super().__init__(optimizer, var_name)
self.factor = factor
self.total_iters = total_iters
self.last_epoch = -1 # Accounts for the upcoming step
# PyTorch's ConstantLR will assert on exactly 1.0
if self.factor > 0.0 and self.factor < 1.0:
self._setup_reference_scheduler(
ConstantLR, factor=factor, total_iters=total_iters
)
# Takes effect immediately
self.step(torch.tensor(0.0))
def get_new_values(self) -> list[int]:
if self.factor == 1.0:
new_values = self.last_values
else:
self._reference_scheduler.last_epoch += 1
new_values = self._reference_scheduler._get_closed_form_lr()
return [max(round(new_value), 1) for new_value in new_values]
class LinearInteger(GCGScheduler):
"""Scheduler that linearly scales the desired optimizer variable between two values.
This scheduler will round to nearest integer with a minimum value of 1. See
:class:`~torch.optim.lr_scheduler.ConstantLR` for more info.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
start_factor: Number we multiply parameter group variable in first epoch.
end_factor: Number we multiply parameter group variable at end of linear
changing process.
total_iters: Number of iterations for linear changing process.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
start_factor: float = 1.0,
end_factor: float = 0.5,
total_iters: int = 20,
):
super().__init__(optimizer, var_name)
self.start_factor = start_factor
self.end_factor = end_factor
self.total_iters = total_iters
self.last_epoch = -1 # Accounts for the upcoming step
self._setup_reference_scheduler(
LinearLR,
start_factor=start_factor,
end_factor=end_factor,
total_iters=total_iters,
)
# Takes effect immediately
self.step(torch.tensor(0.0))
class ExponentialInteger(GCGScheduler):
"""Scheduler that exponentially scales the desired optimizer variable.
This scheduler will round to nearest integer with a minimum value of 1. See
:class:`~torch.optim.lr_scheduler.ExponentialLR` for more info.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
gamma: Multiplicative factor of parameter group variable.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
gamma: float = 1.0,
):
super().__init__(optimizer, var_name)
self.gamma = gamma
self._setup_reference_scheduler(
ExponentialLR,
gamma=gamma,
)
class CosineAnnealingInteger(GCGScheduler):
"""Scheduler that adjusts multiplicative factor following a cosine curve.
This scheduler will round to nearest integer with a minimum value of 1. See
:class:`~torch.optim.lr_scheduler.CosineAnnealingLR` for more info.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the optimizer parameter group variable to schedule.
eta_min: Minimum parameter group variable value.
T_max: Number of iterations for a full cycle.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
eta_min: float = 0.5,
T_max: int = 20,
):
super().__init__(optimizer, var_name)
self.eta_min = eta_min
self.T_max = T_max
self._setup_reference_scheduler(
CosineAnnealingLR,
eta_min=eta_min,
T_max=T_max,
)
class MultiStepInteger(GCGScheduler):
"""Scheduler that changes optimizer variable with factors at specified milestones.
This scheduler will round to nearest integer with a minimum value of 1. See
:class:`~torch.optim.lr_scheduler.MultiStepLR` for more info.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the variable to schedule.
milestones: List of epochs at which to decay values.
gamma: Multiplicative factor at each milestone.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
milestones: list[int] = [20],
gamma: float = 1.0,
):
super().__init__(optimizer, var_name)
self.gamma = gamma
self.milestones = milestones
self._setup_reference_scheduler(
MultiStepLR,
gamma=gamma,
milestones=milestones,
)
class ChangeOnPlateauInteger(GCGScheduler):
"""Scheduler that reduces desired optimizer variable when loss plateaus.
Args:
optimizer: A GreedyCoordinateGradient optimizer instance.
var_name: Name of the variable to schedule.
factor: Factor to multiply values by on plateau.
patience: Number of epochs to wait for improvement.
threshold: Minimum change to qualify as improvement.
threshold_mode: How to measure improvement ('rel' or 'abs').
min_value: Minimum allowed value.
max_value: Maximum allowed value.
"""
def __init__(
self,
optimizer: GreedyCoordinateGradient,
var_name: str,
factor: float = 0.5,
patience: int = 1000,
threshold: float = 0.01,
threshold_mode: str = "abs",
min_value: int = 1,
max_value: int = 8,
):
super().__init__(optimizer, var_name)
self.factor = factor
self.patience = patience
self.threshold = threshold
self.threshold_mode = threshold_mode
self.min_value = min_value
self.max_value = max_value
self.num_bad_steps = 0
# Best loss always assumes "min"
self.best_loss = torch.inf
def get_new_values(self) -> list[int]:
new_values = [last_value * self.factor for last_value in self.last_values]
return [
min(max(round(new_value), self.min_value), self.max_value)
for new_value in new_values
]
def step(self, loss: torch.Tensor): # type: ignore[override]
self.last_epoch += 1
# NOTE: (torch.inf / torch.inf) < 0.1 returns False as of PyTorch 2.5
if self.threshold_mode == "rel" and (loss / self.best_loss) < self.threshold:
self.num_bad_steps = 0
self.best_loss = loss
return
if self.threshold_mode == "abs" and (loss + self.threshold) < self.best_loss:
self.num_bad_steps = 0
self.best_loss = loss
return
# Increment failed step counter
self.num_bad_steps += 1
if self.num_bad_steps == self.patience:
with _enable_get_lr_call(self):
new_values = self.get_new_values()
for idx, new_value in enumerate(new_values):
self.optimizer.param_groups[idx][self.var_name] = new_value
self.last_values = new_values
# Reset
self.num_bad_steps = 0
def from_config(
cfg: SchedulerConf,
optimizer: Optimizer | GreedyCoordinateGradient,
) -> LRScheduler:
"""Creates a scheduler instance from configuration.
Args:
cfg: Scheduler configuration.
optimizer: Optimizer to pass to scheduler.
Returns:
Configured scheduler instance.
Raises:
ValueError: If optimizer name is unknown or not supported.
"""
if isinstance(optimizer, GreedyCoordinateGradient):
if cfg.name == "constant":
return ConstantInteger(optimizer, cfg.var_name, cfg.factor, cfg.total_iters)
if cfg.name == "linear":
return LinearInteger(
optimizer,
cfg.var_name,
cfg.start_factor,
cfg.end_factor,
cfg.total_iters,
)
if cfg.name == "exponential":
return ExponentialInteger(optimizer, cfg.var_name, cfg.gamma)
if cfg.name == "cosine":
return CosineAnnealingInteger(
optimizer, cfg.var_name, cfg.eta_min, cfg.T_max
)
if cfg.name == "multistep":
return MultiStepInteger(optimizer, cfg.var_name, cfg.milestones, cfg.gamma)
if cfg.name == "plateau":
return ChangeOnPlateauInteger(
optimizer,
cfg.var_name,
cfg.factor,
cfg.patience,
cfg.threshold,
cfg.threshold_mode,
cfg.min_value,
cfg.max_value,
)
if isinstance(optimizer, Optimizer):
if cfg.name == "constant":
return ConstantLR(optimizer, factor=1.0)
raise ValueError(f"Invalid {cfg.name = } for soft prompt scheduler!")