Skip to content

Use accumulators to fix all logp calculations when sampling #2630

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 8 commits into from
Aug 1, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jul 25, 2025

Following on from TuringLang/DynamicPPL.jl#998, this PR:

  • in the optimisation code, removes LogPriorWithoutJacobianAccumulator as DynamicPPL's LogPriorAccumulator now serves this purpose;

  • replaces calls to getlogjoint with getlogjoint_internal for all samplers (as that's what they want);

  • closes values of logp in chain do not include logabsdetjac term #2617 by re-calculating logp together with values_as_in_model when constructing a Transition. This also allows us to stick logprior and loglikelihood terms into the chain (lp is still there and refers to the joint).

  • does a little tidy-up on MH: previously the sampler state was just a VarInfo, now we bundle the VarInfo together with its internal-logjoint so that the next iteration doesn't need to recalculate the internal-logjoint.

Between #2629 and this PR all the failing tests should be fixed. 🥳 The remaining issues are 1.10 (doesn't understand [sources] in Project.toml) and 1.12 (Libtask).

Closes #2617

@penelopeysm penelopeysm changed the base branch from main to py/no-set-retained-vns-del July 25, 2025 23:28
Copy link
Contributor

Turing.jl documentation for PR #2630 is available at:
https://TuringLang.github.io/Turing.jl/previews/PR2630/

Copy link

codecov bot commented Jul 25, 2025

Codecov Report

❌ Patch coverage is 97.59036% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.08%. Comparing base (7124864) to head (65f263d).
⚠️ Report is 1 commits behind head on mhauru/dppl-0.37.

Files with missing lines Patch % Lines
src/mcmc/Inference.jl 90.47% 2 Missing ⚠️
Additional details and impacted files
@@                  Coverage Diff                  @@
##           mhauru/dppl-0.37    #2630       +/-   ##
=====================================================
+ Coverage             24.43%   86.08%   +61.65%     
=====================================================
  Files                    22       22               
  Lines                  1502     1445       -57     
=====================================================
+ Hits                    367     1244      +877     
+ Misses                 1135      201      -934     

☔ 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 penelopeysm changed the title Use LogJacobianAccumulator and getlogjoint_internal from DPPL 0.37 Use LogJacobianAccumulator and getlogjoint_internal; tie logp recalculation in with values_as_in_model Jul 26, 2025
@penelopeysm penelopeysm changed the title Use LogJacobianAccumulator and getlogjoint_internal; tie logp recalculation in with values_as_in_model Fix logp calculations when sampling Jul 26, 2025
@penelopeysm penelopeysm changed the title Fix logp calculations when sampling Use accumulators to fix all logp calculations when sampling Jul 26, 2025
@penelopeysm penelopeysm marked this pull request as draft July 26, 2025 01:11
@penelopeysm penelopeysm force-pushed the py/logjac branch 2 times, most recently from f7c234f to 75913e9 Compare July 26, 2025 17:07
@penelopeysm penelopeysm marked this pull request as ready for review July 26, 2025 17:32
@penelopeysm penelopeysm mentioned this pull request Jul 26, 2025
@penelopeysm penelopeysm force-pushed the py/logjac branch 2 times, most recently from d1632c6 to 311ba4f Compare July 28, 2025 17:55
@penelopeysm penelopeysm force-pushed the py/no-set-retained-vns-del branch from db267ce to 1c424ab Compare July 31, 2025 11:17
Base automatically changed from py/no-set-retained-vns-del to mhauru/dppl-0.37 July 31, 2025 11:18
@penelopeysm
Copy link
Member Author

@mhauru Sorry this PR is a bit all over the place. The main change is Transition, everything else kind of follows from that, but to get the right behaviour I had to make some slightly less trivial changes to MH and externalsampler.

I think all the 1.11 things in CI that we expect to pass are passing. I'd be in favour of releasing DPPL 0.37 in its current state, then we can check that things work on 1.10, before beginning the remaining work on 0.38. Thoughts welcome.

@penelopeysm penelopeysm requested a review from mhauru August 1, 2025 14:48
@penelopeysm penelopeysm merged commit 8fdecc0 into mhauru/dppl-0.37 Aug 1, 2025
22 of 33 checks passed
@penelopeysm penelopeysm deleted the py/logjac branch August 1, 2025 15:24
penelopeysm added a commit that referenced this pull request Aug 12, 2025
* First efforts towards DPPL 0.37 compat, WIP

* More DPPL 0.37 compat work, WIP

* Add [sources] for [email protected]

* Remove context argument from `LogDensityFunction`

* Fix MH

* Remove spurious logging

* Remove residual OptimizationContext

* Delete files that were removed in previous releases

* Fix typo

* Simplify ESS

* Fix LDF

* Fix Prior(), fix a couple more imports

* fixes

* actually fix prior

* Remove extra return value from tilde_assume

* fix ldf

* actually fix prior

* fix HMC log-density

* fix ldf

* fix make_evaluate_...

* more fixes for evaluate!!

* fix hmc

* fix run_ad

* even more fixes (oh goodness when will this end)

* more fixes

* fix

* more fix fix fix

* fix return values of tilde pipeline

* even more fixes

* Fix missing import

* More MH fixes

* Fix conversion

* don't think it really needs those type params

* implement copy for LogPriorWithoutJacAcc

* Even more fixes

* More fixes; I think the remaining failures are pMCMC related

* Fix merge

* DPPL 0.37 compat for particle MCMC (#2625)

* Progress in DPPL 0.37 compat for particle MCMC

* WIP PMCMC work

* Gibbs fixes for DPPL 0.37 (plus tiny bugfixes for ESS + HMC) (#2628)

* Obviously this single commit will make Gibbs work

* Fixes for ESS

* Fix HMC call

* improve some comments

* Fixes to ProduceLogLikelihoodAccumulator

* Use LogProbAccumulator for ProduceLogLikelihoodAccumulator

* use get_conditioned_gibbs

---------

Co-authored-by: Penelope Yong <[email protected]>

* "Fixes" for PG-in-Gibbs (#2629)

* WIP PMCMC work

* Fixes to ProduceLogLikelihoodAccumulator

* inline definition of `set_retained_vns_del!`

* Fix ProduceLogLikelihoodAcc

* Remove all uses of `set_retained_vns_del!`

* Use nice functions

* Remove PG tests with dynamic number of Gibbs-conditioned-observations

* Fix essential/container tests

* Update pMCMC implementation as per discussion

* remove extra printing statements

* revert unneeded changes

* Add back (some kind of) dynamic model test

* fix rebase

* Add a todo comment for dynamic model tests

---------

Co-authored-by: Markus Hauru <[email protected]>

* Use accumulators to fix all logp calculations when sampling (#2630)

* Use new `getlogjoint` for optimisation

* Change getlogjoint -> getlogjoint_internal where needed

* Enforce re-evaluation when constructing `Transition`

* fix tests

* Remove extra evaluations from SGLD and SGHMC

* Remove dead `transitions_from_chain` method (used to be part of `predict`)

* metadata -> getstats_with_lp

* Clean up some stray getlogp

* InitContext isn't for 0.37, update comments

* Fix merge

* Do not re-evaluate model for Prior (#2644)

* Allow Prior to skip model re-evaluation

* remove unneeded `default_chain_type` method

* add a test

* add a likelihood term too

* why not test correctness while we're at it

* No need to test AD for SamplingContext{<:HMC} (#2645)

* change breaking -> main

* Remove calls to resetlogp!! & add changelog (#2650)

* Remove calls to resetlogp!!

* Add a changelog for 0.40

* Update HISTORY.md

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* Remove `[sources]`

* Unify Turing `Transition`s, fix some tests (#2651)

* Unify `Transition` methods

* Add tests

* Add same test for SGLD/SGHMC

* Refactor so that it's nice and organised

* Fix failing test on 1.10

* just increase the atol

* Make addlogprob test more robust

* Remove stray `@show`

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* Update changelog for PG in Gibbs

---------

Co-authored-by: Penelope Yong <[email protected]>
penelopeysm added a commit that referenced this pull request Aug 12, 2025
* [no ci] Bump to v0.40.0

* Uncomment tests that should be there

* Support DPPL 0.37 (#2550)

* First efforts towards DPPL 0.37 compat, WIP

* More DPPL 0.37 compat work, WIP

* Add [sources] for [email protected]

* Remove context argument from `LogDensityFunction`

* Fix MH

* Remove spurious logging

* Remove residual OptimizationContext

* Delete files that were removed in previous releases

* Fix typo

* Simplify ESS

* Fix LDF

* Fix Prior(), fix a couple more imports

* fixes

* actually fix prior

* Remove extra return value from tilde_assume

* fix ldf

* actually fix prior

* fix HMC log-density

* fix ldf

* fix make_evaluate_...

* more fixes for evaluate!!

* fix hmc

* fix run_ad

* even more fixes (oh goodness when will this end)

* more fixes

* fix

* more fix fix fix

* fix return values of tilde pipeline

* even more fixes

* Fix missing import

* More MH fixes

* Fix conversion

* don't think it really needs those type params

* implement copy for LogPriorWithoutJacAcc

* Even more fixes

* More fixes; I think the remaining failures are pMCMC related

* Fix merge

* DPPL 0.37 compat for particle MCMC (#2625)

* Progress in DPPL 0.37 compat for particle MCMC

* WIP PMCMC work

* Gibbs fixes for DPPL 0.37 (plus tiny bugfixes for ESS + HMC) (#2628)

* Obviously this single commit will make Gibbs work

* Fixes for ESS

* Fix HMC call

* improve some comments

* Fixes to ProduceLogLikelihoodAccumulator

* Use LogProbAccumulator for ProduceLogLikelihoodAccumulator

* use get_conditioned_gibbs

---------

Co-authored-by: Penelope Yong <[email protected]>

* "Fixes" for PG-in-Gibbs (#2629)

* WIP PMCMC work

* Fixes to ProduceLogLikelihoodAccumulator

* inline definition of `set_retained_vns_del!`

* Fix ProduceLogLikelihoodAcc

* Remove all uses of `set_retained_vns_del!`

* Use nice functions

* Remove PG tests with dynamic number of Gibbs-conditioned-observations

* Fix essential/container tests

* Update pMCMC implementation as per discussion

* remove extra printing statements

* revert unneeded changes

* Add back (some kind of) dynamic model test

* fix rebase

* Add a todo comment for dynamic model tests

---------

Co-authored-by: Markus Hauru <[email protected]>

* Use accumulators to fix all logp calculations when sampling (#2630)

* Use new `getlogjoint` for optimisation

* Change getlogjoint -> getlogjoint_internal where needed

* Enforce re-evaluation when constructing `Transition`

* fix tests

* Remove extra evaluations from SGLD and SGHMC

* Remove dead `transitions_from_chain` method (used to be part of `predict`)

* metadata -> getstats_with_lp

* Clean up some stray getlogp

* InitContext isn't for 0.37, update comments

* Fix merge

* Do not re-evaluate model for Prior (#2644)

* Allow Prior to skip model re-evaluation

* remove unneeded `default_chain_type` method

* add a test

* add a likelihood term too

* why not test correctness while we're at it

* No need to test AD for SamplingContext{<:HMC} (#2645)

* change breaking -> main

* Remove calls to resetlogp!! & add changelog (#2650)

* Remove calls to resetlogp!!

* Add a changelog for 0.40

* Update HISTORY.md

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* Remove `[sources]`

* Unify Turing `Transition`s, fix some tests (#2651)

* Unify `Transition` methods

* Add tests

* Add same test for SGLD/SGHMC

* Refactor so that it's nice and organised

* Fix failing test on 1.10

* just increase the atol

* Make addlogprob test more robust

* Remove stray `@show`

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* Update changelog for PG in Gibbs

---------

Co-authored-by: Penelope Yong <[email protected]>

---------

Co-authored-by: Markus Hauru <[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.

values of logp in chain do not include logabsdetjac term
2 participants