-
Notifications
You must be signed in to change notification settings - Fork 433
Make MatrixNormal sampling non-allocating #2012
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
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2012 +/- ##
=======================================
Coverage 86.36% 86.36%
=======================================
Files 146 146
Lines 8786 8788 +2
=======================================
+ Hits 7588 7590 +2
Misses 1198 1198 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Looks good to me. Could you add a test for it, to ensure that it doesn't regress silently? |
|
I added a test for this case. While I think that at least some, if not all, matrixvariate distributions can be made to sample without allocations, the tests would probably still need to be different for each one. For example, MatrixNormal will only avoid sampling if the |
Co-authored-by: David Müller-Widmann <[email protected]>
Co-authored-by: David Müller-Widmann <[email protected]>
|
So I tried to figure out why CI is failing on 1.10. On my machine with latest 1.10, it goes through, so I bisected and it turns out that it fails on 1.10.4 but passes on 1.10.5. Finding out what actually triggered the change was not very easy and I'm still not quite sure. julia> ((d, out) -> @allocated(rand!(d, out)))(noallocD, output)
32This is why the test fails. So let's do it step by step to see where it happens. julia> ((d, out) -> @allocated(randn!(out)))(noallocD, output)
0
julia> ((d, out) -> @allocated(cholesky(d.U)))(noallocD, output)
0Ok, so both the julia> A = cholesky(noallocD.U);
julia> ((A) -> @allocated(begin A.L; nothing end))(A)
16So here's the culprit: Accessing the julia> f = getfield(A, :factors);
julia> ((f) -> @allocated(LowerTriangular(f)))(f)
0Now that's tricky. All it has to do is to construct the julia> function LinearAlgebra.checksquare(A)
m,n = size(A)
m == n || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(A))"))
m
end;
julia> ((A) -> @allocated(begin A.L; nothing end))(A)
0So problem found. Of course, what this doesn't explain is why the allocation of this exception string plays a role at all, as it can be clearly seen in the The question is, what shall we do: Bump the target version (at least of the tests) to 1.10.5? |
Co-authored-by: David Müller-Widmann <[email protected]>
|
Thank you! |
Currently, sampling from
MatrixNormalalways allocates multiple temporaries: one of the matrixXthat contains the normally-sampled values, one for the matrix productA * X, and one for the matrix product(A * X) * B. However, given thatAandBare triangular (and square) matrices, all products can be done in-place and the sample can directly be stored to the output. So the method doesn't need to allocate at all, provided the Cholesky factors are available without allocation.