Skip to content

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
nabenabe0928:replace-linalg-inv-with-linalg-cholesky
Oct 15, 2025
Merged

Replace np.linalg.inv with np.linalg.cholesky to speed up GPSampler for numpy>=2.0.0#6296
nabenabe0928 merged 5 commits intooptuna:masterfrom
nabenabe0928:replace-linalg-inv-with-linalg-cholesky

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 commented Oct 9, 2025

Motivation

This PR resolves the following issue:

See also the issue in NumPy:

Basically, np.linalg.inv suddenly slows down after cov_Y_Y.shape exceeds (100, 100).
We avoid this issue by using np.linalg.cholesky.

Here are some backgrounds.
Let's denote $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$.
The Gaussian process infers the posterior variance using $k^\top C^{-1} k$, necessitating the matrix inversion in our current implementation.
However, as $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$.
We first obtain $u = L^\top v$ by solving $L u = k$ and then obtain $v$ by solving $L^\top v = u$.
Please note that each linear system can be solved with the time complexity of $O(N^2)$ because $L$ is a triangular matrix.

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:

bench

Description of the changes

  • Replace np.linalg.inv with np.linalg.cholesky

@nabenabe0928 nabenabe0928 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Oct 9, 2025
@nabenabe0928 nabenabe0928 marked this pull request as ready for review October 9, 2025 02:42
@c-bata
Copy link
Copy Markdown
Member

c-bata commented Oct 10, 2025

@kAIto47802 @not522 Could you review this PR?

Co-authored-by: kAIto47802 <115693559+kAIto47802@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.12%. Comparing base (84d3223) to head (055a29f).
⚠️ Report is 17 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@nabenabe0928 nabenabe0928 force-pushed the replace-linalg-inv-with-linalg-cholesky branch from c05679d to 12f03d4 Compare October 12, 2025 17:55
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 removed their assignment Oct 14, 2025
Copy link
Copy Markdown
Collaborator

@kAIto47802 kAIto47802 left a comment

Choose a reason for hiding this comment

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

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!

@kAIto47802 kAIto47802 removed their assignment Oct 15, 2025
@nabenabe0928 nabenabe0928 merged commit c090d59 into optuna:master Oct 15, 2025
19 of 22 checks passed
@not522 not522 added this to the v4.6.0 milestone Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants