aesara-devs / aeppl

Tools for an Aesara-based PPL.
https://aeppl.readthedocs.io
MIT License
64 stars 20 forks source link

Refactor `MeasurableElemwise` and its use #208

Closed brandonwillard closed 1 year ago

brandonwillard commented 1 year ago

This PR refactors the design of MeasurableElemwise and the rewrites that use it (i.e. the changes mostly introduced by https://github.com/aesara-devs/aeppl/pull/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

codecov[bot] commented 1 year ago

Codecov Report

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

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 ```diff @@ 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](https://codecov.io/gh/aesara-devs/aeppl/pull/208?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs) | Coverage Δ | | |---|---|---| | [aeppl/cumsum.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvY3Vtc3VtLnB5) | `100.00% <ø> (ø)` | | | [aeppl/joint\_logprob.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvam9pbnRfbG9ncHJvYi5weQ==) | `95.00% <ø> (ø)` | | | [aeppl/mixture.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvbWl4dHVyZS5weQ==) | `96.59% <ø> (ø)` | | | [aeppl/tensor.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvdGVuc29yLnB5) | `85.84% <ø> (ø)` | | | [aeppl/transforms.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvdHJhbnNmb3Jtcy5weQ==) | `96.45% <98.70%> (+0.20%)` | :arrow_up: | | [aeppl/abstract.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvYWJzdHJhY3QucHk=) | `100.00% <100.00%> (ø)` | | | [aeppl/censoring.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvY2Vuc29yaW5nLnB5) | `96.22% <100.00%> (+0.22%)` | :arrow_up: | | [aeppl/rewriting.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvcmV3cml0aW5nLnB5) | `94.17% <100.00%> (ø)` | | | [aeppl/scan.py](https://codecov.io/gh/aesara-devs/aeppl/pull/208/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs#diff-YWVwcGwvc2Nhbi5weQ==) | `94.73% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aesara-devs)

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

rlouf commented 1 year ago

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:

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.

brandonwillard commented 1 year ago

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.