11module SparseDiffToolsPolyesterForwardDiffExt
22
3- using ADTypes, SparseDiffTools, PolyesterForwardDiff
3+ using ADTypes, SparseDiffTools, PolyesterForwardDiff, UnPack, Random, SparseArrays
44import ForwardDiff
55import SparseDiffTools: AbstractMaybeSparseJacobianCache, AbstractMaybeSparsityDetection,
66 ForwardColorJacCache, NoMatrixColoring, sparse_jacobian_cache,
77 sparse_jacobian!,
8- sparse_jacobian_static_array, __standard_tag, __chunksize
8+ sparse_jacobian_static_array, __standard_tag, __chunksize,
9+ polyesterforwarddiff_color_jacobian,
10+ polyesterforwarddiff_color_jacobian!
911
1012struct PolyesterForwardDiffJacobianCache{CO, CA, J, FX, X} < :
1113 AbstractMaybeSparseJacobianCache
@@ -17,18 +19,14 @@ struct PolyesterForwardDiffJacobianCache{CO, CA, J, FX, X} <:
1719end
1820
1921function sparse_jacobian_cache (
20- ad:: Union {AutoSparsePolyesterForwardDiff,
21- AutoPolyesterForwardDiff},
22- sd:: AbstractMaybeSparsityDetection , f:: F , x;
23- fx = nothing ) where {F}
22+ ad:: Union{AutoSparsePolyesterForwardDiff, AutoPolyesterForwardDiff} ,
23+ sd:: AbstractMaybeSparsityDetection , f:: F , x; fx = nothing ) where {F}
2424 coloring_result = sd (ad, f, x)
2525 fx = fx === nothing ? similar (f (x)) : fx
2626 if coloring_result isa NoMatrixColoring
2727 cache = __chunksize (ad, x)
2828 jac_prototype = nothing
2929 else
30- @warn """ Currently PolyesterForwardDiff does not support sparsity detection
31- natively. Falling back to using ForwardDiff.jl""" maxlog= 1
3230 tag = __standard_tag (nothing , x)
3331 # Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
3432 cache = ForwardColorJacCache (f, x, __chunksize (ad); coloring_result. colorvec,
@@ -39,17 +37,16 @@ function sparse_jacobian_cache(
3937end
4038
4139function sparse_jacobian_cache (
42- ad:: Union {AutoSparsePolyesterForwardDiff,
43- AutoPolyesterForwardDiff},
44- sd:: AbstractMaybeSparsityDetection , f!:: F , fx,
45- x) where {F}
40+ ad:: Union{AutoSparsePolyesterForwardDiff, AutoPolyesterForwardDiff} ,
41+ sd:: AbstractMaybeSparsityDetection , f!:: F , fx, x) where {F}
4642 coloring_result = sd (ad, f!, fx, x)
4743 if coloring_result isa NoMatrixColoring
4844 cache = __chunksize (ad, x)
4945 jac_prototype = nothing
5046 else
5147 @warn """ Currently PolyesterForwardDiff does not support sparsity detection
52- natively. Falling back to using ForwardDiff.jl""" maxlog= 1
48+ natively for inplace functions. Falling back to using
49+ ForwardDiff.jl""" maxlog= 1
5350 tag = __standard_tag (nothing , x)
5451 # Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
5552 cache = ForwardColorJacCache (f!, x, __chunksize (ad); coloring_result. colorvec,
6259function sparse_jacobian! (J:: AbstractMatrix , _, cache:: PolyesterForwardDiffJacobianCache ,
6360 f:: F , x) where {F}
6461 if cache. cache isa ForwardColorJacCache
65- forwarddiff_color_jacobian (J, f, x, cache. cache) # Use Sparse ForwardDiff
62+ polyesterforwarddiff_color_jacobian (J, f, x, cache. cache)
6663 else
6764 PolyesterForwardDiff. threaded_jacobian! (f, J, x, cache. cache) # Don't try to exploit sparsity
6865 end
7269function sparse_jacobian! (J:: AbstractMatrix , _, cache:: PolyesterForwardDiffJacobianCache ,
7370 f!:: F , fx, x) where {F}
7471 if cache. cache isa ForwardColorJacCache
75- forwarddiff_color_jacobian! (J, f!, x, cache. cache) # Use Sparse ForwardDiff
72+ forwarddiff_color_jacobian! (J, f!, x, cache. cache)
7673 else
7774 PolyesterForwardDiff. threaded_jacobian! (f!, fx, J, x, cache. cache) # Don't try to exploit sparsity
7875 end
7976 return J
8077end
8178
79+ # # Approximate Sparsity Detection
80+ function (alg:: ApproximateJacobianSparsity )(
81+ ad:: AutoSparsePolyesterForwardDiff , f:: F , x; fx = nothing , kwargs... ) where {F}
82+ @unpack ntrials, rng = alg
83+ fx = fx === nothing ? f (x) : fx
84+ ck = __chunksize (ad, x)
85+ J = fill! (similar (fx, length (fx), length (x)), 0 )
86+ J_cache = similar (J)
87+ x_ = similar (x)
88+ for _ in 1 : ntrials
89+ randn! (rng, x_)
90+ PolyesterForwardDiff. threaded_jacobian! (f, J_cache, x_, ck)
91+ @. J += abs (J_cache)
92+ end
93+ return (JacPrototypeSparsityDetection (; jac_prototype = sparse (J), alg. alg))(ad, f, x;
94+ fx, kwargs... )
95+ end
96+
97+ function (alg:: ApproximateJacobianSparsity )(
98+ ad:: AutoSparsePolyesterForwardDiff , f:: F , fx, x;
99+ kwargs... ) where {F}
100+ @unpack ntrials, rng = alg
101+ ck = __chunksize (ad, x)
102+ J = fill! (similar (fx, length (fx), length (x)), 0 )
103+ J_cache = similar (J)
104+ x_ = similar (x)
105+ for _ in 1 : ntrials
106+ randn! (rng, x_)
107+ PolyesterForwardDiff. threaded_jacobian! (f, fx, J_cache, x_, ck)
108+ @. J += abs (J_cache)
109+ end
110+ return (JacPrototypeSparsityDetection (; jac_prototype = sparse (J), alg. alg))(ad, f, x;
111+ fx, kwargs... )
112+ end
113+
82114end
0 commit comments