Skip to content

Commit 17daa1d

Browse files
mcabbottToucheSir
andauthored
Add some tests using StaticArrays (#39)
* move many tests to a new file * add one more simple test of all rules * add a test with StaticArrays * feature test for OptimiserChain which discovered a bug * add overall testsets * add some type promotion tests * don't call every loss loss * fixup * also test non-array gradient types * fix indenting, only whitespace * Update test/rules.jl Co-authored-by: Brian Chen <[email protected]> Co-authored-by: Brian Chen <[email protected]>
1 parent be3c943 commit 17daa1d

File tree

4 files changed

+262
-141
lines changed

4 files changed

+262
-141
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ julia = "1.6"
1515

1616
[extras]
1717
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18+
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
19+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1820
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
1921

2022
[targets]
21-
test = ["Test", "Zygote"]
23+
test = ["Test", "ChainRulesCore", "StaticArrays", "Zygote"]

src/rules.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Descent() = Descent(1f-1)
1616
init(o::Descent, x::AbstractArray) = nothing
1717

1818
function apply!(o::Descent, state, x, dx)
19-
η = convert(float(eltype(dx)), o.eta)
19+
η = convert(float(eltype(x)), o.eta)
2020

2121
return state, @.. dx * η
2222
end
@@ -477,7 +477,7 @@ ClipGrad() = ClipGrad(10f0)
477477
init(o::ClipGrad, x::AbstractArray) = nothing
478478

479479
function apply!(o::ClipGrad, state, x, dx)
480-
δ = convert(float(eltype(dx)), o.delta)
480+
δ = convert(float(eltype(x)), o.delta)
481481
dx′ = @.. clamp(dx, -δ, δ)
482482

483483
return state, dx′

test/rules.jl

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using Optimisers
2+
using ChainRulesCore, Functors, StaticArrays, Zygote
3+
using LinearAlgebra, Statistics, Test, Random
4+
5+
Random.seed!(1)
6+
7+
RULES = [
8+
# All the rules at default settings
9+
Descent(), ADAM(), Momentum(), Nesterov(), RMSProp(),
10+
ADAGrad(), AdaMax(), ADADelta(), AMSGrad(), NADAM(),
11+
ADAMW(), RADAM(), OADAM(), AdaBelief(),
12+
# A few chained combinations:
13+
OptimiserChain(WeightDecay(), ADAM(0.001)),
14+
OptimiserChain(ClipNorm(), ADAM(0.001)),
15+
OptimiserChain(ClipGrad(0.5), Momentum()),
16+
]
17+
18+
name(o) = typeof(o).name.name
19+
name(o::OptimiserChain) = join(name.(o.opts), "")
20+
21+
@testset "independence" begin
22+
@testset "$(name(o))" for o in RULES
23+
w = randn(10, 10)
24+
w′ = randn(10, 10)
25+
iloss(x, w, w′) = mean((w*x .- w′*x) .^ 2)
26+
@test iloss(rand(10, 10), w, w′) > 1
27+
st = Optimisers.setup(o, w)
28+
for t = 1:10^5
29+
x = rand(10)
30+
gs = gradient(w -> iloss(x, w, w′), w)
31+
st, w = Optimisers.update!(st, w, gs...)
32+
end
33+
@test iloss(rand(10, 10), w, w′) < 0.01
34+
end
35+
end
36+
37+
@testset verbose=true "simple sum" begin
38+
@testset "$(name(o))" for o in RULES
39+
m = shuffle!(reshape(1:64, 8, 8) .+ 0.0)
40+
s = Optimisers.setup(o, m)
41+
for _ in 1:10^5
42+
g = gradient(x -> sum(abs2, x + x'), m)[1]
43+
s, m = Optimisers.update!(s, m, g)
44+
end
45+
# @test sum(m) < sum(1:64)
46+
if sum(m) < 1
47+
@test sum(m) < 1
48+
else
49+
@show name(o) sum(m)/sum(1:64)
50+
@test_broken sum(m) < 1
51+
end
52+
end
53+
end
54+
55+
@testset "original" begin
56+
@testset "$(name(o))" for o in RULES
57+
w′ == rand(3, 3), β = rand(3, 3))
58+
w == 5rand(3, 3), β = rand(3, 3))
59+
st = Optimisers.setup(o, w)
60+
loss(x, y) = mean((x.α .* x.β .- y.α .* y.β) .^ 2)
61+
@test loss(w, w′) > 1
62+
for i = 1:10^4
63+
gs = gradient(x -> loss(x, w′), w)
64+
st, w = Optimisers.update(st, w, gs...)
65+
end
66+
lw = loss(w, w′)
67+
if o isa ADADelta
68+
@show name(o) loss(w, w′)
69+
@test_broken lw < 0.001
70+
else
71+
@test lw < 0.001
72+
end
73+
end
74+
end
75+
76+
@testset verbose=true "StaticArrays" begin
77+
@testset "$(name(o))" for o in RULES
78+
W1 = @SMatrix randn(10, 10)
79+
b1 = @SVector randn(10)
80+
W2 = @SMatrix randn(10, 10)
81+
model = (; W1, b1, W2, tanh)
82+
s_loss(m, x, y) = sum(abs2, m.W2 * (m.tanh).(m.W1*x .+ m.b1) .- y)
83+
# x = @SMatrix randn(10, 10)
84+
# y = @SMatrix randn(10, 10) # gives an error from sum(; dims=())
85+
x = @SVector randn(10)
86+
y = @SVector randn(10)
87+
@test s_loss(model, x, y) > 10
88+
state = Optimisers.setup(o, model)
89+
for t = 1:10^3
90+
g = gradient(m -> s_loss(m, x, y), model)[1]
91+
state, model = Optimisers.update!(state, model, g)
92+
end
93+
if o isa Union{Descent, RMSProp, ADAGrad, ADADelta, NADAM}
94+
@show name(o) s_loss(model, x, y)
95+
@test_broken s_loss(model, x, y) < 1
96+
else
97+
@test s_loss(model, x, y) < 1
98+
end
99+
end
100+
end
101+
102+
@testset verbose=true "element types" begin
103+
@testset "$(name(o))" for o in RULES
104+
marray = (Float32[1,2], Float64[3,4], Float16[5,6])
105+
types = map(eltype, marray)
106+
107+
# This is a weak test, as it copies & then does `update!`
108+
uparray = Optimisers.update(Optimisers.setup(o, marray), marray, marray)[2]
109+
@test map(eltype, uparray) == types
110+
111+
# Static version is truly out-of-place:
112+
mstatic = (SA{Float32}[1,2], SA{Float64}[3,4]) # , SA{Float16}[5,6]) with Float16, all fail
113+
upstatic = Optimisers.update(Optimisers.setup(o, mstatic), mstatic, mstatic)[2]
114+
if o isa OptimiserChain && o.opts[2] isa ADAM # These promote to Float64
115+
@test_broken map(eltype, upstatic) == types[1:2]
116+
else
117+
@test map(eltype, upstatic) == types[1:2]
118+
end
119+
@test upstatic[1] isa SVector
120+
121+
# With ordinary Array gradient, what happens?
122+
upstatic2 = Optimisers.update(Optimisers.setup(o, mstatic), mstatic, marray[1:2])[2]
123+
# @test map(eltype, upstatic2) == types[1:2] # same information
124+
if upstatic2[1] isa SVector
125+
@test upstatic2[1] isa SVector
126+
else
127+
@test_broken upstatic2[1] isa SVector
128+
end
129+
end
130+
end
131+
132+
@testset "gradient types" begin
133+
@testset "$(name(o))" for o in RULES
134+
x = (a = ones(2,2), b = transpose(ones(2,2)))
135+
s = Optimisers.setup(o, x)
136+
137+
_, x1 = Optimisers.update(s, x, (a = [1 2; 3 4], b = nothing))
138+
@test x1.a != ones(2,2)
139+
@test x1.b == ones(2,2)
140+
141+
_, xfill = Optimisers.update(s, x, (a = Zygote.Fill(2.0,2,2), b = Zygote.Fill(true,2,2)))
142+
@test xfill.a != ones(2,2)
143+
@test xfill.b != ones(2,2)
144+
145+
bc = Optimisers.@.. 1 + log([2 3; 4 5]) / 6
146+
_, xbc = Optimisers.update(s, x, (a = bc, b = bc))
147+
@test xbc.a != ones(2,2)
148+
@test xbc.b != ones(2,2)
149+
150+
th = ChainRulesCore.@thunk @. 1 + log([2 3; 4 5]) / 6
151+
_, xth = Optimisers.update(s, x, (a = bc, b = bc))
152+
@test xth.a != ones(2,2)
153+
@test xth.b != ones(2,2)
154+
end
155+
end
156+

0 commit comments

Comments
 (0)