-
-
Notifications
You must be signed in to change notification settings - Fork 76
Feat: Handle Adjoints through Initialization #1168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
d6290bf
d3199c0
a4fa7c5
5a7dd26
94ec324
0c5564e
aca9cd4
9bec784
72cbb35
2d85e19
9a8a845
95ebbf3
957d7fe
a00574f
4562f0c
6c21324
a675a7f
7941a3c
9557e8c
8feae0e
d3b1669
e01eb77
0ad6c62
6df7987
c4c7807
b85b16e
8fc4136
1f1cce5
0d1abcc
b164b18
915d949
d2fd79a
a0cd94a
885794d
13a1ffb
7826866
6ceaa1a
dfebd0b
396f63e
de7e7da
f5fb559
019a051
91ee019
4b74718
94d5e2b
764d3ff
84b2602
8a4aa79
d69ccb1
2cc3673
22f056a
1f95b25
0d78fa8
6620e8a
5f5633b
984c2ce
82cd5fe
987e8be
056fffa
a122340
e105838
aaddc02
60be1c7
9edbe02
308ae5c
a333588
a2cf0e6
755c9df
75ad141
e07dd53
ee804b2
7abf42c
a7d4e5a
8e660fe
de63cf9
b88f468
7dd1cc7
cdaa2c7
d3608c4
4cf7bd5
2ae712b
229e691
6e1109e
d32b3f2
6e549e7
9789034
35937c0
f934635
a83cd29
2dfbc6e
9aecbfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ jobs: | |
- Core5 | ||
- Core6 | ||
- Core7 | ||
- Core8 | ||
- QA | ||
- SDE1 | ||
- SDE2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,7 @@ function DiffEqBase._concrete_solve_adjoint( | |
tunables, repack = Functors.functor(p) | ||
end | ||
|
||
u0 = state_values(prob) === nothing ? Float64[] : u0 | ||
default_sensealg = automatic_sensealg_choice(prob, u0, tunables, verbose, repack) | ||
DiffEqBase._concrete_solve_adjoint(prob, alg, default_sensealg, u0, p, | ||
originator::SciMLBase.ADOriginator, args...; verbose, | ||
|
@@ -425,6 +426,25 @@ function DiffEqBase._concrete_solve_adjoint( | |
save_end = true, kwargs_fwd...) | ||
end | ||
|
||
# Get gradients for the initialization problem if it exists | ||
igs = if _prob.f.initialization_data != nothing | ||
Zygote.gradient(tunables) do tunables | ||
new_prob = remake(_prob, p = repack(tunables)) | ||
new_u0, new_p, _ = SciMLBase.get_initial_values(new_prob, new_prob, new_prob.f, SciMLBase.OverrideInit(), Val(true); | ||
abstol = 1e-6, | ||
reltol = 1e-6, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These don't make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, these should probably inherit from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 984c2ce |
||
sensealg = SteadyStateAdjoint(autojacvec = ZygoteVJP())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't default to ZygoteVJP. Should use the autojacvec of the ODE There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 9a8a845 |
||
new_tunables, _, _ = SciMLStructures.canonicalize(SciMLStructures.Tunable(), new_p) | ||
if SciMLBase.initialization_status(_prob) == SciMLBase.OVERDETERMINED | ||
sum(new_tunables) | ||
else | ||
sum(new_u0) + sum(new_tunables) | ||
end | ||
end[1] .- one(eltype(tunables)) | ||
else | ||
nothing | ||
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 | ||
|
@@ -642,6 +662,8 @@ function DiffEqBase._concrete_solve_adjoint( | |
dp = p === nothing || p === DiffEqBase.NullParameters() ? nothing : | ||
dp isa AbstractArray ? reshape(dp', size(tunables)) : dp | ||
|
||
dp = Zygote.accum(dp, igs) | ||
|
||
_, repack_adjoint = if p === nothing || p === DiffEqBase.NullParameters() || | ||
!isscimlstructure(p) | ||
nothing, x -> (x,) | ||
|
@@ -1686,6 +1708,25 @@ function DiffEqBase._concrete_solve_adjoint( | |
out = SciMLBase.sensitivity_solution(sol, sol[_save_idxs]) | ||
end | ||
|
||
# Get gradients for the initialization problem if it exists | ||
igs = if _prob.f.initialization_data != nothing | ||
Zygote.gradient(tunables) do tunables | ||
new_prob = remake(_prob, p = repack(tunables)) | ||
new_u0, new_p, _ = SciMLBase.get_initial_values(new_prob, new_prob, new_prob.f, SciMLBase.OverrideInit(), Val(true); | ||
abstol = 1e-6, | ||
reltol = 1e-6, | ||
sensealg = SteadyStateAdjoint(autojacvec = ZygoteVJP())) | ||
new_tunables, _, _ = SciMLStructures.canonicalize(SciMLStructures.Tunable(), new_p) | ||
if SciMLBase.initialization_status(_prob) == SciMLBase.OVERDETERMINED | ||
sum(new_tunables) | ||
else | ||
sum(new_u0) + sum(new_tunables) | ||
end | ||
end[1] .- one(eltype(tunables)) | ||
else | ||
nothing | ||
end | ||
|
||
function steadystatebackpass(Δ) | ||
# Δ = dg/dx or diffcache.dg_val | ||
# del g/del p = 0 | ||
|
@@ -1694,11 +1735,15 @@ function DiffEqBase._concrete_solve_adjoint( | |
_out[_save_idxs] = Δ[_save_idxs] | ||
elseif Δ isa Number | ||
@. _out[_save_idxs] = Δ | ||
else | ||
elseif Δ isa AbstractArray{<:AbstractArray} || Δ isa AbstractVectorOfArray | ||
@. _out[_save_idxs] = Δ[_save_idxs] | ||
else | ||
@. _out[_save_idxs] = Δ.u[_save_idxs] | ||
end | ||
end | ||
# dp = adjoint_sensitivities(sol, alg; sensealg = sensealg, dgdu = df, dgdp = dp) | ||
dp = adjoint_sensitivities(sol, alg; sensealg = sensealg, dgdu = df) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the new reverse ode is built it needs to drop the initial eqs but still keep the dae constraints. It can brownbasic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to drop the initial eqs after its solved? The assumption was since we run with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the reverse pass needs to run with some form of initialization or the starting algebraic conditions may not be satisfied. Don't run this one with NoInit(), that would be prone to hiding issue. For this one, at most CheckInit(), but I'm saying that BrownBasicInit() is likely the one justified here since the 0 initial condition is only true on the differential variables, while the algebraic variable initial conditions will be unknown, but the Newton solve will have zero derivative because all of the inputs are just Newton guesses, so BrownBasic will work out for the reverse. We should probably hardcode that since it's always the solution there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, that will require us to add an OrdinaryDiffEqCore dep in this package. I will add that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the 0 derivative also applicable to parameters? Or only the unknowns? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its applicable to all Newton guess values. There is no parameter init going on to reverse so it's only for algebraic conditions so it's only Newton guesses.
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dp = Zygote.accum(dp, SciMLStructures.replace(SciMLStructures.Tunable(), dp, igs)) | ||
|
||
if originator isa SciMLBase.TrackerOriginator || | ||
originator isa SciMLBase.ReverseDiffOriginator | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using ModelingToolkit, OrdinaryDiffEq | ||
using ModelingToolkitStandardLibrary.Electrical | ||
using ModelingToolkitStandardLibrary.Blocks: Sine | ||
using NonlinearSolve | ||
import SciMLStructures as SS | ||
import SciMLSensitivity | ||
using Zygote | ||
|
||
function create_model(; C₁ = 3e-5, C₂ = 1e-6) | ||
@variables t | ||
@named resistor1 = Resistor(R = 5.0) | ||
@named resistor2 = Resistor(R = 2.0) | ||
@named capacitor1 = Capacitor(C = C₁) | ||
@named capacitor2 = Capacitor(C = C₂) | ||
@named source = Voltage() | ||
@named input_signal = Sine(frequency = 100.0) | ||
@named ground = Ground() | ||
@named ampermeter = CurrentSensor() | ||
|
||
eqs = [connect(input_signal.output, source.V) | ||
connect(source.p, capacitor1.n, capacitor2.n) | ||
connect(source.n, resistor1.p, resistor2.p, ground.g) | ||
connect(resistor1.n, capacitor1.p, ampermeter.n) | ||
connect(resistor2.n, capacitor2.p, ampermeter.p)] | ||
|
||
@named circuit_model = ODESystem(eqs, t, | ||
systems = [ | ||
resistor1, resistor2, capacitor1, capacitor2, | ||
source, input_signal, ground, ampermeter, | ||
]) | ||
end | ||
|
||
desauty_model = create_model() | ||
sys = structural_simplify(desauty_model) | ||
|
||
|
||
prob = ODEProblem(sys, [], (0.0, 0.1), guesses = [sys.resistor1.v => 1.]) | ||
iprob = prob.f.initialization_data.initializeprob | ||
isys = iprob.f.sys | ||
|
||
tunables, repack, aliases = SS.canonicalize(SS.Tunable(), parameter_values(iprob)) | ||
|
||
linsolve = LinearSolve.DefaultLinearSolver(LinearSolve.DefaultAlgorithmChoice.QRFactorization) | ||
sensealg = SciMLSensitivity.SteadyStateAdjoint(autojacvec = SciMLSensitivity.ZygoteVJP(), linsolve = linsolve) | ||
igs, = Zygote.gradient(tunables) do p | ||
iprob2 = remake(iprob, p = repack(p)) | ||
sol = solve(iprob2, | ||
sensealg = sensealg | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
sum(Array(sol)) | ||
end | ||
|
||
@test !iszero(sum(igs)) | ||
|
||
|
||
# tunable_parameters(isys) .=> gs | ||
|
||
# gradient_unk1_idx = only(findfirst(x -> isequal(x, Initial(sys.capacitor1.v)), tunable_parameters(isys))) | ||
|
||
# gs[gradient_unk1_idx] | ||
|
||
# prob.f.initialization_data.update_initializeprob!(iprob, prob) | ||
# prob.f.initialization_data.update_initializeprob!(iprob, ::Vector) | ||
# prob.f.initialization_data.update_initializeprob!(iprob, gs) |
Uh oh!
There was an error while loading. Please reload this page.