Skip to content
Merged
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
7 changes: 5 additions & 2 deletions python/cuml/cuml/accel/_overrides/sklearn/svm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0

import functools
Expand All @@ -20,7 +20,10 @@


def _has_probability(model):
if not model.probability:
# sklearn >= 1.9 defaults `probability` to the sentinel string "deprecated",
# which is truthy. Treat it the same as False (no calibration requested).
prob = getattr(model, "probability", False)
Comment thread
betatim marked this conversation as resolved.
Resolved
if prob == "deprecated" or not prob:
raise AttributeError(
"predict_proba is not available when probability=False"
)
Expand Down
14 changes: 13 additions & 1 deletion python/cuml/cuml/svm/svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
from cuml.svm.svm_base import SVMBase


def _normalize_sk_probability(value):
Comment thread
betatim marked this conversation as resolved.
Resolved
"""Coerce the sklearn >=1.9 ``"deprecated"`` sentinel into the bool cuml expects.

sklearn 1.9 changed the default of ``SVC.probability`` from ``False`` to the
sentinel string ``"deprecated"`` as part of the parameter's deprecation. cuml
stores ``probability`` as a real bool, so we coerce on ingest.
"""
if value == "deprecated":
return False
return bool(value)


class SVC(SVMBase, ClassifierMixin):
"""
SVC (C-Support Vector Classification)
Expand Down Expand Up @@ -204,7 +216,7 @@ def _params_from_cpu(cls, model):
) # SVC doesn't expose `epsilon` in the constructor
params.update(
{
"probability": model.probability,
"probability": _normalize_sk_probability(model.probability),
"random_state": model.random_state,
"class_weight": model.class_weight,
"decision_function_shape": model.decision_function_shape,
Expand Down
6 changes: 6 additions & 0 deletions python/cuml/cuml/svm/svm_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ class SVMBase(Base,
"_probA": self._probA,
"_probB": self._probB,
"_sparse": self._sparse,
# sklearn >= 1.9 added a private fitted attribute that libsvm-facing
# code reads during predict (see sklearn PR #32050). It is set during
# fit(), so we have to set it ourselves. Harmless on older sklearn
# (no code reads it). cuml.SVR has no `probability` attribute;
# default to False to match sklearn.
"_effective_probability": getattr(self, "probability", False),
Comment thread
betatim marked this conversation as resolved.
Resolved
**super()._attrs_to_cpu(model),
}

Expand Down
8 changes: 7 additions & 1 deletion python/cuml/tests/explainer/test_explainer_kernel_shap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import pytest
import scipy.special
import sklearn.neighbors
import sklearn.svm
from sklearn.calibration import CalibratedClassifierCV
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

Expand Down Expand Up @@ -83,7 +85,11 @@ def test_exact_classification_datasets(exact_shap_classification_dataset):

models = []
models.append(cuml.SVC(probability=True).fit(X_train, y_train))
models.append(sklearn.svm.SVC(probability=True).fit(X_train, y_train))
models.append(
CalibratedClassifierCV(sklearn.svm.SVC(), ensemble=False).fit(
X_train, y_train
)
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

for mod in models:
explainer, shap_values = get_shap_values(
Expand Down
10 changes: 8 additions & 2 deletions python/cuml/tests/explainer/test_explainer_permutation_shap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0
#

Expand All @@ -8,6 +8,8 @@
import numpy as np
import pytest
import sklearn.neighbors
import sklearn.svm
from sklearn.calibration import CalibratedClassifierCV
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

Expand Down Expand Up @@ -57,7 +59,11 @@ def test_exact_classification_datasets(exact_shap_classification_dataset):

models = []
models.append(cuml.SVC(probability=True).fit(X_train, y_train))
models.append(sklearn.svm.SVC(probability=True).fit(X_train, y_train))
models.append(
CalibratedClassifierCV(sklearn.svm.SVC(), ensemble=False).fit(
X_train, y_train
)
)

for mod in models:
explainer, shap_values = get_shap_values(
Expand Down
22 changes: 20 additions & 2 deletions python/cuml/tests/test_sklearn_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ def test_svr(random_state, sparse, kernel):
)


# TODO: sklearn 1.11 removes sklearn.svm.SVC(probability=True).
# At that point the probability=True parametrizations of this test must either
# (a) have cuml.SVC.as_sklearn() emit CalibratedClassifierCV for probability=True
# models and update the counterpart here to match, or
# (b) drop the probability=True parametrizations entirely.
# The filter is the only sklearn-1.11-unsafe construct left in the SVM tests.
Comment thread
betatim marked this conversation as resolved.
Resolved
@pytest.mark.filterwarnings(
"ignore:The `probability` parameter was deprecated:FutureWarning"
)
@pytest.mark.parametrize("sparse", [False, True])
@pytest.mark.parametrize("probability", [False, True])
@pytest.mark.parametrize("kernel", ["rbf", "precomputed"])
Expand All @@ -396,9 +405,18 @@ def test_svc(random_state, sparse, probability, kernel):
original = cuml.SVC()
assert_estimator_roundtrip(original, sklearn.svm.SVC, X, y)

# Check inference works after conversion
# Check inference works after conversion. sklearn 1.9 deprecated the
# `probability` parameter.Avoid passing it on the sklearn side when
# False (the default) to avoid getting a FutureWarning.
cu_model = cuml.SVC(probability=probability).fit(X, y)
sk_model = sklearn.svm.SVC(probability=probability).fit(X, y)
if probability:
# TODO: once sklearn 1.11 lands, sklearn.svm.SVC has no
# `probability` parameter and this branch must either drop the
# probability=True parametrization or change cuml.SVC.as_sklearn()
# to emit CalibratedClassifierCV for probability=True models.
sk_model = sklearn.svm.SVC(probability=True).fit(X, y)
else:
sk_model = sklearn.svm.SVC().fit(X, y)

cu_model2 = cuml.SVC.from_sklearn(sk_model)
sk_model2 = cu_model.as_sklearn()
Expand Down
18 changes: 15 additions & 3 deletions python/cuml/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cudf.pandas import LOADED as cudf_pandas_active
from numba import cuda
from sklearn import svm
from sklearn.calibration import CalibratedClassifierCV
from sklearn.datasets import (
load_iris,
make_blobs,
Expand Down Expand Up @@ -215,7 +216,13 @@ def test_svm_skl_cmp_decision_function(params, n_rows=4000, n_cols=20):
df1 = cuSVC.decision_function(X_test)
assert df1.dtype == X_train.dtype

sklSVC = svm.SVC(**params)
# sklearn here is just a reference value for decision_function, which does
# not depend on calibration (the value of `probability`). The `probability`
# parametrization of the test exists to verify that cuml's probability=True
# path doesn't break decision_function on our side, so we drop probability
# from the sklearn kwargs (sklearn 1.9 warns on any explicit value, and
# 1.11 will remove the parameter entirely).
sklSVC = svm.SVC(**{k: v for k, v in params.items() if k != "probability"})
sklSVC.fit(X_train, y_train)
df2 = sklSVC.decision_function(X_test)

Expand Down Expand Up @@ -300,7 +307,8 @@ def test_svc_predict_proba(in_type, n_classes):

cuSVC = cu_svm.SVC(**params)
cuSVC.fit(X_m.to_output(in_type), y_m.to_output(in_type))
sklSVC = svm.SVC(**params)
sk_params = {k: v for k, v in params.items() if k != "probability"}
sklSVC = CalibratedClassifierCV(svm.SVC(**sk_params), ensemble=False)
sklSVC.fit(X_train, y_train)

tol = 1e-2 if n_classes == 2 else 1e-1
Expand Down Expand Up @@ -344,7 +352,11 @@ def test_svc_weights(class_weight, sample_weight, probability):
cu_score = cuSVC.score(X_1, y_1)
assert cu_score > 0.9

sklSVC = svm.SVC(**params)
sk_params = {k: v for k, v in params.items() if k != "probability"}
if probability:
sklSVC = CalibratedClassifierCV(svm.SVC(**sk_params), ensemble=False)
else:
sklSVC = svm.SVC(**sk_params)
sklSVC.fit(X, y, sample_weight)
if not probability:
# TODO: SVC estimators with probability=True don't expose all the fitted
Expand Down
Loading