[cad161]: / tests / training / test_optimizer.py

Download this file

175 lines (151 with data), 4.2 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
# ruff:noqa:E402
import pytest
try:
import torch.nn
except ImportError:
torch = None
if torch is None:
pytest.skip("torch not installed", allow_module_level=True)
pytest.importorskip("rich")
from edsnlp.training.optimizer import LinearSchedule, ScheduledOptimizer
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = torch.nn.Linear(10, 1)
self.fc2 = torch.nn.Linear(1, 1)
def forward(self, x):
return self.fc(x)
@pytest.fixture(scope="module")
def net():
net = Net()
return net
@pytest.mark.parametrize(
"groups",
[
# Old schedule API
{
"fc1[.].*": {
"lr": 0.1,
"weight_decay": 0.01,
"schedules": [
{
"@schedules": "linear",
"start_value": 0.0,
"warmup_rate": 0.2,
},
],
},
"fc2[.]bias": False,
"": {
"lr": 0.0001,
"weight_decay": 0.0,
},
},
# New schedule API
{
"fc1[.].*": {
"lr": {
"@schedules": "linear",
"start_value": 0.0,
"max_value": 0.1,
"warmup_rate": 0.2,
},
"weight_decay": 0.01,
},
"fc2[.]bias": False,
"": {
"lr": 0.0001,
"weight_decay": 0.0,
},
},
],
)
def test_old_parameter_selection(net, groups):
optim = ScheduledOptimizer(
optim="adamw",
module=net,
groups=groups,
total_steps=10,
)
assert len(optim.state) == 0
optim.initialize()
assert all([p in optim.state for p in net.fc1.parameters()])
optim.state = optim.state
fc1_group = optim.param_groups[1]
assert fc1_group["lr"] == pytest.approx(0.0)
assert fc1_group["weight_decay"] == pytest.approx(0.01)
assert set(fc1_group["params"]) == {net.fc1.weight, net.fc1.bias}
fc2_group = optim.param_groups[0]
assert fc2_group["lr"] == pytest.approx(0.0001)
assert set(fc2_group["params"]) == {net.fc2.weight}
lr_values = [fc1_group["lr"]]
for i in range(10):
optim.step()
lr_values.append(fc1_group["lr"])
assert lr_values == pytest.approx(
[
0.0,
0.05,
0.1,
0.0875,
0.075,
0.0625,
0.05,
0.0375,
0.025,
0.0125,
0.0,
]
)
def test_serialization(net):
optim = ScheduledOptimizer(
optim="adamw",
module=net,
groups={
"fc1[.].*": {
"lr": 0.1,
"weight_decay": 0.01,
"schedules": LinearSchedule(start_value=0.0, warmup_rate=0.2),
},
"fc2[.]bias": False,
"": {
"lr": 0.0001,
"weight_decay": 0.0,
},
},
total_steps=10,
)
optim.initialize()
optim.param_groups = optim.param_groups
state_dict = None
for i in range(10):
if i == 5:
state_dict = optim.state_dict()
optim.step()
assert optim.param_groups[-1]["lr"] == pytest.approx(0.0)
optim.load_state_dict(state_dict)
assert optim.param_groups[-1]["lr"] == pytest.approx(0.0625)
optim.reset()
def test_repr(net):
optim = ScheduledOptimizer(
optim="adamw",
module=net,
groups={
"fc1[.].*": {
"lr": 0.1,
"weight_decay": 0.01,
"schedules": [
LinearSchedule(start_value=0.0, warmup_rate=0.2),
LinearSchedule(path="weight_decay"),
],
},
"fc2[.]bias": False,
".*": {
"lr": 0.0001,
"weight_decay": 0.0,
},
},
total_steps=10,
)
optim.initialize()
assert "ScheduledOptimizer[AdamW]" in repr(optim)