-
Notifications
You must be signed in to change notification settings - Fork 418
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
JET reports type-instability when sampling from Beta
#1866
Comments
The reason for this observation is that |
Ok, that explains a bit the situation. The real issue is that sampler(d::Beta) = BetaSampler(d, other_fields...) # type-stable, always returns the same
function rand!(rng, sampler::BetaSampler, container)
if some_condition(sampler)
one_algorithm!(rng, container)
else
another_algorithm!(rng, container)
end
end Or a specialized method for function rand!(rng, dist::Beta, n)
if some_condition(dist)
rand!(rng, OnerBetaSampler(dist), n)
else
rand!(rng, AnotherBetaSampler(dist), n)
end
end |
The motivation for |
I still wonder, is there any issue here? It seems the function barrier in #1350 fixed the performance issues. |
It shouldn't recompute algorithm-dependent quantities in my second proposal with a specialized To answer your question about the performance, yes, it does affect performance in the following case: julia> foo(x, dist) = foreach(1:100_000) do _
rand!(dist, x)
end
julia> x = zeros(10);
julia> dist = Beta(1, 1)
Beta{Float64}(α=1.0, β=1.0)
julia> sr = sampler(dist)
julia> @btime foo($x, $dist);
30.232 ms (100000 allocations: 6.10 MiB)
julia> @btime foo($x, $sr);
27.726 ms (0 allocations: 0 bytes) It's not much in this particular example, but it does accumulate in our larger program and it also allocates extra (while the whole idea of |
Another thing, I mentioned that sampling a single point is fine and is in fact type-stable. julia> @report_opt rand(Beta(1, 1))
No errors detected That suggests that there is a potential for improvement for the in-place version |
The example is a bit contrieved since ideally you would use the The single variate sampling does not use |
In the real code the distribution is not the same unfortunately |
Consider the following:
The
Normal
works just fineAlso sampling a single point is fine
This harms performance, when sampling "inplace" with Beta (with
rand!
)The text was updated successfully, but these errors were encountered: