Thank you for the assistance. I ran into issues with the invocation of SecondOrder, resulting in this stacktrace:
ERROR: MethodError: kwcall(::@NamedTuple{…}, ::typeof(OptimizationBase.instantiate_function), ::OptimizationFunction{…}, ::OptimizationBase.ReInitCache{…}, ::SecondOrder{…}, ::Int64) is ambiguous.
Candidates:
kwcall(::NamedTuple, ::typeof(OptimizationBase.instantiate_function), f::OptimizationFunction{true}, cache::OptimizationBase.ReInitCache, adtype::AbstractADType, num_cons)
@ OptimizationBase ~/.julia/packages/OptimizationBase/ghGuk/src/OptimizationDIExt.jl:267
kwcall(::NamedTuple, ::typeof(OptimizationBase.instantiate_function), f::OptimizationFunction{true}, x, adtype::Union{AutoZygote, SecondOrder{<:AbstractADType, <:AutoZygote}}, p)
@ OptimizationZygoteExt ~/.julia/packages/OptimizationBase/ghGuk/ext/OptimizationZygoteExt.jl:21
Possible fix, define
kwcall(::NamedTuple, ::typeof(OptimizationBase.instantiate_function), ::OptimizationFunction{…}, ::OptimizationBase.ReInitCache, ::Union{…}, ::Any)
Stacktrace:
[1] OptimizationCache(prob::OptimizationProblem{…}, opt::IPNewton{…}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, structural_analysis::Bool, manifold::Nothing, kwargs::@Kwargs{})
@ OptimizationBase ~/.julia/packages/OptimizationBase/ghGuk/src/cache.jl:60
[2] __init(prob::OptimizationProblem{…}, opt::IPNewton{…}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::@Kwargs{})
@ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/e3bUa/src/OptimizationOptimJL.jl:110
[3] __init(prob::OptimizationProblem{…}, opt::IPNewton{…})
@ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/e3bUa/src/OptimizationOptimJL.jl:81
[4] init(::OptimizationProblem{…}, ::IPNewton{…}; kwargs::@Kwargs{})
@ SciMLBase ~/.julia/packages/SciMLBase/tey0W/src/solve.jl:172
[5] init(::OptimizationProblem{…}, ::IPNewton{…})
@ SciMLBase ~/.julia/packages/SciMLBase/tey0W/src/solve.jl:170
[6] solve(::OptimizationProblem{…}, ::IPNewton{…}; kwargs::@Kwargs{})
@ SciMLBase ~/.julia/packages/SciMLBase/tey0W/src/solve.jl:94
[7] solve(::OptimizationProblem{…}, ::IPNewton{…})
@ SciMLBase ~/.julia/packages/SciMLBase/tey0W/src/solve.jl:91
[8] macro expansion
@ ~/Desktop/main/lib/Main.jl:253 [inlined]
[9] macro expansion
@ ~/.julia/packages/ProgressMeter/kVZZH/src/ProgressMeter.jl:1008 [inlined]
[10] top-level scope
@ ~/Desktop/main/lib/Main.jl:241
Some type information was truncated. Use `show(err)` to see complete types.
but at least knowing of DifferentiationInterface gives me a place to look up how to debug.
The second issue involves issues on my end as well. I have a series of functions relying on 2 parameters: a real number x and an integer order n. I define their derivatives and the differentiation rules using the derivative function. While the first order using Zygote would then work perfectly, second order derivatives relying on ForwardDiff would fail. I supplemented the definitions with duals just to get the code to run.
An example of what my implementations would look like is this:
function f(x::Real, n::Integer)
-- Code --
end
function df(x::Real, n::Integer)
-- Derivative code --
end
function f(xdx::Dual{Tag}, ndn::Dual) where {Tag}
x = xdx.value
dx = xdx.partials
n = ndn.value
return Dual{Tag}(f(x), df(x)*dz)
end
I think I am definitely doing something wrong with the dual on the order especially, but the Hessian calls kept failing saying there was no implementation for f(::Dual, ::Dual)
.
The generated stack trace is:
ERROR: Cannot determine ordering of Dual tags Nothing and ForwardDiff.Tag{DifferentiationInterfaceForwardDiffExt.ForwardDiffOverSomethingHVPWrapper{var"#loss#13"{Matrix{Float64}, ComponentVector{Float64, Vector{Float64}, Tuple{Axis{(α = ViewAxis(1:1, ShapedAxis(())), β = ViewAxis(2:2, ShapedAxis(())))}}}, Vector{ComplexF64}}}, Float64}
Stacktrace:
[1] ≺(a::Type, b::Type)
@ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/dual.jl:54
[2] *
@ ~/.julia/packages/ForwardDiff/PcZ48/src/dual.jl:140 [inlined]
[3] macro expansion
@ ~/.julia/packages/ForwardDiff/PcZ48/src/partials.jl:157 [inlined]
[4] scale_tuple
@ ~/.julia/packages/ForwardDiff/PcZ48/src/partials.jl:200 [inlined]
[5] *
@ ~/.julia/packages/ForwardDiff/PcZ48/src/partials.jl:111 [inlined]
[6] *
@ ~/.julia/packages/ForwardDiff/PcZ48/src/partials.jl:84 [inlined]
[7] jn(xdx::ForwardDiff.Dual{Nothing, ForwardDiff.Dual{…}, 2}, ndn::ForwardDiff.Dual{Nothing, Int64, 2})
@ Main.Library ~/Desktop/main/lib/Implementations.jl:192
[8] (::Zygote.var"#1410#1411"{typeof(jn)})(::ForwardDiff.Dual{ForwardDiff.Tag{…}, Float64, 3}, ::Int64)
@ Zygote ~/.julia/packages/Zygote/NRp5C/src/lib/broadcast.jl:276
[9] _broadcast_getindex_evalf
@ ./broadcast.jl:673 [inlined]
[10] _broadcast_getindex
@ ./broadcast.jl:646 [inlined]
[11] getindex
@ ./broadcast.jl:605 [inlined]
[12] copy
@ ./broadcast.jl:906 [inlined]
[13] materialize
@ ./broadcast.jl:867 [inlined]
[14] broadcast_forward
@ ~/.julia/packages/Zygote/NRp5C/src/lib/broadcast.jl:282 [inlined]
[15] _broadcast_generic
@ ~/.julia/packages/Zygote/NRp5C/src/lib/broadcast.jl:212 [inlined]
[16] adjoint
@ ~/.julia/packages/Zygote/NRp5C/src/lib/broadcast.jl:205 [inlined]
[17] _pullback(::Zygote.Context{…}, ::typeof(Base.Broadcast.broadcasted), ::Base.Broadcast.DefaultArrayStyle{…}, ::typeof(jn), ::Matrix{…}, ::Base.ReshapedArray{…})
@ Zygote ~/.julia/packages/ZygoteRules/M4xmc/src/adjoint.jl:67
[18] _apply(::Function, ::Vararg{Any})
@ Core ./boot.jl:946
[19] adjoint
@ ~/.julia/packages/Zygote/NRp5C/src/lib/lib.jl:203 [inlined]
[20] _pullback
@ ~/.julia/packages/ZygoteRules/M4xmc/src/adjoint.jl:67 [inlined]
[21] broadcasted
@ ./broadcast.jl:1326 [inlined]