Hello,
I don’t really understand how Enzyme.jl actually works. I more or less understand that the Const
arguments are not considered in the differentiation, and that Active
and Duplicated
are used otherwise. Duplicated
allows to store the result to a preallocated variable.
Here a simple example
using Enzyme
function dudt!(du, u, p, t)
du[1] = -p[1] * u[2]
du[2] = p[2] * u[1]
# return nothing
end
u = [1.0, 1.0]
du = similar(u)
p = [1.0, 1.0]
t = 0.0
d_p = Enzyme.make_zero(p)
my_f = (p) -> (dudt!(du, u, p, t); return du[2])
Enzyme.autodiff(Enzyme.Reverse, my_f, Active, Duplicated(p, d_p))
But here d_p
is zero, which instead should be [0.0, 1.0]
I think.
I also tried the same function but without the Active
argument (actually I don’t know its purpose)
Enzyme.autodiff(Enzyme.Reverse, my_f, Duplicated(p, d_p))
but I get the error
ERROR: Duplicated Returns not yet handled
Stacktrace:
[1] autodiff
@ ~/.julia/packages/Enzyme/azJki/src/Enzyme.jl:397 [inlined]
[2] autodiff
@ ~/.julia/packages/Enzyme/azJki/src/Enzyme.jl:537 [inlined]
[3] autodiff(mode::ReverseMode{false, false, FFIABI, false, false}, f::var"#15#16", args::Duplicated{Vector{Float64}})
Finally, what if I want to directly differentiate dudt!
only with respect to p
?