Skip to content

The big InitContext PR. Do not merge! #967

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

Draft
wants to merge 16 commits into
base: breaking
Choose a base branch
from
Draft

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jul 4, 2025

Important

I'm trying to split up this PR into bits. Please don't merge this. I will close it when I'm done splitting it up.

Part 1: Adding hasvalue and getvalue to AbstractPPL
Part 2: Removing hasvalue and getvalue from DynamicPPL
Part 3: Introducing InitContext and init!!
Part 4: (WIP) Using ParamsInit to implement predict, returned, and initialize_values
Part 5: The Rest i.e. deleting SamplingContext, SampleFromPrior, SampleFromUniform


This PR largely follows through on the proposal in #955. I would say that the code changes in this PR are ~ 80% representative of the final result. However, I'm trying my best to disentangle the different bits and/or to set out the changes in separate stages, so that it's easier to review.

  • Removes SamplingContext, SampleFromPrior, and SampleFromUniform

  • Creates a new leaf context, InitContext. Its behaviour is to create new values, or overwrite existing variables in a VarInfo if they exist.

  • Introduces initialisation strategies, PriorInit, UniformInit, and ParamsInit. See docstrings for more info.

  • Changes evaluate_and_sample!! (introduced in Remove context from model evaluation (use model.context instead) #952) to init!!

  • Moves all the VarInfo-initialisation code (used to be in sampler.jl) to ParamsInit.

Auxiliary changes that follow from the above:

  • The function initialsampler, which was used to specify how initial values were generated for a given sampler, has been removed. The correct replacement is init_strategy(::AbstractSampler); however, that should be in Turing not DynamicPPL.

  • Note that, after deleting the infamous assume method for SampleFromPrior, there is no longer any code that actually reads the del flag on VarInfo. There is a fair amount of code that writes to the del flag which has to be updated.

    • setval_and_resample! followed by model evaluation is replaced with ParamsInit with an incomplete set of parameters.
    • setval_and_resample! used to use nested_setindex_maybe. The role of this function was to handle cases where the VarName of interest was x, but instead of supplying x, the user supplied (say) x[1] and x[2]. This has now been replaced with hasvalue(dict, vn, dist) which uses dist to determine whether the sub-variables x[1] and x[2] provide enough information to reconstruct the full variable x. For example, this would return true if dist is MvNormal(zeros(2), I).

A long-held goal of mine is to split up contexts.jl and context_implementations.jl such that each context is in its own file. To help get us to this stage, I've put all the InitContext-related code inside src/contexts/init.jl. This should, hopefully, also make reviewing easier.

TODO for this PR

TODO for future PRs

  • Split up contexts code

Look at this!:

Closes #859
Closes #955
Closes #797 (indirectly, since initialize_parameters is now init!!(..., ::ParamsInit))
Closes #774
Closes #375
Closes #983

Furthermore, once an upstream interface to InitContext is put into Turing, this will also solve:

TuringLang/Turing.jl#2476 (sampling)
TuringLang/Turing.jl#1775 (mode estimation)

Copy link
Contributor

github-actions bot commented Jul 5, 2025

Benchmark Report for Commit 7265885

Computer Information

Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 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 |                  8.0 |                 1.5 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                612.9 |                40.6 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                389.1 |                50.2 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |                913.3 |                35.1 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               6151.3 |                24.2 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1397.3 |                26.9 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |                933.0 |                 4.3 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5407.9 |                 3.9 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |                911.8 |                 9.1 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              60700.6 |                 3.5 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               7835.6 |                10.1 |
|               Dynamic |        10 |    mooncake |             typed |   true |                128.9 |                11.3 |
|              Submodel |         1 |    mooncake |             typed |   true |                 12.5 |                 5.9 |
|                   LDA |        12 | reversediff |             typed |   true |               1220.6 |                 2.4 |

Copy link

codecov bot commented Jul 5, 2025

Codecov Report

Attention: Patch coverage is 0% with 87 lines in your changes missing coverage. Please review.

Project coverage is 4.39%. Comparing base (f1d5f20) to head (21bcb0b).

Files with missing lines Patch % Lines
src/test_utils/contexts.jl 0.00% 28 Missing ⚠️
src/model.jl 0.00% 15 Missing ⚠️
src/varinfo.jl 0.00% 15 Missing ⚠️
ext/DynamicPPLMCMCChainsExt.jl 0.00% 11 Missing ⚠️
src/simple_varinfo.jl 0.00% 11 Missing ⚠️
ext/DynamicPPLJETExt.jl 0.00% 2 Missing ⚠️
src/debug_utils.jl 0.00% 2 Missing ⚠️
src/extract_priors.jl 0.00% 1 Missing ⚠️
src/sampler.jl 0.00% 1 Missing ⚠️
src/test_utils/model_interface.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                    @@
##           py/init-prior-uniform    #967       +/-   ##
=========================================================
- Coverage                  33.43%   4.39%   -29.05%     
=========================================================
  Files                         38      39        +1     
  Lines                       4023    3802      -221     
=========================================================
- Hits                        1345     167     -1178     
- Misses                      2678    3635      +957     

☔ 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.

Note that, apart from being simpler code, Distributions.Uniform also
doesn't allow the lower and upper bounds to be exactly equal (but we
might like to keep that option open in DynamicPPL, e.g. if the user
wants to initialise all values to the same value in linked space).
@penelopeysm penelopeysm changed the title [WIP] InitContext The big InitContext PR. Do not merge! Jul 9, 2025
@penelopeysm penelopeysm changed the base branch from breaking to py/init-prior-uniform July 10, 2025 00:30
Copy link
Contributor

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

@penelopeysm penelopeysm changed the base branch from py/init-prior-uniform to breaking July 10, 2025 01:10
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.

1 participant