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

Make _params_to_array faster #1043

Merged
merged 5 commits into from
Jan 5, 2020
Merged

Make _params_to_array faster #1043

merged 5 commits into from
Jan 5, 2020

Conversation

mohamed82008
Copy link
Member

This PR optimizes the _params_to_array function. In the following example, the running time of sample was cut down from 22 s to 8 s. Of these times, < 2 s is spent doing the sampling and the rest is saving the chain. There is a lot of room for improvement here. The 50000 iterations case terminates in 134 s now of which 16 s is sampling. Before this PR, it was too slow to even wait and time it.

using Turing

fake_dat = [5*ones(Int64, 30) for j=1:18]
fake_N = fill(5*30, 18) #Total N

@model multinomial_RE(data, N, ::Type{T}=Vector{Float64}) where {T} = begin
    B = length(data)
    R = length(data[1,1])
    theta = [T(undef, R) for j = 1:B]
    beta ~ Dirichlet(R, 1)
    alpha ~ truncated(Normal(1, 100), 0, Inf)
    for b in 1:B
    	theta[b] ~ Dirichlet(beta * alpha)
    	if any(isnan, theta[b])
    		@logpdf() = -Inf
    		return
    	end
    	data[b] ~ Multinomial(N[b], theta[b])
    end
end
model2 = sample(multinomial_RE(fake_dat, fake_N), MH(), 5000);
@time model2 = sample(multinomial_RE(fake_dat, fake_N), MH(), 5000);

src/utilities/helper.jl Outdated Show resolved Hide resolved
src/utilities/helper.jl Outdated Show resolved Hide resolved
src/utilities/helper.jl Outdated Show resolved Hide resolved
@mohamed82008
Copy link
Member Author

Thanks @devmotion! I have this impulse to add @inline next to small recursive functions I write which I should probably control :)

@yebai yebai requested a review from cpfiffer January 5, 2020 18:31
Copy link
Member

@cpfiffer cpfiffer left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@yebai
Copy link
Member

yebai commented Jan 5, 2020

Looks like we need a rebase before merging.

@mohamed82008
Copy link
Member Author

Yes I was planning to try to make this even faster, but we can merge for now after the rebase and I will make another PR.

@mohamed82008
Copy link
Member Author

I messed up the rebase. Let me fix the mess.

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 this pull request may close these issues.

4 participants