11
11
getsize (:: Val{N} ) where N = N
12
12
getsize (N:: Integer ) = N
13
13
void_setindex! (args... ) = (setindex! (args... ); return )
14
+ gettag (:: Type{ForwardDiff.Dual{T}} ) where {T} = T
14
15
15
16
const default_chunk_size = ForwardDiff. pickchunksize
17
+ const SMALLTAG = ForwardDiff. Tag (missing ,Float64)
16
18
17
19
function ForwardColorJacCache (f:: F ,x,_chunksize = nothing ;
18
20
dx = nothing ,
21
+ tag = nothing ,
19
22
colorvec= 1 : length (x),
20
23
sparsity:: Union{AbstractArray,Nothing} = nothing ) where {F}
21
24
@@ -25,15 +28,21 @@ function ForwardColorJacCache(f::F,x,_chunksize = nothing;
25
28
chunksize = _chunksize
26
29
end
27
30
31
+ if tag === nothing
32
+ T = typeof (ForwardDiff. Tag (f,eltype (vec (x))))
33
+ else
34
+ T = tag
35
+ end
36
+
28
37
if x isa Array
29
38
p = generate_chunked_partials (x,colorvec,chunksize)
30
- t = similar (x,Dual{typeof (ForwardDiff . Tag (f, eltype ( vec (x)))), eltype (x), length ( first ( first (p))) })
39
+ t = similar (x,Dual{T })
31
40
for i in eachindex (t)
32
- t[i] = Dual {typeof(ForwardDiff.Tag(f,eltype(vec(x)))) ,eltype(x),length(first(first(p)))} (x[i],ForwardDiff. Partials (first (p)[i]))
41
+ t[i] = Dual {T ,eltype(x),length(first(first(p)))} (x[i],ForwardDiff. Partials (first (p)[i]))
33
42
end
34
43
else
35
44
p = adapt .(parameterless_type (x),generate_chunked_partials (x,colorvec,chunksize))
36
- _t = Dual {typeof(ForwardDiff.Tag(f ,eltype(vec(x)))) } .(vec (x),first (p))
45
+ _t = Dual {T ,eltype(x),getsize(chunksize) } .(vec (x),ForwardDiff . Partials .( first (p) ))
37
46
t = ArrayInterface. restructure (x,_t)
38
47
end
39
48
@@ -44,7 +53,7 @@ function ForwardColorJacCache(f::F,x,_chunksize = nothing;
44
53
else
45
54
tup = ArrayInterface. allowed_getindex (ArrayInterface. allowed_getindex (p,1 ),1 ) .* false
46
55
_pi = adapt (parameterless_type (dx),[tup for i in 1 : length (dx)])
47
- fx = reshape (Dual {typeof(ForwardDiff.Tag(f ,eltype(vec(x)))) } .(vec (dx),_pi),size (dx)... )
56
+ fx = reshape (Dual {T ,eltype(dx),length(tup) } .(vec (dx),ForwardDiff . Partials .( _pi) ),size (dx)... )
48
57
_dx = dx
49
58
end
50
59
@@ -162,7 +171,7 @@ function forwarddiff_color_jacobian(J::AbstractMatrix{<:Number},f::F,x::Abstract
162
171
163
172
for i in eachindex (p)
164
173
partial_i = p[i]
165
- t = reshape (Dual {typeof(ForwardDiff.Tag(f, eltype(vecx)))} .(vecx, partial_i),size (t))
174
+ t = reshape (eltype (t) .(vecx, ForwardDiff . Partials .( partial_i) ),size (t))
166
175
fx = f (t)
167
176
if ! (sparsity isa Nothing)
168
177
for j in 1 : chunksize
@@ -230,7 +239,7 @@ function forwarddiff_color_jacobian_immutable(f,x::AbstractArray{<:Number},jac_c
230
239
231
240
for i in eachindex (p)
232
241
partial_i = p[i]
233
- t = reshape (Dual {typeof(ForwardDiff.Tag(f, eltype(vecx)))} .(vecx, partial_i),size (t))
242
+ t = reshape (eltype (t) .(vecx, ForwardDiff . Partials .( partial_i) ),size (t))
234
243
fx = f (t)
235
244
if ! (sparsity isa Nothing)
236
245
for j in 1 : chunksize
@@ -311,10 +320,10 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
311
320
312
321
if vect isa Array
313
322
@inbounds @simd ivdep for j in eachindex (vect)
314
- vect[j] = Dual {typeof(ForwardDiff.Tag(f, eltype(vecx)))} (vecx[j], partial_i[j])
323
+ vect[j] = eltype (t) (vecx[j], ForwardDiff . Partials ( partial_i[j]) )
315
324
end
316
325
else
317
- vect .= Dual {typeof(ForwardDiff.Tag(f, eltype(vecx)))} .(vecx, partial_i)
326
+ vect .= eltype (t) .(vecx, ForwardDiff . Partials .( partial_i) )
318
327
end
319
328
320
329
f (fx,t)
0 commit comments