-
Notifications
You must be signed in to change notification settings - Fork 414
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
perf improvements to MultinomialSampler #1834
Changes from 4 commits
d721094
6c4d19a
a12a1a0
22bcfb2
6821dec
95dbff1
5945b45
5f39ae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,31 +39,38 @@ function multinom_rand!(rng::AbstractRNG, n::Int, p::AbstractVector{<:Real}, | |||||
return x | ||||||
end | ||||||
|
||||||
struct MultinomialSampler{T<:Real} <: Sampleable{Multivariate,Discrete} | ||||||
struct MultinomialSamplerBinomial{T<:Real} <: Sampleable{Multivariate,Discrete} | ||||||
n::Int | ||||||
k::Int | ||||||
prob::Vector{T} | ||||||
end | ||||||
|
||||||
struct MultinomialSamplerSequential{T<:Real} <: Sampleable{Multivariate,Discrete} | ||||||
n::Int | ||||||
k::Int | ||||||
alias::AliasTable | ||||||
scratch_idx::Vector{Int} | ||||||
scratch_acc::Vector{T} | ||||||
end | ||||||
|
||||||
function MultinomialSampler(n::Int, prob::Vector{<:Real}) | ||||||
return MultinomialSampler(n, prob, AliasTable(prob)) | ||||||
Base.@deprecate MultinomialSampler(n::Int, prob::Vector{<:Real}) MultinomialSamplerBinomial(n, length(prob), prob) false | ||||||
|
||||||
function _rand!(rng::AbstractRNG, s::MultinomialSamplerBinomial, x::AbstractVector{<:Real}) | ||||||
multinom_rand!(rng, s.n, s.prob, x) | ||||||
end | ||||||
|
||||||
function _rand!(rng::AbstractRNG, s::MultinomialSampler, | ||||||
x::AbstractVector{<:Real}) | ||||||
n = s.n | ||||||
k = length(s) | ||||||
if n^2 > k | ||||||
multinom_rand!(rng, n, s.prob, x) | ||||||
else | ||||||
# Use an alias table | ||||||
fill!(x, zero(eltype(x))) | ||||||
a = s.alias | ||||||
for i = 1:n | ||||||
x[rand(rng, a)] += 1 | ||||||
end | ||||||
function _rand!(rng::AbstractRNG, s::MultinomialSamplerSequential, x::AbstractVector{<:Real}) | ||||||
fill!(x, zero(eltype(x))) | ||||||
at = s.alias | ||||||
rand!(rng, s.scratch_idx, 1:length(at.alias)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe more generically (and safer given that you use
Suggested change
|
||||||
rand!(rng, s.scratch_acc) | ||||||
|
||||||
@inbounds for i = 1:s.n | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't
Suggested change
be simpler? |
||||||
i2 = s.scratch_idx[i] % Int | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. should be. though I had just followed what was existing in the
will remove this |
||||||
x[ifelse(s.scratch_acc[i] < at.accept[i2], i2, at.alias[i2])] += 1 | ||||||
end | ||||||
return x | ||||||
end | ||||||
|
||||||
length(s::MultinomialSampler) = length(s.prob) | ||||||
length(s::MultinomialSamplerBinomial) = s.k | ||||||
length(s::MultinomialSamplerSequential) = s.k |
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.
acc
because of "accept"? Maybe the name could be a bit clearer.