-
Notifications
You must be signed in to change notification settings - Fork 36
VariableOrderAccumulator #940
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
Conversation
function Base.:(==)(vi1::VarInfo, vi2::VarInfo) | ||
return (vi1.metadata == vi2.metadata && vi1.accs == vi2.accs) | ||
end |
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.
In making this PR I learned that the default implementation for structs is
function Base.:(==)(vi1::VarInfo, vi2::VarInfo)
return (vi1.metadata === vi2.metadata && vi1.accs === vi2.accs)
end
i.e. all the fields are compared with ===
even when calling ==
. That was causing trouble with some tests that did ==
checks of comparing SimpleVarInfo
s. So note that before this PR e.g. VarInfo() != VarInfo()
, and now VarInfo() == VarInfo()
.
Benchmark Report for Commit bf5eb42Computer Information
Benchmark Results
|
@@ -1808,13 +1800,12 @@ function BangBang.push!!(vi::VarInfo, vn::VarName, r, dist::Distribution) | |||
[1:length(val)], | |||
val, | |||
[dist], | |||
[get_num_produce(vi)], |
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.
This is a change in behaviour: Previously calling push!!
automatically set the order
for a variable. Now order is set only if the push!!
takes place within tilde_assume!!
. Options for this are
- say that it's the caller's responsibility to call
set_order!!
afterpush!!
. This could be fine because only ParticleGibbs cares aboutorder
. - add an extra hook for accumulators for
push!!
, that gets called on all accumulators on everypush!!
call, so that they can adjust their state accordingly.
If this is only relevant for VariableOrderAccumulator
then I'd lean towards 1. If it comes up with other accumulators too then 2. might be warranted.
Similar considerations apply to at least push!
, merge
, and subset
, which after this PR might result in out-of-sync VariableOrderAccumulator
s.
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.
If this is only relevant for VariableOrderAccumulator then I'd lean towards 1.
I think that it's PG's responsibility to call setorder correctly, rather than DPPL, so I'd agree.
Similar considerations apply to at least push!, merge, and subset
Still think it should be handled in PG, not here. I assume that we could write functions like
function pg_push!!(...)
vi = push!!(...)
return setorder!!(...)
end
and make sure to always use that in the PG code?
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.
I'm happy with that as long as it doesn't turn out that this is a common need for accumulators. One other instance comes to mind: Currently if you have a PointwiseLogDensityAccumulator in your varinfo and you subset
or merge
, the pointwise log densities don't get subsetted/merged, and you end up with an accumulator that tracks different variables from the varinfo. This is inconsequential because the use of PointwiseLogDensityAccumulator is so confined to calling the function that needs it.
I'm happy to make PG deal with this, but let's keep our eyes open in case this comes up with other accumulators.
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.
PointwiseLogDensityAccumulator in your varinfo and you subset or merge
Ah, I see -- this would be true in the past as well with PointwiseLogDensityContext tracking different things from the subsetted varinfo, right?
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.
Yep. I don't think PLDAccumulator by itself is a good enough argument for making these subset
and merge
functions, but it just made me wonder if this is a more common pattern with accumulators than we would at first assume. Easy to leave them out now and add them later if needed though.
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.
Previously calling
push!!
automatically set theorder
for a variable. Now order is set only if thepush!!
takes place withintilde_assume!!
Having looked at lots of this code in more detail recently, I don't think there is actually anywhere in the codebase that uses push!!
outside of tilde_assume!!
. (There are some tests, but we can trivially change the tests to match this new behaviour.) Do you know of any?
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.
Can easily believe that that's the only place.
Benchmark times indicate a horrendous loss of type stability. Will investigate, probably tomorrow. |
I had similar problems with other PRs. How about this?
|
Is there a particular reason to first drop it from default accumulators and then move it to Turing.jl, rather than doing both in one go? Also, regardless of what we do, I would develop the corresponding Turing.jl release in parallel, to avoid having to make a lot of patch DPPL releases when we realise we are missing something. I've started that work in TuringLang/Turing.jl#2550, but not yet for VariableOrderAccumulator. |
Because it's annoyingly difficult to make Turing CI run with an unreleased version of DPPL, short of committing a (I don't think patch releases are really problematic, but there is always the possibility of having to make multiple minor releases to fix bugs, so I see the point) |
The performance problem turned out to not be type stability, but rather that every call to Two thoughts:
|
I'm just going through my list of supposed-to-review PRs and clearing them. Feel free to ping me again whenever you feel this is ready |
DynamicPPL.jl documentation for PR #940 is available at: |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #940 +/- ##
============================================
- Coverage 82.58% 82.07% -0.51%
============================================
Files 38 38
Lines 4007 4023 +16
============================================
- Hits 3309 3302 -7
- Misses 698 721 +23 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Looks good although I think you may have to update the implementation of set_retained_vns_del!
as it still uses vi.orders
or metadata.{sym}.orders
.
@@ -1808,13 +1800,12 @@ function BangBang.push!!(vi::VarInfo, vn::VarName, r, dist::Distribution) | |||
[1:length(val)], | |||
val, | |||
[dist], | |||
[get_num_produce(vi)], |
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.
Previously calling
push!!
automatically set theorder
for a variable. Now order is set only if thepush!!
takes place withintilde_assume!!
Having looked at lots of this code in more detail recently, I don't think there is actually anywhere in the codebase that uses push!!
outside of tilde_assume!!
. (There are some tests, but we can trivially change the tests to match this new behaviour.) Do you know of any?
Good spot, fixed |
For ease of comparison, here's the latest benchmark run:
And here's the same thing from before these changes, on
There are substantial though not massive slowdowns across the board, and an especially significant hit for a model with a lot of varnames. I think this is fine once we make sure that |
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.
Neat :)
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
* Bump minor version to 0.37.0 * Accumulators, stage 1 (#885) * Release 0.36 * AbstractPPL 0.11 + change prefixing behaviour (#830) * AbstractPPL 0.11; change prefixing behaviour * Use DynamicPPL.prefix rather than overloading * Remove VarInfo(VarInfo, params) (#870) * Unify `{untyped,typed}_{vector_,}varinfo` constructor functions (#879) * Unify {Untyped,Typed}{Vector,}VarInfo constructors * Update invocations * NTVarInfo * Fix tests * More fixes * Fixes * Fixes * Fixes * Use lowercase functions, don't deprecate VarInfo * Rewrite VarInfo docstring * Fix methods * Fix methods (really) * Draft of accumulators * Fix some variable names * Fix pointwise_logdensities, gut tilde_observe, remove resetlogp!! * Map rather than broadcast Co-authored-by: Tor Erlend Fjelde <[email protected]> * Start documenting accumulators * Use Val{symbols} instead of AccTypes to index * More documentation for accumulators * Link varinfo by default in AD testing utilities; make test suite run on linked varinfos (#890) * Link VarInfo by default * Tweak interface * Fix tests * Fix interface so that callers can inspect results * Document * Fix tests * Fix changelog * Test linked varinfos Closes #891 * Fix docstring + use AbstractFloat * Fix resetlogp!! and type stability for accumulators * Fix type rigidity of LogProbs and NumProduce * Fix uses of getlogp and other assorted issues * setaccs!! nicer interface and logdensity function fixes * Revert back to calling the macro @addlogprob! * Remove a dead test * Clarify a comment * Implement split/combine for PointwiseLogdensityAccumulator * Switch ThreadSafeVarInfo.accs_by_thread to be a tuple * Fix `condition` and `fix` in submodels (#892) * Fix conditioning in submodels * Simplify contextual_isassumption * Add documentation * Fix some tests * Add tests; fix a bunch of nested submodel issues * Fix fix as well * Fix doctests * Add unit tests for new functions * Add changelog entry * Update changelog Co-authored-by: Hong Ge <[email protected]> * Finish docs * Add a test for conditioning submodel via arguments * Clean new tests up a bit * Fix for VarNames with non-identity lenses * Apply suggestions from code review Co-authored-by: Markus Hauru <[email protected]> * Apply suggestions from code review * Make PrefixContext contain a varname rather than symbol (#896) --------- Co-authored-by: Hong Ge <[email protected]> Co-authored-by: Markus Hauru <[email protected]> * Revert ThreadSafeVarInfo back to Vectors and fix some AD type casting in (Simple)VarInfo * Improve accumulator docs * Add test/accumulators.jl * Docs fixes * Various small fixes * Make DynamicTransformation not use accumulators other than LogPrior * Fix variable order and name of map_accumulator!! * Typo fixing * Small improvement to ThreadSafeVarInfo * Fix demo_dot_assume_observe_submodel prefixing * Typo fixing * Miscellaneous small fixes * HISTORY entry and more miscellanea * Add more tests for accumulators * Improve accumulators docstrings * Fix a typo * Expand HISTORY entry * Add accumulators to API docs * Remove unexported functions from API docs * Add NamedTuple methods for get/set/acclogp * Fix setlogp!! with single scalar to error * Export AbstractAccumulator, fix a docs typo * Apply suggestions from code review Co-authored-by: Penelope Yong <[email protected]> * Rename LogPrior -> LogPriorAccumulator, and Likelihood and NumProduce * Type bound log prob accumulators with T<:Real * Add @addlogprior! and @addloglikelihood! * Apply suggestions from code review Co-authored-by: Penelope Yong <[email protected]> * Move default accumulators to default_accumulators.jl * Fix some tests * Introduce default_accumulators() * Go back to only having @addlogprob! * Fix tilde_observe!! prefixing * Fix default_accumulators internal type * Make unflatten more type stable, and add a test for it * Always print all benchmark results * Move NumProduce VI functions to abstract_varinfo.jl --------- Co-authored-by: Penelope Yong <[email protected]> Co-authored-by: Tor Erlend Fjelde <[email protected]> Co-authored-by: Hong Ge <[email protected]> * Replace PriorExtractorContext with PriorDistributionAccumulator (#907) * Implement values_as_in_model using an accumulator (#908) * Implement values_as_in_model using an accumulator * Make make_varname_expression a function * Refuse to combine ValuesAsInModelAccumulators with different include_colon_eqs * Fix nested context test * Bump DynamicPPL versions * Fix merge (1) * Add benchmark Pkg source * [no ci] Don't need to dev again * Disable use_closure for ReverseDiff * Revert "Disable use_closure for ReverseDiff" This reverts commit 3cb47cd. * Fix LogDensityAt struct * Try not duplicating * Update comment pointing to closure benchmarks * Remove `context` from model evaluation (use `model.context` instead) (#952) * Change `evaluate!!` API, add `sample!!` * Fix literally everything else that I broke * Fix some docstrings * fix ForwardDiffExt (look, multiple dispatch bad...) * Changelog * fix a test * Fix docstrings * use `sample!!` * Fix a couple more cases * Globally rename `sample!!` -> `evaluate_and_sample!!`, add changelog warning * Mark function as Const for Enzyme tests (#957) * Move submodel code to submodel.jl; remove `@submodel` (#959) * Move submodel code to submodel.jl * Remove `@submodel` * Fix missing field tests for 1.12 (#961) * Remove 3-argument `{_,}evaluate!!`; clean up submodel code (#960) * Clean up submodel code, remove 3-arg `_evaluate!!` * Remove 3-argument `evaluate!!` as well * Update changelog * Improve submodel error message * Fix doctest * Add error hint for three-argument evaluate!! * Improve API for AD testing (#964) * Rework API for AD testing * Fix test * Add `rng` keyword argument * Use atol and rtol * remove unbound type parameter (?) * Don't need to do elementwise check * Update changelog * Fix typo * DebugAccumulator (plus tiny bits and pieces) (#976) * DebugContext -> DebugAccumulator * Changelog * Force `conditioned` to return a dict * fix conditioned implementation * revert `conditioned` bugfix (will merge this to main instead) * fix show * Fix doctests * fix doctests 2 * Make VarInfo actually mandatory in check_model * Re-implement `missing` check * Revert `combine` signature in docstring * Revert changes to `Base.show` on AccumulatorTuple * Add TODO comment about VariableOrderAccumulator Co-authored-by: Markus Hauru <[email protected]> * Fix doctests --------- Co-authored-by: Markus Hauru <[email protected]> * VariableOrderAccumulator (#940) * Turn NumProduceAccumulator into VariableOrderAccumulator * Add comparison methods * Make VariableOrderAccumulator use regular Dict * Use copy rather than deepcopy for accumulators * Minor docstring touchup * Remove unnecessary use of NumProduceAccumulator * Fix split(VariableOrderAccumulator) * Remove NumProduceAcc from Debug * Fix set_retained_vns_del! --------- Co-authored-by: Penelope Yong <[email protected]> * Accumulators stage 2 (#925) * Give LogDensityFunction the getlogdensity field * Allow missing LogPriorAccumulator when linking * Trim whitespace * Run formatter * Fix a few typos * Fix comma -> semicolon * Fix `LogDensityAt` invocation * Fix one last test * Fix tests --------- Co-authored-by: Penelope Yong <[email protected]> * Implement more consistent tracking of logp components via `LogJacobianAccumulator` (#998) * logjac accumulator * Fix tests * Fix a whole bunch of stuff * Fix final tests * Fix docs * Fix docs/doctests * Fix maths in LogJacobianAccumulator docstring * Twiddle with a comment * Add changelog * Fix accumulator docstring * logJ -> logjac * Fix logjac accumulation for StaticTransformation * Fix behaviour of `set_retained_vns_del!` for `num_produce == 0` (#1000) * `InitContext`, part 2 - Move `hasvalue` and `getvalue` to AbstractPPL; enforce key type of `AbstractDict` (#980) * point to unmerged AbstractPPL branch * Remove code that was moved to AbstractPPL * Remove Dictionaries with Any key type * Fix bad merge conflict resolution * Fix doctests * Point to [email protected] This reverts commit 709dc9e. * Fix doctests * Fix docs AbstractPPL bound * Remove stray `Pkg.update()` * Accumulator miscellanea: Subset, merge, acclogp, and LogProbAccumulator (#999) * logjac accumulator * Fix tests * Fix a whole bunch of stuff * Fix final tests * Fix docs * Fix docs/doctests * Fix maths in LogJacobianAccumulator docstring * Twiddle with a comment * Add changelog * Simplify accs with LogProbAccumulator * Replace + with accumulate for LogProbAccs * Introduce merge and subset for accs * Improve acc tests * Fix docstring typo. Co-authored-by: Penelope Yong <[email protected]> * Fix merge --------- Co-authored-by: Penelope Yong <[email protected]> * Minor tweak to changelog wording --------- Co-authored-by: Penelope Yong <[email protected]> Co-authored-by: Tor Erlend Fjelde <[email protected]> Co-authored-by: Hong Ge <[email protected]>
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
This should have been changed in #940, but slipped through as the file wasn't listed as one of the changed files.
Removes the
order
field ofMetadata
in favour of having anOrderedDict{VarName,Int}
in the same accumulator asnum_produce
(renamingNumProduceAccumulator
toVariableOrderAccumulator
in the process). Also adds some==
methods we were previously missing.This is currently passing tests except anything related to JET. I think JET freaks out because the
OrderedDict
within the new accumulator has an abstract key type. I think it's fine to have the abstract key type as long as the value type is concrete, at least once we removeVariableOrderAccumulator
from the set of default accumulators and only use it when doing ParticleGibbs. I'm thus tempted to not fix the JET issues and move this whole accumulator from DPPL to Turing.jl's part that interfaces with AdvancedPS. Not sure how to handle merging this PR in that case though.