-
Notifications
You must be signed in to change notification settings - Fork 400
/
optimizer_hparams_registry.py
225 lines (169 loc) · 8.34 KB
/
optimizer_hparams_registry.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
# Copyright 2022 MosaicML Composer authors
# SPDX-License-Identifier: Apache-2.0
"""Hyperparameters for optimizers."""
from abc import ABC
from dataclasses import asdict, dataclass
from typing import Dict, Iterable, List, Optional, Type, Union
import torch
import torch_optimizer
import yahp as hp
from torch.optim import Optimizer
from composer.optim import DecoupledAdamW, DecoupledSGDW
# Optimizer parameters and defaults match those in torch.optim
__all__ = [
'OptimizerHparams',
'AdamHparams',
'RAdamHparams',
'AdamWHparams',
'DecoupledAdamWHparams',
'SGDHparams',
'DecoupledSGDWHparams',
'RMSpropHparams',
]
@dataclass
class OptimizerHparams(hp.Hparams, ABC):
"""Base class for optimizer hyperparameter classes.
Optimizer parameters that are added to :class:`~composer.trainer.trainer_hparams.TrainerHparams` (e.g. via YAML or
the CLI) are initialized in the training loop.
"""
optimizer_cls = None # type: Optional[Type[Optimizer]]
def initialize_object(
self,
param_group: Union[Iterable[torch.Tensor], Iterable[Dict[str, torch.Tensor]]],
) -> Optimizer:
"""Initializes the optimizer.
Args:
param_group (Iterable[torch.Tensor] | Iterable[Dict[str, torch.Tensor]]): Parameters for
this optimizer to optimize.
"""
if self.optimizer_cls is None:
raise ValueError(f'{type(self).__name__}.optimizer_cls must be defined')
return self.optimizer_cls(param_group, **asdict(self))
@dataclass
class AdamHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~torch.optim.Adam` optimizer.
See :class:`~torch.optim.Adam` for documentation.
Args:
lr (float, optional): See :class:`~torch.optim.Adam`.
betas (float, optional): See :class:`~torch.optim.Adam`.
eps (float, optional): See :class:`~torch.optim.Adam`.
weight_decay (float, optional): See :class:`~torch.optim.Adam`.
amsgrad (bool, optional): See :class:`~torch.optim.Adam`.
"""
optimizer_cls = torch.optim.Adam
lr: float = hp.auto(torch.optim.Adam, 'lr', ignore_docstring_errors=True)
betas: List[float] = hp.auto(torch.optim.Adam, 'betas', ignore_docstring_errors=True)
eps: float = hp.auto(torch.optim.Adam, 'eps', ignore_docstring_errors=True)
weight_decay: float = hp.auto(torch.optim.Adam, 'weight_decay', ignore_docstring_errors=True)
amsgrad: bool = hp.auto(torch.optim.Adam, 'amsgrad', ignore_docstring_errors=True)
@dataclass
class RAdamHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~torch_optimizer.RAdam` optimizer.
See :class:`~torch_optimizer.RAdam` for documentation.
Args:
lr (float, optional): See :class:`~torch_optimizer.RAdam`.
betas (float, optional): See :class:`~torch_optimizer.RAdam`.
eps (float, optional): See :class:`~torch_optimizer.RAdam`.
weight_decay (float, optional): See :class:`~torch_optimizer.RAdam`.
"""
optimizer_cls = torch_optimizer.RAdam
lr: float = hp.auto(torch_optimizer.RAdam, 'lr', ignore_docstring_errors=True)
betas: List[float] = hp.auto(torch_optimizer.RAdam, 'betas', ignore_docstring_errors=True)
eps: float = hp.auto(torch_optimizer.RAdam, 'eps', ignore_docstring_errors=True)
weight_decay: float = hp.auto(torch_optimizer.RAdam, 'weight_decay', ignore_docstring_errors=True)
@dataclass
class AdamWHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~torch.optim.AdamW` optimizer.
See :class:`~torch.optim.AdamW` for documentation.
Args:
lr (float, optional): See :class:`~torch.optim.AdamW`.
betas (float, optional): See :class:`~torch.optim.AdamW`.
eps (float, optional): See :class:`~torch.optim.AdamW`.
weight_decay (float, optional): See :class:`~torch.optim.AdamW`.
amsgrad (bool, optional): See :class:`~torch.optim.AdamW`.
"""
optimizer_cls = torch.optim.AdamW
lr: float = hp.auto(torch.optim.AdamW, 'lr', ignore_docstring_errors=True)
betas: List[float] = hp.auto(torch.optim.AdamW, 'betas', ignore_docstring_errors=True)
eps: float = hp.auto(torch.optim.AdamW, 'eps', ignore_docstring_errors=True)
weight_decay: float = hp.auto(torch.optim.AdamW, 'weight_decay', ignore_docstring_errors=True)
amsgrad: bool = hp.auto(torch.optim.AdamW, 'amsgrad', ignore_docstring_errors=True)
@dataclass
class DecoupledAdamWHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~.DecoupledAdamW` optimizer.
See :class:`~.DecoupledAdamW` for documentation.
Args:
lr (float, optional): See :class:`~.DecoupledAdamW`.
betas (float, optional): See :class:`~.DecoupledAdamW`.
eps (float, optional): See :class:`~.DecoupledAdamW`.
weight_decay (float, optional): See :class:`~.DecoupledAdamW`.
amsgrad (bool, optional): See :class:`~.DecoupledAdamW`.
"""
optimizer_cls = DecoupledAdamW
lr: float = hp.auto(DecoupledAdamW, 'lr')
betas: List[float] = hp.auto(DecoupledAdamW, 'betas')
eps: float = hp.auto(DecoupledAdamW, 'eps')
weight_decay: float = hp.auto(DecoupledAdamW, 'weight_decay')
amsgrad: bool = hp.auto(DecoupledAdamW, 'amsgrad')
@dataclass
class SGDHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~torch.optim.SGD` optimizer.
See :class:`~torch.optim.SGD` for documentation.
Args:
lr (float): See :class:`~torch.optim.SGD`.
momentum (float, optional): See :class:`~torch.optim.SGD`.
weight_decay (float, optional): See :class:`~torch.optim.SGD`.
dampening (float, optional): See :class:`~torch.optim.SGD`.
nesterov (bool, optional): See :class:`~torch.optim.SGD`.
"""
optimizer_cls = torch.optim.SGD
lr: float = hp.auto(torch.optim.SGD, 'lr', ignore_docstring_errors=True)
momentum: float = hp.auto(torch.optim.SGD, 'momentum', ignore_docstring_errors=True)
weight_decay: float = hp.auto(torch.optim.SGD, 'weight_decay', ignore_docstring_errors=True)
dampening: float = hp.auto(torch.optim.SGD, 'dampening', ignore_docstring_errors=True)
nesterov: bool = hp.auto(torch.optim.SGD, 'nesterov', ignore_docstring_errors=True)
@dataclass
class DecoupledSGDWHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~.DecoupledSGDW` optimizer.
See :class:`~.DecoupledSGDW` for documentation.
Args:
lr (float): See :class:`~.DecoupledSGDW`.
momentum (float, optional): See :class:`~.DecoupledSGDW`.
weight_decay (float, optional): See :class:`~.DecoupledSGDW`.
dampening (float, optional): See :class:`~.DecoupledSGDW`.
nesterov (bool, optional): See :class:`~.DecoupledSGDW`.
"""
optimizer_cls = DecoupledSGDW
lr: float = hp.auto(DecoupledSGDW, 'lr')
momentum: float = hp.auto(DecoupledSGDW, 'momentum')
weight_decay: float = hp.auto(DecoupledSGDW, 'weight_decay')
dampening: float = hp.auto(DecoupledSGDW, 'dampening')
nesterov: bool = hp.auto(DecoupledSGDW, 'nesterov')
@dataclass
class RMSpropHparams(OptimizerHparams):
"""Hyperparameters for the :class:`~torch.optim.RMSprop` optimizer.
See :class:`~torch.optim.RMSprop` for documentation.
Args:
lr (float): See :class:`~torch.optim.RMSprop`.
alpha (float, optional): See :class:`~torch.optim.RMSprop`.
eps (float, optional): See :class:`~torch.optim.RMSprop`.
momentum (float, optional): See :class:`~torch.optim.RMSprop`.
weight_decay (float, optional): See :class:`~torch.optim.RMSprop`.
centered (bool, optional): See :class:`~torch.optim.RMSprop`.
"""
optimizer_cls = torch.optim.RMSprop
lr: float = hp.auto(torch.optim.RMSprop, 'lr', ignore_docstring_errors=True)
alpha: float = hp.auto(torch.optim.RMSprop, 'alpha', ignore_docstring_errors=True)
eps: float = hp.auto(torch.optim.RMSprop, 'eps', ignore_docstring_errors=True)
momentum: float = hp.auto(torch.optim.RMSprop, 'momentum', ignore_docstring_errors=True)
weight_decay: float = hp.auto(torch.optim.RMSprop, 'weight_decay', ignore_docstring_errors=True)
centered: float = hp.auto(torch.optim.RMSprop, 'centered', ignore_docstring_errors=True)
optimizer_registry = {
'adam': AdamHparams,
'adamw': AdamWHparams,
'decoupled_adamw': DecoupledAdamWHparams,
'radam': RAdamHparams,
'sgd': SGDHparams,
'decoupled_sgdw': DecoupledSGDWHparams,
'rmsprop': RMSpropHparams,
}