-
Notifications
You must be signed in to change notification settings - Fork 37
FastLDF / InitContext unified #1132
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 for Commit 63198fdComputer InformationBenchmark Results |
|
DynamicPPL.jl documentation for PR #1132 is available at: |
248f374 to
a4c71e6
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## breaking #1132 +/- ##
============================================
+ Coverage 81.32% 81.73% +0.41%
============================================
Files 40 42 +2
Lines 3807 3921 +114
============================================
+ Hits 3096 3205 +109
- Misses 711 716 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
|
||
| # Test that various different ways of specifying array types as arguments work with all | ||
| # ADTypes. | ||
| @testset "Array argument types" begin |
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 testset is duplicated from LogDensityFunction so nothing new
| The same problem precludes us from eventually broadening the scope of DynamicPPL.jl to | ||
| support distributions with non-numeric samples. |
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 ran into this issue before, #1003 (comment). The problem is that because we no longer read values from VarInfo, we don't have that information about what the values' eltype are, which can lead to the sort of problems observed in this docstring. (And to be fair, previously we would set values into varinfo and then read eltype(varinfo), which is a terrible idea because parameter types can be homogeneous. Although this seems like more faff, it's probably for the better.)
| # 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 | ||
| accs = map( | ||
| acc -> DynamicPPL.convert_eltype(float_type_with_fallback(eltype(params)), acc), | ||
| accs, | ||
| ) | ||
| ThreadSafeVarInfo(OnlyAccsVarInfo(accs)) | ||
| else | ||
| OnlyAccsVarInfo(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.
| """ | ||
| typed_identity(x) |
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 think eventually this should go to Bijectors, but we can keep it here for now.
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.
FWIW, Distributions hardcodes Float64 to fix what's fundamentally the same issue, so I think this approach is correct, or at least correct enough.
mhauru
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.
Love it. Just some small bits here and there.
Happy to put this in Experimental, but I wouldn't be offended by immediately or very soon trying to take over LogDensityFunction and seeing what happens, especially in Turing.jl's test suite.
src/fasteval.jl
Outdated
| - `fastldf.adtype`: The AD type used for gradient calculations, or `nothing` if no AD | ||
| type was provided. | ||
| ## Extended help |
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 all very useful documentation, and for now this is a good place. Once this comes out of Experimental I wonder if some of the extended stuff should go somewhere else. Maybe either in developer docs, or in 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.
Yes, definitely, somewhere on the developer docs. I am a bit annoyed that I wrote up the models and varinfo evaluation stuff, only to now tear it up (although to be fair writing that was what prompted me to think about this, so... we'll take the win)
Co-authored-by: Markus Hauru <[email protected]>
mhauru
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.
Great, thanks!
* 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 <[email protected]> * Revert removal of dist argument (oops) * Format * Update some outdated bits of FastLDF docstring * remove underscores --------- Co-authored-by: Markus Hauru <[email protected]> * 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 <[email protected]> * Improve warning message * Export `requires_threadsafe` * Add an actual docstring for `requires_threadsafe` --------- Co-authored-by: Markus Hauru <[email protected]> * Standardise `:lp` -> `:logjoint` (#1161) * Standardise `:lp` -> `:logjoint` * changelog * fix a test --------- Co-authored-by: Markus Hauru <[email protected]> Co-authored-by: Markus Hauru <[email protected]>
This reimplements FastLDF, conceptually in the same way as #1113. Please see that PR for the bulk of the explanation. The difference is that this also unifies the implementation of
FastLDFandInitFromParams, such thatFastLDFis now actually justInitFromParamsbut backed by the combination of vector + ranges.Here's a slightly modified diagram from my slides yesterday:
Other speedups
Note that this unification also means that other initialisation strategies, i.e.
InitFromPrior,InitFromUniform, and other forms ofInitFromParams, can also benefit from the speedup (as shown in the top half of the diagram above). This was essentially done in #1125 but lumped into this PR as well. See that PR for benchmarks.Does this still need to be
Experimental?I'd suggest for this PR yes, if only just to prove correctness compared to old LDF. Making this replace old LDF should be a fairly trivial follow-up. Am open to other ideas.
Does this need to be
breaking?Yes, because the expected return value of
DynamicPPL.inithas changed. Technically, that wasn't exported, butAbstractInitStrategywas exported, soinitwas effectively public (it should have been exported).On top of that, this PR relies on changes in #1133, which are also breaking.
Benchmarks
Performance characteristics are exactly the same as in the original PR #1113. Benchmarks run on Julia 1.11.7, 1 thread.
Benchmarking code
Trivial model
Eight-schools centred
Lots of IndexLenses
Submodel
MCMC
is down from around 8.8 seconds to 1.7 seconds.