-
Notifications
You must be signed in to change notification settings - Fork 37
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
Implement optional sparse Gaussian covariance #58
Implement optional sparse Gaussian covariance #58
Conversation
I don't see any obvious error message in the output of the failing style check. Any ideas? |
^^' ok, fixed the issues. It's because I included |
Thanks @EiffL. I did follow the instructions in the design doc, but I hadn't used |
My bad. Thanks for the added documentation! Only comment, is that we might want to move |
This isn't one of the sparse formats supported by Note that we could store Gaussian covariances efficiently using the dia_matrix format, but most of the speedup in the linear algebra comes from the block structure which dia_matrix doesn't use. |
yeah just wanted to highlight that aspect but indeed this is slightly different. I'm very ok with your current solution. |
I was in the middle of implementing the challenge metrics with these new tools. Because my brain is fried I got stuck on this part of the Fisher computation ^^' originally here: https://github.com/LSSTDESC/tomo_challenge/blob/280a5a566158e8775c91ea3cf297b27c5232fd38/tomo_challenge/jax_metrics.py#L159 mu = mean(fid_params)
dmu = jac_mean(fid_params)
# Compute the covariance matrix
cl_noise = jc.angular_cl.noise_cl(ell, probes)
C = jc.angular_cl.gaussian_cl_covariance(ell, probes, mu, cl_noise)
invCov = np.linalg.inv(C)
# Compute Fisher matrix for constant covariance
F = np.einsum('pa,pq,qb->ab', dmu, invCov, dmu) Any ideas for an elegant way to do this einsum at the end when invCov is sparse? |
Isn't that just Btw, why do you assume constant covariance? |
I implemented the sparse determinant, which was trickier than I expected. I was never able to eliminate the outer python loop using any of the lax flow control structures, but any tips are welcome! The computations within each loop can be done in parallel, so it seems a shame to force sequential flow with the for loop. |
I was assuming constant covariance as this is what the challenge metric was using. I think that's all that the Fisher sampler in cosmosis can handle. I don't have issues with adding cosmology dependent covariance, which would be the right thing to do ^^' but that's a discussion for the tomo_challenge thread. I'm gonna have a look at your sparse det :-) |
I implemented a
or to use the compiled kernel directly (without any input validation):
|
I am marking this "ready for review" now but let me know if you think anything is still missing or could be improved. |
I just ran some timing benchmarks on colab of the likelihood calculation in
So we see a nice speed up with an accelerator, in addition to the smaller memory footprint (50x in this case). At this point, the cl calculation is the tall pole. |
It all looks good to me, I'm just finishing testing that I don't get any problems when computing derivatives of FoM. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything works :-) I just made one suggestion on the code, another small suggestion would be to add the code for sparse likelihood evaluation in
jax_cosmo/jax_cosmo/likelihood.py
Line 12 in 99edb4f
def gaussian_log_likelihood(data, mu, C, constant_cov=True, inverse_method="inverse"): |
I am working on updating Should we be using |
I've given it some thought, I think maybe let's not put the stop gradient in the likelihood, and instead I've renamed the option to just specify if yes or no the log determinant is computed, and added some documentation. Regading |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good! I'm approving, thanks so much @dkirkby for this!
@all-contributors please add @dkirkby for code |
I've put up a pull request to add @dkirkby! 🎉 |
Add a
sparse=False
option toangular_cl.gaussian_cl_covariance
andangular_cl.gaussian_cl_covariance_and_mean
that returns a sparse representation of the covariance matrix that uses a factor n_ell less memory. Specifically, the sparse representation has shape (n_cls, n_cls, n_ell) compared with the dense shape (n_cls * n_ell, n_cls * n_ell).Add a
sparse
module to implement efficient linear algebra operations on this sparse representation. Currently, there isto_dense
,inv
andvecdot
. I still need to implementdet
which is more complicated. These are generally a factor ~n_ell faster than their dense equivalents (on a CPU at least).