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

Add flag to disable bounds check for speed-up #4377

Merged
merged 6 commits into from
Dec 23, 2020
Merged

Conversation

twiecki
Copy link
Member

@twiecki twiecki commented Dec 23, 2020

Closes #4376.

Depending on what your PR does, here are a few things you might want to address in the description:

  • what are the (breaking) changes that this PR makes?
    None.
  • important background, or details about the implementation
    Disabling bounds checks gave quite a speed-up (66%) in my experiments.
  • are the changes—especially new features—covered by tests and docstrings?
  • consider adding/updating relevant example notebooks
  • right before it's ready to merge, mention the PR in the RELEASE-NOTES.md

Copy link
Member

@ColCarroll ColCarroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be interested in seeing an example where this can (should?) be enabled, and one where it shouldn't. Sounds too good to be true!

For instance, it is not clear to me what to do with

with pm.Model():
    sd = pm.HalfNormal('sd', 1)
    obs = pm.Normal('obs', 0, sd)

I would guess that the automatic log-transform of sd would mean that I could disable bounds checking?

pymc3/distributions/dist_math.py Outdated Show resolved Hide resolved
@twiecki
Copy link
Member Author

twiecki commented Dec 23, 2020

@ColCarroll Yes, you can disable that for basically any well-specified model.

@@ -67,6 +68,12 @@ def bound(logp, *conditions, **kwargs):
-------
logp with elements set to -inf where any condition is False
"""

# If called inside a model context, see if bounds check is disabled
model = modelcontext(kwargs.get("model"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im more curious than opposed, but why not just skip the bound check where its called instead of no opping in the check bounds function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we would need to add an if-clause to every Distribution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh yea that would suck. This is better

@codecov
Copy link

codecov bot commented Dec 23, 2020

Codecov Report

Merging #4377 (8af44f2) into master (0402aab) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4377   +/-   ##
=======================================
  Coverage   87.95%   87.96%           
=======================================
  Files          88       88           
  Lines       14493    14501    +8     
=======================================
+ Hits        12748    12756    +8     
  Misses       1745     1745           
Impacted Files Coverage Δ
pymc3/distributions/dist_math.py 92.17% <100.00%> (+0.20%) ⬆️
pymc3/model.py 89.29% <100.00%> (+0.01%) ⬆️

@twiecki
Copy link
Member Author

twiecki commented Dec 23, 2020

OK, this is ready for review.

@ColCarroll
Copy link
Member

I can review later today if no one else gets to it -- any chance you can screenshot a usage example, showing the speedup (i'd imagine you fit the same model twice, and just set the flag to True and then False)? No big deal if not, but it'd be nice to be able to point to!

@twiecki
Copy link
Member Author

twiecki commented Dec 23, 2020

image

image

image

@ColCarroll
Copy link
Member

Looks great! I guess there should be general encouragement to enable this for a few weeks, then maybe we think about enabling by default?

@ColCarroll ColCarroll merged commit 37239fc into master Dec 23, 2020
@ColCarroll ColCarroll deleted the disable_bounds_check branch December 23, 2020 23:39
@twiecki
Copy link
Member Author

twiecki commented Dec 24, 2020

Hmmm, thinking I should have just called this bounds_check=False.

@ricardoV94
Copy link
Member

Or check_bounds?

@twiecki
Copy link
Member Author

twiecki commented Dec 24, 2020

Oh, yeah.

bsmith89 pushed a commit to bsmith89/pymc3 that referenced this pull request Dec 29, 2020
* Add flag to disable bounds check.

* Move check for model and flag into single line as per @colcarrolls's suggestion.

* modelcontext raises a TypeError if no model is found. Catch that.

* Add comment.

* Add mention in release-notes.

* Add test for when boundaries are disabled.
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.

66% speed-up by removing bound checks
4 participants