-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathfunctor_params.jl
More file actions
165 lines (148 loc) · 5.88 KB
/
functor_params.jl
File metadata and controls
165 lines (148 loc) · 5.88 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
using OrdinaryDiffEq, SciMLSensitivity, Zygote, Test, Reactant
using Functors: Functors, @functor
using LinearAlgebra: mul!
# Define a custom functor parameter struct that is NOT an AbstractArray
# and does NOT implement SciMLStructures
struct FunctorParams{W <: AbstractMatrix, B <: AbstractVector}
weights::W
bias::B
end
@functor FunctorParams
# ODE right-hand side using functor params (out-of-place)
function ode_f(u, p::FunctorParams, t)
return p.weights * u .+ p.bias
end
# ODE right-hand side using functor params (in-place)
# Note: uses .= instead of mul! because Zygote.Buffer doesn't support mul!
function ode_f!(du, u, p::FunctorParams, t)
du .= p.weights * u .+ p.bias
return nothing
end
# Reference: compute gradient using Zygote with plain array parameters
function reference_gradient_oop(u0, tspan, weights, bias)
flat_p = vcat(vec(weights), bias)
n = length(u0)
function ode_flat(u, p, t)
W = reshape(p[1:(n * n)], n, n)
b = p[(n * n + 1):end]
return W * u .+ b
end
gs = Zygote.gradient(flat_p) do p
prob = ODEProblem(ode_flat, u0, tspan, p)
sol = solve(
prob, Tsit5(); sensealg = GaussAdjoint(autojacvec = ZygoteVJP()),
abstol = 1.0e-12, reltol = 1.0e-12
)
return sum(abs2, last(sol.u))
end
return gs[1]
end
@testset "Functor Parameter Support" begin
@testset "Trait tests" begin
@test SciMLSensitivity.supports_functor_params(QuadratureAdjoint()) == false
@test SciMLSensitivity.supports_functor_params(GaussAdjoint()) == true
@test SciMLSensitivity.supports_functor_params(
GaussKronrodAdjoint()
) == true
@test SciMLSensitivity.supports_functor_params(InterpolatingAdjoint()) == false
@test SciMLSensitivity.supports_functor_params(BacksolveAdjoint()) == false
@test SciMLSensitivity.supports_structured_vjp(ZygoteVJP()) == true
@test SciMLSensitivity.supports_structured_vjp(EnzymeVJP()) == true
@test SciMLSensitivity.supports_structured_vjp(ReactantVJP()) == true
@test SciMLSensitivity.supports_structured_vjp(ReverseDiffVJP()) == false
@test SciMLSensitivity.supports_structured_vjp(false) == false
@test SciMLSensitivity.supports_structured_vjp(true) == false
end
# Setup
n = 2
u0 = Float64[1.0, 2.0]
tspan = (0.0, 0.5)
weights = Float64[-0.5 0.1; -0.1 -0.3]
bias = Float64[0.1, -0.2]
p0 = FunctorParams(weights, bias)
# Reference gradient (ForwardDiff on flat vector)
ref_grad = reference_gradient_oop(u0, tspan, weights, bias)
function loss_oop(p_func, sensealg)
prob = ODEProblem(ode_f, u0, tspan, p_func)
sol = solve(prob, Tsit5(); sensealg, abstol = 1.0e-12, reltol = 1.0e-12)
return sum(abs2, last(sol.u))
end
function loss_ip(p_func, sensealg)
prob = ODEProblem(ode_f!, u0, tspan, p_func)
sol = solve(prob, Tsit5(); sensealg, abstol = 1.0e-12, reltol = 1.0e-12)
return sum(abs2, last(sol.u))
end
function extract_flat_grad(g::FunctorParams)
return vcat(vec(g.weights), g.bias)
end
function extract_flat_grad(g::NamedTuple)
return vcat(vec(g.weights), g.bias)
end
@testset "GaussAdjoint + ZygoteVJP (out-of-place)" begin
sensealg = GaussAdjoint(autojacvec = ZygoteVJP())
gs = Zygote.gradient(p0) do p
loss_oop(p, sensealg)
end
g = gs[1]
@test g !== nothing
flat_g = extract_flat_grad(g)
@test isapprox(flat_g, ref_grad, rtol = 1.0e-4)
end
@testset "GaussAdjoint + ZygoteVJP (in-place)" begin
sensealg = GaussAdjoint(autojacvec = ZygoteVJP())
gs = Zygote.gradient(p0) do p
loss_ip(p, sensealg)
end
g = gs[1]
@test g !== nothing
flat_g = extract_flat_grad(g)
@test isapprox(flat_g, ref_grad, rtol = 1.0e-4)
end
@testset "Error tests: unsupported algorithms with functor params" begin
# InterpolatingAdjoint doesn't support functor params
@test_throws SciMLSensitivity.AdjointSensitivityParameterCompatibilityError begin
Zygote.gradient(p0) do p
prob = ODEProblem(ode_f, u0, tspan, p)
sol = solve(
prob, Tsit5(); sensealg = InterpolatingAdjoint(), abstol = 1.0e-12,
reltol = 1.0e-12
)
return sum(abs2, last(sol.u))
end
end
# BacksolveAdjoint doesn't support functor params
@test_throws SciMLSensitivity.AdjointSensitivityParameterCompatibilityError begin
Zygote.gradient(p0) do p
prob = ODEProblem(ode_f, u0, tspan, p)
sol = solve(
prob, Tsit5(); sensealg = BacksolveAdjoint(), abstol = 1.0e-12,
reltol = 1.0e-12
)
return sum(abs2, last(sol.u))
end
end
# QuadratureAdjoint doesn't support functor params
@test_throws SciMLSensitivity.AdjointSensitivityParameterCompatibilityError begin
Zygote.gradient(p0) do p
prob = ODEProblem(ode_f, u0, tspan, p)
sol = solve(
prob, Tsit5(); sensealg = QuadratureAdjoint(), abstol = 1.0e-12,
reltol = 1.0e-12
)
return sum(abs2, last(sol.u))
end
end
# GaussAdjoint + ReverseDiffVJP doesn't support functor params
@test_throws ErrorException begin
Zygote.gradient(p0) do p
prob = ODEProblem(ode_f, u0, tspan, p)
sol = solve(
prob, Tsit5();
sensealg = GaussAdjoint(autojacvec = ReverseDiffVJP()),
abstol = 1.0e-12, reltol = 1.0e-12
)
return sum(abs2, last(sol.u))
end
end
end
end