-
Notifications
You must be signed in to change notification settings - Fork 421
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
Inconsistent performance between logpdf and logpdf! for MvNormal. #1775
Comments
That's extremely weird. Do you know what's causing the performance difference? |
It's interesting to note that this difference does not exist if using LinearAlgebra, Random, Distributions, BenchmarkTools, Test
samples = [randn(5) for _ in 1:1000]
dist = MvNormal(zeros(5), I)
function mod_logpdf(dist, samples)
out = Array{Float64}(undef, len(samples))
logpdf!(out, dist, samples)
end julia> @btime logpdf($dist, $samples);
89.132 μs (1001 allocations: 101.69 KiB)
julia> @btime mod_logpdf($dist, $samples);
88.147 μs (1001 allocations: 101.69 KiB) So solely for the case where
function _logpdf!(r::AbstractArray{<:Real}, d::AbstractMvNormal, x::AbstractMatrix{<:Real})
sqmahal!(r, d, x)
c0 = mvnormal_c0(d)
for i = 1:size(x, 2)
@inbounds r[i] = c0 - r[i]/2
end
r
end I'm guessing this function is the cause of this discrepancy, although why this is faster than the other methods, I do not know. |
Could you run a profiler on this? You w should then see where it is spending the additional time. |
For
|
Hmm, do you have a flamegraph? |
Ahh, I was suggesting you might want to look at the flamegraphs to see which lines specifically are the ones slowing down Where is it spending most of its time? |
Sorry, my bad XD. This is the profile view plot for Most of the time seems to be taken up by |
The factorization is only computed once upfront when you construct an |
Yep, that would be it. It's creating way more arrays. Could you make a PR to fix this? |
Yes I can. But just a question, is it problematic if |
No, ideally we would not mix both paths, also eg for better compatibility with static arrays. Even though probably currently many methods don't work (in an optimized way) with static arrays. |
Another reason is that generally it is quite challenging and brittle when one starts to come up with heuristics for the output type. |
Sorry, I don't understand whether you meant it's not a problem for |
I meant that generally |
Ok, thanks for the clarification. Then I'm afraid I don't know a solution to this. |
Quick question, is |
Problem
Consider the following minimal example.
logpdf
is around 7x slower thanmod_logpdf
, even though they both do exactly the same thing.Possible solution
Add a method
that does something like
mod_logpdf
.The text was updated successfully, but these errors were encountered: