-
Notifications
You must be signed in to change notification settings - Fork 540
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
Add hamming, jensen-shannon, kl-divergence, correlation and russellrao distance metrics #4155
Changes from 15 commits
dfc28a4
ad67053
38729dc
0a734fd
aa072ac
38e1067
6dd14bf
911eef8
ae9f7a2
5fc77f7
90c8f16
1d5c966
8225ee0
3f6de71
19d44db
b71866a
3ef11bc
1a1dd71
96c0240
675b661
ac4f067
8e2f01d
6427584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
/* | ||
* Copyright (c) 2021, 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 <raft/distance/distance.cuh> | ||
#include <raft/handle.hpp> | ||
#include "pairwise_distance_correlation.cuh" | ||
|
||
namespace ML { | ||
|
||
namespace Metrics { | ||
void pairwise_distance_correlation(const raft::handle_t& handle, | ||
const double* x, | ||
const double* y, | ||
double* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
double metric_arg) | ||
{ | ||
// Allocate workspace | ||
raft::mr::device::buffer<char> workspace(handle.get_device_allocator(), handle.get_stream(), 1); | ||
|
||
// Call the distance function | ||
switch (metric) { | ||
case raft::distance::DistanceType::CorrelationExpanded: | ||
raft::distance:: | ||
pairwise_distance_impl<double, int, raft::distance::DistanceType::CorrelationExpanded>( | ||
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor); | ||
break; | ||
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric); | ||
} | ||
} | ||
|
||
void pairwise_distance_correlation(const raft::handle_t& handle, | ||
const float* x, | ||
const float* y, | ||
float* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
float metric_arg) | ||
{ | ||
// Allocate workspace | ||
raft::mr::device::buffer<char> workspace(handle.get_device_allocator(), handle.get_stream(), 1); | ||
|
||
// Call the distance function | ||
switch (metric) { | ||
case raft::distance::DistanceType::CorrelationExpanded: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the switch state propagated into this in case there is also an unexpanded version in the future? (Asking for the other distances as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No there is no unexpanded version planned for this or other distance metrics. should I replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are named after the associated distance measure so I would call a function named pairwise_distance_correlation expecting it to compute the correlation distance. If it's not possible that this will ever compute a distance other than correlation, why are we passing the DistanceType into these functions at all? Rather than using an if condition, I would propose removing the metric argument altogether and just having the function compute the distance for which its name implies. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! I think the metric arg got propagated to all distances due to euclidean dist having multiple sub-implementations. |
||
raft::distance:: | ||
pairwise_distance_impl<float, int, raft::distance::DistanceType::CorrelationExpanded>( | ||
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor); | ||
break; | ||
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric); | ||
} | ||
} | ||
|
||
} // namespace Metrics | ||
} // namespace ML |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/* | ||
* Copyright (c) 2021, 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/distance/distance.cuh> | ||
#include <raft/handle.hpp> | ||
|
||
namespace ML { | ||
|
||
namespace Metrics { | ||
void pairwise_distance_correlation(const raft::handle_t& handle, | ||
const double* x, | ||
const double* y, | ||
double* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
double metric_arg); | ||
|
||
void pairwise_distance_correlation(const raft::handle_t& handle, | ||
const float* x, | ||
const float* y, | ||
float* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
float metric_arg); | ||
|
||
} // namespace Metrics | ||
} // namespace ML |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
/* | ||
* Copyright (c) 2021, 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 <raft/distance/distance.cuh> | ||
#include <raft/handle.hpp> | ||
#include "pairwise_distance_hamming.cuh" | ||
|
||
namespace ML { | ||
|
||
namespace Metrics { | ||
void pairwise_distance_hamming(const raft::handle_t& handle, | ||
const double* x, | ||
const double* y, | ||
double* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
double metric_arg) | ||
{ | ||
// Allocate workspace | ||
raft::mr::device::buffer<char> workspace(handle.get_device_allocator(), handle.get_stream(), 0); | ||
|
||
// Call the distance function | ||
switch (metric) { | ||
case raft::distance::DistanceType::HammingUnexpanded: | ||
raft::distance:: | ||
pairwise_distance_impl<double, int, raft::distance::DistanceType::HammingUnexpanded>( | ||
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor); | ||
break; | ||
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric); | ||
} | ||
} | ||
|
||
void pairwise_distance_hamming(const raft::handle_t& handle, | ||
const float* x, | ||
const float* y, | ||
float* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
float metric_arg) | ||
{ | ||
// Allocate workspace | ||
raft::mr::device::buffer<char> workspace(handle.get_device_allocator(), handle.get_stream(), 0); | ||
|
||
// Call the distance function | ||
switch (metric) { | ||
case raft::distance::DistanceType::HammingUnexpanded: | ||
raft::distance:: | ||
pairwise_distance_impl<float, int, raft::distance::DistanceType::HammingUnexpanded>( | ||
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor); | ||
break; | ||
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric); | ||
} | ||
} | ||
|
||
} // namespace Metrics | ||
} // namespace ML |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/* | ||
* Copyright (c) 2021, 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/distance/distance.cuh> | ||
#include <raft/handle.hpp> | ||
|
||
namespace ML { | ||
|
||
namespace Metrics { | ||
void pairwise_distance_hamming(const raft::handle_t& handle, | ||
const double* x, | ||
const double* y, | ||
double* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
double metric_arg); | ||
|
||
void pairwise_distance_hamming(const raft::handle_t& handle, | ||
const float* x, | ||
const float* y, | ||
float* dist, | ||
int m, | ||
int n, | ||
int k, | ||
raft::distance::DistanceType metric, | ||
bool isRowMajor, | ||
float metric_arg); | ||
|
||
} // namespace Metrics | ||
} // namespace ML |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder to update this now that the raft side has been merged