-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathquadrature_adjoint.jl
518 lines (463 loc) · 17.1 KB
/
quadrature_adjoint.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
struct ODEQuadratureAdjointSensitivityFunction{C <: AdjointDiffCache,
Alg <: QuadratureAdjoint,
uType, SType,
fType <: DiffEqBase.AbstractDiffEqFunction
} <: SensitivityFunction
diffcache::C
sensealg::Alg
discrete::Bool
y::uType
sol::SType
f::fType
end
TruncatedStacktraces.@truncate_stacktrace ODEQuadratureAdjointSensitivityFunction
function ODEQuadratureAdjointSensitivityFunction(g, sensealg, discrete, sol, dgdu, dgdp,
alg)
diffcache, y = adjointdiffcache(
g, sensealg, discrete, sol, dgdu, dgdp, sol.prob.f, alg;
quad = true)
return ODEQuadratureAdjointSensitivityFunction(diffcache, sensealg, discrete,
y, sol, sol.prob.f)
end
# u = λ'
function (S::ODEQuadratureAdjointSensitivityFunction)(du, u, p, t)
@unpack sol, discrete = S
f = sol.prob.f
λ, grad, y, dλ, dgrad, dy = split_states(du, u, t, S)
vecjacobian!(dλ, y, λ, p, t, S)
dλ .*= -one(eltype(λ))
discrete || accumulate_cost!(dλ, y, p, t, S)
return nothing
end
function (S::ODEQuadratureAdjointSensitivityFunction)(u, p, t)
@unpack sol, discrete = S
f = sol.prob.f
λ, grad, y, dgrad, dy = split_states(u, t, S)
dy, dλ, dgrad = vecjacobian(y, λ, p, t, S; dgrad = dgrad, dy = dy)
dλ *= (-one(eltype(λ)))
if !discrete
dλ, dgrad = accumulate_cost(dλ, y, p, t, S, dgrad)
end
return dλ
end
function split_states(du, u, t, S::ODEQuadratureAdjointSensitivityFunction; update = true)
@unpack y, sol = S
if update
if t isa ForwardDiff.Dual && eltype(y) <: AbstractFloat
y = sol(t, continuity = :right)
else
sol(y, t, continuity = :right)
end
end
λ = u
dλ = du
λ, nothing, y, dλ, nothing, nothing
end
function split_states(u, t, S::ODEQuadratureAdjointSensitivityFunction; update = true)
@unpack y, sol = S
if update
y = sol(t, continuity = :right)
end
λ = u
λ, nothing, y, nothing, nothing
end
# g is either g(t,u,p) or discrete g(t,u,i)
@noinline function ODEAdjointProblem(sol, sensealg::QuadratureAdjoint, alg,
t = nothing,
dgdu_discrete::DG1 = nothing,
dgdp_discrete::DG2 = nothing,
dgdu_continuous::DG3 = nothing,
dgdp_continuous::DG4 = nothing,
g::G = nothing,
::Val{RetCB} = Val(false);
callback = CallbackSet()) where {DG1, DG2, DG3, DG4, G,
RetCB}
dgdu_discrete === nothing && dgdu_continuous === nothing && g === nothing &&
error("Either `dgdu_discrete`, `dgdu_continuous`, or `g` must be specified.")
t !== nothing && dgdu_discrete === nothing && dgdp_discrete === nothing &&
error("It looks like you're using the direct `adjoint_sensitivities` interface
with a discrete cost function but no specified `dgdu_discrete` or `dgdp_discrete`.
Please use the higher level `solve` interface or specify these two contributions.")
@unpack p, u0, tspan = sol.prob
## Force recompile mode until vjps are specialized to handle this!!!
f = if sol.prob.f isa ODEFunction &&
sol.prob.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper
ODEFunction{isinplace(sol.prob), true}(unwrapped_f(sol.prob.f))
else
sol.prob.f
end
terminated = false
if hasfield(typeof(sol), :retcode)
if sol.retcode == ReturnCode.Terminated
tspan = (tspan[1], last(current_time(sol)))
terminated = true
end
end
tspan = reverse(tspan)
discrete = (t !== nothing &&
(dgdu_continuous === nothing && dgdp_continuous === nothing ||
g !== nothing))
if ArrayInterface.ismutable(u0)
len = length(u0)
λ = similar(u0, len)
λ .= false
else
λ = zero(u0)
end
sense = ODEQuadratureAdjointSensitivityFunction(g, sensealg, discrete, sol,
dgdu_continuous, dgdp_continuous, alg)
init_cb = (discrete || dgdu_discrete !== nothing) # && tspan[1] == t[end]
z0 = vec(zero(λ))
cb, rcb, _ = generate_callbacks(sense, dgdu_discrete, dgdp_discrete,
λ, t, tspan[2],
callback, init_cb, terminated)
jac_prototype = sol.prob.f.jac_prototype
adjoint_jac_prototype = !sense.discrete || jac_prototype === nothing ? nothing :
copy(jac_prototype')
original_mm = sol.prob.f.mass_matrix
if original_mm === I || original_mm === (I, I)
odefun = ODEFunction{ArrayInterface.ismutable(z0), true}(sense,
jac_prototype = adjoint_jac_prototype)
else
odefun = ODEFunction{ArrayInterface.ismutable(z0), true}(sense,
mass_matrix = sol.prob.f.mass_matrix',
jac_prototype = adjoint_jac_prototype)
end
if RetCB
return ODEProblem(odefun, z0, tspan, p, callback = cb), rcb
else
return ODEProblem(odefun, z0, tspan, p, callback = cb)
end
end
struct AdjointSensitivityIntegrand{pType, uType, lType, rateType, S, AS, PF, PJC, PJT, DGP,
G}
sol::S
adj_sol::AS
p::pType
y::uType
λ::lType
pf::PF
f_cache::rateType
pJ::PJT
paramjac_config::PJC
sensealg::QuadratureAdjoint
dgdp_cache::DGP
dgdp::G
end
function AdjointSensitivityIntegrand(sol, adj_sol, sensealg, dgdp = nothing)
prob = sol.prob
adj_prob = adj_sol.prob
@unpack f, tspan = prob
p = parameter_values(prob)
u0 = state_values(prob)
numparams = length(p)
y = zero(state_values(prob))
λ = zero(state_values(adj_prob))
# we need to alias `y`
f_cache = zero(y)
isautojacvec = get_jacvec(sensealg)
unwrappedf = unwrapped_f(f)
dgdp_cache = dgdp === nothing ? nothing : zero(p)
if sensealg.autojacvec isa ReverseDiffVJP
tape = if DiffEqBase.isinplace(prob)
ReverseDiff.GradientTape((y, prob.p, [tspan[2]])) do u, p, t
du1 = similar(p, size(u))
du1 .= false
unwrappedf(du1, u, p, first(t))
return vec(du1)
end
else
ReverseDiff.GradientTape((y, prob.p, [tspan[2]])) do u, p, t
vec(unwrappedf(u, p, first(t)))
end
end
if compile_tape(sensealg.autojacvec)
paramjac_config = ReverseDiff.compile(tape)
else
paramjac_config = tape
end
pf = nothing
pJ = nothing
elseif sensealg.autojacvec isa EnzymeVJP
pf = let f = unwrappedf
if DiffEqBase.isinplace(prob) && prob isa RODEProblem
function (out, u, _p, t, W)
f(out, u, _p, t, W)
nothing
end
elseif DiffEqBase.isinplace(prob)
function (out, u, _p, t)
f(out, u, _p, t)
nothing
end
elseif !DiffEqBase.isinplace(prob) && prob isa RODEProblem
function (out, u, _p, t, W)
out .= f(u, _p, t, W)
nothing
end
else
!DiffEqBase.isinplace(prob)
function (out, u, _p, t)
out .= f(u, _p, t)
nothing
end
end
end
paramjac_config = zero(y), zero(y), Enzyme.make_zero(pf)
pJ = nothing
elseif isautojacvec # Zygote
paramjac_config = nothing
pf = nothing
pJ = nothing
else
pf = DiffEqBase.ParamJacobianWrapper(unwrappedf, tspan[1], y)
pJ = similar(u0, length(u0), numparams)
pJ .= false
paramjac_config = build_param_jac_config(sensealg, pf, y, p)
end
AdjointSensitivityIntegrand(sol, adj_sol, p, y, λ, pf, f_cache, pJ, paramjac_config,
sensealg, dgdp_cache, dgdp)
end
# out = λ df(u, p, t)/dp at u=y, p=p, t=t
function vec_pjac!(out, λ, y, t, S::AdjointSensitivityIntegrand)
@unpack pJ, pf, p, f_cache, dgdp_cache, paramjac_config, sensealg, sol, adj_sol = S
f = sol.prob.f
isautojacvec = get_jacvec(sensealg)
# y is aliased
if !isautojacvec
if DiffEqBase.has_paramjac(f)
f.paramjac(pJ, y, p, t) # Calculate the parameter Jacobian into pJ
else
pf.t = t
pf.u = y
jacobian!(pJ, pf, p, f_cache, sensealg, paramjac_config)
end
mul!(out', λ', pJ)
elseif sensealg.autojacvec isa ReverseDiffVJP
tape = paramjac_config
tu, tp, tt = ReverseDiff.input_hook(tape)
output = ReverseDiff.output_hook(tape)
ReverseDiff.unseed!(tu) # clear any "leftover" derivatives from previous calls
ReverseDiff.unseed!(tp)
ReverseDiff.unseed!(tt)
ReverseDiff.value!(tu, y)
ReverseDiff.value!(tp, p)
ReverseDiff.value!(tt, [t])
ReverseDiff.forward_pass!(tape)
ReverseDiff.increment_deriv!(output, λ)
ReverseDiff.reverse_pass!(tape)
copyto!(vec(out), ReverseDiff.deriv(tp))
elseif sensealg.autojacvec isa ZygoteVJP
_dy, back = Zygote.pullback(p) do p
vec(f(y, p, t))
end
tmp = back(λ)
if tmp[1] === nothing
out[:] .= 0
else
out[:] .= vec(tmp[1])
end
elseif sensealg.autojacvec isa EnzymeVJP
tmp3, tmp4, tmp6 = paramjac_config
tmp4 .= λ
Enzyme.make_zero!(out)
Enzyme.make_zero!(tmp6)
Enzyme.autodiff(
Enzyme.Reverse, Enzyme.Duplicated(pf, tmp6), Enzyme.Const,
Enzyme.Duplicated(tmp3, tmp4),
Enzyme.Const(y), Enzyme.Duplicated(p, out), Enzyme.Const(t))
end
# TODO: Add tracker?
return out
end
function (S::AdjointSensitivityIntegrand)(out, t)
@unpack y, λ, pJ, pf, p, f_cache, dgdp_cache, paramjac_config, sensealg, sol, adj_sol = S
if ArrayInterface.ismutable(y)
sol(y, t)
adj_sol(λ, t)
else
y = sol(t)
λ = adj_sol(t)
end
vec_pjac!(out, λ, y, t, S)
if S.dgdp !== nothing
S.dgdp(dgdp_cache, y, p, t)
out .+= dgdp_cache
end
out'
end
function (S::AdjointSensitivityIntegrand)(t)
out = similar(S.p)
out .= false
S(out, t)
end
function _adjoint_sensitivities(sol, sensealg::QuadratureAdjoint, alg; t = nothing,
dgdu_discrete = nothing,
dgdp_discrete = nothing,
dgdu_continuous = nothing,
dgdp_continuous = nothing,
g = nothing,
abstol = sensealg.abstol, reltol = sensealg.reltol,
callback = CallbackSet(),
kwargs...)
adj_prob, rcb = ODEAdjointProblem(sol, sensealg, alg, t, dgdu_discrete, dgdp_discrete,
dgdu_continuous, dgdp_continuous, g, Val(true);
callback)
adj_sol = solve(adj_prob, alg; abstol = abstol, reltol = reltol,
save_everystep = true, save_start = true, kwargs...)
p = sol.prob.p
if p === nothing || p === DiffEqBase.NullParameters()
return state_values(adj_sol)[end], nothing
else
integrand = AdjointSensitivityIntegrand(sol, adj_sol, sensealg, dgdp_continuous)
if t === nothing
res, err = quadgk(integrand, sol.prob.tspan[1], sol.prob.tspan[2],
atol = abstol, rtol = reltol)
else
res = zero(integrand.p)'
# handle discrete dgdp contributions
if dgdp_discrete !== nothing
@unpack y = integrand
cur_time = length(t)
dgdp_cache = copy(res)
dgdp_discrete(dgdp_cache, y, p, t[cur_time], cur_time)
res .+= dgdp_cache
end
if callback !== nothing
cur_time = length(t)
dλ = similar(integrand.λ)
dλ .*= false
dgrad = similar(res)
dgrad .*= false
end
# correction for end interval.
if t[end] != sol.prob.tspan[2] && sol.retcode !== ReturnCode.Terminated
res .+= quadgk(integrand, t[end], sol.prob.tspan[end],
atol = abstol, rtol = reltol)[1]
end
if sol.retcode === ReturnCode.Terminated
integrand = update_integrand_and_dgrad(res, sensealg, callback, integrand,
adj_prob, sol, dgdu_discrete,
dgdp_discrete, dλ, dgrad, t[end],
cur_time)
end
for i in (length(t) - 1):-1:1
if ArrayInterface.ismutable(res)
res .+= quadgk(integrand, t[i], t[i + 1],
atol = abstol, rtol = reltol)[1]
else
res += quadgk(integrand, t[i], t[i + 1],
atol = abstol, rtol = reltol)[1]
end
if t[i] == t[i + 1]
integrand = update_integrand_and_dgrad(res, sensealg, callback,
integrand,
adj_prob, sol, dgdu_discrete,
dgdp_discrete, dλ, dgrad, t[i],
cur_time)
end
if dgdp_discrete !== nothing
@unpack y = integrand
dgdp_discrete(dgdp_cache, y, p, t[cur_time], cur_time)
res .+= dgdp_cache
end
(callback !== nothing || dgdp_discrete !== nothing) &&
(cur_time -= one(cur_time))
end
# correction for start interval
if t[1] != sol.prob.tspan[1]
res .+= quadgk(integrand, sol.prob.tspan[1], t[1],
atol = abstol, rtol = reltol)[1]
end
end
if rcb !== nothing && !isempty(rcb.Δλas)
iλ = zero(rcb.λ)
out = zero(res')
yy = similar(rcb.y)
yy .= false
for (Δλa, tt) in rcb.Δλas
@unpack algevar_idxs = rcb.diffcache
iλ[algevar_idxs] .= Δλa
sol(yy, tt)
vec_pjac!(out, iλ, yy, tt, integrand)
res .+= out'
iλ .= zero(eltype(iλ))
end
end
return state_values(adj_sol)[end], res
end
end
function update_p_integrand(integrand::AdjointSensitivityIntegrand, p)
@unpack sol, adj_sol, y, λ, pf, f_cache, pJ, paramjac_config, sensealg, dgdp_cache, dgdp = integrand
AdjointSensitivityIntegrand(sol, adj_sol, p, y, λ, pf, f_cache, pJ, paramjac_config,
sensealg, dgdp_cache, dgdp)
end
function update_integrand_and_dgrad(res, sensealg::QuadratureAdjoint, callbacks, integrand,
adj_prob, sol, dgdu_discrete, dgdp_discrete, dλ, dgrad,
ti, cur_time)
for cb in callbacks.discrete_callbacks
if ti ∈ cb.affect!.event_times
integrand = _update_integrand_and_dgrad(res, sensealg, cb,
integrand, adj_prob, sol,
dgdu_discrete,
dgdp_discrete, dλ, dgrad,
ti, cur_time)
end
end
for cb in callbacks.continuous_callbacks
if ti ∈ cb.affect!.event_times ||
ti ∈ cb.affect_neg!.event_times
integrand = _update_integrand_and_dgrad(res, sensealg, cb,
integrand, adj_prob, sol,
dgdu_discrete,
dgdp_discrete, dλ, dgrad,
ti, cur_time)
end
end
return integrand
end
function _update_integrand_and_dgrad(res, sensealg::QuadratureAdjoint, cb, integrand,
adj_prob, sol, dgdu, dgdp, dλ, dgrad, t, cur_time)
indx, pos_neg = get_indx(cb, t)
tprev = get_tprev(cb, indx, pos_neg)
wp = let tprev = tprev, pos_neg = pos_neg
function (dp, p, u, t)
_affect! = get_affect!(cb, pos_neg)
fakeinteg = FakeIntegrator([x for x in u], [x for x in p], t, tprev)
_affect!(fakeinteg)
dp .= fakeinteg.p
end
end
_p = similar(integrand.p, size(integrand.p))
_p .= false
wp(_p, integrand.p, integrand.y, t)
if _p != integrand.p
fakeSp = CallbackSensitivityFunction(wp, sensealg, adj_prob.f.f.diffcache, sol.prob)
#vjp with Jacobin given by dw/dp before event and vector given by grad
vecjacobian!(res, integrand.p, res, integrand.y, t, fakeSp;
dgrad = nothing, dy = nothing)
integrand = update_p_integrand(integrand, _p)
end
w = let tprev = tprev, pos_neg = pos_neg
function (du, u, p, t)
_affect! = get_affect!(cb, pos_neg)
fakeinteg = FakeIntegrator([x for x in u], [x for x in p], t, tprev)
_affect!(fakeinteg)
du .= vec(fakeinteg.u)
end
end
# Create a fake sensitivity function to do the vjps needs to be done
# to account for parameter dependence of affect function
fakeS = CallbackSensitivityFunction(w, sensealg, adj_prob.f.f.diffcache, sol.prob)
if dgdu !== nothing # discrete cost
dgdu(dλ, integrand.y, integrand.p, t, cur_time)
else
error("Please provide `dgdu` to use adjoint_sensitivities with `QuadratureAdjoint()` and callbacks.")
end
@assert dgdp === nothing
# account for implicit events
@. dλ = -dλ - integrand.λ
vecjacobian!(dλ, integrand.y, dλ, integrand.p, t, fakeS; dgrad = dgrad)
res .-= dgrad
return integrand
end