Optimization.jl icon indicating copy to clipboard operation
Optimization.jl copied to clipboard

Static arrays + autodiff

Open aplavin opened this issue 2 years ago • 2 comments

I tried using static vectors for parameters, which works nicely. But not when combined with autodiff:

julia> using Optimization, OptimizationOptimJL, StaticArrays, ForwardDiff

# don't specify inplace/outofplace:

julia> of = OptimizationFunction((x, p) -> sum(x), Optimization.AutoForwardDiff())
julia> prob = OptimizationProblem(of, SVector(0., 0.), nothing)
julia> solve(prob, Optim.GradientDescent())
ERROR: setindex!(::SVector{2, Float64}, value, ::Int) is not defined.

# specify out of place:

julia> of = OptimizationFunction{false}((x, p) -> sum(x), Optimization.AutoForwardDiff())
julia> prob = OptimizationProblem(of, SVector(0., 0.), nothing)
julia> solve(prob, Optim.GradientDescent())
ERROR: Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends

Is this expected?

aplavin avatar Feb 27 '23 19:02 aplavin

The ForwardDiff support for StaticArrays need to avoid the mutable DiffResults that we currently have it setup with. So I guess it's expected in that, now that you have mentioned it in an issue, I can see why it fails. Though I think it was just untested before. Not hard to fix though, but needs the effort.

ChrisRackauckas avatar Feb 28 '23 10:02 ChrisRackauckas

Beware that static arrays don't play nice with DiffResults sometimes:

https://github.com/JuliaDiff/DiffResults.jl/issues/26

gdalle avatar Mar 22 '24 14:03 gdalle