-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathfasteval.jl
More file actions
387 lines (346 loc) · 15.2 KB
/
fasteval.jl
File metadata and controls
387 lines (346 loc) · 15.2 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
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
using DynamicPPL:
AbstractVarInfo,
AccumulatorTuple,
InitContext,
InitFromParams,
AbstractInitStrategy,
LogJacobianAccumulator,
LogLikelihoodAccumulator,
LogPriorAccumulator,
Model,
ThreadSafeVarInfo,
VarInfo,
OnlyAccsVarInfo,
RangeAndLinked,
VectorWithRanges,
Metadata,
VarNamedVector,
default_accumulators,
float_type_with_fallback,
getlogjoint,
getlogjoint_internal,
getloglikelihood,
getlogprior,
getlogprior_internal
using ADTypes: ADTypes
using BangBang: BangBang
using AbstractPPL: AbstractPPL, VarName
using LogDensityProblems: LogDensityProblems
import DifferentiationInterface as DI
using Random: Random
"""
DynamicPPL.Experimental.fast_evaluate!!(
[rng::Random.AbstractRNG,]
model::Model,
strategy::AbstractInitStrategy,
accs::AccumulatorTuple, params::AbstractVector{<:Real}
)
Evaluate a model using parameters obtained via `strategy`, and only computing the results in
the provided accumulators.
It is assumed that the accumulators passed in have been initialised to appropriate values,
as this function will not reset them. The default constructors for each accumulator will do
this for you correctly.
Returns a tuple of the model's return value, plus an `OnlyAccsVarInfo`. Note that the `accs`
argument may be mutated (depending on how the accumulators are implemented); hence the `!!`
in the function name.
"""
@inline function fast_evaluate!!(
# Note that this `@inline` is mandatory for performance. If it's not inlined, it leads
# to extra allocations (even for trivial models) and much slower runtime.
rng::Random.AbstractRNG,
model::Model,
strategy::AbstractInitStrategy,
accs::AccumulatorTuple,
)
ctx = InitContext(rng, strategy)
model = DynamicPPL.setleafcontext(model, ctx)
# Calling `evaluate!!` would be fine, but would lead to an extra call to resetaccs!!,
# which is unnecessary. So we shortcircuit this by simply calling `_evaluate!!`
# directly. To preserve thread-safety we need to reproduce the ThreadSafeVarInfo logic
# here.
# TODO(penelopeysm): This should _not_ check Threads.nthreads(). I still don't know what
# it _should_ do, but this is wrong regardless.
# https://github.com/TuringLang/DynamicPPL.jl/issues/1086
vi = if Threads.nthreads() > 1
param_eltype = DynamicPPL.get_param_eltype(strategy)
accs = map(accs) do acc
DynamicPPL.convert_eltype(float_type_with_fallback(param_eltype), acc)
end
ThreadSafeVarInfo(OnlyAccsVarInfo(accs))
else
OnlyAccsVarInfo(accs)
end
return DynamicPPL._evaluate!!(model, vi)
end
@inline function fast_evaluate!!(
model::Model, strategy::AbstractInitStrategy, accs::AccumulatorTuple
)
# This `@inline` is also mandatory for performance
return fast_evaluate!!(Random.default_rng(), model, strategy, accs)
end
"""
FastLDF(
model::Model,
getlogdensity::Function=getlogjoint_internal,
varinfo::AbstractVarInfo=VarInfo(model);
adtype::Union{ADTypes.AbstractADType,Nothing}=nothing,
)
A struct which contains a model, along with all the information necessary to:
- calculate its log density at a given point;
- and if `adtype` is provided, calculate the gradient of the log density at that point.
This information can be extracted using the LogDensityProblems.jl interface, specifically,
using `LogDensityProblems.logdensity` and `LogDensityProblems.logdensity_and_gradient`. If
`adtype` is nothing, then only `logdensity` is implemented. If `adtype` is a concrete AD
backend type, then `logdensity_and_gradient` is also implemented.
There are several options for `getlogdensity` that are 'supported' out of the box:
- [`getlogjoint_internal`](@ref): calculate the log joint, including the log-Jacobian term
for any variables that have been linked in the provided VarInfo.
- [`getlogprior_internal`](@ref): calculate the log prior, including the log-Jacobian term
for any variables that have been linked in the provided VarInfo.
- [`getlogjoint`](@ref): calculate the log joint in the model space, ignoring any effects of
linking
- [`getlogprior`](@ref): calculate the log prior in the model space, ignoring any effects of
linking
- [`getloglikelihood`](@ref): calculate the log likelihood (this is unaffected by linking,
since transforms are only applied to random variables)
!!! note
By default, `FastLDF` uses `getlogjoint_internal`, i.e., the result of
`LogDensityProblems.logdensity(f, x)` will depend on whether the `FastLDF` was created
with a linked or unlinked VarInfo. This is done primarily to ease interoperability with
MCMC samplers.
If you provide one of these functions, a `VarInfo` will be automatically created for you. If
you provide a different function, you have to manually create a VarInfo and pass it as the
third argument.
If the `adtype` keyword argument is provided, then this struct will also store the adtype
along with other information for efficient calculation of the gradient of the log density.
Note that preparing a `FastLDF` with an AD type `AutoBackend()` requires the AD backend
itself to have been loaded (e.g. with `import Backend`).
## Fields
Note that it is undefined behaviour to access any of a `FastLDF`'s fields, apart from:
- `fastldf.model`: The original model from which this `FastLDF` was constructed.
- `fastldf.adtype`: The AD type used for gradient calculations, or `nothing` if no AD
type was provided.
# Extended help
Up until DynamicPPL v0.38, there have been two ways of evaluating a DynamicPPL model at a
given set of parameters:
1. With `unflatten` + `evaluate!!` with `DefaultContext`: this stores a vector of parameters
inside a VarInfo's metadata, then reads parameter values from the VarInfo during evaluation.
2. With `InitFromParams`: this reads parameter values from a NamedTuple or a Dict, and stores
them inside a VarInfo's metadata.
In general, both of these approaches work fine, but the fact that they modify the VarInfo's
metadata can often be quite wasteful. In particular, it is very common that the only outputs
we care about from model evaluation are those which are stored in accumulators, such as log
probability densities, or `ValuesAsInModel`.
To avoid this issue, we use `OnlyAccsVarInfo`, which is a VarInfo that only contains
accumulators. It implements enough of the `AbstractVarInfo` interface to not error during
model evaluation.
Because `OnlyAccsVarInfo` does not store any parameter values, when evaluating a model with
it, it is mandatory that parameters are provided from outside the VarInfo, namely via
`InitContext`.
The main problem that we face is that it is not possible to directly implement
`DynamicPPL.init(rng, vn, dist, strategy)` for `strategy::InitFromParams{<:AbstractVector}`.
In particular, it is not clear:
- which parts of the vector correspond to which random variables, and
- whether the variables are linked or unlinked.
Traditionally, this problem has been solved by `unflatten`, because that function would
place values into the VarInfo's metadata alongside the information about ranges and linking.
That way, when we evaluate with `DefaultContext`, we can read this information out again.
However, we want to avoid using a metadata. Thus, here, we _extract this information from
the VarInfo_ a single time when constructing a `FastLDF` object. Inside the FastLDF, we
store a mapping from VarNames to ranges in that vector, along with link status.
For VarNames with identity optics, this is stored in a NamedTuple for efficiency. For all
other VarNames, this is stored in a Dict. The internal data structure used to represent this
could almost certainly be optimised further. See e.g. the discussion in
https://github.com/TuringLang/DynamicPPL.jl/issues/1116.
When evaluating the model, this allows us to combine the parameter vector together with those
ranges to create an `InitFromParams{VectorWithRanges}`, which lets us very quickly read
parameter values from the vector.
Note that this assumes that the ranges and link status are static throughout the lifetime of
the `FastLDF` object. Therefore, a `FastLDF` object cannot handle models which have variable
numbers of parameters, or models which may visit random variables in different orders depending
on stochastic control flow. **Indeed, silent errors may occur with such models.** This is a
general limitation of vectorised parameters: the original `unflatten` + `evaluate!!`
approach also fails with such models.
"""
struct FastLDF{
M<:Model,
AD<:Union{ADTypes.AbstractADType,Nothing},
F<:Function,
N<:NamedTuple,
ADP<:Union{Nothing,DI.GradientPrep},
}
model::M
adtype::AD
_getlogdensity::F
_iden_varname_ranges::N
_varname_ranges::Dict{VarName,RangeAndLinked}
_adprep::ADP
_dim::Int
function FastLDF(
model::Model,
getlogdensity::Function=getlogjoint_internal,
varinfo::AbstractVarInfo=VarInfo(model);
adtype::Union{ADTypes.AbstractADType,Nothing}=nothing,
)
# Figure out which variable corresponds to which index, and
# which variables are linked.
all_iden_ranges, all_ranges = get_ranges_and_linked(varinfo)
x = [val for val in varinfo[:]]
dim = length(x)
# Do AD prep if needed
prep = if adtype === nothing
nothing
else
# Make backend-specific tweaks to the adtype
adtype = DynamicPPL.tweak_adtype(adtype, model, varinfo)
DI.prepare_gradient(
FastLogDensityAt(model, getlogdensity, all_iden_ranges, all_ranges),
adtype,
x,
)
end
return new{
typeof(model),
typeof(adtype),
typeof(getlogdensity),
typeof(all_iden_ranges),
typeof(prep),
}(
model, adtype, getlogdensity, all_iden_ranges, all_ranges, prep, dim
)
end
end
###################################
# LogDensityProblems.jl interface #
###################################
"""
fast_ldf_accs(getlogdensity::Function)
Determine which accumulators are needed for fast evaluation with the given
`getlogdensity` function.
"""
fast_ldf_accs(::Function) = default_accumulators()
fast_ldf_accs(::typeof(getlogjoint_internal)) = default_accumulators()
function fast_ldf_accs(::typeof(getlogjoint))
return AccumulatorTuple((LogPriorAccumulator(), LogLikelihoodAccumulator()))
end
function fast_ldf_accs(::typeof(getlogprior_internal))
return AccumulatorTuple((LogPriorAccumulator(), LogJacobianAccumulator()))
end
fast_ldf_accs(::typeof(getlogprior)) = AccumulatorTuple((LogPriorAccumulator(),))
fast_ldf_accs(::typeof(getloglikelihood)) = AccumulatorTuple((LogLikelihoodAccumulator(),))
struct FastLogDensityAt{M<:Model,F<:Function,N<:NamedTuple}
model::M
getlogdensity::F
iden_varname_ranges::N
varname_ranges::Dict{VarName,RangeAndLinked}
end
function (f::FastLogDensityAt)(params::AbstractVector{<:Real})
strategy = InitFromParams(
VectorWithRanges(f.iden_varname_ranges, f.varname_ranges, params), nothing
)
accs = fast_ldf_accs(f.getlogdensity)
_, vi = fast_evaluate!!(f.model, strategy, accs)
return f.getlogdensity(vi)
end
function LogDensityProblems.logdensity(fldf::FastLDF, params::AbstractVector{<:Real})
return FastLogDensityAt(
fldf.model, fldf._getlogdensity, fldf._iden_varname_ranges, fldf._varname_ranges
)(
params
)
end
function LogDensityProblems.logdensity_and_gradient(
fldf::FastLDF, params::AbstractVector{<:Real}
)
return DI.value_and_gradient(
FastLogDensityAt(
fldf.model, fldf._getlogdensity, fldf._iden_varname_ranges, fldf._varname_ranges
),
fldf._adprep,
fldf.adtype,
params,
)
end
function LogDensityProblems.capabilities(
::Type{<:DynamicPPL.Experimental.FastLDF{M,Nothing}}
) where {M}
return LogDensityProblems.LogDensityOrder{0}()
end
function LogDensityProblems.capabilities(
::Type{<:DynamicPPL.Experimental.FastLDF{M,<:ADTypes.AbstractADType}}
) where {M}
return LogDensityProblems.LogDensityOrder{1}()
end
function LogDensityProblems.dimension(fldf::FastLDF)
return fldf._dim
end
######################################################
# Helper functions to extract ranges and link status #
######################################################
# This fails for SimpleVarInfo, but honestly there is no reason to support that here. The
# fact is that evaluation doesn't use a VarInfo, it only uses it once to generate the ranges
# and link status. So there is no motivation to use SimpleVarInfo inside a
# LogDensityFunction any more, we can just always use typed VarInfo. In fact one could argue
# that there is no purpose in supporting untyped VarInfo either.
"""
get_ranges_and_linked(varinfo::VarInfo)
Given a `VarInfo`, extract the ranges of each variable in the vectorised parameter
representation, along with whether each variable is linked or unlinked.
This function should return a tuple containing:
- A NamedTuple mapping VarNames with identity optics to their corresponding `RangeAndLinked`
- A Dict mapping all other VarNames to their corresponding `RangeAndLinked`.
"""
function get_ranges_and_linked(varinfo::VarInfo{<:NamedTuple{syms}}) where {syms}
all_iden_ranges = NamedTuple()
all_ranges = Dict{VarName,RangeAndLinked}()
offset = 1
for sym in syms
md = varinfo.metadata[sym]
this_md_iden, this_md_others, offset = get_ranges_and_linked_metadata(md, offset)
all_iden_ranges = merge(all_iden_ranges, this_md_iden)
all_ranges = merge(all_ranges, this_md_others)
end
return all_iden_ranges, all_ranges
end
function get_ranges_and_linked(varinfo::VarInfo{<:Union{Metadata,VarNamedVector}})
all_iden, all_others, _ = get_ranges_and_linked_metadata(varinfo.metadata, 1)
return all_iden, all_others
end
function get_ranges_and_linked_metadata(md::Metadata, start_offset::Int)
all_iden_ranges = NamedTuple()
all_ranges = Dict{VarName,RangeAndLinked}()
offset = start_offset
for (vn, idx) in md.idcs
is_linked = md.is_transformed[idx]
range = md.ranges[idx] .+ (start_offset - 1)
if AbstractPPL.getoptic(vn) === identity
all_iden_ranges = merge(
all_iden_ranges,
NamedTuple((AbstractPPL.getsym(vn) => RangeAndLinked(range, is_linked),)),
)
else
all_ranges[vn] = RangeAndLinked(range, is_linked)
end
offset += length(range)
end
return all_iden_ranges, all_ranges, offset
end
function get_ranges_and_linked_metadata(vnv::VarNamedVector, start_offset::Int)
all_iden_ranges = NamedTuple()
all_ranges = Dict{VarName,RangeAndLinked}()
offset = start_offset
for (vn, idx) in vnv.varname_to_index
is_linked = vnv.is_unconstrained[idx]
range = vnv.ranges[idx] .+ (start_offset - 1)
if AbstractPPL.getoptic(vn) === identity
all_iden_ranges = merge(
all_iden_ranges,
NamedTuple((AbstractPPL.getsym(vn) => RangeAndLinked(range, is_linked),)),
)
else
all_ranges[vn] = RangeAndLinked(range, is_linked)
end
offset += length(range)
end
return all_iden_ranges, all_ranges, offset
end