Replace np.linalg.inv with np.linalg.cholesky to speed up GPSampler for numpy>=2.0.0#6296
Merged
nabenabe0928 merged 5 commits intooptuna:masterfrom Oct 15, 2025
Conversation
not522
reviewed
Oct 10, 2025
Member
|
@kAIto47802 @not522 Could you review this PR? |
not522
reviewed
Oct 10, 2025
Co-authored-by: kAIto47802 <115693559+kAIto47802@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6296 +/- ##
==========================================
- Coverage 89.16% 89.12% -0.05%
==========================================
Files 209 209
Lines 13914 13925 +11
==========================================
+ Hits 12407 12410 +3
- Misses 1507 1515 +8 ☔ View full report in Codecov by Sentry. |
c05679d to
12f03d4
Compare
Contributor
Author
kAIto47802
approved these changes
Oct 15, 2025
Collaborator
kAIto47802
left a comment
There was a problem hiding this comment.
Thank you for the PR and update!
Indeed, even without any issues in NumPy (BLAS), it is numerically recommended to use Cholesky decomposition instead of explicitly computing the inverse matrix.
LGTM!
This was referenced Oct 16, 2025
Open
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR resolves the following issue:
GPSamplersuddenly slows down oncen_trialshits exactly 100 #6230See also the issue in NumPy:
Basically,
np.linalg.invsuddenly slows down aftercov_Y_Y.shapeexceeds(100, 100).We avoid this issue by using
np.linalg.cholesky.Here are some backgrounds.$C \in \mathbb{R}^{N \times N}$ as the kernel matrix with the addition of a noise at the diagonal elements and $k = [k(x^\star, x_1), \dots, k(x^\star, x_N)]\in \mathbb{N}$ as the kernel vector at a new point $x^\star$ .$k^\top C^{-1} k$ , necessitating the matrix inversion in our current implementation.$C$ is a positive definite matrix owing to the kernel matrix nature, we can indeed rewrite $v = C^{-1} k$ as $k = C v = L L^\top v$ where $L \in \mathbb{R}^{N \times N}$ is the lower triangular matrix yielded by the Cholesky decomposition of $C$ .$u = L^\top v$ by solving $L u = k$ and then obtain $v$ by solving $L^\top v = u$ .$O(N^2)$ because $L$ is a triangular matrix.
Let's denote
The Gaussian process infers the posterior variance using
However, as
We first obtain
Please note that each linear system can be solved with the time complexity of
By doing so, we can avoid the matrix inversion, leading to a significant speedup.
The by-product of this modification is the numerical stability.
The speedup effect can be found below:
Description of the changes
np.linalg.invwithnp.linalg.cholesky