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 @inbounds in alias table sampling #630

Closed
vancleve opened this issue Jan 7, 2021 · 4 comments · Fixed by #927
Closed

Add @inbounds in alias table sampling #630

vancleve opened this issue Jan 7, 2021 · 4 comments · Fixed by #927

Comments

@vancleve
Copy link

vancleve commented Jan 7, 2021

Seems like one could add @inbounds to this line

x[i] = rand(rng) < ap[j] ? a[j] : a[alias[j]]

In fact its here already when using alias table sampling for Categorical in Distributions:
https://github.com/JuliaStats/Distributions.jl/blob/d6de7214e6d84df77d1eb3ec7a98d23862d1232d/src/samplers/aliastable.jl#L19

@nalimilan
Copy link
Member

Sure, why not. Though it seems we need an additional check that the indices of a match those of x.

@ParadaCarleton ParadaCarleton closed this as not planned Won't fix, can't repro, duplicate, stale Oct 28, 2023
@nalimilan
Copy link
Member

@ParadaCarleton Can you argument on why you want to close this?

@nalimilan nalimilan reopened this Oct 28, 2023
@ParadaCarleton
Copy link
Contributor

@ParadaCarleton Can you argument on why you want to close this?

I don't like correctness bugs, and thought this was just hanging around because it was old enough to be before that Yuri post on all the correctness issues in JuliaStats packages.

@LilithHafner
Copy link
Contributor

This optimization would be unsafe because it is possible for make_alias_table! to produce invalid alias tables:

julia> StatsBase.alias_sample!(Random.default_rng(), rand(10), weights(fill(0, 10)), rand(10))
ERROR: BoundsError: attempt to access 10-element Vector{Float64} at index [281471800382896]
Stacktrace:
 [1] throw_boundserror(A::Vector{Float64}, I::Tuple{Int64})
   @ Base ./essentials.jl:14
 [2] getindex
   @ ./essentials.jl:891 [inlined]
 [3] alias_sample!(rng::TaskLocalRNG, a::Vector{Float64}, wv::Weights{Int64, Int64, Vector{Int64}}, x::Vector{Float64})
   @ StatsBase ~/.julia/packages/StatsBase/ebrT3/src/sampling.jl:729
 [4] top-level scope
   @ REPL[10]:1

With @inbounds this would be undefined behavior.

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 a pull request may close this issue.

4 participants