Skip to content

Reduce allocations of cov(::MultivariateMixture) #1967

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

Merged
merged 6 commits into from
Apr 16, 2025
Merged

Conversation

devmotion
Copy link
Member

@devmotion devmotion commented Apr 11, 2025

The PR reduces allocations of cov(::MultivatiateMixture) (addresses a TODO item).

@codecov-commenter
Copy link

codecov-commenter commented Apr 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.27%. Comparing base (f33cc10) to head (a065a65).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1967      +/-   ##
==========================================
+ Coverage   86.26%   86.27%   +0.01%     
==========================================
  Files         146      146              
  Lines        8763     8778      +15     
==========================================
+ Hits         7559     7573      +14     
- Misses       1204     1205       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devmotion devmotion changed the title Fix MixtureModel with ScalMat covariances Reduce allocations of cov(::MultivariateMixture) Apr 13, 2025
@devmotion
Copy link
Member Author

Would you be able to quickly review this small PR @jishnub?

@jishnub
Copy link
Contributor

jishnub commented Apr 13, 2025

Looks fine to me. I suppose copytri! is internal to LinearAlgebra, but there isn't a public function to perform this operation.

md = mean(c) - m
axpy!(pi, md*md', V)
md .= mean(c) .- m
BLAS.syr!('U', pi, md, V)
Copy link
Contributor

@jishnub jishnub Apr 13, 2025

Choose a reason for hiding this comment

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

Should the pi be cast to the appropriate type here? I think BLAS expects floating-point numbers. But then, if tests pass, probably this is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

In principle, I'd like to avoid BLAS calls even though md and V are (currently) Arrays. But it seems mul!(::Symmetric, ::Vector, ::Adjoint, ...) is not supported and according to a benchmark on my machine repeated generic mul! calls can be slower than syr! and a single copytri!... Everything apart from pi is hardcoded to Float64 currently though (something to be fixed as well), so

Suggested change
BLAS.syr!('U', pi, md, V)
BLAS.syr!('U', Float64(pi), md, V)

should make it safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I agree that it is a bit unfortunate that there isn't a higher level API for either of these. This should be fine for now with the change above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a test to cover this line? The fact that the tests passed without this change seems to suggest that this isn't covered.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll check. I assume that - as in many tests - only distributions with Float64 parameters are tested. Here, eg, tests with a multivariate mixture with mixture probabilities of type Float64 would have passed even without the change.

Copy link
Member Author

Choose a reason for hiding this comment

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

That was indeed the case, it was only tested with pi::Float64. I added a test with Float32 in a065a65 that would error without the Float64(pi) change.

@devmotion
Copy link
Member Author

I suppose copytri! is internal to LinearAlgebra, but there isn't a public function to perform this operation.

Indeed, I used it only since Distributions already uses it in a few other places.

@devmotion devmotion merged commit b6b1535 into master Apr 16, 2025
15 checks passed
@devmotion devmotion deleted the devmotion-patch-1 branch April 16, 2025 08:26
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.

3 participants