forked from SciML/Optimization.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizationReverseDiffExt.jl
266 lines (243 loc) · 9.31 KB
/
OptimizationReverseDiffExt.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
266
module OptimizationReverseDiffExt
import Optimization
import Optimization.SciMLBase: OptimizationFunction
import Optimization.ADTypes: AutoReverseDiff
# using SparseDiffTools, Symbolics
isdefined(Base, :get_extension) ? (using ReverseDiff, ReverseDiff.ForwardDiff) :
(using ..ReverseDiff, ..ReverseDiff.ForwardDiff)
struct OptimizationReverseDiffTag end
function default_chunk_size(len)
if len < ForwardDiff.DEFAULT_CHUNK_THRESHOLD
len
else
ForwardDiff.DEFAULT_CHUNK_THRESHOLD
end
end
function Optimization.instantiate_function(f, x, adtype::AutoReverseDiff,
p = SciMLBase.NullParameters(),
num_cons = 0)
_f = (θ, args...) -> first(f.f(θ, p, args...))
chunksize = default_chunk_size(length(x))
if f.grad === nothing
if adtype.compile
_tape = ReverseDiff.GradientTape(_f, x)
tape = ReverseDiff.compile(_tape)
grad = function (res, θ, args...)
ReverseDiff.gradient!(res, tape, θ)
end
else
cfg = ReverseDiff.GradientConfig(x)
grad = (res, θ, args...) -> ReverseDiff.gradient!(res, x -> _f(x, args...), θ, cfg)
end
else
grad = (G, θ, args...) -> f.grad(G, θ, p, args...)
end
if f.hess === nothing
if adtype.compile
T = ForwardDiff.Tag(OptimizationReverseDiffTag(),eltype(x))
xdual = ForwardDiff.Dual{typeof(T),eltype(x),chunksize}.(x, Ref(ForwardDiff.Partials((ones(eltype(x), chunksize)...,))))
h_tape = ReverseDiff.GradientTape(_f, xdual)
htape = ReverseDiff.compile(h_tape)
function g(θ)
res1 = zeros(eltype(θ), length(θ))
ReverseDiff.gradient!(res1, htape, θ)
end
jaccfg = ForwardDiff.JacobianConfig(g, x, ForwardDiff.Chunk{chunksize}(), T)
hess = function (res, θ, args...)
ForwardDiff.jacobian!(res, g, θ, jaccfg, Val{false}())
end
else
hess = function (res, θ, args...)
ReverseDiff.hessian!(res, x -> _f(x, args...), θ)
end
end
else
hess = (H, θ, args...) -> f.hess(H, θ, p, args...)
end
if f.hv === nothing
hv = function (H, θ, v, args...)
# _θ = ForwardDiff.Dual.(θ, v)
# res = similar(_θ)
# grad(res, _θ, args...)
# H .= getindex.(ForwardDiff.partials.(res), 1)
res = zeros(length(θ), length(θ))
hess(res, θ, args...)
H .= res * v
end
else
hv = f.hv
end
if f.cons === nothing
cons = nothing
else
cons = (res, θ) -> f.cons(res, θ, p)
cons_oop = (x) -> (_res = zeros(eltype(x), num_cons); cons(_res, x); _res)
end
if cons !== nothing && f.cons_j === nothing
if adtype.compile
_jac_tape = ReverseDiff.JacobianTape(cons_oop, x)
jac_tape = ReverseDiff.compile(_jac_tape)
cons_j = function (J, θ)
ReverseDiff.jacobian!(J, jac_tape, θ)
end
else
cjconfig = ReverseDiff.JacobianConfig(x)
cons_j = function (J, θ)
ReverseDiff.jacobian!(J, cons_oop, θ, cjconfig)
end
end
else
cons_j = (J, θ) -> f.cons_j(J, θ, p)
end
if cons !== nothing && f.cons_h === nothing
fncs = [(x) -> cons_oop(x)[i] for i in 1:num_cons]
if adtype.compile
consh_tapes = ReverseDiff.GradientTape.(fncs, Ref(xdual))
conshtapes = ReverseDiff.compile.(consh_tapes)
function grad_cons(θ, htape)
res1 = zeros(eltype(θ), length(θ))
ReverseDiff.gradient!(res1, htape, θ)
end
gs = [x -> grad_cons(x, conshtapes[i]) for i in 1:num_cons]
jaccfgs = [ForwardDiff.JacobianConfig(gs[i], x, ForwardDiff.Chunk{chunksize}(), T) for i in 1:num_cons]
cons_h = function (res, θ)
for i in 1:num_cons
ForwardDiff.jacobian!(res[i], gs[i], θ, jaccfgs[i], Val{false}())
end
end
else
cons_h = function (res, θ)
for i in 1:num_cons
ReverseDiff.hessian!(res[i], fncs[i], θ)
end
end
end
else
cons_h = (res, θ) -> f.cons_h(res, θ, p)
end
if f.lag_h === nothing
lag_h = nothing # Consider implementing this
else
lag_h = (res, θ, σ, μ) -> f.lag_h(res, θ, σ, μ, p)
end
return OptimizationFunction{true}(f.f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
hess_prototype = f.hess_prototype,
cons_jac_prototype = f.cons_jac_prototype,
cons_hess_prototype = f.cons_hess_prototype,
lag_h, f.lag_hess_prototype)
end
function Optimization.instantiate_function(f, cache::Optimization.ReInitCache,
adtype::AutoReverseDiff, num_cons = 0)
_f = (θ, args...) -> first(f.f(θ, cache.p, args...))
chunksize = default_chunk_size(length(cache.u0))
if f.grad === nothing
if adtype.compile
_tape = ReverseDiff.GradientTape(_f, cache.u0)
tape = ReverseDiff.compile(_tape)
grad = function (res, θ, args...)
ReverseDiff.gradient!(res, tape, θ)
end
else
cfg = ReverseDiff.GradientConfig(cache.u0)
grad = (res, θ, args...) -> ReverseDiff.gradient!(res, x -> _f(x, args...), θ, cfg)
end
else
grad = (G, θ, args...) -> f.grad(G, θ, cache.p, args...)
end
if f.hess === nothing
if adtype.compile
T = ForwardDiff.Tag(OptimizationReverseDiffTag(),eltype(cache.u0))
xdual = ForwardDiff.Dual{typeof(T),eltype(cache.u0),chunksize}.(cache.u0, Ref(ForwardDiff.Partials((ones(eltype(cache.u0), chunksize)...,))))
h_tape = ReverseDiff.GradientTape(_f, xdual)
htape = ReverseDiff.compile(h_tape)
function g(θ)
res1 = zeros(eltype(θ), length(θ))
ReverseDiff.gradient!(res1, htape, θ)
end
jaccfg = ForwardDiff.JacobianConfig(g, cache.u0, ForwardDiff.Chunk{chunksize}(), T)
hess = function (res, θ, args...)
ForwardDiff.jacobian!(res, g, θ, jaccfg, Val{false}())
end
else
hess = function (res, θ, args...)
ReverseDiff.hessian!(res, x -> _f(x, args...), θ)
end
end
else
hess = (H, θ, args...) -> f.hess(H, θ, cache.p, args...)
end
if f.hv === nothing
hv = function (H, θ, v, args...)
# _θ = ForwardDiff.Dual.(θ, v)
# res = similar(_θ)
# grad(res, θ, args...)
# H .= getindex.(ForwardDiff.partials.(res), 1)
res = zeros(length(θ), length(θ))
hess(res, θ, args...)
H .= res * v
end
else
hv = f.hv
end
if f.cons === nothing
cons = nothing
else
cons = (res, θ) -> f.cons(res, θ, cache.p)
cons_oop = (x) -> (_res = zeros(eltype(x), num_cons); cons(_res, x); _res)
end
if cons !== nothing && f.cons_j === nothing
if adtype.compile
_jac_tape = ReverseDiff.JacobianTape(cons_oop, cache.u0)
jac_tape = ReverseDiff.compile(_jac_tape)
cons_j = function (J, θ)
ReverseDiff.jacobian!(J, jac_tape, θ)
end
else
cjconfig = ReverseDiff.JacobianConfig(cache.u0)
cons_j = function (J, θ)
ReverseDiff.jacobian!(J, cons_oop, θ, cjconfig)
end
end
else
cons_j = (J, θ) -> f.cons_j(J, θ, cache.p)
end
if cons !== nothing && f.cons_h === nothing
fncs = [(x) -> cons_oop(x)[i] for i in 1:num_cons]
if adtype.compile
consh_tapes = ReverseDiff.GradientTape.(fncs, Ref(xdual))
conshtapes = ReverseDiff.compile.(consh_tapes)
function grad_cons(θ, htape)
res1 = zeros(eltype(θ), length(θ))
ReverseDiff.gradient!(res1, htape, θ)
end
gs = [x -> grad_cons(x, conshtapes[i]) for i in 1:num_cons]
jaccfgs = [ForwardDiff.JacobianConfig(gs[i], cache.u0, ForwardDiff.Chunk{chunksize}(), T) for i in 1:num_cons]
cons_h = function (res, θ)
for i in 1:num_cons
ForwardDiff.jacobian!(res[i], gs[i], θ, jaccfgs[i], Val{false}())
end
end
else
cons_h = function (res, θ)
for i in 1:num_cons
ReverseDiff.hessian!(res[i], fncs[i], θ)
end
end
end
else
cons_h = (res, θ) -> f.cons_h(res, θ, cache.p)
end
if f.lag_h === nothing
lag_h = nothing # Consider implementing this
else
lag_h = (res, θ, σ, μ) -> f.lag_h(res, θ, σ, μ, cache.p)
end
return OptimizationFunction{true}(f.f, adtype; grad = grad, hess = hess, hv = hv,
cons = cons, cons_j = cons_j, cons_h = cons_h,
hess_prototype = f.hess_prototype,
cons_jac_prototype = f.cons_jac_prototype,
cons_hess_prototype = f.cons_hess_prototype,
lag_h, f.lag_hess_prototype)
end
end