Skip to content
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

Refactor MeasurableElemwise and its use #208

Merged
merged 4 commits into from
Dec 5, 2022

Conversation

brandonwillard
Copy link
Member

@brandonwillard brandonwillard commented Dec 2, 2022

This PR refactors the design of MeasurableElemwise and the rewrites that use it (i.e. the changes mostly introduced by #26).

These changes make it easier for others to extend the coverage of measurable Elemwise transforms using basic rewrite DB registration. They also put each transformation rewrite under the management of Aesara's rewrite system independently, which means that they can be applied more efficiently (e.g. hash-based lookups on distinct Op instances) and their application can be enabled/disabled and tracked with more granularity—among other things.

The additions in #144 have also been added.

Closes #170

@brandonwillard brandonwillard self-assigned this Dec 2, 2022
@brandonwillard brandonwillard marked this pull request as draft December 2, 2022 02:28
@brandonwillard brandonwillard added enhancement New feature or request important This label is used to indicate priority over things not given this label graph rewriting Involves the implementation of rewrites to Aesara graphs rv-transforms Involves transforms applied to random variables refactoring A change that improves the codebase but doesn't necessarily introduce a new feature labels Dec 2, 2022
@brandonwillard brandonwillard requested a review from rlouf December 2, 2022 02:43
@codecov
Copy link

codecov bot commented Dec 2, 2022

Codecov Report

Base: 95.08% // Head: 95.15% // Increases project coverage by +0.06% 🎉

Coverage data is based on head (369b835) compared to base (d0c009d).
Patch coverage: 99.08% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #208      +/-   ##
==========================================
+ Coverage   95.08%   95.15%   +0.06%     
==========================================
  Files          12       12              
  Lines        1974     2023      +49     
  Branches      258      253       -5     
==========================================
+ Hits         1877     1925      +48     
- Misses         55       56       +1     
  Partials       42       42              
Impacted Files Coverage Δ
aeppl/cumsum.py 100.00% <ø> (ø)
aeppl/joint_logprob.py 95.00% <ø> (ø)
aeppl/mixture.py 96.59% <ø> (ø)
aeppl/tensor.py 85.84% <ø> (ø)
aeppl/transforms.py 96.45% <98.70%> (+0.20%) ⬆️
aeppl/abstract.py 100.00% <100.00%> (ø)
aeppl/censoring.py 96.22% <100.00%> (+0.22%) ⬆️
aeppl/rewriting.py 94.17% <100.00%> (ø)
aeppl/scan.py 94.73% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@brandonwillard brandonwillard force-pushed the inverse_transform branch 4 times, most recently from 30c7521 to 3833828 Compare December 2, 2022 23:11
@brandonwillard brandonwillard marked this pull request as ready for review December 2, 2022 23:25
@rlouf
Copy link
Member

rlouf commented Dec 4, 2022

This looks great, and now that it's cleaned up it looks more obvious to me what can be further improved.

Do you want to also go ahead and include the changes in #184 or do you want to leave it for future work?

More general comment

I am not convinced that in the simple situations AePPL currently supports we need to rewrite the graph at all, although it could be useful to have a "canonicalization" phase where we could rewrite transforms of RandomVariable when they return a known distribution (ratio of StandardNormalRV for instance). Here, the model graphs contain almost all the information we need. To be able to compute the model's logdensity without rewriting the graph we would also need:

  • To register every RandomVariable's logdensity.
  • To register the inverse transform of Aesara operators to compute the upstream value variables;
  • To register the way transforms act on logdensities.

However, pushed a little further, rewriting the graph could be very interesting for AeMCMC. We would need to replace every random and observed variable in the graph by their corresponding measure, including their domain and logdensity graph. That means starting from the known measures and pushing them through the transforms, thus getting a graph of Measures; transforms are then irrelevant if we also keep track of the relation between value variables in parallel. This is probably what we should be aiming for in these rewrites, instead of replacing Aesara operators with RVTransforms.

These changes make it possible for users to extend coverage of measurable
`Elemwise` transforms.  They also put these rewrites under the management of
Aesara's rewrite system, which means they can be applied more efficiently and
their use can customized and tracked with more granularity.

`sub`, `neg`, and `true_div` support is added, as well.
@brandonwillard
Copy link
Member Author

Do you want to also go ahead and include the changes in #184 or do you want to leave it for future work?

Let's do that one in a follow-up.

More general comment

Yes, there's a lot to rework here. I'm going to merge this one so that—in the meantime—our existing functionality is preserved and better designed. At the very least, it should be a lot easier to rewrite/redesign in this form compared to the current.

@brandonwillard brandonwillard merged commit 473c1e6 into aesara-devs:main Dec 5, 2022
@brandonwillard brandonwillard deleted the inverse_transform branch December 5, 2022 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request graph rewriting Involves the implementation of rewrites to Aesara graphs important This label is used to indicate priority over things not given this label refactoring A change that improves the codebase but doesn't necessarily introduce a new feature rv-transforms Involves transforms applied to random variables
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order of rewrites is only respected if kwarg position is used
2 participants