1
1
# # Sparse Jacobian tests
2
- using SparseDiffTools, PolyesterForwardDiff, Symbolics, ForwardDiff, LinearAlgebra,
3
- SparseArrays, Zygote, Enzyme, Test, StaticArrays
2
+ using SparseDiffTools,
3
+ Symbolics, ForwardDiff, LinearAlgebra, SparseArrays, Zygote, Enzyme, Test, StaticArrays
4
+
5
+ @static if VERSION ≥ v " 1.9"
6
+ using PolyesterForwardDiff
7
+ end
8
+
9
+ function __chunksize (:: Union {AutoSparseForwardDiff{C}, AutoForwardDiff{C},
10
+ AutoSparsePolyesterForwardDiff{C}, AutoPolyesterForwardDiff{C}}) where {C}
11
+ return C
12
+ end
13
+
14
+ function __isinferrable (difftype)
15
+ return ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
16
+ difftype isa AutoSparsePolyesterForwardDiff ||
17
+ difftype isa AutoPolyesterForwardDiff) ||
18
+ (__chunksize (difftype) isa Int && __chunksize (difftype) > 0 )
19
+ end
4
20
5
21
@views function fdiff (y, x) # in-place
6
22
L = length (x)
@@ -38,16 +54,22 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
38
54
@info " Sparsity Detection: $(nameof (typeof (sd))) "
39
55
@info " Out of Place Function"
40
56
41
- @testset " sparse_jacobian $(nameof (typeof (difftype))) : Out of Place" for difftype in (AutoSparseZygote (),
42
- AutoZygote (), AutoSparseForwardDiff (), AutoForwardDiff (),
43
- AutoSparseForwardDiff (; chunksize = 0 ), AutoForwardDiff (; chunksize = 0 ),
44
- AutoSparseForwardDiff (; chunksize = 4 ), AutoForwardDiff (; chunksize = 4 ),
45
- AutoSparsePolyesterForwardDiff (), AutoPolyesterForwardDiff (),
46
- AutoSparsePolyesterForwardDiff (; chunksize = 0 ),
47
- AutoPolyesterForwardDiff (; chunksize = 0 ),
48
- AutoSparsePolyesterForwardDiff (; chunksize = 4 ),
49
- AutoPolyesterForwardDiff (; chunksize = 4 ), AutoSparseFiniteDiff (),
50
- AutoFiniteDiff (), AutoEnzyme (), AutoSparseEnzyme ())
57
+ DIFFTYPES = [AutoSparseZygote (), AutoZygote (), AutoSparseForwardDiff (),
58
+ AutoForwardDiff (), AutoSparseForwardDiff (; chunksize = 0 ),
59
+ AutoForwardDiff (; chunksize = 0 ), AutoSparseForwardDiff (; chunksize = 4 ),
60
+ AutoForwardDiff (; chunksize = 4 ), AutoSparseFiniteDiff (), AutoFiniteDiff (),
61
+ AutoEnzyme (), AutoSparseEnzyme ()]
62
+
63
+ if VERSION ≥ v " 1.9"
64
+ append! (DIFFTYPES,
65
+ [AutoSparsePolyesterForwardDiff (), AutoPolyesterForwardDiff (),
66
+ AutoSparsePolyesterForwardDiff (; chunksize = 0 ),
67
+ AutoPolyesterForwardDiff (; chunksize = 0 ),
68
+ AutoSparsePolyesterForwardDiff (; chunksize = 4 ),
69
+ AutoPolyesterForwardDiff (; chunksize = 4 )])
70
+ end
71
+
72
+ @testset " sparse_jacobian $(nameof (typeof (difftype))) : Out of Place" for difftype in DIFFTYPES
51
73
@testset " Cache & Reuse" begin
52
74
cache = sparse_jacobian_cache (difftype, sd, fdiff, x)
53
75
J = init_jacobian (cache)
@@ -64,9 +86,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
64
86
65
87
@test J ≈ J_true
66
88
67
- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
68
- difftype isa AutoSparsePolyesterForwardDiff ||
69
- difftype isa AutoPolyesterForwardDiff)
89
+ if __isinferrable (difftype)
70
90
@inferred sparse_jacobian (difftype, cache, fdiff, x)
71
91
end
72
92
@@ -78,9 +98,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
78
98
J = sparse_jacobian (difftype, sd, fdiff, x)
79
99
80
100
@test J ≈ J_true
81
- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
82
- difftype isa AutoSparsePolyesterForwardDiff ||
83
- difftype isa AutoPolyesterForwardDiff)
101
+ if __isinferrable (difftype)
84
102
@inferred sparse_jacobian (difftype, sd, fdiff, x)
85
103
end
86
104
@@ -123,9 +141,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
123
141
J = sparse_jacobian (difftype, cache, fdiff, y, x)
124
142
125
143
@test J ≈ J_true
126
- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
127
- difftype isa AutoSparsePolyesterForwardDiff ||
128
- difftype isa AutoPolyesterForwardDiff)
144
+ if __isinferrable (difftype)
129
145
@inferred sparse_jacobian (difftype, cache, fdiff, y, x)
130
146
end
131
147
@@ -137,9 +153,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
137
153
J = sparse_jacobian (difftype, sd, fdiff, y, x)
138
154
139
155
@test J ≈ J_true
140
- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
141
- difftype isa AutoSparsePolyesterForwardDiff ||
142
- difftype isa AutoPolyesterForwardDiff)
156
+ if __isinferrable (difftype)
143
157
@inferred sparse_jacobian (difftype, sd, fdiff, y, x)
144
158
end
145
159
0 commit comments