JuliaDiff / SparseDiffTools.jl

Fast jacobian computation through sparsity exploitation and matrix coloring
MIT License
237 stars 41 forks source link

auto_hesvec #134

Closed ArnoStrouwen closed 2 months ago

ArnoStrouwen commented 3 years ago
using ForwardDiff
function seed_duals(x::AbstractArray{V},::Type{T},::ForwardDiff.Chunk{N} = ForwardDiff.Chunk(x,typemax(Int64))) where {V,T,N} 
    seeds = ForwardDiff.construct_seeds(ForwardDiff.Partials{N,V}) 
    duals = [ForwardDiff.Dual{T}(x[i],seeds[i]) for i in eachindex(x)] 
end 
function g(x)
    x[1]^3*x[2]^3
end
x = [2.0, 3.0]
v = [4.0, 5.0]
ForwardDiff.gradient(g,x)'*v # 2376
ForwardDiff.hessian(g,x)*v # 2916 2016

test1 =  seed_duals(Dual{Nothing}.(x,v),Nothing)
ForwardDiff.partials.(g(test1).partials)

test2 = Dual{Nothing}.(seed_duals(x,Nothing),v)
g(test2).partials[1].partials

Does these make sense?

ChrisRackauckas commented 3 years ago

This is the more direct way to do it:

https://github.com/SciML/GalacticOptim.jl/blob/master/src/function.jl#L128-L133

It requires these overloads:

https://github.com/SciML/DiffEqFlux.jl/blob/master/src/DiffEqFlux.jl#L57-L68

It would be good to add this all here.

ArnoStrouwen commented 3 years ago

There are also duplicates of some jacobian free methods in https://github.com/SciML/DiffEqSensitivity.jl/blob/25a5290013433bdadec776fa7b5a92f46e5a1777/src/derivative_wrappers.jl#L118-L123 ?

With all this duplication and scattering of methods, perhaps a DirectionalDerivative package might be a good idea?

ChrisRackauckas commented 3 years ago

It should all just be here, and then those packages should just depend on SparseDiffTools.jl. DiffEqFlux.jl and DiffEqSensitivity.jl just predate SparseDiffTools.jl by some time and haven't been refactored yet.

gdalle commented 2 months ago

Note that DifferentiationInterface.jl offers an hvp function which does exactly that, in a backend-agnostic manner and specializing on the mode combination (it chooses different methods for forward-over-reverse than reverse-over-forward for instance)

ChrisRackauckas commented 2 months ago

this exists