-
Notifications
You must be signed in to change notification settings - Fork 37
Implement predict, returned, logjoint, ... with OnlyAccsVarInfo
#1130
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
Benchmark Report
Computer InformationBenchmark Results |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #1130 +/- ##
============================================
- Coverage 81.07% 80.60% -0.48%
============================================
Files 41 41
Lines 3894 3861 -33
============================================
- Hits 3157 3112 -45
- Misses 737 749 +12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
DynamicPPL.jl documentation for PR #1130 is available at: |
699aa23 to
c35dff5
Compare
1d79c7f to
89a5aec
Compare
f211389 to
5e335ff
Compare
| """ | ||
| reevaluate_with_chain( |
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.
Fundamentally, all the functions in this extension really just use this under the hood.
FWIW the FlexiChains extension has a very similar structure and I believe these can be unified pretty much immediately after this PR. To be specific, DynamicPPL.reevaluate_with_chain should be implemented by each chain type in the most performant manner (FlexiChains doesn't use InitFromParams), but once that's done, the definitions of returned, logjoint, ..., pointwise_logdensities, ... can be shared.
predict can't yet be shared unfortunately, because the include_all keyword argument forces custom MCMCChains / FlexiChains code. That would require an extension of the AbstractChains API to support a subset-like operation.
cf33cff to
d1c002f
Compare
5e335ff to
62cd3af
Compare
predict, returned, logjoint, ... with fast evalpredict, returned, logjoint, ... with OnlyAccsVarInfo
sunxd3
left a comment
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.
code is good, maye something to be added to changelog
| params_with_stats = AbstractMCMC.to_samples(DynamicPPL.ParamsWithStats, chain) | ||
| vi = DynamicPPL.OnlyAccsVarInfo(DynamicPPL.AccumulatorTuple(accs)) | ||
| return map(params_with_stats) do ps | ||
| DynamicPPL.init!!(rng, model, vi, DynamicPPL.InitFromParams(ps.params, fallback)) |
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.
fallback being nothing is a change in default behavior, right? Might worth noting in changelog if true
| """ | ||
| supports_varname_indexing(chain::AbstractChains) | ||
| Return `true` if `chain` supports indexing using `VarName` in place of the | ||
| variable name index. | ||
| """ | ||
| supports_varname_indexing(::AbstractChains) = false | ||
|
|
||
| """ | ||
| getindex_varname(chain::AbstractChains, sample_idx, varname::VarName, chain_idx) | ||
| Return the value of `varname` in `chain` at `sample_idx` and `chain_idx`. | ||
| Whether this method is implemented for `chains` is indicated by [`supports_varname_indexing`](@ref). | ||
| """ | ||
| function getindex_varname end | ||
|
|
||
| """ | ||
| varnames(chains::AbstractChains) | ||
| Return an iterator over the varnames present in `chains`. | ||
| Whether this method is implemented for `chains` is indicated by [`supports_varname_indexing`](@ref). | ||
| """ | ||
| function varnames 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.
no objection on getting rid of these, not sure if we should add an entry in changelog though. the likelihood that any of these are used somewhere is low
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.
Oh, you're completely correct, and I just totally neglected the changelog in this PR. My bad.
|
on
maybe we can somehow check Julia's effect analysis pipeline? (https://aviatesk.github.io/posts/effects-analysis/index.html, https://docs.julialang.org/en/v1/base/base/#Base.@assume_effects, https://github.com/JuliaLang/julia/blob/master/Compiler/src/effects.jl) |
|
Hmmm, I thought that the only way to make it work would be to handle the IR (i.e. like Mooncake), but I don't know anything about the effect system! |
* v0.39 * Update DPPL compats for benchmarks and docs * remove merge conflict markers * Remove `NodeTrait` (#1133) * Remove NodeTrait * Changelog * Fix exports * docs * fix a bug * Fix doctests * Fix test * tweak changelog * FastLDF / InitContext unified (#1132) * Fast Log Density Function * Make it work with AD * Optimise performance for identity VarNames * Mark `get_range_and_linked` as having zero derivative * Update comment * make AD testing / benchmarking use FastLDF * Fix tests * Optimise away `make_evaluate_args_and_kwargs` * const func annotation * Disable benchmarks on non-typed-Metadata-VarInfo * Fix `_evaluate!!` correctly to handle submodels * Actually fix submodel evaluate * Document thoroughly and organise code * Support more VarInfos, make it thread-safe (?) * fix bug in parsing ranges from metadata/VNV * Fix get_param_eltype for TSVI * Disable Enzyme benchmark * Don't override _evaluate!!, that breaks ForwardDiff (sometimes) * Move FastLDF to experimental for now * Fix imports, add tests, etc * More test fixes * Fix imports / tests * Remove AbstractFastEvalContext * Changelog and patch bump * Add correctness tests, fix imports * Concretise parameter vector in tests * Add zero-allocation tests * Add Chairmarks as test dep * Disable allocations tests on multi-threaded * Fast InitContext (#1125) * Make InitContext work with OnlyAccsVarInfo * Do not convert NamedTuple to Dict * remove logging * Enable InitFromPrior and InitFromUniform too * Fix `infer_nested_eltype` invocation * Refactor FastLDF to use InitContext * note init breaking change * fix logjac sign * workaround Mooncake segfault * fix changelog too * Fix get_param_eltype for context stacks * Add a test for threaded observe * Export init * Remove dead code * fix transforms for pathological distributions * Tidy up loads of things * fix typed_identity spelling * fix definition order * Improve docstrings * Remove stray comment * export get_param_eltype (unfortunatley) * Add more comment * Update comment * Remove inlines, fix OAVI docstring * Improve docstrings * Simplify InitFromParams constructor * Replace map(identity, x[:]) with [i for i in x[:]] * Simplify implementation for InitContext/OAVI * Add another model to allocation tests Co-authored-by: Markus Hauru <markus@mhauru.org> * Revert removal of dist argument (oops) * Format * Update some outdated bits of FastLDF docstring * remove underscores --------- Co-authored-by: Markus Hauru <markus@mhauru.org> * implement `LogDensityProblems.dimension` * forgot about capabilities... * use interpolation in run_ad * Improvements to benchmark outputs (#1146) * print output * fix * reenable * add more lines to guide the eye * reorder table * print tgrad / trel as well * forgot this type * Allow generation of `ParamsWithStats` from `FastLDF` plus parameters, and also `bundle_samples` (#1129) * Implement `ParamsWithStats` for `FastLDF` * Add comments * Implement `bundle_samples` for ParamsWithStats -> MCMCChains * Remove redundant comment * don't need Statistics? * Make FastLDF the default (#1139) * Make FastLDF the default * Add miscellaneous LogDensityProblems tests * Use `init!!` instead of `fast_evaluate!!` * Rename files, rebalance tests * Implement `predict`, `returned`, `logjoint`, ... with `OnlyAccsVarInfo` (#1130) * Use OnlyAccsVarInfo for many re-evaluation functions * drop `fast_` prefix * Add a changelog * Improve FastLDF type stability when all parameters are linked or unlinked (#1141) * Improve type stability when all parameters are linked or unlinked * fix a merge conflict * fix enzyme gc crash (locally at least) * Fixes from review * Make threadsafe evaluation opt-in (#1151) * Make threadsafe evaluation opt-in * Reduce number of type parameters in methods * Make `warned_warn_about_threads_threads_threads_threads` shorter * Improve `setthreadsafe` docstring * warn on bare `@threads` as well * fix merge * Fix performance issues * Use maxthreadid() in TSVI * Move convert_eltype code to threadsafe eval function * Point to new Turing docs page * Add a test for setthreadsafe * Tidy up check_model * Apply suggestions from code review Fix outdated docstrings Co-authored-by: Markus Hauru <markus@mhauru.org> * Improve warning message * Export `requires_threadsafe` * Add an actual docstring for `requires_threadsafe` --------- Co-authored-by: Markus Hauru <markus@mhauru.org> * Standardise `:lp` -> `:logjoint` (#1161) * Standardise `:lp` -> `:logjoint` * changelog * fix a test --------- Co-authored-by: Markus Hauru <mhauru@turing.ac.uk> Co-authored-by: Markus Hauru <markus@mhauru.org>
This does lead to some improvement in performance, but not as much as I had hoped:
It appears to me that most of this time is spent faffing with MCMCChains. Every time you try to get the value of
@varname(mu)you have to go through thevarname_to_symboldict, etc. Even more importantly, there's an issue withtheta, because that's vector-valued and when you access it you have to reconstruct the vector (withgetvalue(dict, vn, dist). So I believe we are hitting a natural plateau that is caused by the data structure. Probably the performance gains with FlexiChains would be larger.Still, I suppose it's worth putting this in because it's basically free performance, so why not? It's also cleaner code.
Also, I haven't tested this, but I'm about 95% sure that this makes
predict,returned, etc. completely thread-safe if TSVI is used. That's because the combination ofTSVI{<:OAVI}is completely threadsafe to everything.A complete aside
Wouldn't it be fun if we could inspect the model, realise that the return value only involves
muandtau, realise thattauis a bits type and thustau^2 * Icannot mutatetau, and thus optimise away all the things to do withthetaandy?