Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion docs/source/api/cuml.metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@ Pairwise Distances and Kernels
:template: base.rst

pairwise_distances
sparse_pairwise_distances
nan_euclidean_distances
pairwise_kernels
25 changes: 1 addition & 24 deletions python/cuml/cuml/internals/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import re
import threading
import warnings

import pylibraft.common.handle

Expand All @@ -16,10 +15,7 @@
import cuml.internals.logger as logger
import cuml.internals.nvtx as nvtx
from cuml.internals.mixins import TagsMixin, _ensure_transformer_tags
from cuml.internals.outputs import (
infer_output_type,
warn_if_output_type_deprecated,
)
from cuml.internals.outputs import infer_output_type

_THREAD_STATE = threading.local()

Expand Down Expand Up @@ -55,14 +51,6 @@ def get_handle(*, n_streams=0, device_ids=None):
return pylibraft.common.handle.Handle(n_streams=n_streams)


class _DeprecatedOutputTypeDescriptor:
"""A descriptor to warn when a deprecated `output_type` is configured."""

def __set__(self, obj, value):
warn_if_output_type_deprecated(value)
obj.__dict__["output_type"] = value


class Base(TagsMixin):
"""Base class for cuml estimators.

Expand Down Expand Up @@ -125,8 +113,6 @@ def predict(self, X):
return cp.ones(len(X), dtype="int32")
"""

output_type = _DeprecatedOutputTypeDescriptor()

def __init__(
self,
*,
Expand Down Expand Up @@ -253,15 +239,6 @@ class output type and global output type.
else:
# Determine the output from the input
output_type = infer_output_type(inp)
if output_type == "numba":
warnings.warn(
"Outputting `numba` arrays was deprecated "
"in version 26.08 and will be removed "
"in version 26.10. In the future this call will return a "
"`cupy` array instead. You may silence this warning by "
"explicitly setting `output_type='cupy'` now.",
FutureWarning,
)

return output_type

Expand Down
96 changes: 9 additions & 87 deletions python/cuml/cuml/internals/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import contextlib
import functools
import inspect
import warnings

import cudf
import cupy as cp
Expand All @@ -29,18 +28,7 @@
)


OUTPUT_TYPES = (
"input",
"numpy",
"cupy",
"cudf",
"pandas",
"numba",
"array",
"dataframe",
"series",
"df_obj",
)
OUTPUT_TYPES = ("input", "numpy", "cupy", "cudf", "pandas")


def check_output_type(output_type: str) -> str:
Expand All @@ -56,33 +44,6 @@ def check_output_type(output_type: str) -> str:
return output_type


def warn_if_output_type_deprecated(output_type: str):
"""Warn if the specified `output_type` is deprecated"""
if isinstance(output_type, str) and output_type in (
"numba",
"array",
"df_obj",
"dataframe",
"series",
):
alt = "cupy" if output_type in ("numba", "array") else "cudf"
if output_type in ("dataframe", "series"):
suffix = (
" Note that `output_type='cudf'` will return `cudf.Series` "
"objects for 1-dimensional outputs and `cudf.DataFrame` "
"objects for 2-dimensional outputs. You may need to "
"update consumers as necessary."
)
else:
suffix = ""
warnings.warn(
f"`output_type={output_type!r}` was deprecated in version 26.08 "
"and will be removed in version 26.10. Please use "
f"`output_type={alt!r}` instead.{suffix}",
FutureWarning,
)


def set_global_output_type(output_type):
"""Set the global output type.

Expand Down Expand Up @@ -160,7 +121,6 @@ def set_global_output_type(output_type):
"""
if output_type is not None:
output_type = check_output_type(output_type)
warn_if_output_type_deprecated(output_type)
GlobalSettings().output_type = output_type


Expand Down Expand Up @@ -289,7 +249,7 @@ def infer_output_type(array, array_like="numpy"):

Returns
-------
output_type : {"cupy", "numpy", "pandas", "cudf", "numba", "cuml", None}
output_type : {"cupy", "numpy", "pandas", "cudf", None}
The inferred ``output_type``, or ``None`` if not an array-like input.
"""
if isinstance(array, np.ndarray) or sp.issparse(array):
Expand All @@ -300,8 +260,6 @@ def infer_output_type(array, array_like="numpy"):
return "cudf"
elif isinstance(array, (pd.DataFrame, pd.Series, pd.Index)):
return "pandas"
elif hasattr(array, "__cuda_ndarray__"):
return "numba"
elif hasattr(array, "__cuda_array_interface__"):
return "cupy"

Expand Down Expand Up @@ -373,7 +331,7 @@ def to_output(self, output_type=None, index=None):

Parameters
----------
output_type : {'cupy', 'numpy', 'cudf', 'pandas', 'numba'} or None
output_type : {'cupy', 'numpy', 'cudf', 'pandas'} or None
The output type to convert to. If `None`, `cupy` will be used when
possible, falling back to `cudf` if necessary.
index : pandas.Index, cudf.Index, or None, default=None
Expand Down Expand Up @@ -459,17 +417,13 @@ def to_output(self, output_type=None, index=None):
# Coerce result to requested output_type
if isinstance(out, cp.ndarray):
return convert_arrays(out, output_type, index=index)
elif output_type in ("cudf", "df_obj"):
return out
elif output_type == "dataframe":
return out.to_frame() if isinstance(out, cudf.Series) else out
elif output_type == "series" and isinstance(out, cudf.Series):
elif output_type == "cudf":
return out
elif output_type == "pandas":
if cudf.pandas.LOADED:
return cudf.pandas.as_proxy_object(out)
return out.to_pandas()
elif output_type in ("numpy", "array"):
elif output_type == "numpy":
# XXX: dtype coercion not needed for object, and when specified
# cudf will sometimes coerce `None -> <NA>` erroneously.
# See https://github.com/rapidsai/cudf/issues/22419
Expand Down Expand Up @@ -500,7 +454,7 @@ def convert_arrays(
traversed recursively to find array-likes. Other array-likes (pandas,
...) will error as unsupported. Any other type is passed through
unchanged.
output_type : {'cupy', 'numpy', 'cudf', 'pandas', 'numba'}
output_type : {'cupy', 'numpy', 'cudf', 'pandas'}
The output type to convert to.
index : pandas.Index, cudf.Index, or None, default=None
An optional index to attach to arrays when returning dataframe-like
Expand Down Expand Up @@ -542,36 +496,9 @@ def convert_arrays(
if isinstance(obj, cp.ndarray):
if output_type == "numpy":
return obj.get(order="A")
elif output_type in (
"cudf",
"pandas",
"df_obj",
"dataframe",
"series",
):
if output_type == "series":
if obj.ndim == 2:
if obj.shape[1] == 1:
obj = obj.flatten()
else:
raise ValueError(
"Only single dimensional arrays can be transformed to"
" Series."
)
elif obj.ndim == 0:
obj = obj[None]
elif output_type == "dataframe":
if obj.ndim == 1:
obj = obj[:, None]
elif obj.ndim == 0:
obj = obj[None, None]

elif output_type in ("cudf", "pandas"):
if obj.ndim == 2:
if (
one_col_2d_as_series
and obj.shape[1] == 1
and output_type != "dataframe"
):
if one_col_2d_as_series and obj.shape[1] == 1:
df = cudf.Series(obj.flatten(), index=index)
else:
df = cudf.DataFrame(obj, index=index)
Expand All @@ -583,13 +510,8 @@ def convert_arrays(
return cudf.pandas.as_proxy_object(df)
return df.to_pandas()
return df

elif output_type == "numba":
from numba import cuda

return cuda.as_cuda_array(obj)
else:
assert output_type in ("cuml", "cupy", "array")
assert output_type in ("cuml", "cupy")
# Return `cupy` directly
return obj

Expand Down
4 changes: 1 addition & 3 deletions python/cuml/cuml/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#

Expand All @@ -25,7 +25,6 @@
PAIRWISE_DISTANCE_SPARSE_METRICS,
nan_euclidean_distances,
pairwise_distances,
sparse_pairwise_distances,
)
from cuml.metrics.pairwise_kernels import (
PAIRWISE_KERNEL_FUNCTIONS,
Expand Down Expand Up @@ -59,7 +58,6 @@
"entropy",
"nan_euclidean_distances",
"pairwise_distances",
"sparse_pairwise_distances",
"pairwise_kernels",
"hinge_loss",
"kl_divergence",
Expand Down
95 changes: 0 additions & 95 deletions python/cuml/cuml/metrics/pairwise_distances.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -470,98 +470,3 @@ def pairwise_distances(X, Y=None, metric="euclidean", **kwds):
handle.sync()

return out


@mlfunc
def sparse_pairwise_distances(X, Y=None, metric="euclidean", **kwds):
"""
Compute the distance matrix from a vector array `X` and optional `Y`.

.. deprecated:: 26.08

The ``sparse_pairwise_distances`` function was deprecated in version
26.08 and will be removed in version 26.10. Please use
``pairwise_distances`` instead.

This method takes either one or two sparse vector arrays, and returns a
dense distance matrix.

If `Y` is given (default is `None`), then the returned matrix is the
pairwise distance between the arrays from both `X` and `Y`.

Valid values for metric are:

- From scikit-learn: ['cityblock', 'cosine', 'euclidean', 'l1', 'l2', \
'manhattan'].
- From scipy.spatial.distance: ['sqeuclidean', 'canberra', 'minkowski', \
'jaccard', 'chebyshev', 'dice']
See the documentation for scipy.spatial.distance for details on these
metrics.
- ['inner_product', 'hellinger']

Parameters
----------
X : array-like (device or host) of shape (n_samples_x, n_features)
Acceptable formats: SciPy or Cupy sparse array

Y : array-like (device or host) of shape (n_samples_y, n_features),\
optional
Acceptable formats: SciPy or Cupy sparse array

metric : {"cityblock", "cosine", "euclidean", "l1", "l2", "manhattan", \
"sqeuclidean", "canberra", "lp", "inner_product", "minkowski", \
"jaccard", "hellinger", "chebyshev", "linf", "dice"}
The metric to use when calculating distance between instances in a
feature array.

**kwds : optional keyword parameters
Any additional metric-specific parameters. For example, with
``metric="minkowski"``, passing ``p`` sets the norm used.

Returns
-------
D : array [n_samples_x, n_samples_x] or [n_samples_x, n_samples_y]
A dense distance matrix D such that D_{i, j} is the distance between
the ith and jth vectors of the given matrix `X`, if `Y` is None.
If `Y` is not `None`, then D_{i, j} is the distance between the ith
array from `X` and the jth array from `Y`.

Examples
--------

.. code-block:: python

>>> import cupy as cp
>>> import cupyx
>>> from cuml.metrics import sparse_pairwise_distances

>>> X = cupyx.scipy.sparse.csr_matrix(cp.array([[1.0, 2.0, 0.0],
... [0.0, 3.0, 1.0]]))
>>> Y = cupyx.scipy.sparse.csr_matrix(cp.array([[1.0, 0.0, 2.0]]))
>>> # Cosine Pairwise Distance, Single Input:
>>> sparse_pairwise_distances(X, metric='cosine')
array([[0. , 0.151...],
[0.151..., 0. ]])

>>> # Squared euclidean Pairwise Distance, Multi-Input:
>>> sparse_pairwise_distances(X, Y, metric='sqeuclidean')
array([[ 8.],
[11.]])

>>> # Canberra Pairwise Distance, Multi-Input:
>>> sparse_pairwise_distances(X, Y, metric='canberra')
array([[2. ],
[2.333...]])
"""
warnings.warn(
"The ``sparse_pairwise_distances`` function was deprecated "
"in version 26.08 and will be removed in version 26.10. "
"Please use ``pairwise_distances`` instead.",
FutureWarning,
)
return pairwise_distances(
X,
Y,
metric=metric,
**kwds,
)
Loading
Loading