Repository navigation
Description
Warning
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Credit to @ogrisel for the idea.
Describe the workflow you want to enable
With array API dispatch, Ridge only supports solver="svd", and solver="auto" resolves to "svd" with a warning (#27800 started with SVD only). The SVD is much more expensive than the Cholesky path used for NumPy inputs, so Ridge on GPU is often slower than Ridge on CPU with NumPy, and sometimes much slower.
I'd like solver="cholesky" (and so solver="auto") to work with array API inputs, and be fast on GPU.
Describe your proposed solution
Split the Cholesky solver in two: compute the Gram matrix on the device (X.T @ X, or X @ X.T in the dual case when n_features > n_samples), move it to the host, and solve it with the existing NumPy/SciPy code. Then move the coefficients back to the device:
def _solve_cholesky_array_api(X, y, alpha, xp):
n_samples, n_features = X.shape
dual = n_features > n_samples
G = X @ X.T if dual else X.T @ X # the expensive part, on the device
rhs = y if dual else X.T @ y
G, rhs = _convert_to_numpy(G, xp), _convert_to_numpy(rhs, xp)
# same solve as for NumPy inputs: scipy.linalg.solve(..., assume_a="pos")
if dual:
coef = _solve_cholesky_kernel(G, rhs, alpha)
else:
coef = _solve_cholesky_gram(G, rhs, alpha) # _solve_cholesky, minus the Gram matrix
coef = xp.asarray(coef, device=device(X))
if dual:
coef = X.T @ coef
return coefLet's call this policy A. The system to solve is only (n_features, n_features) (or (n_samples, n_samples)), so the host solve and the transfers are cheap compared to the Gram matrix. And the solve behaves exactly like for NumPy inputs, whatever the namespace: same SPD solver, same LinAlgError handling for singular systems.
solver="auto" could then resolve to "cholesky" for array API inputs, like for NumPy inputs, and the warning would go away.
I prototyped it by swapping _solve_svd during Ridge.fit (fit_intercept=True, alpha=1.0, one target, standard normal data, best of 3 after a warmup, torch 2.13). Coefficients match the NumPy Cholesky solver to ~1e-6 in float32 and ~1e-14 in float64. Fit times in seconds, policy B is the fully on-device alternative described below:
NVIDIA L4, NumPy on 8 vCPUs (AMD EPYC 7R13):
| shape | dtype | NumPy cholesky | torch svd (current) | policy A | policy B |
|---|---|---|---|---|---|
| 500,000 x 100 | float32 | 0.13 | 0.091 | 0.011 | 0.011 |
| 500,000 x 100 | float64 | 0.20 | 0.45 | 0.059 | 0.059 |
| 50,000 x 1000 | float32 | 0.27 | 0.21 | 0.024 | 0.018 |
| 50,000 x 1000 | float64 | 0.44 | 2.3 | 0.29 | 0.28 |
| 463,810 x 990 | float32 | 1.9 | not run | 0.15 | 0.15 |
| 463,810 x 990 | float64 | 3.6 | not run | 2.5 | 2.5 |
| 2,500 x 20,000 | float32 | 0.52 | 1.5 | 0.12 | 0.044 |
| 2,500 x 20,000 | float64 | 0.93 | 13 | 0.80 | 0.69 |
| 5,000 x 40,000 | float32 | 3.1 | not run | 0.66 | 0.27 |
Intel Arc B390 iGPU (Panther Lake), NumPy on the 16 cores of the Core Ultra X7 358H:
| shape | dtype | NumPy cholesky | torch svd (current) | policy A | policy B |
|---|---|---|---|---|---|
| 500,000 x 100 | float32 | 0.12 | 0.49 | 0.021 | 0.021 |
| 50,000 x 1000 | float32 | 0.14 | 1.1 | 0.037 | 0.036 |
| 50,000 x 1000 | float64 | 0.35 | 2.0 | 0.27 | 0.27 |
| 463,810 x 990 | float32 | 1.1 | not run | 0.30 | 0.30 |
| 2,500 x 20,000 | float32 | 0.27 | 10 | 0.11 | 0.075 |
| 2,500 x 20,000 | float64 | 0.66 | 17 | 0.79 | 0.71 |
| 5,000 x 40,000 | float32 | 2.5 | not run | 0.85 | 0.74 |
Policy A is 7x to 90x faster than the current SVD, and in float32 2.5x to 13x faster than NumPy on CPU. In float64, with ~1000 features or more, it is only 0.8x to 1.5x the speed of NumPy: both GPUs have a low float64 throughput (~450 GFLOP/s measured on the Arc B390), and the Gram matrix is a float64 matmul.
Describe alternatives you've considered, if relevant
Policy B: solve on the device with xp.linalg.solve, which is in the array API linalg extension.
It is faster than policy A only when the system to solve is large, in practice in the dual case: up to 2.7x in float32 on the L4. Elsewhere both are within noise, because the Gram matrix dominates.
But each namespace handles the solve its own way, and checking them all is hard. For instance, on a singular system torch.linalg.solve raises a _LinAlgError, while cupy.linalg.solve returns NaNs by default. Some namespaces may also not implement the linalg extension. Policy A avoids all of this by reusing the code path we already test with NumPy.
Additional context
#34793 (centering trick for the Cholesky solver) would help here too: it avoids the centered copy of X, which halves the device memory needed by fit.
I'd go with policy A. We could switch the solve to the device later for large systems if it turns out to matter.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
github-actions commentedon Oct 1, 2026
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
ogrisel commentedon Oct 2, 2026
Thanks for running those experiments.
I agree that implementing policy A sounds pragmatic too me.
Here is semi-pendantic note on a potential naming confusion related to implementing policy B: using the name
solver="cholesky"is only meaningful when callingsp.linalg.solvewith theassume_a="pos"kwarg because then SciPy relies on the POTRF/POCON/POTRS LAPACK routines in this case (which implements the Cholesky decomposition).Unfortunately, the
assume_a="pos"option is not part of the Python array API standard:Therefore, if we were to call
xp.linalg.solve(A, b)without passing theassume_a="pos"kwarg, this would effectively call the GESV, GETRF or GETRS LAPACK drivers (depending on the choice of array library such as scipy, CuPy, torch, dpnp...). All those drivers rely on a LU decomposition rather than an Cholesky decomposition. In practice the speed is almost the same as Cholesky but since it's not the same linear algebra algorithm, I thing I would rather to calling the genericxp.linalg.solve(A, b)under thesolver="cholesky"name to avoid confusion.To implement policy B without introducing solver name confusion, we could decide to introduce a new
solver="lu"argument and update the "auto" solver to keep on routing to "cholesky" for numpy inputs and route to "lu" otherwise. Or we could keepsolver="cholesky"but then we would need to update the docstring to make it explicit that it's actually using an LU decomposition under the hood whenever the input is not a NumPy array but that feels ugly.EDIT: as an alternative we could implement option B under
solver="lu"and consider deprecatingsolver="cholesky"in favor ofsolver="lu"if we want to limit the proliferation of redundant solvers.That being said, implementing policy B still has the problem of guessing which exception type to catch if the system is singular. Maybe we could
except Exceptionand check for NaN values in the solution to implement a library agnostic fallback to SVD.ogrisel commentedon Oct 2, 2026
I just noticed that policy B is almost 3 times faster for the
float322500 x 20000 case. So it might still be worth implementing...ogrisel commentedon Oct 2, 2026
Another note about policy a: let's use
move_to(..., xp=np, device="cpu")instead of_convert_to_numpy(as per this function own's docstring): this allows to support any array library with dlpack support instead of only the ones explicitly listed in the body of_convert_to_numpythat is only meant to be used as fallback in case of lack of dlpack support.ogrisel commentedon Oct 5, 2026
In hindsights: let's start with a PR that implements option A because it does not change the public API and should be a great performance improvement in all cases.
This does not close the possibility to implement option B later to further optimize the wide data case.
lorentzenchr commentedon Oct 5, 2026
Only skimming over the issue, I would prefer option B and pass additional args to solve whenever that is possible (eg keep using scipy‘s solve for ndarrays). And I would just keep the solver name "cholesky".
ogrisel commentedon Oct 6, 2026
One potential problem with that approach: the LU-based solution (obtained when using non-NumPy inputs) and the Cholesky-based (which we would get on NumPy inputs) might differ if the kernel (or Gram) matrix is near singular with different sensitivities: as a result the solver fallback to SVD could be triggered in different conditions whether we pass inputs as NumPy vs arrays of alternative libraries.
But I am ok to try to implement this approach first and empirically check whether such discrepancies happen in practice or not. We can write dedicated array API consistency tests to focus on near singular cases (e.g. small alpha and collinear features, e.g. from one-hot encoding a ternary categorical variable).
ogrisel commentedon Oct 13, 2026
Related note: SciPy developers are discussing the possibility to implement array API support for
solvewithassume_a="pos"in scipy/scipy#24596 (comment) either via array library-specific constructs beyond the array API spec.lorentzenchr commentedon Oct 16, 2026
We explicitly advice to use an L2 penalty. So this is more of an edge case. For me, that is an acceptable trade-off (have diffent solutions, like non-convergence for unpenalized ill-conditioned X, but support array API).
cakedev0 commentedon Nov 16, 2026
I think there is a consensus for option B, or at least to try it first and see how it does. So I'll give it a try 😄
cakedev0 commentedon Nov 16, 2026
The PR is ready: #35418
I tested it on a MAC M2 GPU (torch-mps), Nvidia L4 (cupy & torch), and Intel B580 GPUs (dpnp & torch-xpu).
virchan commentedon Nov 20, 2026
Shall we close this issue as "completed", given #35418?
cakedev0 commentedon Nov 20, 2026
I think we can, I'm closing it. But not opposed to re-opening if there is something I missed.