Skip to content
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

Feat: adjoints through observable functions #689

Merged
merged 26 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
92ad6a8
feat: adjoints through observable functions
DhairyaLGandhi May 2, 2024
22dc7ec
Update ext/SciMLBaseZygoteExt.jl
DhairyaLGandhi May 6, 2024
0c2b69d
feat: allow observables in collections
DhairyaLGandhi May 8, 2024
c61e08c
chore: handle no observables in collection
DhairyaLGandhi May 8, 2024
8600a8d
fix: typo
DhairyaLGandhi May 8, 2024
a69d087
Merge branch 'master' into dg/obsfn
DhairyaLGandhi May 8, 2024
785b052
test: add test for observable functions
DhairyaLGandhi May 12, 2024
adee4f0
test: add AD testset
DhairyaLGandhi May 12, 2024
2197a30
Update test/downstream/observables_autodiff.jl
ChrisRackauckas May 13, 2024
9172014
test: add a simple DAE example; disable till sensitivities are turned on
DhairyaLGandhi May 15, 2024
95cf416
test: add missing imports
DhairyaLGandhi May 15, 2024
4ce8257
chore: format
DhairyaLGandhi May 16, 2024
839bd63
chore: rm unwanted adjoint
DhairyaLGandhi May 16, 2024
2474a8d
test: check failures with SciMLSensitivity + SII
DhairyaLGandhi May 18, 2024
f68cb05
ci(SciMLSensitivity): checkout SII branch
DhairyaLGandhi May 18, 2024
9ab29d9
ci(SciMLSensitivity): use correct path
DhairyaLGandhi May 18, 2024
a417cdd
ci: revert changes
DhairyaLGandhi May 18, 2024
ff9bb2c
chore: revert literal_getproperty adjoint
DhairyaLGandhi May 19, 2024
032b927
chore: try to avoid returning object
DhairyaLGandhi May 19, 2024
44bfc91
build: add MSL to test deps
DhairyaLGandhi May 19, 2024
de2d6cd
chore: don't return structural tangent
DhairyaLGandhi May 20, 2024
c63dfbf
chore: fix imports
DhairyaLGandhi May 20, 2024
940ea78
test: add MSL to downstream env
DhairyaLGandhi May 20, 2024
8e48f1c
test: rm MSL from test env
DhairyaLGandhi May 20, 2024
d061ce4
chore: format
DhairyaLGandhi May 20, 2024
f817b52
Update CI.yml
ChrisRackauckas May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 57 additions & 26 deletions ext/SciMLBaseZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using SciMLBase: ODESolution, remake,
getobserved, build_solution, EnsembleSolution,
NonlinearSolution, AbstractTimeseriesSolution
using SymbolicIndexingInterface: symbolic_type, NotSymbolic, variable_index
using SymbolicIndexingInterface: symbolic_type, NotSymbolic, variable_index, is_observed, observed, parameter_values
using RecursiveArrayTools

# This method resolves the ambiguity with the pullback defined in
Expand Down Expand Up @@ -109,7 +109,15 @@
@adjoint function Base.getindex(VA::ODESolution, sym)
function ODESolution_getindex_pullback(Δ)
i = symbolic_type(sym) != NotSymbolic() ? variable_index(VA, sym) : sym
if i === nothing
if is_observed(VA, sym)
y, back = Zygote.pullback(VA) do sol
f = observed(sol, sym)
p = parameter_values(sol)
f.(sol.u,Ref(p), sol.t)

Check warning on line 116 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L112-L116

Added lines #L112 - L116 were not covered by tests
end
gs = back(Δ)
(gs[1], nothing)
elseif i === nothing

Check warning on line 120 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L118-L120

Added lines #L118 - L120 were not covered by tests
throw(error("Zygote AD of purely-symbolic slicing for observed quantities is not yet supported. Work around this by using `A[sym,i]` to access each element sequentially in the function being differentiated."))
else
Δ′ = [[i == k ? Δ[j] : zero(x[1]) for k in 1:length(x)]
Expand All @@ -120,26 +128,49 @@
VA[sym], ODESolution_getindex_pullback
end

function obs_grads(VA, sym, obss_idx, Δ)
y, back = Zygote.pullback(VA) do sol
getindex.(Ref(sol), sym[obss_idx])

Check warning on line 133 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L131-L133

Added lines #L131 - L133 were not covered by tests
end
Dprime = reduce(hcat, Δ)
Dobss = eachrow(Dprime[obss_idx, :])
back(Dobss)

Check warning on line 137 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L135-L137

Added lines #L135 - L137 were not covered by tests
end

function obs_grads(VA, sym, ::Nothing, Δ)
Zygote.nt_nothing(VA)

Check warning on line 141 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L140-L141

Added lines #L140 - L141 were not covered by tests
end

function not_obs_grads(VA::ODESolution{T}, sym, not_obss_idx, i, Δ) where T
Δ′ = map(enumerate(VA.u)) do (t_idx, us)
map(enumerate(us)) do (u_idx, u)
if u_idx in i
idx = findfirst(isequal(u_idx), i)
Δ[t_idx][idx]

Check warning on line 149 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L144-L149

Added lines #L144 - L149 were not covered by tests
else
zero(T)

Check warning on line 151 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L151

Added line #L151 was not covered by tests
end
end
end

nt = Zygote.nt_nothing(VA)
Zygote.accum(nt, (u = Δ′,))

Check warning on line 157 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L156-L157

Added lines #L156 - L157 were not covered by tests
end

@adjoint function Base.getindex(
VA::ODESolution{T}, sym::Union{Tuple, AbstractVector}) where {T}
function ODESolution_getindex_pullback(Δ)
sym = sym isa Tuple ? collect(sym) : sym
i = map(x -> symbolic_type(x) != NotSymbolic() ? variable_index(VA, x) : x, sym)
if i === nothing
throw(error("Zygote AD of purely-symbolic slicing for observed quantities is not yet supported. Work around this by using `A[sym,i]` to access each element sequentially in the function being differentiated."))
else
Δ′ = map(enumerate(VA.u)) do (t_idx, us)
map(enumerate(us)) do (u_idx, u)
if u_idx in i
idx = findfirst(isequal(u_idx), i)
Δ[t_idx][idx]
else
zero(T)
end
end
end
(Δ′, nothing)
end

obss_idx = findall(s -> is_observed(VA, s), sym)
not_obss_idx = setdiff(1:length(sym), obss_idx)

Check warning on line 167 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L166-L167

Added lines #L166 - L167 were not covered by tests

gs_obs = obs_grads(VA, sym, isempty(obss_idx) ? nothing : obss_idx, Δ)
gs_not_obs = not_obs_grads(VA, sym, not_obss_idx, i, Δ)

Check warning on line 170 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L169-L170

Added lines #L169 - L170 were not covered by tests

a = Zygote.accum(gs_obs[1], gs_not_obs)
(a, nothing)

Check warning on line 173 in ext/SciMLBaseZygoteExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SciMLBaseZygoteExt.jl#L172-L173

Added lines #L172 - L173 were not covered by tests
end
VA[sym], ODESolution_getindex_pullback
end
Expand Down Expand Up @@ -183,15 +214,15 @@
NonlinearSolution{T, N, uType, R, P, A, O, uType2}(u, args...), NonlinearSolutionAdjoint
end

@adjoint function literal_getproperty(sol::AbstractTimeseriesSolution,
::Val{:u})
function solu_adjoint(Δ)
zerou = zero(sol.prob.u0)
_Δ = @. ifelse(Δ === nothing, (zerou,), Δ)
(build_solution(sol.prob, sol.alg, sol.t, _Δ),)
end
sol.u, solu_adjoint
end
# @adjoint function literal_getproperty(sol::AbstractTimeseriesSolution,
# ::Val{:u})
# function solu_adjoint(Δ)
# zerou = zero(sol.prob.u0)
# _Δ = @. ifelse(Δ === nothing, (zerou,), Δ)
# (build_solution(sol.prob, sol.alg, sol.t, _Δ),)
# end
# sol.u, solu_adjoint
# end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was returning the ODESolution as the adjoint. It is also an issue because it shortcuts the gradients through parameters and instead replaces it with the sol.prob, whereas we need to accumulate the gradients here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a unit test in the downstream set which shows this is fine?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to. In fact, that's why I asked if anything was relying on this behavior previously. Could you suggest what kind of test you have in mind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the root cause of many of the test failures? So that means it's caught by the tests already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is what the error is referring to. I am missing a branch https://github.com/DhairyaLGandhi/RecursiveArrayTools.jl/tree/dg/noproj which removes an extra projection rule.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does refer to projecting to a VectorOfArray, and that rule wasn't defined for Tangent. Removing it gets us the expected results. If we want to project back to a VectorOfArray type, then that needs to be handled elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that we have restored the adjoint, I believe this can be resolved


@adjoint function literal_getproperty(sol::SciMLBase.AbstractNoTimeSolution,
::Val{:u})
Expand Down
34 changes: 34 additions & 0 deletions test/downstream/observables_autodiff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ModelingToolkit, OrdinaryDiffEq
using Zygote

@parameters σ ρ β
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
@variables x(t) y(t) z(t) w(t)

eqs = [D(D(x)) ~ σ * (y - x),
D(y) ~ x * (ρ - z) - y,
D(z) ~ x * y - β * z,
w ~ x + y + z + 2 * β]

@mtkbuild sys = ODESystem(eqs, t)

u0 = [D(x) => 2.0,
x => 1.0,
y => 0.0,
z => 0.0]

p = [σ => 28.0,
ρ => 10.0,
β => 8 / 3]

tspan = (0.0, 100.0)
prob = ODEProblem(sys, u0, tspan, p, jac = true)
sol = solve(prob, Tsit5())

@testset "AutoDiff Observable Functions" begin
gs, = gradient(sol) do sol
sum(sol[sys.w])
end
du_ = [0., 1., 1., 1.]
du = [du_ for _ = sol.u]
@test du == gs.u
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ end
@time @safetestset "Partial Functions" begin
include("downstream/partial_functions.jl")
end
@time @safetestset "Autodiff Observable Functions" begin
include("downstream/observables_autodiff.jl")
end
end

if !is_APPVEYOR && (GROUP == "Downstream" || GROUP == "SymbolicIndexingInterface")
Expand Down
Loading