Skip to content

Commit

Permalink
Add CAGRA InnerProduct test and fix a bug (#595)
Browse files Browse the repository at this point in the history
There is no CAGRA InnerProduct test in cuVS while there was in RAFT. This PR adds the test.

Also, this PR
- fixes an error that occurs when building a CAGRA graph with NN Descent + InnerProduct (`cpp/src/neighbors/nn_descent_*.cu`)
  - Could not pass the `idx.metric() == params.metric` validation in the `nn_descent::detail::build` function.
- updates `RAFT_EXPECT` in the `nn_descent::detail::build` function for better error understanding
- fixes the NN Descent API sample codes

Authors:
  - tsuki (https://github.com/enp1s0)
  - Corey J. Nolet (https://github.com/cjnolet)

Approvers:
  - Corey J. Nolet (https://github.com/cjnolet)

URL: #595
  • Loading branch information
enp1s0 authored Jan 24, 2025
1 parent 80370a1 commit 2f18645
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 54 deletions.
18 changes: 9 additions & 9 deletions cpp/include/cuvs/neighbors/nn_descent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ auto build(raft::resources const& res,
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors::experimental;
* using namespace cuvs::neighbors;
* // use default index parameters
* nn_descent::index_params index_params;
* // create and fill the index from a [N, D] raft::host_matrix_view dataset
* auto index = cagra::build(res, index_params, dataset);
* auto index = nn_descent::build(res, index_params, dataset);
* // index.graph() provides a raft::host_matrix_view of an
* // all-neighbors knn graph of dimensions [N, k] of the input
* // dataset
Expand Down Expand Up @@ -316,11 +316,11 @@ auto build(raft::resources const& res,
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors::experimental;
* using namespace cuvs::neighbors;
* // use default index parameters
* nn_descent::index_params index_params;
* // create and fill the index from a [N, D] raft::host_matrix_view dataset
* auto index = cagra::build(res, index_params, dataset);
* auto index = nn_descent::build(res, index_params, dataset);
* // index.graph() provides a raft::host_matrix_view of an
* // all-neighbors knn graph of dimensions [N, k] of the input
* // dataset
Expand Down Expand Up @@ -384,11 +384,11 @@ auto build(raft::resources const& res,
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors::experimental;
* using namespace cuvs::neighbors;
* // use default index parameters
* nn_descent::index_params index_params;
* // create and fill the index from a [N, D] raft::host_matrix_view dataset
* auto index = cagra::build(res, index_params, dataset);
* auto index = nn_descent::build(res, index_params, dataset);
* // index.graph() provides a raft::host_matrix_view of an
* // all-neighbors knn graph of dimensions [N, k] of the input
* // dataset
Expand Down Expand Up @@ -452,11 +452,11 @@ auto build(raft::resources const& res,
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors::experimental;
* using namespace cuvs::neighbors;
* // use default index parameters
* nn_descent::index_params index_params;
* // create and fill the index from a [N, D] raft::host_matrix_view dataset
* auto index = cagra::build(res, index_params, dataset);
* auto index = nn_descent::build(res, index_params, dataset);
* // index.graph() provides a raft::host_matrix_view of an
* // all-neighbors knn graph of dimensions [N, k] of the input
* // dataset
Expand Down Expand Up @@ -492,4 +492,4 @@ bool has_enough_device_memory(raft::resources const& res,
raft::matrix_extent<int64_t> dataset,
size_t idx_size = 4);

} // namespace cuvs::neighbors::nn_descent
} // namespace cuvs::neighbors::nn_descent
5 changes: 4 additions & 1 deletion cpp/src/neighbors/detail/nn_descent.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -1442,8 +1442,11 @@ void build(raft::resources const& res,
auto allowed_metrics = params.metric == cuvs::distance::DistanceType::L2Expanded ||
params.metric == cuvs::distance::DistanceType::CosineExpanded ||
params.metric == cuvs::distance::DistanceType::InnerProduct;
RAFT_EXPECTS(allowed_metrics && idx.metric() == params.metric,
RAFT_EXPECTS(allowed_metrics,
"The metric for NN Descent should be L2Expanded, CosineExpanded or InnerProduct");
RAFT_EXPECTS(
idx.metric() == params.metric,
"The metrics set in nn_descent::index_params and nn_descent::index are inconsistent");
size_t intermediate_degree = params.intermediate_graph_degree;
size_t graph_degree = params.graph_degree;
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/neighbors/nn_descent_float.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
}; \
Expand All @@ -47,7 +48,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/neighbors/nn_descent_half.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand All @@ -48,7 +49,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/neighbors/nn_descent_int8.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand All @@ -48,7 +49,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/neighbors/nn_descent_uint8.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand All @@ -48,7 +49,8 @@ namespace cuvs::neighbors::nn_descent {
} else { \
std::optional<raft::device_matrix_view<float, int64_t, raft::row_major>> distances = \
std::nullopt; \
cuvs::neighbors::nn_descent::index<IdxT> idx{handle, graph.value(), distances}; \
cuvs::neighbors::nn_descent::index<IdxT> idx{ \
handle, graph.value(), distances, params.metric}; \
cuvs::neighbors::nn_descent::build<T, IdxT>(handle, params, dataset, idx); \
return idx; \
} \
Expand Down
72 changes: 36 additions & 36 deletions cpp/test/neighbors/ann_cagra.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ inline std::vector<AnnCagraInputs> generate_inputs()
{0},
{256},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false},
{true},
{0.995});
Expand All @@ -903,7 +903,7 @@ inline std::vector<AnnCagraInputs> generate_inputs()
{0},
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false},
{true},
{0.995});
Expand All @@ -919,7 +919,7 @@ inline std::vector<AnnCagraInputs> generate_inputs()
{0, 8, 16, 32}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false},
{false},
{0.995});
Expand All @@ -936,27 +936,27 @@ inline std::vector<AnnCagraInputs> generate_inputs()
{0}, // team_size
{32, 64, 128, 256, 512, 768},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false},
{true},
{0.995});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());

inputs2 =
raft::util::itertools::product<AnnCagraInputs>({100},
{10000, 20000},
{32},
{10},
{graph_build_algo::AUTO},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false, true},
{false},
{0.985});
inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{10000, 20000},
{32},
{10},
{graph_build_algo::AUTO},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false, true},
{false},
{0.985});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());

// a few PQ configurations
Expand Down Expand Up @@ -988,22 +988,22 @@ inline std::vector<AnnCagraInputs> generate_inputs()
}

// refinement options
inputs2 =
raft::util::itertools::product<AnnCagraInputs>({100},
{5000},
{32, 64},
{16},
{graph_build_algo::IVF_PQ},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false, true},
{false},
{0.99},
{1.0f, 2.0f, 3.0f});
inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{5000},
{32, 64},
{16},
{graph_build_algo::IVF_PQ},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false, true},
{false},
{0.99},
{1.0f, 2.0f, 3.0f});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());

inputs2 = raft::util::itertools::product<AnnCagraInputs>(
Expand All @@ -1017,7 +1017,7 @@ inline std::vector<AnnCagraInputs> generate_inputs()
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{cuvs::distance::DistanceType::L2Expanded, cuvs::distance::DistanceType::InnerProduct},
{false},
{false},
{0.995});
Expand Down

0 comments on commit 2f18645

Please sign in to comment.