Skip to content

Accumulators stage 2 #925

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

Merged
merged 13 commits into from
Jul 18, 2025
Merged

Accumulators stage 2 #925

merged 13 commits into from
Jul 18, 2025

Conversation

mhauru
Copy link
Member

@mhauru mhauru commented May 21, 2025

I'm making various changes to accumulators as I integrate Turing.jl with them. This isn't ready for review yet, but @penelopeysm, can I ask you for your thoughts on the design of the proposed changes to LogDensityFunction? It would now have a new field, given as the second constructor argument, called getlogdensity. By default it's getlogjoint, but you can set it to getlogprior or getloglikelihood or any other function that takes an AbstractVarInfo. Its return value will be the return value of logdensity_and_gradient etc. The default VarInfo, if one isn't given by the user, is also now set based on getlogdensity, to make sure it has the right accumulators.

Copy link
Contributor

github-actions bot commented May 21, 2025

Benchmark Report for Commit 082a780

Computer Information

Julia Version 1.11.6
Commit 9615af0f269 (2025-07-09 12:58 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                 13.1 |                 1.4 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                773.2 |                37.7 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                437.0 |                55.7 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1072.3 |                34.2 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               6645.3 |                24.0 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1587.2 |                26.2 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |               1119.6 |                12.1 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               7112.6 |                42.6 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |               1052.7 |                 8.7 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              85216.8 |                40.2 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               8908.2 |                 9.5 |
|               Dynamic |        10 |    mooncake |             typed |   true |                146.5 |                31.8 |
|              Submodel |         1 |    mooncake |             typed |   true |                 17.9 |                21.5 |
|                   LDA |        12 | reversediff |             typed |   true |               1180.4 |                 1.8 |

Copy link

codecov bot commented May 21, 2025

Codecov Report

Attention: Patch coverage is 73.52941% with 9 lines in your changes missing coverage. Please review.

Project coverage is 82.11%. Comparing base (f4dd46a) to head (082a780).
Report is 1 commits behind head on breaking.

Files with missing lines Patch % Lines
src/logdensityfunction.jl 77.77% 4 Missing ⚠️
src/simple_varinfo.jl 0.00% 4 Missing ⚠️
src/test_utils/ad.jl 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           breaking     #925      +/-   ##
============================================
+ Coverage     81.97%   82.11%   +0.13%     
============================================
  Files            38       38              
  Lines          4023     4031       +8     
============================================
+ Hits           3298     3310      +12     
+ Misses          725      721       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@penelopeysm
Copy link
Member

penelopeysm commented May 21, 2025

Hmmm, in terms of design, the other potential option I see is to make the new getlogdensity a keyword argument (which means that varinfo can't depend on it)

user specifies... getlogdensity as positional argument getlogdensity as keyword argument
+getlogdensity +varinfo have to make sure they are consistent have to make sure they are consistent
+getlogdensity -varinfo generates the most efficient varinfo uses default varinfo, potentially inefficient
-getlogdensity +varinfo not possible preserves current behaviour
-getlogdensity -varinfo preserves current behaviour preserves current behaviour

So it seems the main tradeoff is that having it as a positional argument allows for the most efficient varinfo (one without a likelihood accumulator) to be generated if you do e.g. LogDensityFunction(model, getlogprior), but in return, you can't call LogDensityFunction(model, varinfo), you have to do LogDensityFunction(model, getlogjoint, varinfo).

I think my first instinct is that I prefer the latter, and you could either insert a check in the inner constructor to see if the accumulators are consistent (i.e. warn if you have extra accs, or error if you don't have enough accs), or just straight-up call setaccs in the inner constructor to force consistency. I'm generally not a huge fan of Julia's optional positional arguments because they aren't truly optional – you can only omit them starting from the end (unless you start declaring 2^N methods).

Do you prefer the former? I'll mull over it a bit more too and see if any ideas come up.

@mhauru
Copy link
Member Author

mhauru commented May 21, 2025

I wouldn't even bother checking for consistency if the user provides both getlogdensity and varinfo. If you're getting that hands-on with your LogDensityFunction I assume you know what you're doing and if not that's on you. If there's a mismatch, you'll probably just get an error saying "no accumulator X available in your VarInfo".

I would be happy to give up the case of LogDensityFunction(model, varinfo) and force people to do LogDensityFunction(model, getlogjoint, varinfo) because giving the varinfo argument I think means you're dealing with some pretty low-level stuff, most likely either trying to use a non-standard subtype of AbstractVarInfo or a linked VarInfo. If you're thinking about that sorts of things, I think having to give it a few seconds and one more line of code to specify which log probability you are after (a much more "user-level"/statistical concern) seems fine to me. (Similarly this is why I think the context should be the last argument, as it is now, because that's getting even deeper into the internals of DPPL.)

There's also something about type dispatch treating keyword args differently. Sometimes methods are specialised on them and sometimes they are not, and I don't understand the details.

Copy link
Member

@penelopeysm penelopeysm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it and I think I would prefer to be able to keep the new getlogdensity argument optional by declaring the extra method:

function LogDensityFunction(
    model::Model,
    varinfo::AbstractVarInfo,
    context::AbstractContext=DefaultContext();
    adtype::Union{ADTypes.AbstractADType,Nothing}=nothing
)
    return LogDensityFunction(model, getlogjoint, varinfo, context; adtype)
end

This is the 2^N method problem but in this case N = 1 so I'm still fine with it. Main reason is because (in my experience working with this codebase) LogDensityFunction(model, varinfo) is used far more commonly than LogDensityFunction(model, varinfo, PriorContext()) (or LikelihoodContext), so it's a nice convenience.

Also I think LogDensityFunction is likely to become more user-facing so that would likely be useful for many people.

@mhauru
Copy link
Member Author

mhauru commented May 27, 2025

It's not the worst, but I'm not a big fan of the behaviour of LogDensityFunction(model, x) being very different for different types of x. It means that there's no answer to the question of "what are the semantics of the second argument".

Also I think LogDensityFunction is likely to become more user-facing so that would likely be useful for many people.

If this is true, then I agree, but I'm not sure it is. How often do users want to customise their VarInfos?

If we were making this from scratch, what would we do? LogDensityFunction(model) makes an LDF for a Turing model, that makes plenty of sense. All the other arguments are because there are different flavours of LDFs:

  1. They can evaluate different notions of log density, e.g. log joint or log likelihood.
  2. They can use different types of AbstractVarInfos internally. This includes specifying that you are using a linked varinfo.
  3. They can use a different evaluation context, which is to say the user may customise the model's behaviour post-construction in arbitrary ways.
  4. They can use different AD backends internally.

I think of these 1. is a thing that makes sense to a statistician/modeller, and should thus be more prominent than the others. EDIT: It's also the only one that changes the output of logdensity, except if you're using a weird context. The others are only there because of implementation details and quirks of Turing/Julia, except debatably the linked varinfo case. I might make 1. an optional positional argument and the others keyword arguments, or have them all as optional positionals in the order above. What would your blank-slate design be?

@penelopeysm
Copy link
Member

penelopeysm commented May 27, 2025

It means that there's no answer to the question of "what are the semantics of the second argument".

Yeah, that's fair -- I don't really like it too and I feel like I'm kind of stumbling around trying to find what I really want...

What would your blank-slate design be?

...I thought about it and I think my real hang-up is not so much convenience, but I think that the information about which part of logp is to be calculated is being specified twice — once in the function, and once in the VarInfo's accumulators. And I don't like that there's potential inconsistency between the two sources of information — and although we can resolve it by declaring the function to be the source of truth — this isn't obvious from the interface, and future readers will have to look into this file to figure it out.*

In my ideal world,† I would want there to be a single source of truth (this would necessarily be the varinfo, since you can't create an LDF without a varinfo) and for the user to specify this information (by setting the accumulators themselves). i.e instead of

LogDensityFunction(model, getlogprior, varinfo, ...)

they would call

LogDensityFunction(model, setaccs!!(VarInfo(model), (LogPriorAccumulator(),)), ...)

and to make that easier I'd probably make setaccs!!(...) a convenience function, in the same way that contextualize used to be a convenience function back when this was specified using contexts. Or maybe, VarInfo(model, ...) could take an extra argument to specify which logp accs to include.


* It's still overall better than the previous world where the context was magically doing things to modify how varinfo was being changed.

† Okay, in my ideal-ideal world everything would be a keyword argument, but Julia probably doesn't like this world.

@mhauru
Copy link
Member Author

mhauru commented May 28, 2025

Or maybe, VarInfo(model, ...) could take an extra argument to specify which logp accs to include.

A bit off topic, but I want to do something like this regardless of what we do with LDF, but I've so far struggled with the fact that VarInfo constructors are already quite overloaded, leaving little syntax available. Ideas very welcome.

I see what you mean by a single source of truth. The way I explain this to myself, which is definitely imperfect, is that getlogdensity defines what is to be gotten from the varinfo and returned, and then which accumulators varinfo carries is more like a technicality of "make sure you've packed everything you need if that's where you want to go". We could make it such that the LDF constructor overwrites the accumulators of the provided varinfo, but that seems like a restriction we might come to regret at some point, and really starts to beg the question of why are we giving LDF an AbstractVarInfo in the first place if we discard both the parameter values and the accumulators in it.

Note that accumulators and possible getlogdensity functions aren't necessarily one-to-one. For instance, getlogjoint needs two accumulators. I was considering having the getter function be determined somehow from the accumulators but the thing that made me go with having them separate was having to, for MAP/MLE optimisation, implement LogPriorWithoutJacobianAccumulator, and then also have a getlogjoint_withoutjacobian(vi) = getlogprior_withoutjacobian(vi) + getloglikelihood(vi).

EDIT: Maybe a better example of getlogdensity and accumulators not being one-to-one would be a getter equivalent to MiniBatchContext, which returns getlogprior(x) + weight * getloglikelihood(x) where weight is a parameter of the getter.

@penelopeysm
Copy link
Member

I said I'd post something on this, then never did. OK, after thinking about it on and off for a couple of weeks, I've pretty much come round to the notion that the getlogp function is the single source of truth about what logp is being calculated, and the VarInfo object purely exists as a means to an end.

For some reason I had it in my mind that, for example, MiniBatchContext would be translated into a LogPriorAcc which carried a multiplicative factor with it, i.e., the number inside LogPriorAcc would actually be k times the real logprior. I think it makes much more sense though that the number inside LogPriorAcc is always the true logprior, and the function is responsible for multiplying that by k.

To repent for the error of my ways, I'll fix the merge conflicts on this PR 😄

Copy link
Contributor

DynamicPPL.jl documentation for PR #925 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR925/

@penelopeysm penelopeysm mentioned this pull request Jul 10, 2025
22 tasks
@penelopeysm penelopeysm dismissed their stale review July 18, 2025 17:44

i mean it's me why do i have to provide a message when dismissing my own review

@mhauru mhauru marked this pull request as ready for review July 18, 2025 17:56
@mhauru mhauru requested a review from penelopeysm July 18, 2025 17:56
@penelopeysm
Copy link
Member

Will merge so that the upstream PR targeting breaking can incorporate these changes.

@penelopeysm penelopeysm merged commit e60eab0 into breaking Jul 18, 2025
18 of 21 checks passed
@penelopeysm penelopeysm deleted the mhauru/accumulators-stage2 branch July 18, 2025 19:53
github-merge-queue bot pushed a commit that referenced this pull request Aug 7, 2025
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants