-
Notifications
You must be signed in to change notification settings - Fork 419
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
GammaIPSampler rand: avoid recursion #1615
Conversation
The recursion here seems to prevent Julia from inferring that `rand(::FixedRNG, ::Gamma)` terminates, preventing constant folding. Eliminating it is as simple as short-circuiting to GammaGDSampler. This is correct because the shape parameter here is one more than with the original Gamma distribution, so it must be greater than one, assuming it was positive to begin with. Note that I'm not an expert and in fact I'm having much trouble with passing Probability&Statistics, so there might be something I'm not taking into account.
Inspired by this Discourse discussion: https://discourse.julialang.org/t/constant-propagation-irrationals-doing-much-better-than-float64/87142/ Note that the pull request doesn't actually accomplish the goal of making Before my patches:
After (now we have "+t"):
|
Codecov ReportBase: 85.94% // Head: 85.94% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #1615 +/- ##
=======================================
Coverage 85.94% 85.94%
=======================================
Files 129 129
Lines 8080 8080
=======================================
Hits 6944 6944
Misses 1136 1136
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. |
What's |
It's just an example static RNG from the linked Discourse post, also defined in my previous message.
I don't know. When making the commit message, I didn't realize that I refer to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the motivation for this change is a bit unclear. However, there's actually a bug here but IMO it should be fixed differently (see my comment).
@@ -205,7 +205,7 @@ end | |||
GammaIPSampler(d::Gamma) = GammaIPSampler(d,GammaMTSampler) | |||
|
|||
function rand(rng::AbstractRNG, s::GammaIPSampler) | |||
x = rand(rng, s.s) | |||
x = rand(rng, GammaGDSampler(s.s)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think this change is undesirable - but the current implementation is as well. The motivation for samplers is to pre-compute and pre-allocate everything required for sampling since it should pay off when you want to generate many samples. Hence by default, distributions only use dedicated samplers if multiple samples are requested (similar to the concept in Random).
So s.s
should already be a sampler at this point here. However, it seems that's currently not the case - even though it seems that was the intention behing
Distributions.jl/src/samplers/gamma.jl
Lines 202 to 205 in e67d6b3
function GammaIPSampler(d::Gamma,::Type{S}) where S<:Sampleable | |
GammaIPSampler(Gamma(1.0 + shape(d), scale(d)), -1.0 / shape(d)) | |
end | |
GammaIPSampler(d::Gamma) = GammaIPSampler(d,GammaMTSampler) |
S
though (and that it should be designed a bit differently probably).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I noticed that S
and GammaMTSampler
are unused. I think I found some issues on this repo from like eight years ago that mention that GammaMTSampler was "disabled".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That comment does not refer to these lines, in any case. This bug was already present when GammaIPSampler
was added: #313
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bug in the implementaiton of GammaMTSampler
, fixing it improves performance significantly: #1617 (comment)
I close this PR in favour of #1617. Generally, we don't want to construct the sampler in the |
The recursion here seems to prevent Julia from inferring that
rand(::FixedRNG, ::Gamma)
terminates, preventing constant folding.Eliminating it is as simple as short-circuiting to GammaGDSampler. This is correct because the shape parameter here is one more than with the original Gamma distribution, so it must be greater than one, assuming it was positive to begin with.
Note that I'm not an expert and in fact I'm having much trouble with passing Probability&Statistics, so there might be something I'm not taking into account.