Skip to content

Commit

Permalink
Merge pull request #362 from rapidsai/branch-24.10
Browse files Browse the repository at this point in the history
Forward-merge branch-24.10 into branch-24.12
  • Loading branch information
GPUtester authored Sep 27, 2024
2 parents aa717ec + c616a22 commit 845f88e
Show file tree
Hide file tree
Showing 25 changed files with 770 additions and 112 deletions.
11 changes: 7 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ endif()
add_library(
cuvs-cagra-search STATIC
src/neighbors/cagra_search_float.cu
src/neighbors/cagra_search_half.cu
src/neighbors/cagra_search_int8.cu
src/neighbors/cagra_search_uint8.cu
src/neighbors/detail/cagra/compute_distance.cu
Expand Down Expand Up @@ -257,14 +258,10 @@ add_library(
src/neighbors/detail/cagra/search_multi_cta_half_uint32.cu
src/neighbors/detail/cagra/search_multi_cta_int8_uint32.cu
src/neighbors/detail/cagra/search_multi_cta_uint8_uint32.cu
src/neighbors/detail/cagra/search_multi_cta_float_uint64.cu
src/neighbors/detail/cagra/search_multi_cta_half_uint64.cu
src/neighbors/detail/cagra/search_single_cta_float_uint32.cu
src/neighbors/detail/cagra/search_single_cta_half_uint32.cu
src/neighbors/detail/cagra/search_single_cta_int8_uint32.cu
src/neighbors/detail/cagra/search_single_cta_uint8_uint32.cu
src/neighbors/detail/cagra/search_single_cta_float_uint64.cu
src/neighbors/detail/cagra/search_single_cta_half_uint64.cu
)

file(GLOB_RECURSE compute_distance_sources "src/neighbors/detail/cagra/compute_distance_*.cu")
Expand Down Expand Up @@ -350,13 +347,15 @@ add_library(
src/distance/pairwise_distance.cu
src/neighbors/brute_force.cu
src/neighbors/cagra_build_float.cu
src/neighbors/cagra_build_half.cu
src/neighbors/cagra_build_int8.cu
src/neighbors/cagra_build_uint8.cu
src/neighbors/cagra_extend_float.cu
src/neighbors/cagra_extend_int8.cu
src/neighbors/cagra_extend_uint8.cu
src/neighbors/cagra_optimize.cu
src/neighbors/cagra_serialize_float.cu
src/neighbors/cagra_serialize_half.cu
src/neighbors/cagra_serialize_int8.cu
src/neighbors/cagra_serialize_uint8.cu
src/neighbors/detail/cagra/cagra_build.cpp
Expand All @@ -378,6 +377,7 @@ add_library(
src/neighbors/ivf_pq/ivf_pq_serialize.cu
src/neighbors/ivf_pq/ivf_pq_deserialize.cu
src/neighbors/ivf_pq/detail/ivf_pq_build_extend_float_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_build_extend_half_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_build_extend_int8_t_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_build_extend_uint8_t_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_compute_similarity_half_fp8_false.cu
Expand All @@ -395,13 +395,16 @@ add_library(
src/neighbors/ivf_pq/detail/ivf_pq_compute_similarity_float_fp8_false_bitset64.cu
src/neighbors/ivf_pq/detail/ivf_pq_compute_similarity_float_fp8_true_bitset64.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_float_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_half_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_int8_t_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_uint8_t_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_with_filter_float_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_with_filter_half_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_with_filter_int8_t_int64_t.cu
src/neighbors/ivf_pq/detail/ivf_pq_search_with_filter_uint8_t_int64_t.cu
src/neighbors/nn_descent.cu
src/neighbors/nn_descent_float.cu
src/neighbors/nn_descent_half.cu
src/neighbors/nn_descent_int8.cu
src/neighbors/nn_descent_uint8.cu
src/neighbors/refine/detail/refine_device_float_float.cu
Expand Down
22 changes: 11 additions & 11 deletions cpp/bench/ann/src/common/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,17 @@ inline auto run_main(int argc, char** argv) -> int
override_kv,
metric_objective,
threads);
// } else if (dtype == "half") {
// dispatch_benchmark<half>(cmdline
// conf,
// force_overwrite,
// build_mode,
// search_mode,
// data_prefix,
// index_prefix,
// override_kv,
// metric_objective,
// threads);
} else if (dtype == "half") {
dispatch_benchmark<half>(cmdline,
conf,
force_overwrite,
build_mode,
search_mode,
data_prefix,
index_prefix,
override_kv,
metric_objective,
threads);
} else if (dtype == "uint8") {
dispatch_benchmark<std::uint8_t>(cmdline,
conf,
Expand Down
2 changes: 1 addition & 1 deletion cpp/bench/ann/src/cuvs/cuvs_benchmark.cu
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ auto create_search_param(const std::string& algo_name, const nlohmann::json& con
}; // namespace cuvs::bench

REGISTER_ALGO_INSTANCE(float);
// REGISTER_ALGO_INSTANCE(half);
REGISTER_ALGO_INSTANCE(half);
REGISTER_ALGO_INSTANCE(std::int8_t);
REGISTER_ALGO_INSTANCE(std::uint8_t);

Expand Down
2 changes: 1 addition & 1 deletion cpp/bench/ann/src/cuvs/cuvs_cagra_half.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
#include "cuvs_cagra_wrapper.h"

namespace cuvs::bench {
// template class cuvs_cagra<half, uint32_t>;
template class cuvs_cagra<half, uint32_t>;
} // namespace cuvs::bench
6 changes: 5 additions & 1 deletion cpp/bench/ann/src/cuvs/cuvs_cagra_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ void cuvs_cagra<T, IdxT>::save(const std::string& file) const
template <typename T, typename IdxT>
void cuvs_cagra<T, IdxT>::save_to_hnswlib(const std::string& file) const
{
cuvs::neighbors::cagra::serialize_to_hnswlib(handle_, file, *index_);
if constexpr (!std::is_same_v<T, half>) {
cuvs::neighbors::cagra::serialize_to_hnswlib(handle_, file, *index_);
} else {
RAFT_FAIL("Cannot save fp16 index to hnswlib format");
}
}

template <typename T, typename IdxT>
Expand Down
2 changes: 1 addition & 1 deletion cpp/bench/ann/src/cuvs/cuvs_ivf_pq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace cuvs::bench {
template class cuvs_ivf_pq<float, int64_t>;
// template class cuvs_ivf_pq<half, int64_t>;
template class cuvs_ivf_pq<half, int64_t>;
template class cuvs_ivf_pq<uint8_t, int64_t>;
template class cuvs_ivf_pq<int8_t, int64_t>;
} // namespace cuvs::bench
205 changes: 197 additions & 8 deletions cpp/include/cuvs/neighbors/cagra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,78 @@ auto build(raft::resources const& res,
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const half, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<half, uint32_t>;

/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors, distances);
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (host) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::host_matrix_view<const half, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<half, uint32_t>;

/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors, distances);
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (device) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> dataset)
Expand Down Expand Up @@ -975,9 +1047,6 @@ void extend(
*
* See the [cagra::build](#cagra::build) documentation for a usage example.
*
* @tparam T data element type
* @tparam IdxT type of the indices
*
* @param[in] res raft resources
* @param[in] params configure the search
* @param[in] idx cagra index
Expand All @@ -1000,8 +1069,26 @@ void search(raft::resources const& res,
*
* See the [cagra::build](#cagra::build) documentation for a usage example.
*
* @tparam T data element type
* @tparam IdxT type of the indices
* @param[in] res raft resources
* @param[in] params configure the search
* @param[in] index cagra index
* @param[in] queries a device matrix view to a row-major matrix [n_queries, index->dim()]
* @param[out] neighbors a device matrix view to the indices of the neighbors in the source dataset
* [n_queries, k]
* @param[out] distances a device matrix view to the distances to the selected neighbors [n_queries,
* k]
*/
void search(raft::resources const& res,
cuvs::neighbors::cagra::search_params const& params,
const cuvs::neighbors::cagra::index<half, uint32_t>& index,
raft::device_matrix_view<const half, int64_t, raft::row_major> queries,
raft::device_matrix_view<uint32_t, int64_t, raft::row_major> neighbors,
raft::device_matrix_view<float, int64_t, raft::row_major> distances);

/**
* @brief Search ANN using the constructed index.
*
* See the [cagra::build](#cagra::build) documentation for a usage example.
*
* @param[in] res raft resources
* @param[in] params configure the search
Expand All @@ -1024,9 +1111,6 @@ void search(raft::resources const& res,
*
* See the [cagra::build](#cagra::build) documentation for a usage example.
*
* @tparam T data element type
* @tparam IdxT type of the indices
*
* @param[in] res raft resources
* @param[in] params configure the search
* @param[in] index cagra index
Expand Down Expand Up @@ -1156,6 +1240,111 @@ void serialize(raft::resources const& handle,
void deserialize(raft::resources const& handle,
std::istream& is,
cuvs::neighbors::cagra::index<float, uint32_t>* index);
/**
* Save the index to file.
*
* Experimental, both the API and the serialization format are subject to change.
*
* @code{.cpp}
* #include <raft/core/resources.hpp>
* #include <cuvs/neighbors/cagra.hpp>
*
* raft::resources handle;
*
* // create a string with a filepath
* std::string filename("/path/to/index");
* // create an index with `auto index = cuvs::neighbors::cagra::build(...);`
* cuvs::neighbors::cagra::serialize(handle, filename, index);
* @endcode
*
* @param[in] handle the raft handle
* @param[in] filename the file name for saving the index
* @param[in] index CAGRA index
* @param[in] include_dataset Whether or not to write out the dataset to the file.
*
*/
void serialize(raft::resources const& handle,
const std::string& filename,
const cuvs::neighbors::cagra::index<half, uint32_t>& index,
bool include_dataset = true);

/**
* Load index from file.
*
* Experimental, both the API and the serialization format are subject to change.
*
* @code{.cpp}
* #include <raft/core/resources.hpp>
* #include <cuvs/neighbors/cagra.hpp>
*
* raft::resources handle;
*
* // create a string with a filepath
* std::string filename("/path/to/index");
* cuvs::neighbors::cagra::index<half, uint32_t> index;
* cuvs::neighbors::cagra::deserialize(handle, filename, &index);
* @endcode
*
* @param[in] handle the raft handle
* @param[in] filename the name of the file that stores the index
* @param[out] index the cagra index
*/
void deserialize(raft::resources const& handle,
const std::string& filename,
cuvs::neighbors::cagra::index<half, uint32_t>* index);

/**
* Write the index to an output stream
*
* Experimental, both the API and the serialization format are subject to change.
*
* @code{.cpp}
* #include <raft/core/resources.hpp>
* #include <cuvs/neighbors/cagra.hpp>
*
* raft::resources handle;
*
* // create an output stream
* std::ostream os(std::cout.rdbuf());
* // create an index with `auto index = cuvs::neighbors::cagra::build(...);`
* cuvs::neighbors::cagra::serialize(handle, os, index);
* @endcode
*
* @param[in] handle the raft handle
* @param[in] os output stream
* @param[in] index CAGRA index
* @param[in] include_dataset Whether or not to write out the dataset to the file.
*/
void serialize(raft::resources const& handle,
std::ostream& os,
const cuvs::neighbors::cagra::index<half, uint32_t>& index,
bool include_dataset = true);

/**
* Load index from input stream
*
* Experimental, both the API and the serialization format are subject to change.
*
* @code{.cpp}
* #include <raft/core/resources.hpp>
* #include <cuvs/neighbors/cagra.hpp>
*
* raft::resources handle;
*
* // create an input stream
* std::istream is(std::cin.rdbuf());
* cuvs::neighbors::cagra::index<half, uint32_t> index;
* cuvs::neighbors::cagra::deserialize(handle, is, &index);
* @endcode
*
* @param[in] handle the raft handle
* @param[in] is input stream
* @param[out] index the cagra index
*/
void deserialize(raft::resources const& handle,
std::istream& is,
cuvs::neighbors::cagra::index<half, uint32_t>* index);

/**
* Save the index to file.
Expand Down
Loading

0 comments on commit 845f88e

Please sign in to comment.