Skip to content

Check p while choosing default sensealg #1079

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

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Changes from all commits
Commits
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
16 changes: 12 additions & 4 deletions src/concrete_solve.jl
Original file line number Diff line number Diff line change
@@ -79,12 +79,20 @@ function automatic_sensealg_choice(
prob::Union{SciMLBase.AbstractODEProblem,
SciMLBase.AbstractSDEProblem},
u0, p, verbose, repack)
if p === nothing || p isa SciMLBase.NullParameters
tunables, repack = p, identity
elseif isscimlstructure(p)
tunables, repack, _ = canonicalize(Tunable(), p)
else
throw(SciMLStructuresCompatibilityError())
end

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
length(u0) + length(tunables) <= 100
ForwardDiffSensitivity()
elseif u0 isa GPUArraysCore.AbstractGPUArray || !DiffEqBase.isinplace(prob)
# only Zygote is GPU compatible and fast
@@ -124,7 +132,7 @@ function automatic_sensealg_choice(
ReverseDiff.gradient((u) -> sum(prob.f(u, p, prob.tspan[1])), u0)
else
ReverseDiff.gradient(
(u, _p) -> sum(prob.f(u, repack(_p), prob.tspan[1])), u0, p)
(u, _p) -> sum(prob.f(u, repack(_p), prob.tspan[1])), u0, tunables)
end
ReverseDiffVJP()
catch e
@@ -151,8 +159,8 @@ function automatic_sensealg_choice(
end
tmp1 = back(λ)
else
_dy, back = Tracker.forward(y, p) do u, p
vec(f(u, p, t))
_dy, back = Tracker.forward(y, tunables) do u, tunables
vec(f(u, repack(tunables), t))
end
tmp1, tmp2 = back(λ)
end