-
Notifications
You must be signed in to change notification settings - Fork 32
Accumulators, stage 1 #885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: breaking
Are you sure you want to change the base?
Changes from all commits
061acbe
1496868
324e623
bb59885
fc32398
cc5e581
b9c368b
1b8f555
ae9b1cd
4fb0bf4
e410f47
97788bd
7fe03ec
5ba3530
d49f7be
28bbf1c
a0ed665
be27636
e6453fe
c59400d
47033ce
8b841c9
3ee3989
13163f2
37dd6dd
d7013b6
40d4caa
ff5f2cb
c68f1bb
13da08a
d52feec
221e797
1dbcb2c
e1b70e0
3f195e5
68b974a
2b405d9
00cd304
6d1048d
4fef20f
905b874
557954a
6f702c9
f748775
5f4a532
31967fd
ad2f564
10b4f2f
d2b670d
8241d12
7b7a3e2
0b08237
2a4b874
c1e90f7
cb1c6c6
00ef0cf
14f4788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
# DynamicPPL Changelog | ||
|
||
## 0.37.0 | ||
|
||
**Breaking changes** | ||
|
||
### Accumulators | ||
|
||
This release overhauls how VarInfo objects track variables such as the log joint probability. The new approach is to use what we call accumulators: Objects that the VarInfo carries on it that may change their state at each `tilde_assume!!` and `tilde_observe!!` call based on the value of the variable in question. They replace both variables that were previously hard-coded in the `VarInfo` object (`logp` and `num_produce`) and some contexts. This brings with it a number of breaking changes: | ||
|
||
- `PriorContext` and `LikelihoodContext` no longer exist. By default, a `VarInfo` tracks both the log prior and the log likelihood separately, and they can be accessed with `getlogprior` and `getloglikelihood`. If you want to execute a model while only accumulating one of the two (to save clock cycles), you can do so by creating a `VarInfo` that only has one accumulator in it, e.g. `varinfo = setaccs!!(varinfo, (LogPriorAccumulator(),))`. | ||
- `MiniBatchContext` does not exist anymore. It can be replaced by creating and using a custom accumulator that replaces the default `LikelihoodContext`. We may introduce such an accumulator in DynamicPPL in the future, but for now you'll need to do it yourself. | ||
- `tilde_observe` and `observe` have been removed. `tilde_observe!!` still exists, and any contexts should modify its behaviour. We may further rework the call stack under `tilde_observe!!` in the near future. | ||
- `tilde_assume` no longer returns the log density of the current assumption as its second return value. We may further rework the `tilde_assume!!` call stack as well. | ||
- For literal observation statements like `0.0 ~ Normal(blahblah)` we used to call `tilde_observe!!` without the `vn` argument. This method no longer exists. Rather we call `tilde_observe!!` with `vn` set to `nothing`. | ||
- `set/reset/increment_num_produce!` have become `set/reset/increment_num_produce!!` (note the second exclamation mark). They are no longer guaranteed to modify the `VarInfo` in place, and one should always use the return value. | ||
- `@addlogprob!` now _always_ adds to the log likelihood. Previously it added to the log probability that the execution context specified, e.g. the log prior when using `PriorContext`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding to the log likelihood is a sensible default. For advanced use by programmable inference, let's also support @addlogprob (logprior=0., loglikelihood=0.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, still need to add that, but happy to do so. I would still prefer to nudge people to use the above syntax or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the name tuple version |
||
- `getlogp` now returns a `NamedTuple` with keys `logprior` and `loglikelihood`. If you want the log joint probability, which is what `getlogp` used to return, use `getlogjoint`. | ||
- Correspondingly `setlogp!!` and `acclogp!!` should now be called with a `NamedTuple` with keys `logprior` and `loglikelihood`. The `acclogp!!` method with a single scalar value has been deprecated and falls back on `accloglikelihood!!`, and the single scalar version of `setlogp!!` has been removed. Corresponding setter/accumulator functions exist for the log prior as well. | ||
|
||
## 0.36.0 | ||
|
||
**Breaking changes** | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -160,10 +160,12 @@ returned(::Model) | |||||
|
||||||
## Utilities | ||||||
|
||||||
It is possible to manually increase (or decrease) the accumulated log density from within a model function. | ||||||
It is possible to manually increase (or decrease) the accumulated log likelihood or prior from within a model function. | ||||||
|
||||||
```@docs | ||||||
@addlogprob! | ||||||
@addloglikelihood! | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add support for more flexible accumulation via @addlogprob! 0.
@addlogprob! (logprior=0., loglike=0.) This would avoid breaking the existing interface which is likely one of the most used features of DynamicPPL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there situations where user code might break if Note that currently in this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, it would be a bug if any. I am leaning towards keeping the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might anything weird happen with ESS? I don't know how it works, but it seems to use I guess the same applies then to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ESS only deals with Gaussian priors, so users should
But, using
I suspect so, since these functions were mainly designed for MH and HMC. In these use cases, accumulating log joint and log likelihood are equivalent as long as the model is differentiable. That said, for prudence, we should provide an explanation on this behavioural change in HISTORY and mark this PR as a breaking change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, in that case I'm happy to keep |
||||||
@addlogprior! | ||||||
``` | ||||||
|
||||||
Return values of the model function for a collection of samples can be obtained with [`returned(model, chain)`](@ref). | ||||||
|
@@ -328,9 +330,9 @@ The following functions were used for sequential Monte Carlo methods. | |||||
|
||||||
```@docs | ||||||
get_num_produce | ||||||
set_num_produce! | ||||||
increment_num_produce! | ||||||
reset_num_produce! | ||||||
set_num_produce!! | ||||||
increment_num_produce!! | ||||||
reset_num_produce!! | ||||||
setorder! | ||||||
set_retained_vns_del! | ||||||
``` | ||||||
|
@@ -345,6 +347,22 @@ Base.empty! | |||||
SimpleVarInfo | ||||||
``` | ||||||
|
||||||
### Accumulators | ||||||
|
||||||
The subtypes of [`AbstractVarInfo`](@ref) store the cumulative log prior and log likelihood, and sometimes other variables that change during executing, in what are called accumulators. | ||||||
|
||||||
```@docs | ||||||
AbstractAccumulator | ||||||
``` | ||||||
|
||||||
DynamicPPL provides the following default accumulators. | ||||||
|
||||||
```@docs | ||||||
LogPriorAccumulator | ||||||
LogLikelihoodAccumulator | ||||||
NumProduceAccumulator | ||||||
``` | ||||||
|
||||||
### Common API | ||||||
|
||||||
#### Accumulation of log-probabilities | ||||||
|
@@ -353,6 +371,13 @@ SimpleVarInfo | |||||
getlogp | ||||||
setlogp!! | ||||||
acclogp!! | ||||||
getlogjoint | ||||||
getlogprior | ||||||
setlogprior!! | ||||||
acclogprior!! | ||||||
getloglikelihood | ||||||
setloglikelihood!! | ||||||
accloglikelihood!! | ||||||
resetlogp!! | ||||||
``` | ||||||
|
||||||
|
@@ -427,9 +452,6 @@ Contexts are subtypes of `AbstractPPL.AbstractContext`. | |||||
```@docs | ||||||
SamplingContext | ||||||
DefaultContext | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity
Suggested change
|
||||||
LikelihoodContext | ||||||
PriorContext | ||||||
MiniBatchContext | ||||||
PrefixContext | ||||||
ConditionContext | ||||||
``` | ||||||
|
@@ -476,7 +498,3 @@ DynamicPPL.Experimental.is_suitable_varinfo | |||||
```@docs | ||||||
tilde_assume | ||||||
``` | ||||||
|
||||||
```@docs | ||||||
tilde_observe | ||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ using Bijectors | |
using Compat | ||
using Distributions | ||
using OrderedCollections: OrderedCollections, OrderedDict | ||
using Printf: Printf | ||
|
||
using AbstractMCMC: AbstractMCMC | ||
using ADTypes: ADTypes | ||
|
@@ -46,17 +47,28 @@ import Base: | |
export AbstractVarInfo, | ||
VarInfo, | ||
SimpleVarInfo, | ||
AbstractAccumulator, | ||
LogLikelihoodAccumulator, | ||
LogPriorAccumulator, | ||
NumProduceAccumulator, | ||
push!!, | ||
empty!!, | ||
subset, | ||
getlogp, | ||
getlogjoint, | ||
getlogprior, | ||
getloglikelihood, | ||
setlogp!!, | ||
setlogprior!!, | ||
setloglikelihood!!, | ||
acclogp!!, | ||
acclogprior!!, | ||
accloglikelihood!!, | ||
resetlogp!!, | ||
get_num_produce, | ||
set_num_produce!, | ||
reset_num_produce!, | ||
increment_num_produce!, | ||
set_num_produce!!, | ||
reset_num_produce!!, | ||
increment_num_produce!!, | ||
set_retained_vns_del!, | ||
is_flagged, | ||
set_flag!, | ||
|
@@ -92,15 +104,10 @@ export AbstractVarInfo, | |
# Contexts | ||
SamplingContext, | ||
DefaultContext, | ||
LikelihoodContext, | ||
PriorContext, | ||
MiniBatchContext, | ||
PrefixContext, | ||
ConditionContext, | ||
assume, | ||
observe, | ||
tilde_assume, | ||
tilde_observe, | ||
# Pseudo distributions | ||
NamedDist, | ||
NoDist, | ||
|
@@ -120,6 +127,8 @@ export AbstractVarInfo, | |
to_submodel, | ||
# Convenience macros | ||
@addlogprob!, | ||
@addlogprior!, | ||
@addloglikelihood!, | ||
@submodel, | ||
value_iterator_from_chain, | ||
check_model, | ||
|
@@ -146,6 +155,9 @@ macro prob_str(str) | |
)) | ||
end | ||
|
||
# TODO(mhauru) We should write down the list of methods that any subtype of AbstractVarInfo | ||
# has to implement. Not sure what the full list is for parameters values, but for | ||
# accumulators we only need `getaccs` and `setaccs!!`. | ||
Comment on lines
+158
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put a list of AbstractVarInfo interface methods together the other day, feel free to ask me for it on Slack or something. We'd have to add the accumulator bits in, of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be a good contribution to #899 |
||
""" | ||
AbstractVarInfo | ||
|
||
|
@@ -166,6 +178,7 @@ include("varname.jl") | |
include("distribution_wrappers.jl") | ||
include("contexts.jl") | ||
include("varnamedvector.jl") | ||
include("accumulators.jl") | ||
include("abstract_varinfo.jl") | ||
include("threadsafe.jl") | ||
include("varinfo.jl") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why we can't remove
tilde_assume
liketilde_observe
andobserve
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's on my list to do, but I didn't want to put in the same PR since I didn't have to. (I had to do
tilde_observe
because of complications withPointwiseLogdensityAccumulator
). Or, more precisely, what's on my list is to revisit the whole call stack for bothtilde_assume!!
andtilde_observe!!
and see what the best way to do things is.