You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All the examples in the SciMLSensitivity.jl Documentation use a user-defined function for the ODEProblem. I need instead to define a parameter-dependent SciMLOperator (e.g., a MatrixOperator, AddedOperator, ...), but it fails.
Expected behavior
Returning the correct gradient without errors.
Minimal Reproducible Example 👇
using LinearAlgebra
using OrdinaryDiffEq
using SciMLOperators
using Zygote
using Enzyme
using SciMLSensitivity
const T = Float64
const N =10const u0 =ones(T, N)
H_tmp =rand(T, N, N)
const H = H_tmp + H_tmp'coef(a, u, p, t) =- p[1]
functionmy_f(γ)
# U = MatrixOperator(-1im * H - γ * Diagonal(H))
U =ScalarOperator(one(γ), coef) *MatrixOperator(Diagonal(H))
tspan = (0.0, 1.0)
# prob = ODEProblem{true}(U, u0, tspan, [γ], sensealg = InterpolatingAdjoint(autojacvec=false))
prob =ODEProblem{true}(U, u0, tspan, [γ])
sol =solve(prob, Tsit5())
returnreal(sol.u[end][end])
endmy_f(1.9) # 0.25621142049273665
ERROR: Constant memory is stored (or returned) to a differentiable variable.
As a result, Enzyme cannot provably ensure correctness and throws this error.
This might be due to the use of a constant variable as temporary storage for active memory (https://enzyme.mit.edu/julia/stable/faq/#Runtime-Activity).
If Enzyme should be able to prove this use non-differentable, open an issue!
To work around this issue, either:
a) rewrite this variable to not be conditionally active (fastest, but requires a code change), or
b) set the Enzyme mode to turn on runtime activity (e.g. autodiff(set_runtime_activity(Reverse), ...) ). This will maintain correctness, but may slightly reduce performance.
Mismatched activity for: store atomic {} addrspace(10)* addrspacecast ({}* inttoptr (i64 138286044220400 to {}*) to {} addrspace(10)*), {} addrspace(10)*addrspace(11)*%48 release, align 8, !dbg !410, !tbaa !336, !alias.scope !340, !noalias !343 const val: {} addrspace(10)* addrspacecast ({}* inttoptr (i64 138286044220400 to {}*) to {} addrspace(10)*)
value=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] of type Vector{Float64}
llvalue={} addrspace(10)* addrspacecast ({}* inttoptr (i64 138286044220400 to {}*) to {} addrspace(10)*)
Stacktrace:
[1] _
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:118
[2] ODEProblem (repeats 2 times)
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:111
[3] #ODEProblem#318
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:194
[4] ODEProblem
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:193
[5] _
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:140
[6] ODEProblem
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:136
[7] my_f
@ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:134
Stacktrace:
[1] _
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:118 [inlined]
[2] ODEProblem (repeats 2 times)
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:111 [inlined]
[3] #ODEProblem#318
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:194 [inlined]
[4] ODEProblem
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:193 [inlined]
[5] _
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:140 [inlined]
[6] ODEProblem
@ ~/.julia/packages/SciMLBase/hJh6T/src/problems/ode_problems.jl:136 [inlined]
[7] my_f
@ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:134 [inlined]
[8] diffejulia_my_f_49982wrap
@ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:0
[9] macro expansion
@ ~/.julia/packages/Enzyme/BRtTP/src/compiler.jl:8137 [inlined]
[10] enzyme_call
@ ~/.julia/packages/Enzyme/BRtTP/src/compiler.jl:7703 [inlined]
[11] CombinedAdjointThunk
@ ~/.julia/packages/Enzyme/BRtTP/src/compiler.jl:7476 [inlined]
[12] autodiff
@ ~/.julia/packages/Enzyme/BRtTP/src/Enzyme.jl:491 [inlined]
[13] autodiff
@ ~/.julia/packages/Enzyme/BRtTP/src/Enzyme.jl:537 [inlined]
[14] autodiff(mode::ReverseMode{false, false, FFIABI, false, false}, f::typeof(my_f), args::Active{Float64})
@ Enzyme ~/.julia/packages/Enzyme/BRtTP/src/Enzyme.jl:504
[15] top-level scope
@ ~/GitHub/Research/Undef/Autodiff QuantumToolbox/autodiff.jl:147
Environment (please complete the following information):
I think that the Zygote problem is related to the update of the ScalarOperator here. Where the struct contains a Float64, but during the differentiation it is trying to convert its field val to a Dual number, thus changing the type of the structure.
I found a possible partial fix for the out-of-place case. If I consider the out-of-place ODEProblem prob = ODEProblem{false}(U, u0, tspan, [γ]), and I update of the ScalarOperator is updated in the out-of-place case
function SciMLOperators.update_coefficients(L::ScalarOperator, u, p, t; kwargs...)
returnScalarOperator(L.update_func(L.val, u, p, t; kwargs...), L.update_func)
end
instead of the current implementation
functionupdate_coefficients!(L::ScalarOperator, u, p, t; kwargs...)
L.val = L.update_func(L.val, u, p, t; kwargs...)
nothingendfunctionupdate_coefficients(L::ScalarOperator, u, p, t; kwargs...)
update_coefficients!(L, u, p, t; kwargs...)
L
end
Describe the bug 🐞
All the examples in the
SciMLSensitivity.jl
Documentation use a user-defined function for the ODEProblem. I need instead to define a parameter-dependentSciMLOperator
(e.g., aMatrixOperator
,AddedOperator
, ...), but it fails.Expected behavior
Returning the correct gradient without errors.
Minimal Reproducible Example 👇
Error & Stacktrace⚠️
Zygote Error
Enzyme Error
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
The text was updated successfully, but these errors were encountered: