22using SparseDiffTools,
33 Symbolics, ForwardDiff, LinearAlgebra, SparseArrays, Zygote, Enzyme, Test, StaticArrays
44
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
20+
521@views function fdiff (y, x) # in-place
622 L = length (x)
723 y[2 : (L - 1 )] .= x[1 : (L - 2 )] .- 2 .* x[2 : (L - 1 )] .+ x[3 : L]
@@ -38,11 +54,22 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
3854 @info " Sparsity Detection: $(nameof (typeof (sd))) "
3955 @info " Out of Place Function"
4056
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- AutoSparseFiniteDiff (), 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
4673 @testset " Cache & Reuse" begin
4774 cache = sparse_jacobian_cache (difftype, sd, fdiff, x)
4875 J = init_jacobian (cache)
@@ -59,7 +86,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
5986
6087 @test J ≈ J_true
6188
62- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff )
89+ if __isinferrable (difftype)
6390 @inferred sparse_jacobian (difftype, cache, fdiff, x)
6491 end
6592
@@ -71,7 +98,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
7198 J = sparse_jacobian (difftype, sd, fdiff, x)
7299
73100 @test J ≈ J_true
74- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff )
101+ if __isinferrable (difftype)
75102 @inferred sparse_jacobian (difftype, sd, fdiff, x)
76103 end
77104
@@ -114,7 +141,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
114141 J = sparse_jacobian (difftype, cache, fdiff, y, x)
115142
116143 @test J ≈ J_true
117- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff )
144+ if __isinferrable (difftype)
118145 @inferred sparse_jacobian (difftype, cache, fdiff, y, x)
119146 end
120147
@@ -126,7 +153,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
126153 J = sparse_jacobian (difftype, sd, fdiff, y, x)
127154
128155 @test J ≈ J_true
129- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff )
156+ if __isinferrable (difftype)
130157 @inferred sparse_jacobian (difftype, sd, fdiff, y, x)
131158 end
132159
0 commit comments