-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
concrete_solve.jl
1143 lines (1050 loc) · 50.4 KB
/
concrete_solve.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## High level
# Here is where we can add a default algorithm for computing sensitivities
# Based on problem information!
function inplace_vjp(prob, u0, p, verbose)
du = copy(u0)
ez = try
Enzyme.autodiff(Enzyme.Duplicated(du, du),
copy(u0), copy(p), prob.tspan[1]) do out, u, _p, t
prob.f(out, u, _p, t)
nothing
end
true
catch
false
end
if ez
return EnzymeVJP()
end
# Determine if we can compile ReverseDiff
compile = try
if DiffEqBase.isinplace(prob)
!hasbranching(prob.f, copy(u0), u0, p, prob.tspan[1])
else
!hasbranching(prob.f, u0, p, prob.tspan[1])
end
catch
false
end
vjp = try
ReverseDiff.GradientTape((copy(u0), p, [prob.tspan[1]])) do u, p, t
du1 = similar(u, size(u))
prob.f(du1, u, p, first(t))
return vec(du1)
end
ReverseDiffVJP(compile)
catch
false
end
return vjp
end
function automatic_sensealg_choice(prob::Union{ODEProblem, SDEProblem}, u0, p, verbose)
default_sensealg = if p !== DiffEqBase.NullParameters() &&
!(eltype(u0) <: ForwardDiff.Dual) &&
!(eltype(p) <: ForwardDiff.Dual) &&
!(eltype(u0) <: Complex) &&
!(eltype(p) <: Complex) &&
length(u0) + length(p) <= 100
ForwardDiffSensitivity()
elseif u0 isa GPUArraysCore.AbstractGPUArray || !DiffEqBase.isinplace(prob)
# only Zygote is GPU compatible and fast
# so if out-of-place, try Zygote
if p === nothing || p === DiffEqBase.NullParameters()
# QuadratureAdjoint skips all p calculations until the end
# So it's the fastest when there are no parameters
QuadratureAdjoint(autojacvec = ZygoteVJP())
else
InterpolatingAdjoint(autojacvec = ZygoteVJP())
end
else
vjp = inplace_vjp(prob, u0, p, verbose)
if p === nothing || p === DiffEqBase.NullParameters()
QuadratureAdjoint(autojacvec = vjp)
else
InterpolatingAdjoint(autojacvec = vjp)
end
end
return default_sensealg
end
function automatic_sensealg_choice(prob::Union{NonlinearProblem, SteadyStateProblem}, u0, p,
verbose)
default_sensealg = if u0 isa GPUArraysCore.AbstractGPUArray ||
!DiffEqBase.isinplace(prob)
# autodiff = false because forwarddiff fails on many GPU kernels
# this only effects the Jacobian calculation and is same computation order
SteadyStateAdjoint(autodiff = false, autojacvec = ZygoteVJP())
else
vjp = inplace_vjp(prob, u0, p, verbose)
SteadyStateAdjoint(autojacvec = vjp)
end
return default_sensealg
end
function DiffEqBase._concrete_solve_adjoint(prob::Union{ODEProblem, SDEProblem},
alg, sensealg::Nothing, u0, p,
originator::SciMLBase.ADOriginator, args...;
verbose = true, kwargs...)
if haskey(kwargs, :callback)
has_cb = kwargs[:callback] !== nothing
else
has_cb = false
end
default_sensealg = automatic_sensealg_choice(prob, u0, p, verbose)
if has_cb && typeof(default_sensealg) <: AbstractAdjointSensitivityAlgorithm
default_sensealg = setvjp(default_sensealg, ReverseDiffVJP())
end
DiffEqBase._concrete_solve_adjoint(prob, alg, default_sensealg, u0, p,
originator::SciMLBase.ADOriginator, args...; verbose,
kwargs...)
end
function DiffEqBase._concrete_solve_adjoint(prob::Union{NonlinearProblem, SteadyStateProblem
}, alg,
sensealg::Nothing, u0, p,
originator::SciMLBase.ADOriginator, args...;
verbose = true, kwargs...)
default_sensealg = automatic_sensealg_choice(prob, u0, p, verbose)
DiffEqBase._concrete_solve_adjoint(prob, alg, default_sensealg, u0, p,
originator::SciMLBase.ADOriginator, args...; verbose,
kwargs...)
end
function DiffEqBase._concrete_solve_adjoint(prob::Union{DiscreteProblem, DDEProblem,
SDDEProblem, DAEProblem},
alg, sensealg::Nothing,
u0, p, originator::SciMLBase.ADOriginator,
args...; kwargs...)
if length(u0) + length(p) > 100
default_sensealg = ReverseDiffAdjoint()
else
default_sensealg = ForwardDiffSensitivity()
end
DiffEqBase._concrete_solve_adjoint(prob, alg, default_sensealg, u0, p,
originator::SciMLBase.ADOriginator, args...;
kwargs...)
end
function DiffEqBase._concrete_solve_adjoint(prob, alg,
sensealg::AbstractAdjointSensitivityAlgorithm,
u0, p, originator::SciMLBase.ADOriginator,
args...; save_start = true, save_end = true,
saveat = eltype(prob.tspan)[],
save_idxs = nothing,
kwargs...)
if !(typeof(p) <: Union{Nothing, SciMLBase.NullParameters, AbstractArray}) ||
(p isa AbstractArray && !Base.isconcretetype(eltype(p)))
throw(AdjointSensitivityParameterCompatibilityError())
end
# Remove saveat, etc. from kwargs since it's handled separately
# and letting it jump back in there can break the adjoint
kwargs_prob = NamedTuple(filter(x -> x[1] != :saveat && x[1] != :save_start &&
x[1] != :save_end && x[1] != :save_idxs,
prob.kwargs))
if haskey(kwargs, :callback)
cb = track_callbacks(CallbackSet(kwargs[:callback]), prob.tspan[1], prob.u0, prob.p,
sensealg)
_prob = remake(prob; u0 = u0, p = p, kwargs = merge(kwargs_prob, (; callback = cb)))
else
cb = nothing
_prob = remake(prob; u0 = u0, p = p, kwargs = kwargs_prob)
end
# Remove callbacks, saveat, etc. from kwargs since it's handled separately
kwargs_fwd = NamedTuple{Base.diff_names(Base._nt_names(values(kwargs)), (:callback,))}(values(kwargs))
# Capture the callback_adj for the reverse pass and remove both callbacks
kwargs_adj = NamedTuple{
Base.diff_names(Base._nt_names(values(kwargs)),
(:callback_adj, :callback))}(values(kwargs))
isq = sensealg isa QuadratureAdjoint
if typeof(sensealg) <: BacksolveAdjoint
sol = solve(_prob, alg, args...; save_noise = true,
save_start = save_start, save_end = save_end,
saveat = saveat, kwargs_fwd...)
elseif ischeckpointing(sensealg)
sol = solve(_prob, alg, args...; save_noise = true,
save_start = true, save_end = true,
saveat = saveat, kwargs_fwd...)
else
sol = solve(_prob, alg, args...; save_noise = true, save_start = true,
save_end = true, kwargs_fwd...)
end
# Force `save_start` and `save_end` in the forward pass This forces the
# solver to do the backsolve all the way back to `u0` Since the start aliases
# `_prob.u0`, this doesn't actually use more memory But it cleans up the
# implementation and makes `save_start` and `save_end` arg safe.
if typeof(sensealg) <: BacksolveAdjoint
# Saving behavior unchanged
ts = sol.t
only_end = length(ts) == 1 && ts[1] == _prob.tspan[2]
out = DiffEqBase.sensitivity_solution(sol, sol.u, ts)
elseif saveat isa Number
if _prob.tspan[2] > _prob.tspan[1]
ts = _prob.tspan[1]:convert(typeof(_prob.tspan[2]), abs(saveat)):_prob.tspan[2]
else
ts = _prob.tspan[2]:convert(typeof(_prob.tspan[2]), abs(saveat)):_prob.tspan[1]
end
# if _prob.tspan[2]-_prob.tspan[1] is not a multiple of saveat, one looses the last ts value
sol.t[end] !== ts[end] && (ts = fix_endpoints(sensealg, sol, ts))
if cb === nothing
_out = sol(ts)
else
_, duplicate_iterator_times = separate_nonunique(sol.t)
_out, ts = out_and_ts(ts, duplicate_iterator_times, sol)
end
out = if save_idxs === nothing
out = DiffEqBase.sensitivity_solution(sol, _out.u, ts)
else
out = DiffEqBase.sensitivity_solution(sol,
[_out[i][save_idxs]
for i in 1:length(_out)], ts)
end
only_end = length(ts) == 1 && ts[1] == _prob.tspan[2]
elseif isempty(saveat)
no_start = !save_start
no_end = !save_end
sol_idxs = 1:length(sol)
no_start && (sol_idxs = sol_idxs[2:end])
no_end && (sol_idxs = sol_idxs[1:(end - 1)])
only_end = length(sol_idxs) <= 1
_u = sol.u[sol_idxs]
u = save_idxs === nothing ? _u : [x[save_idxs] for x in _u]
ts = sol.t[sol_idxs]
out = DiffEqBase.sensitivity_solution(sol, u, ts)
else
_saveat = saveat isa Array ? sort(saveat) : saveat # for minibatching
if cb === nothing
_saveat = eltype(_saveat) <: typeof(prob.tspan[2]) ?
convert.(typeof(_prob.tspan[2]), _saveat) : _saveat
ts = _saveat
_out = sol(ts)
else
_ts, duplicate_iterator_times = separate_nonunique(sol.t)
_out, ts = out_and_ts(_saveat, duplicate_iterator_times, sol)
end
out = if save_idxs === nothing
out = DiffEqBase.sensitivity_solution(sol, _out.u, ts)
else
out = DiffEqBase.sensitivity_solution(sol,
[_out[i][save_idxs]
for i in 1:length(_out)], ts)
end
only_end = length(ts) == 1 && ts[1] == _prob.tspan[2]
end
_save_idxs = save_idxs === nothing ? Colon() : save_idxs
function adjoint_sensitivity_backpass(Δ)
function df(_out, u, p, t, i)
outtype = typeof(_out) <: SubArray ?
DiffEqBase.parameterless_type(_out.parent) :
DiffEqBase.parameterless_type(_out)
if only_end
eltype(Δ) <: NoTangent && return
if typeof(Δ) <: AbstractArray{<:AbstractArray} && length(Δ) == 1 && i == 1
# user did sol[end] on only_end
if typeof(_save_idxs) <: Number
x = vec(Δ[1])
_out[_save_idxs] .= adapt(outtype, @view(x[_save_idxs]))
elseif _save_idxs isa Colon
if ArrayInterfaceCore.ismutable(u)
vec(_out) .= adapt(outtype, vec(Δ[1]))
else
_out = adapt(outtype, vec(Δ[1]))
end
else
vec(@view(_out[_save_idxs])) .= adapt(outtype,
vec(Δ[1])[_save_idxs])
end
else
Δ isa NoTangent && return
if typeof(_save_idxs) <: Number
x = vec(Δ)
_out[_save_idxs] .= adapt(outtype, @view(x[_save_idxs]))
elseif _save_idxs isa Colon
if ArrayInterfaceCore.ismutable(u)
vec(_out) .= adapt(outtype, vec(Δ))
else
_out = adapt(outtype, vec(Δ))
end
else
x = vec(Δ)
vec(@view(_out[_save_idxs])) .= adapt(outtype, @view(x[_save_idxs]))
end
end
else
!Base.isconcretetype(eltype(Δ)) &&
(Δ[i] isa NoTangent || eltype(Δ) <: NoTangent) && return
if typeof(Δ) <: AbstractArray{<:AbstractArray} || typeof(Δ) <: DESolution
x = Δ[i]
if typeof(_save_idxs) <: Number
_out[_save_idxs] = @view(x[_save_idxs])
elseif _save_idxs isa Colon
if ArrayInterfaceCore.ismutable(u)
vec(_out) .= vec(x)
else
_out = vec(x)
end
else
vec(@view(_out[_save_idxs])) .= vec(@view(x[_save_idxs]))
end
else
if typeof(_save_idxs) <: Number
_out[_save_idxs] = adapt(outtype,
reshape(Δ, prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[_save_idxs, i])
elseif _save_idxs isa Colon
if ArrayInterfaceCore.ismutable(u)
vec(_out) .= vec(adapt(outtype,
reshape(Δ, prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[:, i]))
else
_out = vec(adapt(outtype,
reshape(Δ, prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[:, i]))
end
else
vec(@view(_out[_save_idxs])) .= vec(adapt(outtype,
reshape(Δ,
prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[:,
i]))
end
end
end
if !(ArrayInterfaceCore.ismutable(u0))
return _out
end
end
function df(u, p, t, i;outtype=nothing)
if only_end
eltype(Δ) <: NoTangent && return
if typeof(Δ) <: AbstractArray{<:AbstractArray} && length(Δ) == 1 && i == 1
# user did sol[end] on only_end
if typeof(_save_idxs) <: Number
x = vec(Δ[1])
_out = adapt(outtype, @view(x[_save_idxs]))
elseif _save_idxs isa Colon
_out = adapt(outtype, vec(Δ[1]))
else
_out = adapt(outtype,
vec(Δ[1])[_save_idxs])
end
else
Δ isa NoTangent && return
if typeof(_save_idxs) <: Number
x = vec(Δ)
_out = adapt(outtype, @view(x[_save_idxs]))
elseif _save_idxs isa Colon
_out = adapt(outtype, vec(Δ))
else
x = vec(Δ)
_out = adapt(outtype, @view(x[_save_idxs]))
end
end
else
!Base.isconcretetype(eltype(Δ)) &&
(Δ[i] isa NoTangent || eltype(Δ) <: NoTangent) && return
if typeof(Δ) <: AbstractArray{<:AbstractArray} || typeof(Δ) <: DESolution
x = Δ[i]
if typeof(_save_idxs) <: Number
_out = @view(x[_save_idxs])
elseif _save_idxs isa Colon
_out = vec(x)
else
_out = vec(@view(x[_save_idxs]))
end
else
if typeof(_save_idxs) <: Number
_out = adapt(outtype,
reshape(Δ, prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[_save_idxs, i])
elseif _save_idxs isa Colon
_out = vec(adapt(outtype,
reshape(Δ, prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[:, i]))
else
_out = vec(adapt(outtype,
reshape(Δ,
prod(size(Δ)[1:(end - 1)]),
size(Δ)[end])[:,i]))
end
end
end
return _out
end
if haskey(kwargs_adj, :callback_adj)
cb2 = CallbackSet(cb, kwargs[:callback_adj])
else
cb2 = cb
end
du0, dp = adjoint_sensitivities(sol, alg, args...; t = ts, dg_discrete = df,
sensealg = sensealg,
callback = cb2,
kwargs_adj...)
du0 = reshape(du0, size(u0))
dp = p === nothing || p === DiffEqBase.NullParameters() ? nothing :
reshape(dp', size(p))
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), du0, dp, NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
else
(NoTangent(), NoTangent(), NoTangent(), du0, dp, NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
end
end
out, adjoint_sensitivity_backpass
end
# Prefer this route since it works better with callback AD
function DiffEqBase._concrete_solve_adjoint(prob, alg,
sensealg::AbstractForwardSensitivityAlgorithm,
u0, p, originator::SciMLBase.ADOriginator,
args...;
save_idxs = nothing,
kwargs...)
if !(typeof(p) <: Union{Nothing, SciMLBase.NullParameters, AbstractArray}) ||
(p isa AbstractArray && !Base.isconcretetype(eltype(p)))
throw(ForwardSensitivityParameterCompatibilityError())
end
if p isa AbstractArray && eltype(p) <: ForwardDiff.Dual &&
!(eltype(u0) <: ForwardDiff.Dual)
# Handle double differentiation case
u0 = eltype(p).(u0)
end
_prob = ODEForwardSensitivityProblem(prob.f, u0, prob.tspan, p, sensealg)
sol = solve(_prob, alg, args...; kwargs...)
_, du = extract_local_sensitivities(sol, sensealg, Val(true))
u = if save_idxs === nothing
[reshape(sol[i][1:length(u0)], size(u0)) for i in 1:length(sol)]
else
[sol[i][_save_idxs] for i in 1:length(sol)]
end
out = DiffEqBase.sensitivity_solution(sol, u, sol.t)
function forward_sensitivity_backpass(Δ)
adj = sum(eachindex(du)) do i
J = du[i]
if Δ isa AbstractVector || Δ isa DESolution || Δ isa AbstractVectorOfArray
v = Δ[i]
elseif Δ isa AbstractMatrix
v = @view Δ[:, i]
else
v = @view Δ[.., i]
end
J'vec(v)
end
du0 = @not_implemented("ForwardSensitivity does not differentiate with respect to u0. Change your sensealg.")
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), du0, adj, NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
else
(NoTangent(), NoTangent(), NoTangent(), du0, adj, NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
end
end
out, forward_sensitivity_backpass
end
function DiffEqBase._concrete_solve_forward(prob, alg,
sensealg::AbstractForwardSensitivityAlgorithm,
u0, p, originator::SciMLBase.ADOriginator,
args...; save_idxs = nothing,
kwargs...)
_prob = ODEForwardSensitivityProblem(prob.f, u0, prob.tspan, p, sensealg)
sol = solve(_prob, args...; kwargs...)
u, du = extract_local_sensitivities(sol, Val(true))
_save_idxs = save_idxs === nothing ? (1:length(u0)) : save_idxs
out = DiffEqBase.sensitivity_solution(sol,
[ForwardDiff.value.(sol[i][_save_idxs])
for i in 1:length(sol)], sol.t)
function _concrete_solve_pushforward(Δself, ::Nothing, ::Nothing, x3, Δp, args...)
x3 !== nothing && error("Pushforward currently requires no u0 derivatives")
du * Δp
end
out, _concrete_solve_pushforward
end
const FORWARDDIFF_SENSITIVITY_PARAMETER_COMPATABILITY_MESSAGE = """
ForwardDiffSensitivity assumes the `AbstractArray` interface for `p`. Thus while
DifferentialEquations.jl can support any parameter struct type, usage
with ForwardDiffSensitivity requires that `p` could be a valid
type for being the initial condition `u0` of an array. This means that
many simple types, such as `Tuple`s and `NamedTuple`s, will work as
parameters in normal contexts but will fail during ForwardDiffSensitivity
construction. To work around this issue for complicated cases like nested structs,
look into defining `p` using `AbstractArray` libraries such as RecursiveArrayTools.jl
or ComponentArrays.jl.
"""
struct ForwardDiffSensitivityParameterCompatibilityError <: Exception end
function Base.showerror(io::IO, e::ForwardDiffSensitivityParameterCompatibilityError)
print(io, FORWARDDIFF_SENSITIVITY_PARAMETER_COMPATABILITY_MESSAGE)
end
# Generic Fallback for ForwardDiff
function DiffEqBase._concrete_solve_adjoint(prob, alg,
sensealg::ForwardDiffSensitivity{CS, CTS},
u0, p, originator::SciMLBase.ADOriginator,
args...; saveat = eltype(prob.tspan)[],
kwargs...) where {CS, CTS}
if !(typeof(p) <: Union{Nothing, SciMLBase.NullParameters, AbstractArray}) ||
(p isa AbstractArray && !Base.isconcretetype(eltype(p)))
throw(ForwardDiffSensitivityParameterCompatibilityError())
end
if saveat isa Number
_saveat = prob.tspan[1]:saveat:prob.tspan[2]
else
_saveat = saveat
end
sol = solve(remake(prob, p = p, u0 = u0), alg, args...; saveat = _saveat, kwargs...)
# saveat values
# seems overcomplicated, but see the PR
if length(sol.t) == 1
ts = sol.t
else
ts = eltype(sol.t)[]
if sol.t[2] != sol.t[1]
push!(ts, sol.t[1])
end
for i in 2:(length(sol.t) - 1)
if sol.t[i] != sol.t[i + 1] && sol.t[i] != sol.t[i - 1]
push!(ts, sol.t[i])
end
end
if sol.t[end] != sol.t[end - 1]
push!(ts, sol.t[end])
end
end
function forward_sensitivity_backpass(Δ)
dp = @thunk begin
chunk_size = if CS === 0 && length(p) < 12
length(p)
elseif CS !== 0
CS
else
12
end
num_chunks = length(p) ÷ chunk_size
num_chunks * chunk_size != length(p) && (num_chunks += 1)
pparts = typeof(p[1:1])[]
for j in 0:(num_chunks - 1)
local chunk
if ((j + 1) * chunk_size) <= length(p)
chunk = ((j * chunk_size + 1):((j + 1) * chunk_size))
pchunk = vec(p)[chunk]
pdualpart = seed_duals(pchunk, prob.f, ForwardDiff.Chunk{chunk_size}())
else
chunk = ((j * chunk_size + 1):length(p))
pchunk = vec(p)[chunk]
pdualpart = seed_duals(pchunk, prob.f,
ForwardDiff.Chunk{length(chunk)}())
end
pdualvec = if j == 0
vcat(pdualpart, p[((j + 1) * chunk_size + 1):end])
elseif j == num_chunks - 1
vcat(p[1:(j * chunk_size)], pdualpart)
else
vcat(p[1:(j * chunk_size)], pdualpart,
p[(((j + 1) * chunk_size) + 1):end])
end
pdual = ArrayInterfaceCore.restructure(p, pdualvec)
u0dual = convert.(eltype(pdualvec), u0)
if (convert_tspan(sensealg) === nothing && ((haskey(kwargs, :callback) &&
has_continuous_callback(kwargs[:callback])))) ||
(convert_tspan(sensealg) !== nothing && convert_tspan(sensealg))
tspandual = convert.(eltype(pdual), prob.tspan)
else
tspandual = prob.tspan
end
if typeof(prob.f) <: ODEFunction && prob.f.jac_prototype !== nothing
_f = ODEFunction{SciMLBase.isinplace(prob.f), true}(prob.f,
jac_prototype = convert.(eltype(u0dual),
prob.f.jac_prototype))
elseif typeof(prob.f) <: SDEFunction && prob.f.jac_prototype !== nothing
_f = SDEFunction{SciMLBase.isinplace(prob.f), true}(prob.f,
jac_prototype = convert.(eltype(u0dual),
prob.f.jac_prototype))
else
_f = prob.f
end
_prob = remake(prob, f = _f, u0 = u0dual, p = pdual, tspan = tspandual)
if _prob isa SDEProblem
_prob.noise_rate_prototype !== nothing && (_prob = remake(_prob,
noise_rate_prototype = convert.(eltype(pdual),
_prob.noise_rate_prototype)))
end
if saveat isa Number
_saveat = prob.tspan[1]:saveat:prob.tspan[2]
else
_saveat = saveat
end
_sol = solve(_prob, alg, args...; saveat = ts, kwargs...)
_, du = extract_local_sensitivities(_sol, sensealg, Val(true))
_dp = sum(eachindex(du)) do i
J = du[i]
if Δ isa AbstractVector || Δ isa DESolution ||
Δ isa AbstractVectorOfArray
v = Δ[i]
elseif Δ isa AbstractMatrix
v = @view Δ[:, i]
else
v = @view Δ[.., i]
end
if !(Δ isa NoTangent)
ForwardDiff.value.(J'vec(v))
else
zero(p)
end
end
push!(pparts, vec(_dp))
end
ArrayInterfaceCore.restructure(p, reduce(vcat, pparts))
end
du0 = @thunk begin
chunk_size = if CS === 0 && length(u0) < 12
length(u0)
elseif CS !== 0
CS
else
12
end
num_chunks = length(u0) ÷ chunk_size
num_chunks * chunk_size != length(u0) && (num_chunks += 1)
du0parts = typeof(u0[1:1])[]
for j in 0:(num_chunks - 1)
local chunk
if ((j + 1) * chunk_size) <= length(u0)
chunk = ((j * chunk_size + 1):((j + 1) * chunk_size))
u0chunk = vec(u0)[chunk]
u0dualpart = seed_duals(u0chunk, prob.f,
ForwardDiff.Chunk{chunk_size}())
else
chunk = ((j * chunk_size + 1):length(u0))
u0chunk = vec(u0)[chunk]
u0dualpart = seed_duals(u0chunk, prob.f,
ForwardDiff.Chunk{length(chunk)}())
end
u0dualvec = if j == 0
vcat(u0dualpart, u0[((j + 1) * chunk_size + 1):end])
elseif j == num_chunks - 1
vcat(u0[1:(j * chunk_size)], u0dualpart)
else
vcat(u0[1:(j * chunk_size)], u0dualpart,
u0[(((j + 1) * chunk_size) + 1):end])
end
u0dual = ArrayInterfaceCore.restructure(u0, u0dualvec)
pdual = convert.(eltype(u0dual), p)
if (convert_tspan(sensealg) === nothing && ((haskey(kwargs, :callback) &&
has_continuous_callback(kwargs[:callback])))) ||
(convert_tspan(sensealg) !== nothing && convert_tspan(sensealg))
tspandual = convert.(eltype(pdual), prob.tspan)
else
tspandual = prob.tspan
end
if typeof(prob.f) <: ODEFunction && prob.f.jac_prototype !== nothing
_f = ODEFunction{SciMLBase.isinplace(prob.f), true}(prob.f,
jac_prototype = convert.(eltype(pdual),
prob.f.jac_prototype))
elseif typeof(prob.f) <: SDEFunction && prob.f.jac_prototype !== nothing
_f = SDEFunction{SciMLBase.isinplace(prob.f), true}(prob.f,
jac_prototype = convert.(eltype(pdual),
prob.f.jac_prototype))
else
_f = prob.f
end
_prob = remake(prob, f = _f, u0 = u0dual, p = pdual, tspan = tspandual)
if _prob isa SDEProblem
_prob.noise_rate_prototype !== nothing && (_prob = remake(_prob,
noise_rate_prototype = convert.(eltype(pdual),
_prob.noise_rate_prototype)))
end
if saveat isa Number
_saveat = prob.tspan[1]:saveat:prob.tspan[2]
else
_saveat = saveat
end
_sol = solve(_prob, alg, args...; saveat = ts, kwargs...)
_, du = extract_local_sensitivities(_sol, sensealg, Val(true))
_du0 = sum(eachindex(du)) do i
J = du[i]
if Δ isa AbstractVector || Δ isa DESolution ||
Δ isa AbstractVectorOfArray
v = Δ[i]
elseif Δ isa AbstractMatrix
v = @view Δ[:, i]
else
v = @view Δ[.., i]
end
if !(Δ isa NoTangent)
ForwardDiff.value.(J'vec(v))
else
zero(u0)
end
end
push!(du0parts, vec(_du0))
end
ArrayInterfaceCore.restructure(u0, reduce(vcat, du0parts))
end
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), unthunk(du0), unthunk(dp), NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
else
(NoTangent(), NoTangent(), NoTangent(), du0, dp, NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
end
end
sol, forward_sensitivity_backpass
end
function DiffEqBase._concrete_solve_adjoint(prob, alg, sensealg::ZygoteAdjoint,
u0, p, originator::SciMLBase.ADOriginator,
args...; kwargs...)
Zygote.pullback((u0, p) -> solve(prob, alg, args...; u0 = u0, p = p,
sensealg = SensitivityADPassThrough(), kwargs...), u0,
p)
end
function DiffEqBase._concrete_solve_adjoint(prob, alg, sensealg::TrackerAdjoint,
u0, p, originator::SciMLBase.ADOriginator,
args...;
kwargs...)
local sol
function tracker_adjoint_forwardpass(_u0, _p)
if (convert_tspan(sensealg) === nothing &&
((haskey(kwargs, :callback) && has_continuous_callback(kwargs[:callback])))) ||
(convert_tspan(sensealg) !== nothing && convert_tspan(sensealg))
_tspan = convert.(eltype(_p), prob.tspan)
else
_tspan = prob.tspan
end
if DiffEqBase.isinplace(prob)
# use Array{TrackedReal} for mutation to work
# Recurse to all Array{TrackedArray}
_prob = remake(prob, u0 = map(identity, _u0), p = _p, tspan = _tspan)
else
# use TrackedArray for efficiency of the tape
if typeof(prob) <:
Union{SciMLBase.AbstractDDEProblem, SciMLBase.AbstractDAEProblem,
SciMLBase.AbstractSDDEProblem}
_f = function (u, p, h, t) # For DDE, but also works for (du,u,p,t) DAE
out = prob.f(u, p, h, t)
if out isa TrackedArray
return out
else
Tracker.collect(out)
end
end
# Only define `g` for the stochastic ones
if typeof(prob) <: SciMLBase.AbstractSDEProblem
_g = function (u, p, h, t)
out = prob.g(u, p, h, t)
if out isa TrackedArray
return out
else
Tracker.collect(out)
end
end
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){false, true}(_f,
_g),
u0 = _u0, p = _p, tspan = _tspan)
else
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){false, true}(_f),
u0 = _u0, p = _p, tspan = _tspan)
end
elseif typeof(prob) <:
Union{SciMLBase.AbstractODEProblem, SciMLBase.AbstractSDEProblem}
_f = function (u, p, t)
out = prob.f(u, p, t)
if out isa TrackedArray
return out
else
Tracker.collect(out)
end
end
if typeof(prob) <: SciMLBase.AbstractSDEProblem
_g = function (u, p, t)
out = prob.g(u, p, t)
if out isa TrackedArray
return out
else
Tracker.collect(out)
end
end
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){false, true}(_f,
_g),
u0 = _u0, p = _p, tspan = _tspan)
else
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){false, true}(_f),
u0 = _u0, p = _p, tspan = _tspan)
end
else
error("TrackerAdjont does not currently support the specified problem type. Please open an issue.")
end
end
sol = solve(_prob, alg, args...; sensealg = DiffEqBase.SensitivityADPassThrough(),
kwargs...)
if typeof(sol.u[1]) <: Array
return Array(sol)
else
tmp = vec(sol.u[1])
for i in 2:length(sol.u)
tmp = hcat(tmp, vec(sol.u[i]))
end
return reshape(tmp, size(sol.u[1])..., length(sol.u))
end
#adapt(typeof(u0),arr)
sol
end
out, pullback = Tracker.forward(tracker_adjoint_forwardpass, u0, p)
function tracker_adjoint_backpass(ybar)
tmp = if eltype(ybar) <: Number && typeof(u0) <: Array
Array(ybar)
elseif eltype(ybar) <: Number # CuArray{Floats}
ybar
elseif typeof(ybar[1]) <: Array
return Array(ybar)
else
tmp = vec(ybar.u[1])
for i in 2:length(ybar.u)
tmp = hcat(tmp, vec(ybar.u[i]))
end
return reshape(tmp, size(ybar.u[1])..., length(ybar.u))
end
u0bar, pbar = pullback(tmp)
_u0bar = u0bar isa Tracker.TrackedArray ? Tracker.data(u0bar) : Tracker.data.(u0bar)
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), _u0bar, Tracker.data(pbar), NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
else
(NoTangent(), NoTangent(), NoTangent(), _u0bar, Tracker.data(pbar), NoTangent(),
ntuple(_ -> NoTangent(), length(args))...)
end
end
u = u0 isa Tracker.TrackedArray ? Tracker.data.(sol.u) :
Tracker.data.(Tracker.data.(sol.u))
DiffEqBase.sensitivity_solution(sol, u, Tracker.data.(sol.t)), tracker_adjoint_backpass
end
const REVERSEDIFF_ADJOINT_GPU_COMPATABILITY_MESSAGE = """
ReverseDiffAdjoint is not compatible GPU-based array types. Use a different
sensitivity analysis method, like InterpolatingAdjoint or TrackerAdjoint,
in order to combine with GPUs.
"""
struct ReverseDiffGPUStateCompatibilityError <: Exception end
function Base.showerror(io::IO, e::ReverseDiffGPUStateCompatibilityError)
print(io, FORWARDDIFF_SENSITIVITY_PARAMETER_COMPATABILITY_MESSAGE)
end
function DiffEqBase._concrete_solve_adjoint(prob, alg, sensealg::ReverseDiffAdjoint,
u0, p, originator::SciMLBase.ADOriginator,
args...; kwargs...)
if typeof(u0) isa GPUArraysCore.AbstractGPUArray
throw(ReverseDiffGPUStateCompatibilityError())
end
t = eltype(prob.tspan)[]
u = typeof(u0)[]
local sol
function reversediff_adjoint_forwardpass(_u0, _p)
if (convert_tspan(sensealg) === nothing &&
((haskey(kwargs, :callback) && has_a_callback(kwargs[:callback])))) ||
(convert_tspan(sensealg) !== nothing && convert_tspan(sensealg))
_tspan = convert.(eltype(_p), prob.tspan)
else
_tspan = prob.tspan
end
if DiffEqBase.isinplace(prob)
# use Array{TrackedReal} for mutation to work
# Recurse to all Array{TrackedArray}
_prob = remake(prob, u0 = reshape([x for x in _u0], size(_u0)), p = _p,
tspan = _tspan)
else
# use TrackedArray for efficiency of the tape
_f(args...) = reduce(vcat, prob.f(args...))
if prob isa SDEProblem
_g(args...) = reduce(vcat, prob.g(args...))
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){
SciMLBase.isinplace(prob),
true}(_f, _g),
u0 = _u0, p = _p, tspan = _tspan)
else
_prob = remake(prob,
f = DiffEqBase.parameterless_type(prob.f){
SciMLBase.isinplace(prob),
true}(_f),
u0 = _u0, p = _p, tspan = _tspan)
end
end
sol = solve(_prob, alg, args...; sensealg = DiffEqBase.SensitivityADPassThrough(),
kwargs...)
t = sol.t
if DiffEqBase.isinplace(prob)
u = map.(ReverseDiff.value, sol.u)
else
u = map(ReverseDiff.value, sol.u)
end
Array(sol)
end
tape = ReverseDiff.GradientTape(reversediff_adjoint_forwardpass, (u0, p))
tu, tp = ReverseDiff.input_hook(tape)
output = ReverseDiff.output_hook(tape)
ReverseDiff.value!(tu, u0)
typeof(p) <: DiffEqBase.NullParameters || ReverseDiff.value!(tp, p)
ReverseDiff.forward_pass!(tape)
function reversediff_adjoint_backpass(ybar)
_ybar = if ybar isa VectorOfArray
Array(ybar)
elseif eltype(ybar) <: AbstractArray
Array(VectorOfArray(ybar))
else
ybar
end
ReverseDiff.increment_deriv!(output, _ybar)
ReverseDiff.reverse_pass!(tape)
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), ReverseDiff.deriv(tu), ReverseDiff.deriv(tp),
NoTangent(), ntuple(_ -> NoTangent(), length(args))...)
else
(NoTangent(), NoTangent(), NoTangent(), ReverseDiff.deriv(tu),
ReverseDiff.deriv(tp), NoTangent(), ntuple(_ -> NoTangent(), length(args))...)
end
end
Array(VectorOfArray(u)), reversediff_adjoint_backpass
end
function DiffEqBase._concrete_solve_adjoint(prob, alg,
sensealg::AbstractShadowingSensitivityAlgorithm,
u0, p, originator::SciMLBase.ADOriginator,
args...; save_start = true, save_end = true,
saveat = eltype(prob.tspan)[],
save_idxs = nothing,
kwargs...)
if haskey(kwargs, :callback)
error("Sensitivity analysis based on Least Squares Shadowing is not compatible with callbacks. Please select another `sensealg`.")
else
_prob = remake(prob, u0 = u0, p = p)
end
sol = solve(_prob, alg, args...; save_start = save_start, save_end = save_end,