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
Error in AD when using the Optimization generated grad function. Works fine if we set autodiff=false within the solver. Also does not give the similar issue when using DifferentiationInterface.
Expected behavior
Should output the solution to the problem below something like this
using DifferentialEquations, Optimization
functionrosenbrock(u, p)
(p[1] - u[1])^2+ p[2] * (u[2] - u[1]^2)^2endfunctioncreate_prob()
u0 =zeros(2)
p = [1.0, 100.0]
optf =OptimizationFunction(rosenbrock, AutoForwardDiff())
prob =OptimizationProblem(optf, u0, p)
return prob
endfunctionsolve_prob(prob)
u0, p = prob.u0, prob.p
opt_cache = Optimization.OptimizationCache(prob, Optimization.LBFGS())
optf = opt_cache.f
functiongradient_flow!(du, u, p, t)
optf.grad(du, u, p)
du .=-du
end
ode_prob =SteadyStateProblem(gradient_flow!, u0, p)
sol =solve(ode_prob, DynamicSS(Rodas5()); maxiters=Int(1e6))
return sol.u
endfunctionmain()
prob =create_prob()
solution =solve_prob(prob)
println("Optimized solution:", solution)
endmain()
Error & Stacktrace ⚠️
ERROR: First call to automatic differentiation for the Jacobian
failed. This means that the user `f`function is not compatible
with automatic differentiation. Methods to fix this include:1. Turn off automatic differentiation (e.g. Rosenbrock23() becomes
Rosenbrock23(autodiff=false)). More details can befound at
https://docs.sciml.ai/DiffEqDocs/stable/features/performance_overloads/2. Improving the compatibility of `f` with ForwardDiff.jl automatic
differentiation (using tools like PreallocationTools.jl). More details
can be found at https://docs.sciml.ai/DiffEqDocs/stable/basics/faq/#Autodifferentiation-and-Dual-Numbers3. Defining analytical Jacobians. More details can be
found at https://docs.sciml.ai/DiffEqDocs/stable/types/ode_types/#SciMLBase.ODEFunction
Note: turning off automatic differentiation tends to have a very minimal
performance impact (for this use case, because it's forward mode for a
square Jacobian. This is different from optimization gradient scenarios).
However, one should be careful as some methods are more sensitive to
accurate gradients than others. Specifically, Rodas methods like `Rodas4`
and `Rodas5P` require accurate Jacobians in order to have good convergence,
while many other methods like BDF (`QNDF`, `FBDF`), SDIRK (`KenCarp4`),
and Rosenbrock-W (`Rosenbrock23`) do not. Thus ifusing an algorithm which
is sensitive to autodiff and solving at a low tolerance, please change the
algorithm as well.
Invalid Tag object:
Expected ForwardDiff.Tag{DifferentiationInterface.FixTail{typeof(rosenbrock), Tuple{Vector{Float64}}}, ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}, Float64, 2}},
Observed ForwardDiff.Tag{DifferentiationInterface.FixTail{typeof(rosenbrock), Tuple{Vector{Float64}}}, Float64}.
Stacktrace:
[1] WARNING: both NonlinearSolve and JumpProcesses export"NormTerminationMode"; uses of it inmodule DifferentialEquations must be qualified
jacobian!(J::Matrix{…}, f::SciMLBase.UJacobianWrapper{…}, x::Vector{…}, fx::Vector{…}, integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, jac_config::SparseDiffTools.ForwardColorJacCache{…})
@ OrdinaryDiffEqDifferentiation ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_wrappers.jl:238
[2] calc_J!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:152 [inlined]
[3] calc_W!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:557 [inlined]
[4] calc_W!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:495 [inlined]
[5] calc_rosenbrock_differentiation!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:631 [inlined]
[6] perform_step!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, cache::OrdinaryDiffEqRosenbrock.RosenbrockCache{…}, repeat_step::Bool)
@ OrdinaryDiffEqRosenbrock ~/.julia/packages/OrdinaryDiffEqRosenbrock/TfqbQ/src/rosenbrock_perform_step.jl:1337
[7] perform_step!
@ ~/.julia/packages/OrdinaryDiffEqRosenbrock/TfqbQ/src/rosenbrock_perform_step.jl:1320 [inlined]
[8] solve!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…})
@ OrdinaryDiffEqCore ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:620
[9] #__solve#62
@ ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:7 [inlined]
[10] __solve
@ ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:1 [inlined]
[11] solve_call(_prob::ODEProblem{…}, args::Rodas5{…}; merge_callbacks::Bool, kwargshandle::Nothing, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:635
[12] #solve_up#44
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1128 [inlined]
[13] solve_up
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1106 [inlined]
[14] solve(prob::ODEProblem{…}, args::Rodas5{…}; sensealg::Nothing, u0::Nothing, p::Nothing, wrap::Val{…}, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1043
[15] solve
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1033 [inlined]
[16] __solve(::SteadyStateProblem{…}, ::DynamicSS{…}; abstol::Float64, reltol::Float64, odesolve_kwargs::@NamedTuple{}, save_idxs::Nothing, termination_condition::DiffEqBase.NormTerminationMode{…}, kwargs::@Kwargs{…})
@ SteadyStateDiffEq ~/.julia/packages/SteadyStateDiffEq/KVNLU/src/solve.jl:56
[17] __solve
@ ~/.julia/packages/SteadyStateDiffEq/KVNLU/src/solve.jl:16 [inlined]
[18] #solve_call#35
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:635 [inlined]
[19] solve_up(prob::SteadyStateProblem{…}, sensealg::Nothing, u0::Vector{…}, p::Vector{…}, args::DynamicSS{…}; kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1128
[20] solve_up
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1106 [inlined]
[21] solve(prob::SteadyStateProblem{…}, args::DynamicSS{…}; sensealg::Nothing, u0::Nothing, p::Nothing, wrap::Val{…}, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1043
[22] solve_prob(prob::OptimizationProblem{…})
@ Main ~/Documents/julia/optf_grad.jl:26
[23] main()
@ Main ~/Documents/julia/optf_grad.jl:32
[24] top-level scope
@ ~/Documents/julia/optf_grad.jl:36
caused by: Invalid Tag object:
Expected ForwardDiff.Tag{DifferentiationInterface.FixTail{typeof(rosenbrock), Tuple{Vector{Float64}}}, ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}, Float64, 2}},
Observed ForwardDiff.Tag{DifferentiationInterface.FixTail{typeof(rosenbrock), Tuple{Vector{Float64}}}, Float64}.
Stacktrace:
[1] checktag(::Type{…}, f::DifferentiationInterface.FixTail{…}, x::Vector{…})
@ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/config.jl:34
[2] gradient!(result::Vector{…}, f::DifferentiationInterface.FixTail{…}, x::Vector{…}, cfg::ForwardDiff.GradientConfig{…}, ::Val{…})
@ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/gradient.jl:37
[3] gradient!(f::typeof(rosenbrock), grad::Vector{…}, prep::DifferentiationInterfaceForwardDiffExt.ForwardDiffGradientPrep{…}, backend::AutoForwardDiff{…}, x::Vector{…}, contexts::DifferentiationInterface.Constant{…})
@ DifferentiationInterfaceForwardDiffExt ~/.julia/packages/DifferentiationInterface/qrWdQ/ext/DifferentiationInterfaceForwardDiffExt/onearg.jl:362
[4] (::OptimizationBase.var"#grad#16"{…})(res::Vector{…}, θ::Vector{…}, p::Vector{…})
@ OptimizationBase ~/.julia/packages/OptimizationBase/gvXsf/src/OptimizationDIExt.jl:32
[5] (::var"#gradient_flow!#13"{…})(du::Vector{…}, u::Vector{…}, p::Vector{…}, t::Float64)
@ Main ~/Documents/julia/optf_grad.jl:21
[6] ODEFunction (repeats 2 times)
@ ~/.julia/packages/SciMLBase/sYmAV/src/scimlfunctions.jl:2468 [inlined]
[7] UJacobianWrapper
@ ~/.julia/packages/SciMLBase/sYmAV/src/function_wrappers.jl:32 [inlined]
[8] forwarddiff_color_jacobian!(J::Matrix{…}, f::SciMLBase.UJacobianWrapper{…}, x::Vector{…}, jac_cache::SparseDiffTools.ForwardColorJacCache{…})
@ SparseDiffTools ~/.julia/packages/SparseDiffTools/rjbzw/src/differentiation/compute_jacobian_ad.jl:376
[9] jacobian!(J::Matrix{…}, f::SciMLBase.UJacobianWrapper{…}, x::Vector{…}, fx::Vector{…}, integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, jac_config::SparseDiffTools.ForwardColorJacCache{…})
@ OrdinaryDiffEqDifferentiation ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_wrappers.jl:236
[10] calc_J!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:152 [inlined]
[11] calc_W!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:557 [inlined]
[12] calc_W!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:495 [inlined]
[13] calc_rosenbrock_differentiation!
@ ~/.julia/packages/OrdinaryDiffEqDifferentiation/6Bzim/src/derivative_utils.jl:631 [inlined]
[14] perform_step!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, cache::OrdinaryDiffEqRosenbrock.RosenbrockCache{…}, repeat_step::Bool)
@ OrdinaryDiffEqRosenbrock ~/.julia/packages/OrdinaryDiffEqRosenbrock/TfqbQ/src/rosenbrock_perform_step.jl:1337
[15] perform_step!
@ ~/.julia/packages/OrdinaryDiffEqRosenbrock/TfqbQ/src/rosenbrock_perform_step.jl:1320 [inlined]
[16] solve!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…})
@ OrdinaryDiffEqCore ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:620
[17] #__solve#62
@ ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:7 [inlined]
[18] __solve
@ ~/.julia/packages/OrdinaryDiffEqCore/vS7Uo/src/solve.jl:1 [inlined]
[19] solve_call(_prob::ODEProblem{…}, args::Rodas5{…}; merge_callbacks::Bool, kwargshandle::Nothing, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:635
[20] #solve_up#44
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1128 [inlined]
[21] solve_up
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1106 [inlined]
[22] solve(prob::ODEProblem{…}, args::Rodas5{…}; sensealg::Nothing, u0::Nothing, p::Nothing, wrap::Val{…}, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1043
[23] solve
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1033 [inlined]
[24] __solve(::SteadyStateProblem{…}, ::DynamicSS{…}; abstol::Float64, reltol::Float64, odesolve_kwargs::@NamedTuple{}, save_idxs::Nothing, termination_condition::DiffEqBase.NormTerminationMode{…}, kwargs::@Kwargs{…})
@ SteadyStateDiffEq ~/.julia/packages/SteadyStateDiffEq/KVNLU/src/solve.jl:56
[25] __solve
@ ~/.julia/packages/SteadyStateDiffEq/KVNLU/src/solve.jl:16 [inlined]
[26] #solve_call#35
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:635 [inlined]
[27] solve_up(prob::SteadyStateProblem{…}, sensealg::Nothing, u0::Vector{…}, p::Vector{…}, args::DynamicSS{…}; kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1128
[28] solve_up
@ ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1106 [inlined]
[29] solve(prob::SteadyStateProblem{…}, args::DynamicSS{…}; sensealg::Nothing, u0::Nothing, p::Nothing, wrap::Val{…}, kwargs::@Kwargs{…})
@ DiffEqBase ~/.julia/packages/DiffEqBase/PbBEl/src/solve.jl:1043
[30] solve_prob(prob::OptimizationProblem{…})
@ Main ~/Documents/julia/optf_grad.jl:26
[31] main()
@ Main ~/Documents/julia/optf_grad.jl:32
[32] top-level scope
@ ~/Documents/julia/optf_grad.jl:36
Some type information was truncated. Use `show(err)` to see complete types.
After setting autodiff=false(works fine)
using DifferentialEquations, Optimization
functionrosenbrock(u, p)
(p[1] - u[1])^2+ p[2] * (u[2] - u[1]^2)^2endfunctioncreate_prob()
u0 =zeros(2)
p = [1.0, 100.0]
optf =OptimizationFunction(rosenbrock, AutoForwardDiff())
prob =OptimizationProblem(optf, u0, p)
return prob
endfunctionsolve_prob(prob)
u0, p = prob.u0, prob.p
opt_cache = Optimization.OptimizationCache(prob, Optimization.LBFGS())
optf = opt_cache.f
functiongradient_flow!(du, u, p, t)
optf.grad(du, u, p)
du .=-du
end
ode_prob =SteadyStateProblem(gradient_flow!, u0, p)
sol =solve(ode_prob, DynamicSS(Rodas5(autodiff=false)); maxiters=Int(1e6))
return sol.u
endfunctionmain()
prob =create_prob()
solution =solve_prob(prob)
println("Optimized solution:", solution)
endmain()
DI code(also works fine)
using DifferentialEquations, Optimization, DifferentiationInterface, ForwardDiff
functionrosenbrock(u, p)
(p[1] - u[1])^2+ p[2] * (u[2] - u[1]^2)^2endfunctioncreate_prob()
u0 =zeros(2)
p = [1.0, 100.0]
optf =OptimizationFunction(rosenbrock, AutoForwardDiff())
prob =OptimizationProblem(optf, u0, p)
return prob
endfunctionsolve_prob(prob)
u0, p = prob.u0, prob.p
backend =AutoForwardDiff()
functiongradient_flow!(du, u, p, t)
f = x ->rosenbrock(x, p)
grad =gradient(f, backend, u)
du .=-grad
end
ode_prob =SteadyStateProblem(gradient_flow!, u0, p)
sol =solve(ode_prob, DynamicSS(Rodas5()); maxiters=Int(1e6))
return sol.u
endfunctionmain()
prob =create_prob()
solution =solve_prob(prob)
println("Optimized solution:", solution)
endmain()
Environment (please complete the following information):
Output of using Pkg; Pkg.status()
julia>using Pkg; Pkg.status()
Status `~/.julia/environments/v1.10/Project.toml`
[0c46a032] DifferentialEquations v7.16.0
[a0c0ee7d] DifferentiationInterface v0.6.43
[f6369f11] ForwardDiff v0.10.38
[7f7a1694] Optimization v4.1.1
[36348300] OptimizationOptimJL v0.4.1
[0bca4576] SciMLBase v2.75.1
⌃ [e88e6eb3] Zygote v0.6.75
Info Packages marked with ⌃ have new versions available and may be upgradable.
Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
Looks like mismatched FD tags. The manual construction of the OptimizationCache is pretty suspicious here I wouldn't recommend that. I am not on my computer right now to run it but avoiding that might do the trick
Describe the bug 🐞
Error in AD when using the Optimization generated grad function. Works fine if we set autodiff=false within the solver. Also does not give the similar issue when using DifferentiationInterface.
Expected behavior
Should output the solution to the problem below something like this
Minimal Reproducible Example 👇
Error & Stacktrace⚠️
After setting autodiff=false(works fine)
DI code(also works fine)
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: