-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtraining_tests.jl
265 lines (190 loc) · 9.02 KB
/
training_tests.jl
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
@testitem "TrainState" setup=[SharedTestSetup] tags=[:helpers] begin
using Optimisers
rng = StableRNG(12345)
@testset "$mode" for (mode, aType, dev, ongpu) in MODES
model = Dense(3, 2)
opt = Adam(0.01f0)
ps, st = Lux.setup(Lux.replicate(rng), model) |> dev
tstate = Training.TrainState(model, ps, st, opt)
x = randn(Lux.replicate(rng), Float32, (3, 1))
opt_st = Optimisers.setup(opt, tstate.parameters)
@test check_approx(tstate.model, model)
@test check_approx(tstate.optimizer_state, opt_st)
@test tstate.step == 0
end
end
@testitem "AbstractADTypes" setup=[SharedTestSetup] tags=[:helpers] begin
using ADTypes, Optimisers
function _loss_function(model, ps, st, data)
y, st = model(data, ps, st)
return sum(y), st, ()
end
rng = StableRNG(12345)
@testset "$mode" for (mode, aType, dev, ongpu) in MODES
model = Dense(3, 2)
opt = Adam(0.01f0)
ps, st = Lux.setup(rng, model) |> dev
tstate = Training.TrainState(model, ps, st, opt)
x = randn(Lux.replicate(rng), Float32, (3, 1)) |> aType
for ad in (AutoZygote(), AutoTracker(), AutoReverseDiff(), AutoEnzyme())
ongpu && (ad isa AutoReverseDiff || ad isa AutoEnzyme) && continue
!LuxTestUtils.ENZYME_TESTING_ENABLED && ad isa AutoEnzyme && continue
grads, _, _, _ = Training.compute_gradients(ad, _loss_function, x, tstate)
tstate_ = Training.apply_gradients(tstate, grads)
@test tstate_.step == 1
@test tstate != tstate_
end
end
end
@testitem "Training API" setup=[SharedTestSetup] tags=[:helpers] begin
using ADTypes, Optimisers
mse = MSELoss()
rng = StableRNG(12345)
x_data = randn(rng, Float32, 4, 32)
y_data = evalpoly.(x_data, ((1, 2, 3),)) .- evalpoly.(x_data, ((5, 2),))
y_data = (y_data .- minimum(y_data)) ./ (maximum(y_data) - minimum(y_data))
dataset = [(x_data[:, i], y_data[:, i]) for i in Iterators.partition(1:32, 8)]
@testset "$mode" for (mode, aType, dev, ongpu) in MODES
model = Chain(Dense(4, 32, tanh), BatchNorm(32),
Dense(32, 32, tanh), BatchNorm(32), Dense(32, 4))
dataset_ = [dev((x, y)) for (x, y) in dataset]
opt = Adam(0.001f0)
@testset "$(ad)" for ad in (
AutoZygote(), AutoTracker(), AutoReverseDiff(), AutoEnzyme())
ongpu && (ad isa AutoReverseDiff || ad isa AutoEnzyme) && continue
!LuxTestUtils.ENZYME_TESTING_ENABLED && ad isa AutoEnzyme && continue
ps, st = Lux.setup(rng, model) |> dev
tstate = Training.TrainState(model, ps, st, opt)
initial_loss = first(mse(model, tstate.parameters, tstate.states, dataset_[1]))
for epoch in 1:1000, (x, y) in dataset_
grads, loss, _, tstate = allow_unstable() do
Training.compute_gradients(ad, mse, (x, y), tstate)
end
tstate = Training.apply_gradients!(tstate, grads)
end
for epoch in 1:1000, (x, y) in dataset_
grads, loss, _, tstate = allow_unstable() do
Training.single_train_step!(ad, mse, (x, y), tstate)
end
end
for epoch in 1:1000, (x, y) in dataset_
grads, loss, _, tstate = allow_unstable() do
Training.single_train_step(ad, mse, (x, y), tstate)
end
end
final_loss = first(mse(model, tstate.parameters, tstate.states, dataset_[1]))
@test final_loss * 100 < initial_loss
# Test the adjust API
tstate = Optimisers.adjust(tstate, 0.1f0)
@test tstate.optimizer_state.layer_1.weight.rule.eta ≈ 0.1f0
tstate = Optimisers.adjust(tstate; eta=0.5f0)
@test tstate.optimizer_state.layer_1.weight.rule.eta ≈ 0.5f0
Optimisers.adjust!(tstate, 0.01f0)
@test tstate.optimizer_state.layer_1.weight.rule.eta ≈ 0.01f0
Optimisers.adjust!(tstate; eta=0.11f0)
@test tstate.optimizer_state.layer_1.weight.rule.eta ≈ 0.11f0
end
struct AutoCustomAD <: ADTypes.AbstractADType end
ps, st = Lux.setup(rng, model) |> dev
tstate = Training.TrainState(model, ps, st, opt)
@test_throws ArgumentError Training.compute_gradients(
AutoCustomAD(), mse, dataset_[1], tstate)
end
end
@testitem "Enzyme: Invalidate Cache on State Update" setup=[SharedTestSetup] tags=[:helpers] skip=:(using LuxTestUtils; !LuxTestUtils.ENZYME_TESTING_ENABLED) begin
using ADTypes, Optimisers
mse = MSELoss()
function mse2(model, ps, st, (x, y))
z, st = model(x, ps, st)
return sum(abs2, z .- y), st, ()
end
rng = StableRNG(12345)
model = Chain(Dense(4 => 3), VariationalHiddenDropout(0.5f0), Dense(3 => 4))
ps, st = Lux.setup(rng, model)
x = randn(rng, Float32, 4, 32)
opt = Adam(0.001f0)
tstate = Training.TrainState(model, ps, st, opt)
_, _, _, tstate_new = @inferred Training.compute_gradients(
AutoEnzyme(), mse, (x, x), tstate)
@test tstate_new.states !== tstate.states
model = Chain(Dense(4 => 3), Dense(3 => 4))
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, opt)
_, _, _, tstate_new = @inferred Training.compute_gradients(
AutoEnzyme(), mse, (x, x), tstate)
@test @inferred(Training.compute_gradients(AutoEnzyme(), mse, (x, x), tstate_new)) isa
Any
_, _, _, tstate_new2 = @inferred Training.compute_gradients(
AutoEnzyme(), mse2, (x, x), tstate_new)
@test hasfield(typeof(tstate_new2.cache.extras), :forward)
@test hasfield(typeof(tstate_new2.cache.extras), :reverse)
rng = StableRNG(12345)
model = Chain(Dense(4 => 3), VariationalHiddenDropout(0.5f0), Dense(3 => 4))
ps, st = Lux.setup(rng, model)
x = randn(rng, Float32, 4, 32)
opt = Adam(0.001f0)
tstate = Training.TrainState(model, ps, st, opt)
_, _, _, tstate_new = @inferred Training.compute_gradients(
AutoEnzyme(), mse, (x, x), tstate)
@test tstate_new.states !== tstate.states
model = Chain(Dense(4 => 3), Dense(3 => 4))
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, opt)
_, _, _, tstate_new = @inferred Training.compute_gradients(
AutoEnzyme(), mse, (x, x), tstate)
@test @inferred(Training.compute_gradients(AutoEnzyme(), mse, (x, x), tstate_new)) isa
Any
_, _, _, tstate_new2 = @inferred Training.compute_gradients(
AutoEnzyme(), mse2, (x, x), tstate_new)
@test hasfield(typeof(tstate_new2.cache.extras), :forward)
@test hasfield(typeof(tstate_new2.cache.extras), :reverse)
end
@testitem "Compiled ReverseDiff" setup=[SharedTestSetup] tags=[:helpers] begin
using ADTypes, Optimisers, ReverseDiff
mse1 = MSELoss()
function mse2(model, ps, st, data)
l, st_, stats = mse1(model, ps, st, data)
return l, st_, (; data=2.0f0)
end
rng = StableRNG(12345)
dataset = [(randn(rng, Float32, 4, 32), randn(rng, Float32, 4, 32)) for _ in 1:100]
@testset "Unhandled Cases" begin
model = Chain(Dense(4, 32, tanh), BatchNorm(32),
Dense(32, 32, tanh), BatchNorm(32), Dense(32, 4))
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, Adam(0.001f0))
# Stateful models are not supported
@test_throws ArgumentError Training.compute_gradients(
AutoReverseDiff(; compile=true), mse1, dataset[1], tstate)
model = Chain(Dense(4, 32, tanh), Dense(32, 32, tanh), Dense(32, 4))
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, Adam(0.001f0))
# Loss functions that return non-empty `stats` are not supported
@test_throws ArgumentError Training.compute_gradients(
AutoReverseDiff(; compile=true), mse2, dataset[1], tstate)
struct StrangeModel <: Lux.AbstractLuxLayer end
function (m::StrangeModel)(x, ps, st)
return x, (; new_state=0.0)
end
model = StrangeModel()
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, Adam(0.001f0))
# Stateful models are not supported
@test_throws ArgumentError Training.compute_gradients(
AutoReverseDiff(; compile=true), mse1, dataset[1], tstate)
end
model = Chain(Dense(4, 32, tanh), Dense(32, 32, tanh), Dense(32, 4))
ps, st = Lux.setup(rng, model)
tstate = Training.TrainState(model, ps, st, Adam(0.001f0))
loss_initial = first(mse1(model, ps, st, dataset[1]))
for i in 1:100
for (x, y) in dataset
_, _, _, tstate = allow_unstable() do
Training.single_train_step!(
AutoReverseDiff(; compile=true), mse1, (x, y), tstate)
end
end
end
loss_final = first(mse1(model, tstate.parameters, tstate.states, dataset[1]))
@test loss_final * 100 < loss_initial
end