@@ -10,20 +10,26 @@ __getfield(c::ForwardDiffJacobianCache, ::Val{:jac_prototype}) = c.jac_prototype
10
10
11
11
struct SparseDiffToolsTag end
12
12
13
+ function ForwardDiff. checktag (:: Type{<:ForwardDiff.Tag{<:SparseDiffToolsTag, <:T}} , f:: F ,
14
+ x:: AbstractArray{T} ) where {T, F}
15
+ return true
16
+ end
17
+
13
18
__standard_tag (:: Nothing , x) = ForwardDiff. Tag (SparseDiffToolsTag (), eltype (x))
14
- __standard_tag (tag, _ ) = tag
19
+ __standard_tag (tag, x ) = ForwardDiff . Tag ( tag, eltype (x))
15
20
16
21
function sparse_jacobian_cache (ad:: Union{AutoSparseForwardDiff, AutoForwardDiff} ,
17
22
sd:: AbstractMaybeSparsityDetection , f:: F , x; fx = nothing ) where {F}
18
23
coloring_result = sd (ad, f, x)
19
24
fx = fx === nothing ? similar (f (x)) : fx
25
+ tag = __standard_tag (ad. tag, x)
20
26
if coloring_result isa NoMatrixColoring
21
- cache = ForwardDiff. JacobianConfig (f, x, __chunksize (ad, x),
22
- __standard_tag (ad. tag, x))
27
+ cache = ForwardDiff. JacobianConfig (f, x, __chunksize (ad, x), tag)
23
28
jac_prototype = nothing
24
29
else
30
+ # Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
25
31
cache = ForwardColorJacCache (f, x, __chunksize (ad); coloring_result. colorvec,
26
- dx = fx, sparsity = coloring_result. jacobian_sparsity, ad . tag)
32
+ dx = fx, sparsity = coloring_result. jacobian_sparsity, tag = typeof (tag) )
27
33
jac_prototype = coloring_result. jacobian_sparsity
28
34
end
29
35
return ForwardDiffJacobianCache (coloring_result, cache, jac_prototype, fx, x)
32
38
function sparse_jacobian_cache (ad:: Union{AutoSparseForwardDiff, AutoForwardDiff} ,
33
39
sd:: AbstractMaybeSparsityDetection , f!:: F , fx, x) where {F}
34
40
coloring_result = sd (ad, f!, fx, x)
41
+ tag = __standard_tag (ad. tag, x)
35
42
if coloring_result isa NoMatrixColoring
36
- cache = ForwardDiff. JacobianConfig (f!, fx, x, __chunksize (ad, x),
37
- __standard_tag (ad. tag, x))
43
+ cache = ForwardDiff. JacobianConfig (f!, fx, x, __chunksize (ad, x), tag)
38
44
jac_prototype = nothing
39
45
else
46
+ # Colored ForwardDiff passes `tag` directly into Dual so we need the `typeof`
40
47
cache = ForwardColorJacCache (f!, x, __chunksize (ad); coloring_result. colorvec,
41
- dx = fx, sparsity = coloring_result. jacobian_sparsity, ad . tag)
48
+ dx = fx, sparsity = coloring_result. jacobian_sparsity, tag = typeof (tag) )
42
49
jac_prototype = coloring_result. jacobian_sparsity
43
50
end
44
51
return ForwardDiffJacobianCache (coloring_result, cache, jac_prototype, fx, x)
@@ -49,8 +56,7 @@ function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache,
49
56
if cache. cache isa ForwardColorJacCache
50
57
forwarddiff_color_jacobian (J, f, x, cache. cache) # Use Sparse ForwardDiff
51
58
else
52
- # Disable tag checking since we set the tag to our custom tag
53
- ForwardDiff. jacobian! (J, f, x, cache. cache, Val (false )) # Don't try to exploit sparsity
59
+ ForwardDiff. jacobian! (J, f, x, cache. cache) # Don't try to exploit sparsity
54
60
end
55
61
return J
56
62
end
@@ -60,8 +66,7 @@ function sparse_jacobian!(J::AbstractMatrix, _, cache::ForwardDiffJacobianCache,
60
66
if cache. cache isa ForwardColorJacCache
61
67
forwarddiff_color_jacobian! (J, f!, x, cache. cache) # Use Sparse ForwardDiff
62
68
else
63
- # Disable tag checking since we set the tag to our custom tag
64
- ForwardDiff. jacobian! (J, f!, fx, x, cache. cache, Val (false )) # Don't try to exploit sparsity
69
+ ForwardDiff. jacobian! (J, f!, fx, x, cache. cache) # Don't try to exploit sparsity
65
70
end
66
71
return J
67
72
end
0 commit comments