Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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: 3 additions & 4 deletions docs/source/cuml-accel/limitations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,9 @@ TargetEncoder
``TargetEncoder`` will fall back to CPU in the following cases:

- If ``categories`` is not ``"auto"``.
- If ``y`` is a multiclass target (sklearn uses one-hot encoding internally).
- If ``random_state`` is a ``numpy.random.RandomState`` object (integer seeds work fine).
- If ``X`` has object dtype with numeric values.
- If ``y`` has object dtype.
- If ``y`` is a multiclass target.
- If ``random_state`` is a ``numpy.random.RandomState`` object (integer seeds
work fine).

Additional notes:

Expand Down
45 changes: 6 additions & 39 deletions python/cuml/cuml/accel/_overrides/sklearn/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,63 +53,30 @@ def _params_from_cpu(model):
return model.get_params(deep=False)


def _check_unsupported_inputs(X, y, cpu_model):
def _check_targetencoder_y(y):
"""Check if inputs are supported on GPU.

Raises UnsupportedOnGPU for unsupported cases to trigger CPU fallback.
"""
from sklearn.utils.multiclass import type_of_target

# Check for custom categories (cuML doesn't support sklearn's categories param)
if hasattr(cpu_model, "categories") and cpu_model.categories != "auto":
raise UnsupportedOnGPU(
"Custom categories parameter not supported on GPU"
)

# Check for multiclass targets (sklearn uses one-hot encoding internally)
# Check for unsupported target types
target_type = type_of_target(y)
if target_type == "multiclass":
raise UnsupportedOnGPU(
"Multiclass targets not supported on GPU "
"(sklearn uses one-hot encoding internally)"
)

# Check for RandomState objects - cuML uses different CV fold assignment
# algorithm, so we can't reproduce exact sklearn behavior with RandomState.
# Integer seeds work fine, but RandomState objects require CPU fallback
# to ensure statistical tests that depend on exact CV splits pass.
if hasattr(cpu_model, "random_state") and isinstance(
cpu_model.random_state, np.random.RandomState
):
raise UnsupportedOnGPU(
"RandomState objects require CPU fallback for exact sklearn "
"CV fold assignment behavior (use integer seed for GPU)"
)

# Check for object dtype X with float values - cudf can't handle this
if hasattr(X, "dtype") and X.dtype == np.object_:
raise UnsupportedOnGPU(
"Object dtype arrays with numeric values not supported on GPU"
)

# Check for object dtype y - cudf can't handle float objects
if hasattr(y, "dtype") and y.dtype == np.object_:
if target_type not in ("binary", "continuous"):
raise UnsupportedOnGPU(
"Object dtype target arrays not supported on GPU"
f"y with target type {target_type!r} not supported on GPU"
)
Comment thread
jcrist marked this conversation as resolved.
Resolved


class TargetEncoder(ProxyBase):
_gpu_class = cuml.preprocessing.TargetEncoder

def _gpu_fit(self, X, y, **kwargs):
# Check for unsupported inputs (triggers CPU fallback)
_check_unsupported_inputs(X, y, self._cpu)
_check_targetencoder_y(y)
return self._gpu.fit(X, y, **kwargs)

def _gpu_fit_transform(self, X, y, **kwargs):
# Check for unsupported inputs (triggers CPU fallback)
_check_unsupported_inputs(X, y, self._cpu)
_check_targetencoder_y(y)
return self._gpu.fit_transform(X, y, **kwargs)

def _gpu_get_feature_names_out(self, input_features=None):
Expand Down
10 changes: 10 additions & 0 deletions python/cuml/cuml/internals/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,16 @@ def check_cudf(
)
if array.dtype == "float16":
array = array.astype("float32")
elif (
array.dtype == "object"
and array.size
and not isinstance(array.flat[0], str)
):
# XXX: cudf doesn't support coercing numeric object arrays, while
# sklearn has a common check that object arrays of floats are
# supported. To support this uncommon case, we attempt to coerce
# numeric object types here.
array = array.astype("float64")
Comment thread
jcrist marked this conversation as resolved.
Resolved
array = (cudf.DataFrame if array.ndim == 2 else cudf.Series)(
array, dtype=(np.dtype("O") if array.dtype.kind in "U" else None)
)
Expand Down
Loading
Loading