Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plumb metric and metric_kwds through to UMAP with nn_descent #6304

Open
wants to merge 3 commits into
base: branch-25.04
Choose a base branch
from
Open
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
46 changes: 21 additions & 25 deletions python/cuml/cuml/manifold/umap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -483,39 +483,20 @@ class UMAP(UniversalBase,
umap_params.verbosity = <level_enum> self.verbose
umap_params.a = <float> self.a
umap_params.b = <float> self.b
umap_params.target_n_neighbors = <int> self.target_n_neighbors
umap_params.target_weight = <float> self.target_weight
umap_params.random_state = <uint64_t> check_random_seed(self.random_state)
umap_params.deterministic = <bool> self.deterministic

if self.init == "spectral":
umap_params.init = <int> 1
else: # self.init == "random"
umap_params.init = <int> 0
umap_params.target_n_neighbors = <int> self.target_n_neighbors

if self.target_metric == "euclidean":
umap_params.target_metric = MetricType.EUCLIDEAN
else: # self.target_metric == "categorical"
umap_params.target_metric = MetricType.CATEGORICAL
if self.build_algo == "brute_force_knn":
umap_params.build_algo = graph_build_algo.BRUTE_FORCE_KNN
else: # self.init == "nn_descent"
umap_params.build_algo = graph_build_algo.NN_DESCENT
if self.build_kwds is None:
umap_params.nn_descent_params.graph_degree = <uint64_t> 64
umap_params.nn_descent_params.intermediate_graph_degree = <uint64_t> 128
umap_params.nn_descent_params.max_iterations = <uint64_t> 20
umap_params.nn_descent_params.termination_threshold = <float> 0.0001
umap_params.nn_descent_params.return_distances = <bool> True
umap_params.nn_descent_params.n_clusters = <uint64_t> 1
else:
umap_params.nn_descent_params.graph_degree = <uint64_t> self.build_kwds.get("nnd_graph_degree", 64)
umap_params.nn_descent_params.intermediate_graph_degree = <uint64_t> self.build_kwds.get("nnd_intermediate_graph_degree", 128)
umap_params.nn_descent_params.max_iterations = <uint64_t> self.build_kwds.get("nnd_max_iterations", 20)
umap_params.nn_descent_params.termination_threshold = <float> self.build_kwds.get("nnd_termination_threshold", 0.0001)
umap_params.nn_descent_params.return_distances = <bool> self.build_kwds.get("nnd_return_distances", True)
if self.build_kwds.get("nnd_n_clusters", 1) < 1:
logger.info("Negative number of nnd_n_clusters not allowed. Changing nnd_n_clusters to 1")
umap_params.nn_descent_params.n_clusters = <uint64_t> self.build_kwds.get("nnd_n_clusters", 1)

umap_params.target_weight = <float> self.target_weight
umap_params.random_state = <uint64_t> check_random_seed(self.random_state)
umap_params.deterministic = <bool> self.deterministic

try:
umap_params.metric = metric_parsing[self.metric.lower()]
Expand All @@ -533,6 +514,21 @@ class UMAP(UniversalBase,
else:
umap_params.p = <float>self.metric_kwds.get('p')

if self.build_algo == "brute_force_knn":
umap_params.build_algo = graph_build_algo.BRUTE_FORCE_KNN
else:
umap_params.build_algo = graph_build_algo.NN_DESCENT
build_kwds = self.build_kwds or {}
umap_params.nn_descent_params.graph_degree = <uint64_t> build_kwds.get("nnd_graph_degree", 64)
umap_params.nn_descent_params.intermediate_graph_degree = <uint64_t> build_kwds.get("nnd_intermediate_graph_degree", 128)
umap_params.nn_descent_params.max_iterations = <uint64_t> build_kwds.get("nnd_max_iterations", 20)
umap_params.nn_descent_params.termination_threshold = <float> build_kwds.get("nnd_termination_threshold", 0.0001)
umap_params.nn_descent_params.return_distances = <bool> build_kwds.get("nnd_return_distances", True)
umap_params.nn_descent_params.n_clusters = <uint64_t> build_kwds.get("nnd_n_clusters", 1)
# Forward metric & metric_kwds to nn_descent
umap_params.nn_descent_params.metric = <RaftDistanceType> umap_params.metric
umap_params.nn_descent_params.metric_arg = umap_params.p
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix is here (plumbing through the metric options to nn_descent_params). In the long run we should redesign the C++ layer to remove duplicate options - for now just ensuring they're forwarded correctly seems sufficient.

Everything else here is a simplification of the current pre-existing code.


cdef uintptr_t callback_ptr = 0
if self.callback:
callback_ptr = self.callback.get_native_callback()
Expand Down
4 changes: 4 additions & 0 deletions python/cuml/cuml/manifold/umap_utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from libc.stdint cimport uint64_t, uintptr_t, int64_t
from libcpp cimport bool
from libcpp.memory cimport shared_ptr
from cuml.metrics.distance_type cimport DistanceType
from cuml.metrics.raft_distance_type cimport DistanceType as RaftDistanceType
from cuml.internals.logger cimport level_enum

cdef extern from "cuml/manifold/umapparams.h" namespace "ML::UMAPParams":
Expand All @@ -39,6 +40,7 @@ cdef extern from "cuml/common/callback.hpp" namespace "ML::Internals":

cdef cppclass GraphBasedDimRedCallback


cdef extern from "raft/neighbors/nn_descent_types.hpp" namespace "raft::neighbors::experimental::nn_descent":
cdef struct index_params:
uint64_t graph_degree,
Expand All @@ -47,6 +49,8 @@ cdef extern from "raft/neighbors/nn_descent_types.hpp" namespace "raft::neighbor
float termination_threshold,
bool return_distances,
uint64_t n_clusters,
RaftDistanceType metric,
float metric_arg

cdef extern from "cuml/manifold/umapparams.h" namespace "ML":

Expand Down
45 changes: 11 additions & 34 deletions python/cuml/cuml/tests/test_umap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
# Please install UMAP before running the code
# use 'conda install -c conda-forge umap-learn' command to install it

import platform
import pytest
import copy
import joblib
import umap
from sklearn.metrics import adjusted_rand_score
from sklearn.manifold import trustworthiness
from sklearn.datasets import make_blobs
Expand All @@ -45,12 +45,6 @@
scipy_sparse = cpu_only_import("scipy.sparse")


IS_ARM = platform.processor() == "aarch64"

if not IS_ARM:
import umap


dataset_names = ["iris", "digits", "wine", "blobs"]


Expand Down Expand Up @@ -81,9 +75,6 @@ def test_blobs_cluster(nrows, n_feats, build_algo):
@pytest.mark.parametrize(
"n_feats", [unit_param(10), quality_param(100), stress_param(1000)]
)
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
@pytest.mark.parametrize("build_algo", ["brute_force_knn", "nn_descent"])
def test_umap_fit_transform_score(nrows, n_feats, build_algo):

Expand Down Expand Up @@ -256,9 +247,6 @@ def test_umap_transform_on_digits(target_metric):

@pytest.mark.parametrize("target_metric", ["categorical", "euclidean"])
@pytest.mark.parametrize("name", dataset_names)
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_umap_fit_transform_trust(name, target_metric):

if name == "iris":
Expand Down Expand Up @@ -302,9 +290,6 @@ def test_umap_fit_transform_trust(name, target_metric):
@pytest.mark.parametrize("should_downcast", [True])
@pytest.mark.parametrize("input_type", ["dataframe", "ndarray"])
@pytest.mark.parametrize("build_algo", ["brute_force_knn", "nn_descent"])
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_umap_data_formats(
input_type,
should_downcast,
Expand Down Expand Up @@ -343,9 +328,6 @@ def test_umap_data_formats(
@pytest.mark.parametrize("target_metric", ["categorical", "euclidean"])
@pytest.mark.filterwarnings("ignore:(.*)connected(.*):UserWarning:sklearn[.*]")
@pytest.mark.parametrize("build_algo", ["brute_force_knn", "nn_descent"])
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_umap_fit_transform_score_default(target_metric, build_algo):

n_samples = 500
Expand Down Expand Up @@ -545,9 +527,6 @@ def test_umap_transform_trustworthiness_with_consistency_enabled():


@pytest.mark.filterwarnings("ignore:(.*)zero(.*)::scipy[.*]|umap[.*]")
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
@pytest.mark.parametrize("build_algo", ["brute_force_knn", "nn_descent"])
def test_exp_decay_params(build_algo):
def compare_exp_decay_params(a=None, b=None, min_dist=0.1, spread=1.0):
Expand Down Expand Up @@ -692,9 +671,6 @@ def correctness_sparse(a, b, atol=0.1, rtol=0.2, threshold=0.95):
@pytest.mark.parametrize("n_rows", [200, 800])
@pytest.mark.parametrize("n_features", [8, 32])
@pytest.mark.parametrize("n_neighbors", [8, 16])
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_fuzzy_simplicial_set(n_rows, n_features, n_neighbors):
n_clusters = 30
random_state = 42
Expand Down Expand Up @@ -738,12 +714,12 @@ def test_fuzzy_simplicial_set(n_rows, n_features, n_neighbors):
("canberra", True),
],
)
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_umap_distance_metrics_fit_transform_trust(metric, supported):
@pytest.mark.parametrize("build_algo", ["brute_force_knn", "nn_descent"])
def test_umap_distance_metrics_fit_transform_trust(
metric, supported, build_algo
):
data, labels = make_blobs(
n_samples=1000, n_features=64, centers=5, random_state=42
n_samples=500, n_features=64, centers=5, random_state=42
)

if metric == "jaccard":
Expand All @@ -753,7 +729,11 @@ def test_umap_distance_metrics_fit_transform_trust(metric, supported):
n_neighbors=10, min_dist=0.01, metric=metric, init="random"
)
cuml_model = cuUMAP(
n_neighbors=10, min_dist=0.01, metric=metric, init="random"
n_neighbors=10,
min_dist=0.01,
metric=metric,
init="random",
build_algo=build_algo,
)
if not supported:
with pytest.raises(NotImplementedError):
Expand Down Expand Up @@ -791,9 +771,6 @@ def test_umap_distance_metrics_fit_transform_trust(metric, supported):
("canberra", True, True),
],
)
@pytest.mark.skipif(
IS_ARM, reason="https://github.com/rapidsai/cuml/issues/5441"
)
def test_umap_distance_metrics_fit_transform_trust_on_sparse_input(
metric, supported, umap_learn_supported
):
Expand Down
Loading