Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Switch Ridge auto to use cholesky for dense
  • Loading branch information
jcrist committed Nov 20, 2026
commit 8f7af144f84df07987c5c288eee8a1d19975bab0
14 changes: 5 additions & 9 deletions python/cuml/cuml/linear_model/ridge.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class Ridge(InteropMixin,
solver : {'auto', 'cholesky', 'eig', 'svd', 'lsmr'}, default='auto'
The solver to use when fitting:

- 'auto': will select 'eig' if supported, falling back to 'lsmr' if X
is sparse, and 'svd' otherwise.
- 'auto': will select 'cholesky' for dense ``X``, and 'lsmr' for
sparse ``X``.

- 'cholesky': uses a cholesky decomposition of the covariance matrix,
falling back to an eigendecomposition if that's singular. This is
Expand Down Expand Up @@ -394,6 +394,7 @@ class Ridge(InteropMixin,
X_is_copy = cuda_ptr(X) != cuda_ptr(X_orig)
y_is_copy = cuda_ptr(y) != cuda_ptr(y_orig)
sample_weight_is_copy = cuda_ptr(sample_weight) != cuda_ptr(sample_weight_orig)
X_is_sparse = sp.issparse(X)

n_targets = 1 if y.ndim == 1 else y.shape[1]

Expand Down Expand Up @@ -433,14 +434,9 @@ class Ridge(InteropMixin,
"solver='svd' or solver='auto' instead"
)
elif solver == "auto":
if sp.issparse(X):
solver = "lsmr"
elif X.shape[1] == 1 or n_targets != 1:
solver = "svd"
else:
solver = "eig"
solver = "lsmr" if X_is_sparse else "cholesky"
Comment thread
jcrist marked this conversation as resolved.
Resolved

if sp.issparse(X):
if X_is_sparse:
if solver not in ("lsmr", "cholesky"):
raise ValueError(
f"{solver=!r} doesn't support sparse X, please select "
Expand Down
18 changes: 3 additions & 15 deletions python/cuml/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,15 @@ def test_cholesky_sparse_invalid_fit_intercept():


def test_solver_auto():
# 1 feature defaults to svd
X, y = make_regression(n_features=1, random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "svd"

# multi-target defaults to svd
X, y = make_regression(n_targets=3, random_state=42)
# dense defaults to cholesky
X, y = make_regression(random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "svd"
assert model.solver_ == "cholesky"

# sparse defaults to lsmr
model = cuml.Ridge().fit(sp.csr_matrix(X), y)
assert model.solver_ == "lsmr"
model = cuml.Ridge().fit(cp_sp.csr_matrix(cp.asarray(X)), y)
assert model.solver_ == "lsmr"

# otherwise defaults to eig
X, y = make_regression(random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "eig"


@pytest.mark.parametrize("solver", ["eig", "svd", "cholesky", "lsmr"])
def test_more_features_than_samples(solver):
Expand Down