-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start using DifferentiationInterface.jl #415
Comments
@gdalle I am nearly done integrating DI into NonlinearSolve. The only feature I don't know how to use is specifying
|
Currently the code is like: @inline function __sparsity_detection_alg(f::NonlinearFunction, ad::AutoSparse)
if f.sparsity === nothing
if f.jac_prototype === nothing
is_extension_loaded(Val(:Symbolics)) && return SymbolicsSparsityDetection()
return ApproximateJacobianSparsity()
else
jac_prototype = f.jac_prototype
end
elseif f.sparsity isa AbstractSparsityDetection
f.jac_prototype === nothing && return f.sparsity
jac_prototype = f.jac_prototype
elseif f.sparsity isa AbstractMatrix
jac_prototype = f.sparsity
elseif f.jac_prototype isa AbstractMatrix
jac_prototype = f.jac_prototype
else
error("`sparsity::typeof($(typeof(f.sparsity)))` & \
`jac_prototype::typeof($(typeof(f.jac_prototype)))` is not supported. \
Use `sparsity::AbstractMatrix` or `sparsity::AbstractSparsityDetection` or \
set to `nothing`. `jac_prototype` can be set to `nothing` or an \
`AbstractMatrix`.")
end
if SciMLBase.has_colorvec(f)
return PrecomputedJacobianColorvec(; jac_prototype,
f.colorvec,
partition_by_rows = ADTypes.mode(ad) isa ADTypes.ReverseMode)
else
return JacPrototypeSparsityDetection(; jac_prototype)
end
end
|
For passing a known sparsity pattern, you can use For passing the color vector I don't have a good answer right now, because this logic is handled by SparseMatrixColorings.jl and the coloring result types there are a bit more complex than just a vector of colors (this is especially true for Hessian coloring, which didn't use to be a thing in SparseDiffTools.jl). Let me think about it a little. Also, |
Thanks. I am fine with some manual combination of SparseMatrixColorings and DI if that is easy. This functionality is not exposed to the user so we have some leeway here. |
The main issue I see is that this code mixes sparsity detection and coloring, like SparseDiffTools does, whereas the |
the
|
The only part that doesn't fit in here is that we can have the colorvec, so we just need to do the AD in that case |
Maybe I could add a KnownColorsColoringAlgorithm to SparseMatrixColorings, mirroring the KnownJacobianSparsityDetector? |
I think that would do it |
Approximate colorings can be used in order to give an accelerated Jacobian. Is there a way to do approximate colorings? Also, using colorings on a dense Jacobian can be useful if the sparsity would is not enough and would slow down the LU. Can you sparse fill a dense matrix? |
At the moment there is no approximate coloring but I could add it to DI. As for filling matrices with the results of sparse AD, SparseMatrixColorings should work with any mutable AbstractMatrix. However it is only optimized for SparseMatrixCSC right now. More structured matrix types could be added in package extensions. |
I think the main case of it is using a chosen color vector with a dense matrix. The reason for that is again if sparse LU is slow on the matrix (sparse matrices really need to be "sufficiently large" for sparse factorization to make sense) but coloring still makes sense to cut down the Jacobian cost. |
Is it possible to query DI to get a Jacobian structure (basically the result of Note that |
Currently, we have four variants of coloring in
Star and acyclic coloring are generally used on Hessians, while row and column coloring do not exploit symmetry and are mostly used for Jacobians. |
Not at the moment no. Are we talking about sparse AD or general AD?
But then what kind of cache do you want to get in this case? |
Sorry I meant cache for NonlinearSolve. I am currently running it once to get the structure as you mention. My structure I mean, if the returned |
Then I would recommend you just |
Right. This is what I will do (am doing on master) and seems like a fine solution since users caring too much about performance (1 jac call) should be using the caching interface anyways |
@avik-pal does this do what you want in terms of fixing the vector of colors? And it is explained clearly enough? |
I think so. Do you need to make the conversion to sparse array? If you have a BandedMatrix it seems like a bad idea to make it Sparse because then the jacobian function with presumably return a sparse array instead of a banded matrix and linear solve will hit a regression |
At the moment yes, there will be a conversion to What matters to me right now is getting the necessary functionality up and running, then we can see what the benchmarks tell us about performance regressions. But a fair warning that may not be obvious to everyone: when switching to the DI ecosystem, there will be performance regressions in some cases, and performance improvements in others. A good example is the Brusselator AD benchmark, where sparsity detection is 100x faster, coloring 10x faster, and differentiation 2x slower (although this has probably improved with the latest DI and SMC releases, I'll have to run it again). |
Let me make a branch using DI for sparse jacobians, but I will hold off on merging that for now. |
FYI the new version of SparseMatrixColorings that includes |
I re-ran the Brusselator sparse AD benchmark with the latest versions of everything, and DI is now on par with SparseDiffTools: https://docs.sciml.ai/SciMLBenchmarksOutput/dev/AutomaticDifferentiationSparse/BrusselatorSparseAD/ |
Seeing Optimization.jl attempt to use DI, it seems like we have most of the features in place. We might be able to drop a lot of unnecessary code my making that shift!
The text was updated successfully, but these errors were encountered: