Skip to content

Allow CustomDist with inferred logp in Mixture #6746

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 7 commits into from
Jun 5, 2023

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented May 31, 2023

This PR changes the logic used for logprob inference. Instead of eager bottom-up conversion to measurable variables in the IR rewrites, we only convert nodes whose outputs were marked as "needs_measuring". This is achieved with the new PreserveRVMappings.request_measurable method.

This strategy obviates the need to undo unnecessary conversions. It also obviates a subtle need for graph cloning via the ignore_logprob helper, which prevented intermediate measurable rewrites from being reversed when they were needed to derive the logprob of valued variables, but were not directly valued. This indirect role of ignore_logprob is now done more explicitly and efficiently via the request_measurable method.

All other uses of ignore_logprob (and reconsider_logprob) were removed from the codebase

The get_measurable_outputs dispatching was also abandoned in favor of only considering outputs associated with value variables.

A new MergeOptimizerRewrite was written to further target local rewrites to only those nodes whose variables have been marked as needs_measuring. This should be rather more efficient

Closes #6728
Supersedes #6698

CC @shreyas3156 @Dhruvanshu-Joshi


📚 Documentation preview 📚: https://pymc--6746.org.readthedocs.build/en/6746/

@ricardoV94 ricardoV94 force-pushed the remove_ignore_logprob branch 2 times, most recently from adfb3bf to fe3ed61 Compare May 31, 2023 19:38
@codecov
Copy link

codecov bot commented May 31, 2023

Codecov Report

Merging #6746 (b23ec35) into main (f3df36b) will decrease coverage by 1.51%.
The diff coverage is 91.12%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6746      +/-   ##
==========================================
- Coverage   91.99%   90.48%   -1.51%     
==========================================
  Files          95       95              
  Lines       16205    16125      -80     
==========================================
- Hits        14907    14591     -316     
- Misses       1298     1534     +236     
Impacted Files Coverage Δ
pymc/data.py 89.34% <ø> (-0.25%) ⬇️
pymc/distributions/bound.py 100.00% <ø> (ø)
pymc/variational/opvi.py 87.48% <ø> (ø)
pymc/logprob/abstract.py 93.61% <77.77%> (-5.20%) ⬇️
pymc/logprob/rewriting.py 89.32% <81.39%> (-8.84%) ⬇️
pymc/logprob/tensor.py 79.48% <84.61%> (-1.87%) ⬇️
pymc/testing.py 91.37% <85.71%> (-0.66%) ⬇️
pymc/logprob/checks.py 97.91% <91.66%> (+31.25%) ⬆️
pymc/logprob/mixture.py 96.33% <92.85%> (-2.20%) ⬇️
pymc/distributions/distribution.py 96.01% <100.00%> (-0.63%) ⬇️
... and 14 more

... and 7 files with indirect coverage changes

@ricardoV94 ricardoV94 force-pushed the remove_ignore_logprob branch 4 times, most recently from 628008f to afe476b Compare June 1, 2023 08:52
@ricardoV94 ricardoV94 changed the title Allow CustomDist with inferred logp in Mixture and other distribution factories Allow CustomDist with inferred logp in Mixture Jun 1, 2023
@ricardoV94 ricardoV94 force-pushed the remove_ignore_logprob branch 2 times, most recently from 950494a to 8529ff0 Compare June 1, 2023 09:27
@twiecki
Copy link
Member

twiecki commented Jun 1, 2023

image 🔥

This commit changes the logic used for logprob inference. Instead of eager bottom-up conversion to measurable variables in the IR rewrites, we only convert nodes whose outputs were marked as "needs_measuring". This is achieved with the new `PreserveRVMappings.request_measurable` method.

This strategy obviates the need to undo unnecessary conversions. It also obviates a subtle need for graph cloning via the `ignore_logprob` helper, which prevented intermediate measurable rewrites from being reversed when they were needed to derive the logprob of valued variables, but were not directly valued. This indirect role of `ignore_logprob` is now done more explicitly and efficiently via the `request_measurable` method.

All other uses of `ignore_logprob` (and `reconsider_logprob`) were removed from the codebase

The `get_measurable_outputs` dispatching was also abandoned in favor of only considering outputs associated with value variables.

A new MergeOptimizerRewrite was written to further target local rewrites to only those nodes whose variables have been marked as `needs_measuring`.
@ricardoV94 ricardoV94 force-pushed the remove_ignore_logprob branch from 90e17c0 to 3dd6dcb Compare June 5, 2023 09:02
Join/MakeVector/IfElse can output multiple interdependent variables. These are potentially measurable because in the logp each output is given a distinct value variable. However, this isn't known during the IR rewrites.

To circumvent this issue, we run an inner IR rewrite after giving dummy value variables to each output
@twiecki twiecki merged commit 261862d into pymc-devs:main Jun 5, 2023
@ricardoV94 ricardoV94 deleted the remove_ignore_logprob branch September 18, 2023 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mixture with CustomDist with inferred logp fails
4 participants