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

perf improvements to MultinomialSampler #1834

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/multivariate/multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,26 @@ end
_rand!(rng::AbstractRNG, d::Multinomial, x::AbstractVector{<:Real}) =
multinom_rand!(rng, ntrials(d), probs(d), x)

sampler(d::Multinomial) = MultinomialSampler(ntrials(d), probs(d))
function sampler(d::Multinomial)
n = ntrials(d)
p = probs(d)
k = length(p)

# the constant λ term should be proportional to the perfomance ratio of
# constructing and sampling from a Binomial vs sampling from an AliasTable
λ = 10
return if n > λ * k
MultinomialSamplerBinomial(n, k, p)
else
MultinomialSamplerSequential(
n,
k,
AliasTable(p),
Vector{Int}(undef, n),
similar(p, n)
)
end
end


## Fit model
Expand Down
41 changes: 24 additions & 17 deletions src/samplers/multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Member

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.

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))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe more generically (and safer given that you use @inbounds below):

Suggested change
rand!(rng, s.scratch_idx, 1:length(at.alias))
rand!(rng, s.scratch_idx, eachindex(at.alias, at.accept))

rand!(rng, s.scratch_acc)

@inbounds for i = 1:s.n
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't

Suggested change
@inbounds for i = 1:s.n
@inbounds for (i, acc) in zip(s.scratch_idx, s.scratch_acc)

be simpler?

i2 = s.scratch_idx[i] % Int
Copy link
Member

Choose a reason for hiding this comment

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

s.scratch_idx[i] is always an Int, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 rand(::AliasTable) implementation

i = rand(rng, 1:length(s.alias)) % Int

will remove this % Int

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
Loading