This repository was archived by the owner on Apr 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcompute_jacobian_ad.jl
442 lines (401 loc) · 16.4 KB
/
compute_jacobian_ad.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
struct ForwardColorJacCache{T, T2, T3, T4, T5, T6}
t::T
fx::T2
dx::T3
p::T4
colorvec::T5
sparsity::T6
chunksize::Int
end
ForwardDiff.value(cache::ForwardColorJacCache) = ForwardDiff.value.(cache.fx)
value!(fx, cache::ForwardColorJacCache) = fx .= ForwardDiff.value.(cache.fx)
getsize(::Val{N}) where {N} = N
getsize(N::Integer) = N
void_setindex!(args...) = (setindex!(args...); return)
gettag(::Type{ForwardDiff.Dual{T}}) where {T} = T
const default_chunk_size = ForwardDiff.pickchunksize
const SMALLTAG = typeof(ForwardDiff.Tag(missing, Float64))
function ForwardColorJacCache(f::F, x, _chunksize = nothing; dx = nothing, tag = nothing,
colorvec = 1:length(x), sparsity::Union{AbstractArray, Nothing} = nothing) where {F}
if _chunksize isa Nothing
chunksize = ForwardDiff.pickchunksize(maximum(colorvec))
else
chunksize = _chunksize
end
if tag === nothing
T = typeof(ForwardDiff.Tag(f, eltype(vec(x))))
else
T = tag
end
if x isa Array
p = generate_chunked_partials(x, colorvec, chunksize)
DT = Dual{T, eltype(x), length(first(first(p)))}
t = _get_t(DT, x, p)
else
p = adapt.(parameterless_type(x), generate_chunked_partials(x, colorvec, chunksize))
_t = Dual{
T,
eltype(x),
getsize(chunksize)
}.(vec(x), ForwardDiff.Partials.(first(p)))
t = ArrayInterface.restructure(x, _t)
end
if dx isa Nothing
fx = similar(t)
_dx = similar(x)
else
tup = ArrayInterface.allowed_getindex(ArrayInterface.allowed_getindex(p, 1),
1) .* false
_pi = adapt(parameterless_type(dx), [tup for i in 1:length(dx)])
fx = reshape(
Dual{T, eltype(dx), length(tup)}.(vec(dx), ForwardDiff.Partials.(_pi)),
size(dx)...)
_dx = dx
end
ForwardColorJacCache(t, fx, _dx, p, colorvec, sparsity, getsize(chunksize))
end
# Function barrier for unknown constructor type
function _get_t(::Type{DT}, x, p) where {DT}
t = similar(x, DT)
for i in eachindex(t)
t[i] = DT(x[i], ForwardDiff.Partials(first(p)[i]))
end
t
end
function generate_chunked_partials(x, colorvec, N::Integer)
generate_chunked_partials(x, colorvec, Val(N))
end
function generate_chunked_partials(x, colorvec, cs::Val{chunksize}) where {chunksize}
maxcolor = maximum(colorvec)
num_of_chunks = cld(maxcolor, chunksize)
padding_size = (chunksize - (maxcolor % chunksize)) % chunksize
# partials = colorvec .== (1:maxcolor)'
partials = BitMatrix(undef, length(colorvec), maxcolor)
for i in 1:maxcolor, j in 1:length(colorvec)
partials[j, i] = colorvec[j] == i
end
padding_matrix = BitMatrix(undef, length(x), padding_size)
partials = hcat(partials, padding_matrix)
#chunked_partials = map(i -> Tuple.(eachrow(partials[:,(i-1)*chunksize+1:i*chunksize])),1:num_of_chunks)
chunked_partials = Vector{Vector{NTuple{chunksize, eltype(x)}}}(undef, num_of_chunks)
for i in 1:num_of_chunks
tmp = Vector{NTuple{chunksize, eltype(x)}}(undef, size(partials, 1))
for j in 1:size(partials, 1)
tmp[j] = partials_view_tup(partials, j, i, cs)
end
chunked_partials[i] = tmp
end
chunked_partials
end
@generated function partials_view_tup(partials, j, i, ::Val{chunksize}) where {chunksize}
:(Base.@ntuple $chunksize k->partials[j, (i - 1) * $chunksize + k])
end
function forwarddiff_color_jacobian(f::F,
x::AbstractArray{<:Number};
colorvec = 1:length(x),
sparsity = nothing,
jac_prototype = nothing,
chunksize = nothing,
dx = sparsity === nothing && jac_prototype === nothing ?
nothing : copy(x)) where {F} #if dx is nothing, we will estimate dx at the cost of a function call
if sparsity === nothing && jac_prototype === nothing
cfg = if chunksize === nothing
if typeof(x) <: StaticArrays.StaticArray
ForwardDiff.JacobianConfig(f, x,
ForwardDiff.Chunk{StaticArrays.Size(vec(x))[1]}())
else
ForwardDiff.JacobianConfig(f, x)
end
else
ForwardDiff.JacobianConfig(f, x, ForwardDiff.Chunk{getsize(chunksize)}())
end
return ForwardDiff.jacobian(f, x, cfg)
end
if dx isa Nothing
dx = f(x)
end
return forwarddiff_color_jacobian(f, x,
ForwardColorJacCache(f, x, chunksize, dx = dx,
colorvec = colorvec,
sparsity = sparsity),
jac_prototype)
end
function forwarddiff_color_jacobian(J::AbstractArray{<:Number}, f::F,
x::AbstractArray{<:Number};
colorvec = 1:length(x),
sparsity = nothing,
jac_prototype = nothing,
chunksize = nothing,
dx = similar(x, size(J, 1))) where {F} #dx kwarg can be used to avoid re-allocating dx every time
if sparsity === nothing && jac_prototype === nothing
cfg = chunksize === nothing ? ForwardDiff.JacobianConfig(f, x) :
ForwardDiff.JacobianConfig(f, x, ForwardDiff.Chunk(getsize(chunksize)))
return ForwardDiff.jacobian(f, x, cfg)
end
return forwarddiff_color_jacobian(J, f, x,
ForwardColorJacCache(f, x, chunksize, dx = dx,
colorvec = colorvec,
sparsity = sparsity))
end
function forwarddiff_color_jacobian(f::F, x::AbstractArray{<:Number},
jac_cache::ForwardColorJacCache,
jac_prototype = nothing) where {F}
if jac_prototype isa Nothing ? ArrayInterface.ismutable(x) :
ArrayInterface.ismutable(jac_prototype)
# Whenever J is mutable, we mutate it to avoid allocations
dx = jac_cache.dx
vecx = vec(x)
sparsity = jac_cache.sparsity
J = jac_prototype isa Nothing ?
(sparsity isa Nothing ? false .* vec(dx) .* vecx' :
zeros(eltype(x), size(sparsity))) : zero(jac_prototype)
return forwarddiff_color_jacobian(J, f, x, jac_cache)
else
return forwarddiff_color_jacobian_immutable(f, x, jac_cache, jac_prototype)
end
end
# Defined in extension. Polyester version of `forwarddiff_color_jacobian`
function polyesterforwarddiff_color_jacobian end
# When J is mutable, this version of forwarddiff_color_jacobian will mutate J to avoid allocations
function forwarddiff_color_jacobian(J::AbstractMatrix{<:Number}, f::F,
x::AbstractArray{<:Number},
jac_cache::ForwardColorJacCache) where {F}
t = jac_cache.t
dx = jac_cache.dx
p = jac_cache.p
colorvec = jac_cache.colorvec
sparsity = jac_cache.sparsity
chunksize = jac_cache.chunksize
color_i = 1
maxcolor = maximum(colorvec)
vecx = vec(x)
nrows, ncols = size(J)
if !(sparsity isa Nothing)
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
rows_index = [rows_index[i] for i in 1:length(rows_index)]
cols_index = [cols_index[i] for i in 1:length(cols_index)]
end
for i in eachindex(p)
partial_i = p[i]
t = reshape(eltype(t).(vecx, ForwardDiff.Partials.(partial_i)), size(t))
fx = f(t)
if !(sparsity isa Nothing)
for j in 1:chunksize
dx = vec(partials.(fx, j))
pick_inds = [i
for i in 1:length(rows_index)
if colorvec[cols_index[i]] == color_i]
rows_index_c = rows_index[pick_inds]
cols_index_c = cols_index[pick_inds]
if J isa SparseMatrixCSC || j > 1
# Use sparse matrix to add to J column by column except . . .
Ji = sparse(rows_index_c, cols_index_c, dx[rows_index_c], nrows, ncols)
else
# To overwrite pre-allocated matrix J, using sparse will cause an error
# so we use this step to overwrite J
len_rows = length(pick_inds)
unused_rows = setdiff(1:nrows, rows_index_c)
perm_rows = sortperm(vcat(rows_index_c, unused_rows))
cols_index_c = vcat(cols_index_c, zeros(Int, nrows - len_rows))[perm_rows]
Ji = [j == cols_index_c[i] ? dx[i] : false
for i in 1:nrows, j in 1:ncols]
end
if j == 1 && i == 1
J .= Ji # overwrite pre-allocated matrix J
else
J .+= Ji
end
color_i += 1
(color_i > maxcolor) && return J
end
else
for j in 1:chunksize
col_index = (i - 1) * chunksize + j
(col_index > ncols) && return J
Ji = mapreduce(
i -> i == col_index ? partials.(vec(fx), j) :
adapt(parameterless_type(J), zeros(eltype(J), nrows)),
hcat, 1:ncols)
if j == 1 && i == 1
J .= (size(Ji) != size(J) ? reshape(Ji, size(J)) : Ji) # overwrite pre-allocated matrix
else
J .+= (size(Ji) != size(J) ? reshape(Ji, size(J)) : Ji) #branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
end
end
end
end
return J
end
# When J is immutable, this version of forwarddiff_color_jacobian will avoid mutating J
function forwarddiff_color_jacobian_immutable(f::F, x::AbstractArray{<:Number},
jac_cache::ForwardColorJacCache, jac_prototype = nothing) where {F}
t = jac_cache.t
dx = jac_cache.dx
p = jac_cache.p
colorvec = jac_cache.colorvec
sparsity = jac_cache.sparsity
chunksize = jac_cache.chunksize
color_i = 1
maxcolor = maximum(colorvec)
vecx = vec(x)
J = jac_prototype isa Nothing ?
(sparsity isa Nothing ? false .* vec(dx) .* vecx' :
zeros(eltype(x), size(sparsity))) : zero(jac_prototype)
nrows, ncols = size(J)
if !(sparsity isa Nothing)
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
rows_index = [rows_index[i] for i in 1:length(rows_index)]
cols_index = [cols_index[i] for i in 1:length(cols_index)]
end
for i in eachindex(p)
partial_i = p[i]
t = reshape(eltype(t).(vecx, ForwardDiff.Partials.(partial_i)), size(t))
fx = f(t)
if !(sparsity isa Nothing)
for j in 1:chunksize
dx = vec(partials.(fx, j))
pick_inds = [i
for i in 1:length(rows_index)
if colorvec[cols_index[i]] == color_i]
rows_index_c = rows_index[pick_inds]
cols_index_c = cols_index[pick_inds]
if J isa SparseMatrixCSC
Ji = sparse(rows_index_c, cols_index_c, dx[rows_index_c], nrows, ncols)
else
len_rows = length(pick_inds)
unused_rows = setdiff(1:nrows, rows_index_c)
perm_rows = sortperm(vcat(rows_index_c, unused_rows))
cols_index_c = vcat(cols_index_c, zeros(Int, nrows - len_rows))[perm_rows]
Ji = [j == cols_index_c[i] ? dx[i] : false
for i in 1:nrows, j in 1:ncols]
end
J = J + Ji
color_i += 1
(color_i > maxcolor) && return J
end
else
for j in 1:chunksize
col_index = (i - 1) * chunksize + j
(col_index > ncols) && return J
Ji = mapreduce(
i -> i == col_index ? partials.(vec(fx), j) :
adapt(parameterless_type(J), zeros(eltype(J), nrows)),
hcat, 1:ncols)
J = J + (size(Ji) != size(J) ? reshape(Ji, size(J)) : Ji) #branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
end
end
end
return J
end
function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number}, f::F,
x::AbstractArray{<:Number}; dx = similar(x, size(J, 1)), colorvec = 1:length(x),
sparsity = ArrayInterface.has_sparsestruct(J) ? J : nothing) where {F}
forwarddiff_color_jacobian!(J, f, x, ForwardColorJacCache(f, x; dx, colorvec, sparsity))
end
function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
f::F,
x::AbstractArray{<:Number},
jac_cache::ForwardColorJacCache) where {F}
t = jac_cache.t
fx = jac_cache.fx
dx = jac_cache.dx
p = jac_cache.p
colorvec = jac_cache.colorvec
sparsity = jac_cache.sparsity
chunksize = jac_cache.chunksize
color_i = 1
adaptedcolorvec = adapt(__parameterless_type(typeof(dx)), colorvec)
maxcolor = maximum(colorvec)
if J isa AbstractSparseMatrix
fill!(nonzeros(J), zero(eltype(J)))
else
fill!(J, zero(eltype(J)))
end
if FiniteDiff._use_findstructralnz(sparsity)
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
else
rows_index = 1:size(J, 1)
cols_index = 1:size(J, 2)
end
# fast path if J and sparsity are both AbstractSparseMatrix and have the same sparsity pattern
sparseCSC_common_sparsity = FiniteDiff._use_sparseCSC_common_sparsity(J, sparsity)
vecx = vec(x)
vect = vec(t)
vecfx = vec(fx)
vecdx = vec(dx)
ncols = size(J, 2)
for i in eachindex(p)
partial_i = p[i]
if vect isa Array
@inbounds @simd ivdep for j in eachindex(vect)
vect[j] = eltype(t)(vecx[j], ForwardDiff.Partials(partial_i[j]))
end
else
vect .= eltype(t).(vecx, ForwardDiff.Partials.(partial_i))
end
f(fx, t)
if !(sparsity isa Nothing)
for j in 1:chunksize
if dx isa Array
@inbounds @simd for k in eachindex(dx)
dx[k] = partials(fx[k], j)
end
else
dx .= partials.(fx, j)
end
if ArrayInterface.fast_scalar_indexing(dx)
#dx is implicitly used in vecdx
if sparseCSC_common_sparsity
FiniteDiff._colorediteration!(J, vecdx, colorvec, color_i, ncols)
else
FiniteDiff._colorediteration!(J, sparsity, rows_index, cols_index,
vecdx, colorvec, color_i, ncols)
end
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* dx[rows_index]
or
J[rows_index, cols_index] .+= (colorvec[cols_index] .== color_i) .* dx[rows_index]
+= means requires a zero'd out start
=#
if J isa AbstractSparseMatrix
if J isa SparseMatrixCSC
@. void_setindex!(Ref(nonzeros(J)),
getindex(Ref(nonzeros(J)), rows_index) +
(getindex(Ref(adaptedcolorvec), cols_index) ==
color_i) * getindex(Ref(vecdx), rows_index),
rows_index)
else
nzval = @view nonzeros(J)[rows_index]
cv = @view adaptedcolorvec[cols_index]
vdx = @view dx[rows_index]
tmp = cv .== color_i
nzval .+= tmp .* vdx
end
else
@. void_setindex!(Ref(J),
getindex(Ref(J), rows_index, cols_index) +
(getindex(Ref(colorvec), cols_index) == color_i) *
getindex(Ref(vecdx), rows_index), rows_index,
cols_index)
end
end
color_i += 1
(color_i > maxcolor) && return J
end
else
for j in 1:chunksize
col_index = (i - 1) * chunksize + j
(col_index > ncols) && return J
if J isa Array
@inbounds @simd for k in 1:size(J, 1)
J[k, col_index] = partials(vecfx[k], j)
end
else
J[:, col_index] .= partials.(vecfx, j)
end
end
end
end
return J
end