-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_ad.jl
282 lines (250 loc) · 7.66 KB
/
test_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
using SparseDiffTools
using ForwardDiff: Dual, jacobian, value
using SparseArrays, Test
using LinearAlgebra
using BlockBandedMatrices
using BandedMatrices
using StaticArrays
fcalls = 0
function f(dx, x)
global fcalls += 1
for i in 2:(length(x) - 1)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
dx[end] = x[end - 1] - 2x[end]
nothing
end
function oopf(x)
global fcalls += 1
dx = zero(x)
for i in 2:(length(x) - 1)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
dx[end] = x[end - 1] - 2x[end]
dx
end
function nsqf(x)#length(dx)<length(x)
global fcalls += 1
dx = zero(x)[1:div(length(x), 2)]
for i in 2:length(dx)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
dx
end
function nsqf2(x)#length(dx)>length(x)
global fcalls += 1
dx = zeros(eltype(x), length(x) * 2)
for i in 2:(length(x) - 1)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
dx
end
function nsqf!(dx, x)
global fcalls += 1
for i in 2:length(dx)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
nothing
end
function nsqf2!(dx, x)
global fcalls += 1
for i in 2:(length(x) - 1)
dx[i] = x[i - 1] - 2x[i] + x[i + 1]
end
dx[1] = -2x[1] + x[2]
nothing
end
function staticf(x, N = length(x))
global fcalls += 1
SVector{N}([i == 1 ? -2x[1] + x[2] :
(i == N ? x[N - 1] - 2x[N] : x[i - 1] - 2x[i] + x[i + 1]) for i in 1:N])
end
function staticnsqf(x, N = div(length(x), 2))
global fcalls += 1
SVector{N}(vcat([-2x[1] + x[2]], [x[i - 1] - 2x[i] + x[i + 1] for i in 2:N]))
end
function second_derivative_stencil(N)
A = zeros(N, N)
for i in 1:N, j in 1:N
(j - i == -1 || j - i == 1) && (A[i, j] = 1)
j - i == 0 && (A[i, j] = -2)
end
A
end
@info "ended definitions"
x = rand(30)
dx = rand(30)
J = jacobian(f, dx, x)
@test J ≈ second_derivative_stencil(30)
_J = sparse(J)
@test fcalls == 3
fcalls = 0
_J1 = similar(_J)
forwarddiff_color_jacobian!(_J1, f, x, colorvec = repeat(1:3, 10))
@test _J1 ≈ J
@test fcalls == 1
@info "second passed"
fcalls = 0
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec = repeat(1:3, 10), sparsity = _J,
jac_prototype = _J)
@test _J1 ≈ J
@test typeof(_J1) == typeof(_J)
@test fcalls == 1
@info "third passed"
fcalls = 0
_J1 = forwarddiff_color_jacobian(oopf, x, colorvec = repeat(1:3, 10), sparsity = _J)
@test _J1 ≈ J
@test fcalls == 1
#oop with in-place Jacobian
fcalls = 0
_oop_jacout = sparse(1.01 .* J) # want to be nonzero to check that the pre-allocated matrix is overwritten properly
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec = repeat(1:3, 10), sparsity = _J,
jac_prototype = _J)
@test _oop_jacout ≈ J
@test typeof(_oop_jacout) == typeof(_J)
@test fcalls == 1
# BandedMatrix
_oop_jacout = BandedMatrix(-1 => diag(J, -1) .* 1.01, 0 => diag(J, 0) .* 1.01,
1 => diag(J, 1) .* 1.01) # check w/BandedMatrix instead of sparse
fcalls = 0
forwarddiff_color_jacobian(_oop_jacout, oopf, x; colorvec = repeat(1:3, 10), sparsity = _J)
@test _oop_jacout ≈ J
@test isa(_oop_jacout, BandedMatrix)
@test fcalls == 1
@info "4th passed"
fcalls = 0
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x), colorvec = repeat(1:3, 10),
sparsity = _J, jac_prototype = SMatrix{30, 30}(_J))
@test _J1 ≈ J
@test fcalls == 1
@info "5"
_J1 = forwarddiff_color_jacobian(staticf, SVector{30}(x),
jac_prototype = SMatrix{30, 30}(_J))
@test _J1 ≈ J
_J1 = forwarddiff_color_jacobian(oopf, x, jac_prototype = similar(_J))
@test _J1 ≈ J
_J1 = forwarddiff_color_jacobian(oopf, x)
@test _J1 ≈ J
#Non-square Jacobian
#length(dx)<length(x)
nsqJ = jacobian(nsqf, x)
spnsqJ = sparse(nsqJ)
_nsqJ = forwarddiff_color_jacobian(nsqf, x, dx = nothing)
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ)
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype = similar(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ,
jac_prototype = similar(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf, x, jac_prototype = SMatrix{15, 30}(nsqJ))
@test _nsqJ ≈ nsqJ
@test typeof(_nsqJ) == typeof(SMatrix{15, 30}(nsqJ))
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x),
jac_prototype = SMatrix{15, 30}(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(staticnsqf, SVector{30}(x),
jac_prototype = SMatrix{15, 30}(nsqJ),
colorvec = repeat(1:3, 10), sparsity = spnsqJ)
@test _nsqJ ≈ nsqJ
_nsqJ = similar(nsqJ)
forwarddiff_color_jacobian!(_nsqJ, nsqf!, x)
@test _nsqJ ≈ nsqJ
_nsqJ = similar(nsqJ)
forwarddiff_color_jacobian!(_nsqJ, nsqf!, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ)
@test _nsqJ ≈ nsqJ
#length(dx)>length(x)
nsqJ = jacobian(nsqf2, x)
spnsqJ = sparse(nsqJ)
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, dx = nothing)
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ)
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype = similar(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ,
jac_prototype = similar(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = forwarddiff_color_jacobian(nsqf2, x, jac_prototype = SMatrix{60, 30}(nsqJ))
@test _nsqJ ≈ nsqJ
_nsqJ = similar(nsqJ)
forwarddiff_color_jacobian!(_nsqJ, nsqf2!, x)
@test _nsqJ ≈ nsqJ
_nsqJ = similar(nsqJ)
forwarddiff_color_jacobian!(_nsqJ, nsqf2!, x, colorvec = repeat(1:3, 10), sparsity = spnsqJ)
@test _nsqJ ≈ nsqJ
fcalls = 0
_J1 = similar(_J)
jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J1)
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
@test _J1 ≈ J
@test fcalls == 1
dx = similar(x)
f(dx, x)
@test value(jac_cache) == dx
dx1 = similar(dx)
value!(dx1, jac_cache)
@test dx1 == dx
fcalls = 0
_J1 = similar(_J)
_denseJ1 = collect(_J1)
forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3, 10), sparsity = _J1)
@test _denseJ1 ≈ J
@test fcalls == 1
fcalls = 0
_J1 = similar(_J)
_denseJ1 = collect(_J1)
jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J1)
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
@test _denseJ1 ≈ J
@test fcalls == 1
dx = similar(x)
f(dx, x)
@test value(jac_cache) == dx
dx1 = similar(dx)
value!(dx1, jac_cache)
@test dx1 == dx
_Jt = similar(Tridiagonal(J))
forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3, 10), sparsity = _Jt)
@test _Jt ≈ J
#https://github.com/JuliaDiff/FiniteDiff.jl/issues/67#issuecomment-516871956
function f(out, x)
x = reshape(x, 100, 100)
out = reshape(out, 100, 100)
for i in 1:100
for j in 1:100
out[i, j] = x[i, j] + x[max(i - 1, 1), j] + x[min(i + 1, size(x, 1)), j] +
x[i, max(j - 1, 1)] + x[i, min(j + 1, size(x, 2))]
end
end
return vec(out)
end
x = rand(10000)
J = BandedBlockBandedMatrix(Ones(10000, 10000), fill(100, 100), fill(100, 100), (1, 1),
(1, 1))
Jsparse = sparse(J)
colors = matrix_colors(J)
forwarddiff_color_jacobian!(J, f, x, colorvec = colors)
forwarddiff_color_jacobian!(Jsparse, f, x, colorvec = colors)
@test J ≈ Jsparse
# Non vector input
x = rand(2, 2)
oopf(x) = x
iipf(fx, x) = (fx .= x)
J = forwarddiff_color_jacobian(oopf, x)
@test J ≈ Matrix(I, 4, 4)
J = zero(J)
forwarddiff_color_jacobian!(J, iipf, x, dx = similar(x))
@test J ≈ Matrix(I, 4, 4)
#1x1 SVector test
x = SVector{1}([1.0])
f(x) = x
J = forwarddiff_color_jacobian(f, x)
@test J isa SArray
@test J ≈ SMatrix{1, 1}([1.0])