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
13 changes: 10 additions & 3 deletions python/cuml/cuml/manifold/spectral_embedding.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import scipy.sparse as sp
from pylibraft.common.handle import Handle

import cuml
from cuml.common import input_to_cuml_array
from cuml.common.array_descriptor import CumlArrayDescriptor
from cuml.internals.array import CumlArray
from cuml.internals.base import Base
from cuml.internals.input_utils import input_to_cupy_array
from cuml.internals.interop import (
InteropMixin,
UnsupportedOnGPU,
Expand Down Expand Up @@ -154,9 +154,10 @@ def spectral_embedding(A,
cdef device_resources *h = <device_resources*><size_t>handle.getHandle()

if affinity == "nearest_neighbors":
A = input_to_cuml_array(
A = input_to_cupy_array(
A, order="C", check_dtype=np.float32, convert_to_dtype=cp.float32
).array
isfinite = cp.isfinite(A).all()
elif affinity == "precomputed":
# Coerce `A` to a canonical float32 COO sparse matrix
if cp_sp.issparse(A):
Expand All @@ -168,6 +169,7 @@ def spectral_embedding(A,
else:
A = cp_sp.coo_matrix(cp.asarray(A, dtype="float32"))
A.sum_duplicates()
isfinite = cp.isfinite(A.data).all()
else:
raise ValueError(
f"`affinity={affinity!r}` is not supported, expected one of "
Expand All @@ -176,6 +178,11 @@ def spectral_embedding(A,

n_samples, n_features = A.shape

if not isfinite:
raise ValueError(
"Input contains NaN or inf; nonfinite values are not supported"
Comment thread
jcrist marked this conversation as resolved.
)

if n_samples < 2:
raise ValueError(
f"Found array with {n_samples} sample(s) (shape={A.shape}) while a "
Expand Down Expand Up @@ -220,7 +227,7 @@ def spectral_embedding(A,
deref(h),
config,
make_device_matrix_view[float, int, row_major](
<float *><uintptr_t>A.ptr,
<float *><uintptr_t>A.data.ptr,
<int>A.shape[0],
<int>A.shape[1],
),
Expand Down
9 changes: 9 additions & 0 deletions python/cuml/tests/test_spectral_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ def test_spectral_embedding_invalid_affinity():
spectral_embedding(X, affinity="oops!")


@pytest.mark.parametrize("value", [float("inf"), float("nan")])
@pytest.mark.parametrize("affinity", ["nearest_neighbors", "precomputed"])
def test_spectral_embedding_nonfinite(value, affinity):
X = np.array([[0, 1], [2, 3], [0, value]], dtype="float32")

with pytest.raises(ValueError, match="nonfinite"):
spectral_embedding(X, affinity=affinity)


@pytest.mark.parametrize(
"input_type,expected_type",
[
Expand Down