Possibility to store outcome of `ForwardDiff.jacobian` already in sparse format

I use ForwardDiff.jacobian in the following way:

function _jacobian_ad_forward(p, t0, u0_ode, du_ode, config)
    J = ForwardDiff.jacobian(du_ode, u0_ode, config) do du_ode, u_ode
        f!(du_ode, u_ode, p, t0)
    end

    return J
end

where my Jacobian J is typically very sparse.
I am aware of SparseDiffTools.jl which offers a sparse Jacobian computation based on a to-be-supplied sparsity pattern.
To my understanding SparseDiffTools.jl does computations then only for the non-zero entries according to the sparsity pattern.

In my case, obtaining the sparsity pattern is not easy without major refactoring of the code behind f.
Also, the actual computation of the Jacobian is not too time consuming, I am more worried about the memory requirements for storing entire J.

Thus, my question: Is it possible to use ForwardDiff.jacobian (or a related function) such that only the non-zero outcomes are stored (in a sparse format)?

Not really. You could setup a different accumulation using jvps though, basically calculate each vector, call sparse, and concatenate.

1 Like

FastDifferentiation.jl might help you. It’s good at detecting sparsity in Jacobians and stores them directly in a sparse format. It has limitations (see the docs) but if it works for your problem it should be efficient.

2 Likes