-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathoncedifferentiable.jl
157 lines (133 loc) · 5.65 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
# 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,
chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x)) =
OnceDifferentiable(f, x, F, DF, autodiff, chunk)
#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, chunk) 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
backend = get_adtype(autodiff, chunk)
grad_prep = DI.prepare_gradient(f, backend, x_seed)
function g!(_g, _x)
DI.gradient!(f, _g, grad_prep, backend, _x)
return nothing
end
function fg!(_g, _x)
y, _ = DI.value_and_gradient!(f, _g, grad_prep, backend, _x)
return y
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
F2 = similar(F)
backend = get_adtype(autodiff, chunk)
jac_prep = DI.prepare_jacobian(f, F2, backend, x_seed)
function j!(_j, _x)
DI.jacobian!(f, F2, _j, jac_prep, backend, _x)
return _j
end
function fj!(_y, _j, _x)
y, _ = DI.value_and_jacobian!(f, _y, _j, jac_prep, backend, _x)
return y
end
return OnceDifferentiable(f, j!, fj!, x_seed, F, DF)
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