Skip to content

Re-enable EnzymeAdjoint testing #1232

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 6 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ DiffEqCallbacks = "4"
DiffEqNoiseProcess = "5.19"
Distributed = "1"
Distributions = "0.25"
Enzyme = "0.13.50"
Enzyme = "0.13.62"
ExplicitImports = "1"
FastBroadcast = "0.3.5"
FiniteDiff = "2"
Expand Down
20 changes: 15 additions & 5 deletions src/concrete_solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,10 @@ function DiffEqBase._concrete_solve_adjoint(
dp = Enzyme.make_zero(p)
mode = sensealg.mode

f = (u0, p) -> solve(prob, alg, args...; u0 = u0, p = p,
# Force no FunctionWrappers for Enzyme
_prob = remake(prob, f=f = ODEFunction{isinplace(prob), SciMLBase.FullSpecialize}(unwrapped_f(prob.f)))

diff_func = (u0, p) -> solve(_prob, alg, args...; u0 = u0, p = p,
sensealg = SensitivityADPassThrough(),
kwargs_filtered...)

Expand All @@ -1287,11 +1290,18 @@ function DiffEqBase._concrete_solve_adjoint(
Enzyme.set_runtime_activity(Enzyme.ReverseSplitWithPrimal)
end

forward, reverse = Enzyme.autodiff_thunk(splitmode, Enzyme.Const{typeof(f)}, Enzyme.Duplicated, Enzyme.Duplicated{typeof(u0)}, Enzyme.Duplicated{typeof(p)})
tape, result, shadow_result = forward(Enzyme.Const(f), Enzyme.Duplicated(copy(u0), du0), Enzyme.Duplicated(copy(p), dp))
forward, reverse = Enzyme.autodiff_thunk(splitmode, Enzyme.Const{typeof(diff_func)}, Enzyme.Duplicated, Enzyme.Duplicated{typeof(u0)}, Enzyme.Duplicated{typeof(p)})
tape, result, shadow_result = forward(Enzyme.Const(diff_func), Enzyme.Duplicated(copy(u0), du0), Enzyme.Duplicated(copy(p), dp))

function enzyme_sensitivity_backpass(Δ)
reverse(Const(f), Duplicated(u0, du0), Duplicated(p, dp), Δ, tape)
if (Δ isa AbstractArray{<:AbstractArray} || Δ isa AbstractVectorOfArray)
for (x,y) in zip(shadow_result.u,Δ.u)
x .= y
end
else
error("typeof(Δ) = $(typeof(Δ)) is not currently handled in EnzymeAdjoint. Please open an issue with an MWE to add support")
end
reverse(Enzyme.Const(diff_func), Enzyme.Duplicated(u0, du0), Enzyme.Duplicated(p, dp), tape)
if originator isa SciMLBase.TrackerOriginator ||
originator isa SciMLBase.ReverseDiffOriginator
(NoTangent(), NoTangent(), du0, dp, NoTangent(),
Expand All @@ -1301,7 +1311,7 @@ function DiffEqBase._concrete_solve_adjoint(
ntuple(_ -> NoTangent(), length(args))...)
end
end
sol, enzyme_sensitivity_backpass
result, enzyme_sensitivity_backpass
end

# NOTE: This is needed to prevent a method ambiguity error
Expand Down
12 changes: 6 additions & 6 deletions test/concrete_solve_derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ du06, dp6 = Zygote.gradient(
sensealg = MooncakeAdjoint())),
u0,
p)
@test_broken du08, dp8 = Zygote.gradient(
du08, dp8 = Zygote.gradient(
(u0, p) -> sum(solve(prob, Tsit5(), u0 = u0, p = p,
abstol = 1e-14, reltol = 1e-14,
saveat = 0.1,
Expand All @@ -94,15 +94,15 @@ du06, dp6 = Zygote.gradient(
#@test ū0 ≈ du05 rtol=1e-12
@test ū0≈du06 rtol=1e-12
@test_broken ū0≈du07 rtol=1e-12
@test_broken ū0≈du08 rtol=1e-12
@test ū0≈du08 rtol=1e-12
@test adj≈dp1' rtol=1e-12
@test adj == dp2'
@test adj≈dp3' rtol=1e-12
@test adj≈dp4' rtol=1e-12
#@test adj ≈ dp5' rtol=1e-12
@test adj≈dp6' rtol=1e-12
@test_broken adj≈dp7' rtol=1e-12
@test_broken adj≈dp8' rtol=1e-12
@test adj≈dp8' rtol=1e-12

###
### Direct from prob
Expand Down Expand Up @@ -331,7 +331,7 @@ du06, dp6 = Zygote.gradient(
sensealg = MooncakeAdjoint())),
u0,
p)
@test_broken du08, dp8 = Zygote.gradient(
du08, dp8 = Zygote.gradient(
(u0, p) -> sum(solve(proboop, Tsit5(), u0 = u0, p = p,
abstol = 1e-14, reltol = 1e-14,
saveat = 0.1,
Expand All @@ -353,7 +353,7 @@ du09, dp9 = Zygote.gradient(
#@test ū0 ≈ du05 rtol=1e-12
@test ū0≈du06 rtol=1e-12
@test_broken ū0≈du07 rtol=1e-12
@test_broken ū0≈du08 rtol=1e-12
@test ū0≈du08 rtol=1e-12
@test ū0≈du09 rtol=1e-12
@test adj≈dp1' rtol=1e-12
@test adj≈dp2' rtol=1e-12
Expand All @@ -362,7 +362,7 @@ du09, dp9 = Zygote.gradient(
#@test adj ≈ dp5' rtol=1e-12
@test adj≈dp6' rtol=1e-12
@test_broken adj≈dp7' rtol=1e-12
@test_broken adj≈dp8' rtol=1e-12
@test adj≈dp8' rtol=1e-12
@test adj≈dp9' rtol=1e-12

###
Expand Down
Loading