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

Enable dice metric for UMAP and Pairwise Distances #5943

Open
wants to merge 13 commits into
base: branch-24.08
Choose a base branch
from
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ if(BUILD_CUML_CPP_LIBRARY)
src/metrics/pairwise_distance_chebyshev.cu
src/metrics/pairwise_distance_correlation.cu
src/metrics/pairwise_distance_cosine.cu
src/metrics/pairwise_distance_dice.cu
src/metrics/pairwise_distance_euclidean.cu
src/metrics/pairwise_distance_hamming.cu
src/metrics/pairwise_distance_hellinger.cu
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/metrics/pairwise_distance.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "pairwise_distance_chebyshev.cuh"
#include "pairwise_distance_correlation.cuh"
#include "pairwise_distance_cosine.cuh"
#include "pairwise_distance_dice.cuh"
#include "pairwise_distance_euclidean.cuh"
#include "pairwise_distance_hamming.cuh"
#include "pairwise_distance_hellinger.cuh"
Expand Down Expand Up @@ -88,6 +89,9 @@ void pairwise_distance(const raft::handle_t& handle,
case raft::distance::DistanceType::RusselRaoExpanded:
pairwise_distance_russell_rao(handle, x, y, dist, m, n, k, isRowMajor, metric_arg);
break;
case raft::distance::DistanceType::DiceExpanded:
pairwise_distance_dice(handle, x, y, dist, m, n, k, isRowMajor, metric_arg);
break;
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric);
};
}
Expand Down Expand Up @@ -143,6 +147,9 @@ void pairwise_distance(const raft::handle_t& handle,
case raft::distance::DistanceType::RusselRaoExpanded:
pairwise_distance_russell_rao(handle, x, y, dist, m, n, k, isRowMajor, metric_arg);
break;
case raft::distance::DistanceType::DiceExpanded:
pairwise_distance_dice(handle, x, y, dist, m, n, k, isRowMajor, metric_arg);
break;
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric);
};
}
Expand Down
56 changes: 56 additions & 0 deletions cpp/src/metrics/pairwise_distance_dice.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "pairwise_distance_dice.cuh"

#include <raft/core/handle.hpp>
#include <raft/distance/distance.cuh>

#include <rmm/device_uvector.hpp>

namespace ML {

namespace Metrics {
void pairwise_distance_dice(const raft::handle_t& handle,
const double* x,
const double* y,
double* dist,
int m,
int n,
int k,
bool isRowMajor,
double metric_arg)
{
raft::distance::distance<raft::distance::DistanceType::DiceExpanded, double, double, double, int>(
handle, x, y, dist, m, n, k, isRowMajor);
}

void pairwise_distance_dice(const raft::handle_t& handle,
const float* x,
const float* y,
float* dist,
int m,
int n,
int k,
bool isRowMajor,
float metric_arg)
{
raft::distance::distance<raft::distance::DistanceType::DiceExpanded, float, float, float, int>(
handle, x, y, dist, m, n, k, isRowMajor);
}

} // namespace Metrics
} // namespace ML
45 changes: 45 additions & 0 deletions cpp/src/metrics/pairwise_distance_dice.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <raft/core/handle.hpp>
#include <raft/distance/distance.cuh>

namespace ML {

namespace Metrics {
void pairwise_distance_dice(const raft::handle_t& handle,
const double* x,
const double* y,
double* dist,
int m,
int n,
int k,
bool isRowMajor,
double metric_arg);

void pairwise_distance_dice(const raft::handle_t& handle,
const float* x,
const float* y,
float* dist,
int m,
int n,
int k,
bool isRowMajor,
float metric_arg);

} // namespace Metrics
} // namespace ML
25 changes: 14 additions & 11 deletions python/cuml/manifold/umap_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,31 @@ def find_ab_params(spread, min_dist):


metric_parsing = {
"l2": DistanceType.L2SqrtUnexpanded,
"euclidean": DistanceType.L2SqrtUnexpanded,
"sqeuclidean": DistanceType.L2Unexpanded,
"cityblock": DistanceType.L1,
"l1": DistanceType.L1,
"manhattan": DistanceType.L1,
"taxicab": DistanceType.L1,
"minkowski": DistanceType.LpUnexpanded,
"canberra": DistanceType.Canberra,
"chebyshev": DistanceType.Linf,
"linf": DistanceType.Linf,
"cityblock": DistanceType.L1,
"cosine": DistanceType.CosineExpanded,
"correlation": DistanceType.CorrelationExpanded,
"hellinger": DistanceType.HellingerExpanded,
"dice": DistanceType.DiceExpanded,
"euclidean": DistanceType.L2SqrtUnexpanded,
"hamming": DistanceType.HammingUnexpanded,
"hellinger": DistanceType.HellingerExpanded,
"jaccard": DistanceType.JaccardExpanded,
"canberra": DistanceType.Canberra
"l1": DistanceType.L1,
"l2": DistanceType.L2SqrtUnexpanded,
"linf": DistanceType.Linf,
"manhattan": DistanceType.L1,
"minkowski": DistanceType.LpUnexpanded,
"sqeuclidean": DistanceType.L2Unexpanded,
"taxicab": DistanceType.L1,
}


DENSE_SUPPORTED_METRICS = [
DistanceType.Canberra,
DistanceType.CorrelationExpanded,
DistanceType.CosineExpanded,
DistanceType.DiceExpanded,
DistanceType.HammingUnexpanded,
DistanceType.HellingerExpanded,
# DistanceType.JaccardExpanded, # not supported
Expand All @@ -173,6 +175,7 @@ SPARSE_SUPPORTED_METRICS = [
DistanceType.Canberra,
DistanceType.CorrelationExpanded,
DistanceType.CosineExpanded,
DistanceType.DiceExpanded,
DistanceType.HammingUnexpanded,
DistanceType.HellingerExpanded,
DistanceType.JaccardExpanded,
Expand Down
11 changes: 6 additions & 5 deletions python/cuml/metrics/pairwise_distances.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,7 +80,8 @@ PAIRWISE_DISTANCE_METRICS = {
"hamming": DistanceType.HammingUnexpanded,
"kldivergence": DistanceType.KLDivergence,
"russellrao": DistanceType.RusselRaoExpanded,
"nan_euclidean": DistanceType.L2Expanded
"nan_euclidean": DistanceType.L2Expanded,
"dice": DistanceType.DiceExpanded,
}

PAIRWISE_DISTANCE_SPARSE_METRICS = {
Expand All @@ -97,7 +98,7 @@ PAIRWISE_DISTANCE_SPARSE_METRICS = {
"jaccard": DistanceType.JaccardExpanded,
"hellinger": DistanceType.HellingerExpanded,
"chebyshev": DistanceType.Linf,
"dice": DistanceType.DiceExpanded
"dice": DistanceType.DiceExpanded,
}


Expand Down Expand Up @@ -344,7 +345,7 @@ def pairwise_distances(X, Y=None, metric="euclidean", handle=None,
if metric in ['nan_euclidean']:
return nan_euclidean_distances(X, Y, **kwds)

if metric in ['russellrao'] and not np.all(X.data == 1.):
if metric in {'russellrao'} and not np.all(X.data == 1.):
warnings.warn("X was converted to boolean for metric {}"
.format(metric))
X = np.where(X != 0., 1.0, 0.0)
Expand All @@ -367,7 +368,7 @@ def pairwise_distances(X, Y=None, metric="euclidean", handle=None,
if (n_samples_x == 1 or n_features_x == 1):
input_order = "K"

if metric in ['russellrao'] and not np.all(Y.data == 1.):
if metric in {'russellrao'} and not np.all(Y.data == 1.):
warnings.warn("Y was converted to boolean for metric {}"
.format(metric))
Y = np.where(Y != 0., 1.0, 0.0)
Expand Down
2 changes: 2 additions & 0 deletions python/cuml/tests/test_umap.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ def test_fuzzy_simplicial_set(n_rows, n_features, n_neighbors):
("chebyshev", True),
("cosine", True),
("correlation", True),
("dice", True),
("jaccard", False),
("hamming", True),
("canberra", True),
Expand Down Expand Up @@ -738,6 +739,7 @@ def test_umap_distance_metrics_fit_transform_trust(metric, supported):
("chebyshev", True, True),
("cosine", True, True),
("correlation", True, True),
("dice", True, True),
("jaccard", True, True),
("hamming", True, True),
("canberra", True, True),
Expand Down
Loading