11# # 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
420
521@views function fdiff(y, x) # in-place
622 L = length(x)
@@ -38,16 +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- 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
5173 @testset " Cache & Reuse" begin
5274 cache = sparse_jacobian_cache(difftype, sd, fdiff, x)
5375 J = init_jacobian(cache)
@@ -64,9 +86,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
6486
6587 @test J ≈ J_true
6688
67- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
68- difftype isa AutoSparsePolyesterForwardDiff ||
69- difftype isa AutoPolyesterForwardDiff)
89+ if __isinferrable(difftype)
7090 @inferred sparse_jacobian(difftype, cache, fdiff, x)
7191 end
7292
@@ -78,9 +98,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
7898 J = sparse_jacobian(difftype, sd, fdiff, x)
7999
80100 @test J ≈ J_true
81- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
82- difftype isa AutoSparsePolyesterForwardDiff ||
83- difftype isa AutoPolyesterForwardDiff)
101+ if __isinferrable(difftype)
84102 @inferred sparse_jacobian(difftype, sd, fdiff, x)
85103 end
86104
@@ -123,9 +141,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
123141 J = sparse_jacobian(difftype, cache, fdiff, y, x)
124142
125143 @test J ≈ J_true
126- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
127- difftype isa AutoSparsePolyesterForwardDiff ||
128- difftype isa AutoPolyesterForwardDiff)
144+ if __isinferrable(difftype)
129145 @inferred sparse_jacobian(difftype, cache, fdiff, y, x)
130146 end
131147
@@ -137,9 +153,7 @@ SPARSITY_DETECTION_ALGS = [JacPrototypeSparsityDetection(; jac_prototype = J_spa
137153 J = sparse_jacobian(difftype, sd, fdiff, y, x)
138154
139155 @test J ≈ J_true
140- if ! (difftype isa AutoSparseForwardDiff || difftype isa AutoForwardDiff ||
141- difftype isa AutoSparsePolyesterForwardDiff ||
142- difftype isa AutoPolyesterForwardDiff)
156+ if __isinferrable(difftype)
143157 @inferred sparse_jacobian(difftype, sd, fdiff, y, x)
144158 end
145159
0 commit comments