/
oncedifferentiable.jl
232 lines (193 loc) · 8.66 KB
/
oncedifferentiable.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
# Used for objectives and solvers where the gradient is available/exists
mutable struct OnceDifferentiable{TF, TDF, TX} <: AbstractObjective
f # objective
df # (partial) derivative of objective
fdf # objective and (partial) derivative of objective
F::TF # cache for f output
DF::TDF # cache for df output
x_f::TX # x used to evaluate f (stored in F)
x_df::TX # x used to evaluate df (stored in DF)
f_calls::Vector{Int}
df_calls::Vector{Int}
end
### Only the objective
# Ambiguity
OnceDifferentiable(f, x::AbstractArray,
F::Real = real(zero(eltype(x))),
DF::AbstractArray = alloc_DF(x, F); inplace = true, autodiff = :finite) =
OnceDifferentiable(f, x, F, DF, autodiff)
#OnceDifferentiable(f, x::AbstractArray, F::AbstractArray; autodiff = :finite) =
# OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, alloc_DF(x, F))
function OnceDifferentiable(f, x::AbstractArray,
F::AbstractArray, DF::AbstractArray = alloc_DF(x, F);
inplace = true, autodiff = :finite)
f! = f!_from_f(f, F, inplace)
OnceDifferentiable(f!, x::AbstractArray, F::AbstractArray, DF, autodiff)
end
function OnceDifferentiable(f, x_seed::AbstractArray{T},
F::Real,
DF::AbstractArray,
autodiff) where T
# When here, at the constructor with positional autodiff, it should already
# be the case, that f is inplace.
if typeof(f) <: Union{InplaceObjective, NotInplaceObjective}
fF = make_f(f, x_seed, F)
dfF = make_df(f, x_seed, F)
fdfF = make_fdf(f, x_seed, F)
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
else
if is_finitediff(autodiff)
# Figure out which Val-type to use for DiffEqDiffTools based on our
# symbol interface.
fdtype = diffeqdiff_fdtype(autodiff)
df_array_spec = DF
x_array_spec = x_seed
return_spec = typeof(F)
gcache = DiffEqDiffTools.GradientCache(df_array_spec, x_array_spec, fdtype, return_spec)
function g!(storage, x)
DiffEqDiffTools.finite_difference_gradient!(storage, f, x, gcache)
return
end
function fg!(storage, x)
g!(storage, x)
return f(x)
end
elseif is_forwarddiff(autodiff)
gcfg = ForwardDiff.GradientConfig(f, x_seed)
g! = (out, x) -> ForwardDiff.gradient!(out, f, x, gcfg)
fg! = (out, x) -> begin
gr_res = DiffResults.DiffResult(zero(T), out)
ForwardDiff.gradient!(gr_res, f, x, gcfg)
DiffResults.value(gr_res)
end
else
error("The autodiff value $autodiff is not support. Use :finite or :forward.")
end
return OnceDifferentiable(f, g!, fg!, x_seed, F, DF)
end
end
has_not_dep_symbol_in_ad = Ref{Bool}(true)
OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, autodiff::Symbol, chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x)) =
OnceDifferentiable(f, x, F, alloc_DF(x, F), autodiff, chunk)
function OnceDifferentiable(f, x::AbstractArray, F::AbstractArray,
autodiff::Bool, chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x))
if autodiff == false
throw(ErrorException("It is not possible to set the `autodiff` keyword to `false` when constructing a OnceDifferentiable instance from only one function. Pass in the (partial) derivative or specify a valid `autodiff` symbol."))
elseif has_not_dep_symbol_in_ad[]
@warn("Setting the `autodiff` keyword to `true` is deprecated. Please use a valid symbol instead.")
has_not_dep_symbol_in_ad[] = false
end
OnceDifferentiable(f, x, F, alloc_DF(x, F), :forward, chunk)
end
function OnceDifferentiable(f, x_seed::AbstractArray, F::AbstractArray, DF::AbstractArray,
autodiff::Symbol , chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x_seed))
if typeof(f) <: Union{InplaceObjective, NotInplaceObjective}
fF = make_f(f, x_seed, F)
dfF = make_df(f, x_seed, F)
fdfF = make_fdf(f, x_seed, F)
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
else
if is_finitediff(autodiff)
# Figure out which Val-type to use for DiffEqDiffTools based on our
# symbol interface.
fdtype = diffeqdiff_fdtype(autodiff)
# Apparently only the third input is aliased.
j_diffeqdiff_cache = DiffEqDiffTools.JacobianCache(similar(x_seed), similar(F), similar(F), fdtype)
if autodiff == :finiteforward
# These copies can be done away with if we add a keyword for
# reusing arrays instead for overwriting them.
Fx = copy(F)
DF = copy(DF)
x_f, x_df = x_of_nans(x_seed), x_of_nans(x_seed)
f_calls, j_calls = [0,], [0,]
function j_finiteforward!(J, x)
# Exploit the possibility that it might be that x_f == x
# then we don't have to call f again.
# if at least one element of x_f is different from x, update
if any(x_f .!= x)
Fx = similar(Fx)
f(Fx, x)
f_calls .+= 1
end
DiffEqDiffTools.finite_difference_jacobian!(J, f, x, j_diffeqdiff_cache, Fx)
end
function fj_finiteforward!(F, J, x)
f(F, x)
DiffEqDiffTools.finite_difference_jacobian!(J, f, x, j_diffeqdiff_cache, F)
end
return OnceDifferentiable(f, j_finiteforward!, fj_finiteforward!, copy(F), copy(DF), x_f, x_df, f_calls, j_calls)
end
function fj_finitediff!(F, J, x)
f(F, x)
DiffEqDiffTools.finite_difference_jacobian!(J, f, x, j_diffeqdiff_cache)
F
end
function j_finitediff!(J, x)
F_cache = similar(F)
fj_finitediff!(F_cache, J, x)
end
return OnceDifferentiable(f, j_finitediff!, fj_finitediff!, x_seed, F, DF)
elseif is_forwarddiff(autodiff)
jac_cfg = ForwardDiff.JacobianConfig(f, F, x_seed, chunk)
ForwardDiff.checktag(jac_cfg, f, x_seed)
F2 = copy(F)
function j_forwarddiff!(J, x)
ForwardDiff.jacobian!(J, f, F2, x, jac_cfg, Val{false}())
end
function fj_forwarddiff!(F, J, x)
jac_res = DiffResults.DiffResult(F, J)
ForwardDiff.jacobian!(jac_res, f, F2, x, jac_cfg, Val{false}())
DiffResults.value(jac_res)
end
return OnceDifferentiable(f, j_forwarddiff!, fj_forwarddiff!, x_seed, F, DF)
else
error("The autodiff value $(autodiff) is not supported. Use :finite or :forward.")
end
end
end
### Objective and derivative
function OnceDifferentiable(f, df,
x::AbstractArray,
F::Real = real(zero(eltype(x))),
DF::AbstractArray = alloc_DF(x, F);
inplace = true)
df! = df!_from_df(df, F, inplace)
fdf! = make_fdf(x, F, f, df!)
OnceDifferentiable(f, df!, fdf!, x, F, DF)
end
function OnceDifferentiable(f, j,
x::AbstractArray,
F::AbstractArray,
J::AbstractArray = alloc_DF(x, F);
inplace = true)
f! = f!_from_f(f, F, inplace)
j! = df!_from_df(j, F, inplace)
fj! = make_fdf(x, F, f!, j!)
OnceDifferentiable(f!, j!, fj!, x, F, J)
end
### Objective, derivative and combination
function OnceDifferentiable(f, df, fdf,
x::AbstractArray,
F::Real = real(zero(eltype(x))),
DF::AbstractArray = alloc_DF(x, F);
inplace = true)
# f is never "inplace" since F is scalar
df! = df!_from_df(df, F, inplace)
fdf! = fdf!_from_fdf(fdf, F, inplace)
x_f, x_df = x_of_nans(x), x_of_nans(x)
OnceDifferentiable{typeof(F),typeof(DF),typeof(x)}(f, df!, fdf!,
copy(F), copy(DF),
x_f, x_df,
[0,], [0,])
end
function OnceDifferentiable(f, df, fdf,
x::AbstractArray,
F::AbstractArray,
DF::AbstractArray = alloc_DF(x, F);
inplace = true)
f = f!_from_f(f, F, inplace)
df! = df!_from_df(df, F, inplace)
fdf! = fdf!_from_fdf(fdf, F, inplace)
x_f, x_df = x_of_nans(x), x_of_nans(x)
OnceDifferentiable(f, df!, fdf!, copy(F), copy(DF), x_f, x_df, [0,], [0,])
end