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 all 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
alias = AliasTable(p)
MultinomialSamplerSequential(
n,
k,
alias,
Vector{typeof(alias.at.mask)}(undef, n),
)
end
end


## Fit model
Expand Down
37 changes: 20 additions & 17 deletions src/samplers/multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,34 @@ 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{S} <: Sampleable{Multivariate,Discrete}
n::Int
k::Int
alias::AliasTable
scratch_alias_rng::Vector{S}
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)))
rand!(rng, s.scratch_alias_rng)

for r in s.scratch_alias_rng
x[AliasTables.sample(r, s.alias.at)] += 1
end
return x
end

length(s::MultinomialSampler) = length(s.prob)
length(s::MultinomialSamplerBinomial) = s.k
length(s::MultinomialSamplerSequential) = s.k
Loading