-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhessian.jl
More file actions
238 lines (206 loc) · 6.97 KB
/
hessian.jl
File metadata and controls
238 lines (206 loc) · 6.97 KB
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
## Docstrings
"""
prepare_hessian(f, backend, x, [contexts...]; strict=Val(true)) -> prep
$(docstring_prepare("hessian"))
"""
function prepare_hessian(
f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}; strict::Val=Val(true)
) where {F,C}
return prepare_hessian_nokwarg(strict, f, backend, x, contexts...)
end
"""
prepare!_hessian(f, backend, x, [contexts...]) -> new_prep
$(docstring_prepare!("hessian"))
"""
function prepare!_hessian(
f::F, old_prep::HessianPrep, backend::AbstractADType, x, contexts::Vararg{Context,C};
) where {F,C}
check_prep(f, old_prep, backend, x, contexts...)
return prepare_hessian_nokwarg(is_strict(old_prep), f, backend, x, contexts...)
end
"""
hessian(f, [prep,] backend, x, [contexts...]) -> hess
Compute the Hessian matrix of the function `f` at point `x`.
$(docstring_preparation_hint("hessian"))
"""
function hessian(f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}) where {F,C}
prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...)
return hessian(f, prep, backend, x, contexts...)
end
"""
hessian!(f, hess, [prep,] backend, x, [contexts...]) -> hess
Compute the Hessian matrix of the function `f` at point `x`, overwriting `hess`.
$(docstring_preparation_hint("hessian"))
"""
function hessian!(
f::F, hess, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...)
return hessian!(f, hess, prep, backend, x, contexts...)
end
"""
value_gradient_and_hessian(f, [prep,] backend, x, [contexts...]) -> (y, grad, hess)
Compute the value, gradient vector and Hessian matrix of the function `f` at point `x`.
$(docstring_preparation_hint("hessian"))
"""
function value_gradient_and_hessian(
f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...)
return value_gradient_and_hessian(f, prep, backend, x, contexts...)
end
"""
value_gradient_and_hessian!(f, grad, hess, [prep,] backend, x, [contexts...]) -> (y, grad, hess)
Compute the value, gradient vector and Hessian matrix of the function `f` at point `x`, overwriting `grad` and `hess`.
$(docstring_preparation_hint("hessian"))
"""
function value_gradient_and_hessian!(
f::F, grad, hess, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
prep = prepare_hessian_nokwarg(Val(true), f, backend, x, contexts...)
return value_gradient_and_hessian!(f, grad, hess, prep, backend, x, contexts...)
end
## Preparation
struct HVPGradientHessianPrep{
SIG,
BS<:BatchSizeSettings,
S<:AbstractVector{<:NTuple},
R<:AbstractVector{<:NTuple},
SE<:NTuple,
E2<:HVPPrep,
E1<:GradientPrep,
} <: HessianPrep{SIG}
_sig::Val{SIG}
batch_size_settings::BS
batched_seeds::S
batched_results::R
seed_example::SE
hvp_prep::E2
gradient_prep::E1
end
function prepare_hessian_nokwarg(
strict::Val, f::F, backend::AbstractADType, x, contexts::Vararg{Context,C}
) where {F,C}
# type-unstable
batch_size_settings = pick_batchsize(outer(backend), x)
# function barrier
return _prepare_hessian_aux(strict, batch_size_settings, f, backend, x, contexts...)
end
function _prepare_hessian_aux(
strict::Val,
batch_size_settings::BatchSizeSettings{B},
f::F,
backend::AbstractADType,
x,
contexts::Vararg{Context,C};
) where {B,F,C}
_sig = signature(f, backend, x, contexts...; strict)
(; N, A) = batch_size_settings
seeds = [basis(x, ind) for ind in eachindex(x)]
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % N], Val(B)) for a in 1:A
]
batched_results = [ntuple(b -> similar(x), Val(B)) for _ in batched_seeds]
seed_example = ntuple(b -> basis(x), Val(B))
hvp_prep = prepare_hvp_nokwarg(strict, f, backend, x, seed_example, contexts...)
gradient_prep = prepare_gradient_nokwarg(strict, f, inner(backend), x, contexts...)
return HVPGradientHessianPrep(
_sig,
batch_size_settings,
batched_seeds,
batched_results,
seed_example,
hvp_prep,
gradient_prep,
)
end
## One argument
function hessian(
f::F,
prep::HVPGradientHessianPrep{SIG,<:BatchSizeSettings{B,true}},
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,SIG,B,C}
check_prep(f, prep, backend, x, contexts...)
(; batched_seeds, hvp_prep) = prep
dg_batch = hvp(f, hvp_prep, backend, x, only(batched_seeds), contexts...)
block = stack_vec_col(dg_batch)
return block
end
function hessian(
f::F,
prep::HVPGradientHessianPrep{SIG,<:BatchSizeSettings{B,false,aligned}},
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,SIG,B,aligned,C}
check_prep(f, prep, backend, x, contexts...)
(; batch_size_settings, batched_seeds, seed_example, hvp_prep) = prep
(; A, B_last) = batch_size_settings
hvp_prep_same = prepare_hvp_same_point(
f, hvp_prep, backend, x, seed_example, contexts...
)
hess = mapreduce(hcat, eachindex(batched_seeds)) do a
dg_batch = hvp(f, hvp_prep_same, backend, x, batched_seeds[a], contexts...)
block = stack_vec_col(dg_batch)
if !aligned && a == A
return block[:, 1:B_last]
else
return block
end
end
return hess
end
function hessian!(
f::F,
hess,
prep::HVPGradientHessianPrep{SIG,<:BatchSizeSettings{B}},
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,SIG,B,C}
check_prep(f, prep, backend, x, contexts...)
(; batch_size_settings, batched_seeds, batched_results, seed_example, hvp_prep) = prep
(; N) = batch_size_settings
hvp_prep_same = prepare_hvp_same_point(
f, hvp_prep, backend, x, seed_example, contexts...
)
for a in eachindex(batched_seeds, batched_results)
hvp!(
f, batched_results[a], hvp_prep_same, backend, x, batched_seeds[a], contexts...
)
for b in eachindex(batched_results[a])
copyto!(
view(hess, :, 1 + ((a - 1) * B + (b - 1)) % N), vec(batched_results[a][b])
)
end
end
return hess
end
function value_gradient_and_hessian(
f::F,
prep::HVPGradientHessianPrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, contexts...)
y, grad = value_and_gradient(f, prep.gradient_prep, inner(backend), x, contexts...)
hess = hessian(f, prep, backend, x, contexts...)
return y, grad, hess
end
function value_gradient_and_hessian!(
f::F,
grad,
hess,
prep::HVPGradientHessianPrep,
backend::AbstractADType,
x,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, contexts...)
y, _ = value_and_gradient!(f, grad, prep.gradient_prep, inner(backend), x, contexts...)
hessian!(f, hess, prep, backend, x, contexts...)
return y, grad, hess
end