1
1
module SparseDiffToolsPolyesterForwardDiffExt
2
2
3
- using ADTypes, SparseDiffTools, PolyesterForwardDiff
3
+ using ADTypes, SparseDiffTools, PolyesterForwardDiff, UnPack, Random, SparseArrays
4
4
import ForwardDiff
5
5
import SparseDiffTools: AbstractMaybeSparseJacobianCache, AbstractMaybeSparsityDetection,
6
6
ForwardColorJacCache, NoMatrixColoring, sparse_jacobian_cache,
7
7
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!
9
11
10
12
struct PolyesterForwardDiffJacobianCache{CO, CA, J, FX, X} < :
11
13
AbstractMaybeSparseJacobianCache
@@ -17,18 +19,14 @@ struct PolyesterForwardDiffJacobianCache{CO, CA, J, FX, X} <:
17
19
end
18
20
19
21
function 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}
24
24
coloring_result = sd (ad, f, x)
25
25
fx = fx === nothing ? similar (f (x)) : fx
26
26
if coloring_result isa NoMatrixColoring
27
27
cache = __chunksize (ad, x)
28
28
jac_prototype = nothing
29
29
else
30
- @warn """ Currently PolyesterForwardDiff does not support sparsity detection
31
- natively. Falling back to using ForwardDiff.jl""" maxlog= 1
32
30
tag = __standard_tag (nothing , x)
33
31
# Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
34
32
cache = ForwardColorJacCache (f, x, __chunksize (ad); coloring_result. colorvec,
@@ -39,17 +37,16 @@ function sparse_jacobian_cache(
39
37
end
40
38
41
39
function 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}
46
42
coloring_result = sd (ad, f!, fx, x)
47
43
if coloring_result isa NoMatrixColoring
48
44
cache = __chunksize (ad, x)
49
45
jac_prototype = nothing
50
46
else
51
47
@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
53
50
tag = __standard_tag (nothing , x)
54
51
# Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
55
52
cache = ForwardColorJacCache (f!, x, __chunksize (ad); coloring_result. colorvec,
62
59
function sparse_jacobian! (J:: AbstractMatrix , _, cache:: PolyesterForwardDiffJacobianCache ,
63
60
f:: F , x) where {F}
64
61
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)
66
63
else
67
64
PolyesterForwardDiff. threaded_jacobian! (f, J, x, cache. cache) # Don't try to exploit sparsity
68
65
end
72
69
function sparse_jacobian! (J:: AbstractMatrix , _, cache:: PolyesterForwardDiffJacobianCache ,
73
70
f!:: F , fx, x) where {F}
74
71
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)
76
73
else
77
74
PolyesterForwardDiff. threaded_jacobian! (f!, fx, J, x, cache. cache) # Don't try to exploit sparsity
78
75
end
79
76
return J
80
77
end
81
78
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
+
82
114
end
0 commit comments