Skip to content

ENH Ridge: support solver="cholesky" for array API inputs - #35418

Merged
OmarManzoor merged 13 commits into
scikit-learn:mainfrom
cakedev0:ridge-cholesky-array-api
Nov 20, 2026
Merged

OmarManzoor merged 13 commits into
scikit-learn:mainfrom
cakedev0:ridge-cholesky-array-api

Conversation

@cakedev0

@cakedev0 cakedev0 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Implements option B of #35060

What does this implement/fix? Explain your changes.

Ridge(solver="cholesky") now works with array API inputs, and solver="auto" resolves to "cholesky" for them, like for NumPy inputs (the warning about "svd" is gone).

  • The Gram matrix (X.T @ X, or X @ X.T when n_features > n_samples) and the solve both stay on the device. The solve uses xp.linalg.solve, so a LU decomposition.
  • alpha is added to the diagonal in place with A[idx, idx] += alpha when the namespace supports it (torch, cupy, dpnp). This is not in the array API. The alternative (and fallback) is A += alpha * xp.eye(n) and I don't like making memory allocation, so I made this fast-path to avoid it. WDYT?
  • Singular systems: like for NumPy inputs, Ridge falls back to the SVD solver. Namespaces signal a singular matrix differently (torch and dpnp raise their own LinAlgError, cupy returns non-finite values), so _solve_spd turns the three cases into a private exception caught in _ridge_regression.

AI usage disclosure

Code and tests generated by Claude Code, reviewed by me (not for the tests yet).

Benchmarks

In float32 the speed-up compared to CPU is really nice (except on MPS): NVIDIA L4 & Intel Arc B580: 3.5x to 15x faster (and tremendously faster than the SVD solver).

On a MacBook M2 (MPS) it is also faster than the SVD solver but not faster than NumPy Cholesky (the 10 s for 5,000 x 40,000 comes from a known torch slowdown on M1/M2: since torch 2.14, MPS matmuls with an inner dimension above 32,768 use a slower kernel for correctness, see pytorch/pytorch#198182).

In float64, it's not much faster than CPU.

Full tables:

Fit times in seconds, Ridge(alpha=1.0), one target, standard normal data, best of 3 after a warmup. "xp svd" is what array API inputs get on main. The NumPy column runs on the CPU of the same machine.

float32

machine shape NumPy cholesky xp svd torch cholesky cupy / dpnp cholesky
L4 (NumPy on 8 vCPUs) 500,000 x 100 0.12 0.091 0.011 0.021
50,000 x 1000 0.24 0.22 0.018 0.019
463,810 x 990 1.9 not run 0.15 0.15
2,500 x 20,000 0.48 not run 0.040 0.043
5,000 x 40,000 3.1 not run 0.26 0.26
Arc B580 (NumPy on 24 cores) 500,000 x 100 0.12 0.43 0.0079 0.016
50,000 x 1000 0.10 0.74 0.017 0.017
463,810 x 990 0.91 not run 0.13 0.14
2,500 x 20,000 0.17 not run 0.042 0.031
5,000 x 40,000 0.99 not run 0.28 0.17
M2 (MPS, NumPy on 8 cores) 500,000 x 100 0.10 1.1 0.098
50,000 x 1000 0.22 1.8 0.40
2,500 x 20,000 0.50 not run 0.38
5,000 x 40,000 3.5 not run 10

float64 (not run on the M2: MPS has no float64)

machine shape NumPy cholesky xp svd torch cholesky cupy / dpnp cholesky
L4 (NumPy on 8 vCPUs) 500,000 x 100 0.21 0.45 0.060 0.069
50,000 x 1000 0.43 2.3 0.28 0.28
463,810 x 990 3.7 not run 2.5 2.5
2,500 x 20,000 0.94 not run 0.69 0.69
5,000 x 40,000 6.1 not run 5.3 5.3
Arc B580 (NumPy on 24 cores) 500,000 x 100 0.23 0.88 0.030 0.032
50,000 x 1000 0.18 1.4 0.14 0.099
463,810 x 990 1.7 not run 1.2 0.84
2,500 x 20,000 0.33 not run 0.35 0.22
5,000 x 40,000 1.9 not run 2.6 1.5

Comment thread sklearn/linear_model/_ridge.py Resolved
@cakedev0
cakedev0 marked this pull request as ready for review November 16, 2026 11:30
@cakedev0 cakedev0 added this to Labs Nov 16, 2026
@cakedev0 cakedev0 moved this to PR waiting for reviews in Labs Nov 16, 2026
@github-actions github-actions Bot removed the CUDA CI label Nov 16, 2026
@ogrisel

ogrisel commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

@github-actions github-actions Bot removed the CUDA CI label Nov 16, 2026
@ogrisel ogrisel changed the title ENH Ridge: support solver="cholesky" for array API inputs ENH Ridge: support solver="cholesky" for array API inputs Nov 16, 2026

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks very much @cakedev0. Here is a first pass of feedback.

I like the _SingularMatrixError / _solve_spd trick to handle the discrepancies between libraries while keeping the code base as library agnostic as possible.

Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved
Comment thread sklearn/linear_model/_ridge.py Resolved

@ogrisel ogrisel left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@lorentzenchr I think _solve_spd might be helpful to add array API support to newton-cholesky on GLMs in a follow-up.

@ogrisel

ogrisel commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

BTW, I tried on Apple M4 with PyTorch:

  • the Cholesky solver is ~2x faster with MPS than with CPU;
  • the Cholesky solver is 18x faster than the SVD solver on MPS (on a 100k rows x 300 cols).

@OmarManzoor OmarManzoor left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @cakedev0

@OmarManzoor
OmarManzoor merged commit 2dd5e2c into scikit-learn:main Nov 20, 2026
38 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Nov 20, 2026
@cakedev0

Copy link
Copy Markdown
Member Author

Cool to have it for the release!! 🎉

@ogrisel

ogrisel commented Nov 20, 2026

Copy link
Copy Markdown
Member

@cakedev0 looking forward to see the updated benchmark dashboards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants