Skip to content
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
6 changes: 1 addition & 5 deletions c/src/neighbors/cagra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,7 @@ static void update_dataset(raft::resources* res_ptr,
}

static void _set_graph_build_params(
std::variant<std::monostate,
cuvs::neighbors::cagra::graph_build_params::ivf_pq_params,
cuvs::neighbors::cagra::graph_build_params::nn_descent_params,
cuvs::neighbors::cagra::graph_build_params::ace_params,
cuvs::neighbors::cagra::graph_build_params::iterative_search_params>& out_params,
cuvs::neighbors::cagra::graph_build_params_t& out_params,
cuvsCagraIndexParams& params,
cuvsCagraGraphBuildAlgo algo,
int64_t n_rows,
Expand Down
91 changes: 90 additions & 1 deletion cpp/include/cuvs/neighbors/cagra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cuvs/core/bitset.hpp>
#include <cuvs/core/export.hpp>
#include <cuvs/distance/distance.hpp>
#include <cuvs/neighbors/brute_force.hpp>
#include <cuvs/neighbors/common.hpp>
#include <cuvs/neighbors/ivf_pq.hpp>
#include <cuvs/neighbors/nn_descent.hpp>
Expand Down Expand Up @@ -254,7 +255,8 @@ using graph_build_params_t = std::variant<std::monostate,
graph_build_params::ivf_pq_params,
graph_build_params::nn_descent_params,
graph_build_params::ace_params,
graph_build_params::iterative_search_params>;
graph_build_params::iterative_search_params,
graph_build_params::brute_force_params>;

/**
* @brief A strategy for selecting the graph build parameters based on similar HNSW index
Expand Down Expand Up @@ -317,6 +319,9 @@ struct index_params : cuvs::neighbors::index_params {
* // 4. Choose iterative graph building using CAGRA's search() and optimize() [Experimental]
* params.graph_build_params =
* cagra::graph_build_params::iterative_search_params();
*
* // 5. Choose exact brute-force kNN construction (float and half datasets)
* params.graph_build_params = cagra::graph_build_params::brute_force_params{};
* @endcode
*/
graph_build_params_t graph_build_params;
Expand Down Expand Up @@ -4415,6 +4420,90 @@ void distribute(const raft::resources& clique,
const std::string& filename,
cuvs::neighbors::mg_index<cagra::device_padded_index<T, IdxT>, T, IdxT>* index);

/**
* @brief Build an exact kNN graph using brute-force search.
* @ingroup cagra_cpp_index_build
*
* Each row of the output contains the nearest neighbor indices for the corresponding dataset
* point, ordered by distance and excluding the point itself. The graph is stored in host memory.
* The dataset is processed in chunks, allowing graphs larger than available device memory to be
* built.
*
* @param[in] res RAFT resources.
* @param[in] dataset Row-major host dataset of float values, with shape [n_rows, dim].
* @param[out] knn_graph Host output with shape [n_rows, degree], where degree is in [1, n_rows -
* 1].
* @param[in] build_params Brute-force index and search parameters. Supported metrics are
* L2Expanded, L2SqrtExpanded, InnerProduct and CosineExpanded.
*/
void build_knn_graph(raft::resources const& res,
raft::host_matrix_view<const float, int64_t, raft::row_major> dataset,
raft::host_matrix_view<uint32_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params build_params);

/**
* @brief Build an exact kNN graph using brute-force search.
* @ingroup cagra_cpp_index_build
*
* Each row of the output contains the nearest neighbor indices for the corresponding dataset
* point, ordered by distance and excluding the point itself. The graph is stored in host memory.
* The dataset is processed in chunks, allowing graphs larger than available device memory to be
* built.
*
* @param[in] res RAFT resources.
* @param[in] dataset Row-major device dataset of float values, with shape [n_rows, dim].
* @param[out] knn_graph Host output with shape [n_rows, degree], where degree is in [1, n_rows -
* 1].
* @param[in] build_params Brute-force index and search parameters. Supported metrics are
* L2Expanded, L2SqrtExpanded, InnerProduct and CosineExpanded.
*/
void build_knn_graph(raft::resources const& res,
raft::device_matrix_view<const float, int64_t, raft::row_major> dataset,
raft::host_matrix_view<uint32_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params build_params);

/**
* @brief Build an exact kNN graph using brute-force search.
* @ingroup cagra_cpp_index_build
*
* Each row of the output contains the nearest neighbor indices for the corresponding dataset
* point, ordered by distance and excluding the point itself. The graph is stored in host memory.
* The dataset is processed in chunks, allowing graphs larger than available device memory to be
* built.
*
* @param[in] res RAFT resources.
* @param[in] dataset Row-major host dataset of half values, with shape [n_rows, dim].
* @param[out] knn_graph Host output with shape [n_rows, degree], where degree is in [1, n_rows -
* 1].
* @param[in] build_params Brute-force index and search parameters. Supported metrics are
* L2Expanded, L2SqrtExpanded, InnerProduct and CosineExpanded.
*/
void build_knn_graph(raft::resources const& res,
raft::host_matrix_view<const half, int64_t, raft::row_major> dataset,
raft::host_matrix_view<uint32_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params build_params);

/**
* @brief Build an exact kNN graph using brute-force search.
* @ingroup cagra_cpp_index_build
*
* Each row of the output contains the nearest neighbor indices for the corresponding dataset
* point, ordered by distance and excluding the point itself. The graph is stored in host memory.
* The dataset is processed in chunks, allowing graphs larger than available device memory to be
* built.
*
* @param[in] res RAFT resources.
* @param[in] dataset Row-major device dataset of half values, with shape [n_rows, dim].
* @param[out] knn_graph Host output with shape [n_rows, degree], where degree is in [1, n_rows -
* 1].
* @param[in] build_params Brute-force index and search parameters. Supported metrics are
* L2Expanded, L2SqrtExpanded, InnerProduct and CosineExpanded.
*/
void build_knn_graph(raft::resources const& res,
raft::device_matrix_view<const half, int64_t, raft::row_major> dataset,
raft::host_matrix_view<uint32_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params build_params);

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/**
* @brief Build a kNN graph using IVF-PQ.
*
Expand Down
10 changes: 10 additions & 0 deletions cpp/src/neighbors/cagra.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ void build_knn_graph(
detail::build_knn_graph<DataT, IdxT>(res, dataset, knn_graph, build_params);
}

template <typename DataT, typename IdxT, typename Accessor>
void build_knn_graph(
raft::resources const& res,
raft::mdspan<const DataT, raft::matrix_extent<int64_t>, raft::row_major, Accessor> dataset,
raft::host_matrix_view<IdxT, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params params)
{
detail::build_knn_graph(res, dataset, knn_graph, params);
}

/**
* @brief Sort a KNN graph index.
* Preprocessing step for `cagra::optimize`: If a KNN graph is not built using
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/neighbors/cagra_build_inst.cu.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ void build_knn_graph(raft::resources const& handle,
cuvs::neighbors::cagra::build_knn_graph<data_t, index_t>(handle, dataset, knn_graph, params);
}

void build_knn_graph(raft::resources const& res,
raft::host_matrix_view<const data_t, int64_t, raft::row_major> dataset,
raft::host_matrix_view<index_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params params)
{
detail::build_knn_graph(res, dataset, knn_graph, params);
}

void build_knn_graph(raft::resources const& res,
raft::device_matrix_view<const data_t, int64_t, raft::row_major> dataset,
raft::host_matrix_view<index_t, int64_t, raft::row_major> knn_graph,
graph_build_params::brute_force_params params)
{
detail::build_knn_graph(res, dataset, knn_graph, params);
}

#define CUVS_INST_CAGRA_BUILD(DatasetViewT) \
auto build(raft::resources const& res, \
const cuvs::neighbors::cagra::index_params& params, \
Expand Down
11 changes: 9 additions & 2 deletions cpp/src/neighbors/detail/cagra/cagra_build.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../../../util/kvikio_io.hpp"
#include "../../ivf_pq/ivf_pq_fp16_overflow.cuh"
#include "cagra_ace.cuh"
#include "cagra_build_brute_force.cuh"
#include "cagra_search.cuh"
#include "graph_core.cuh"
#include <cuvs/preprocessing/quantize/pq.hpp>
Expand Down Expand Up @@ -982,11 +983,12 @@ inline void validate_cagra_knn_graph_build_constraints(index_params const& param
std::holds_alternative<cagra::graph_build_params::iterative_search_params>(
knn_build_params) ||
std::holds_alternative<cagra::graph_build_params::nn_descent_params>(knn_build_params),
"IVF_PQ for CAGRA graph build does not support BitwiseHamming as a metric. Please "
"The selected CAGRA graph builder does not support BitwiseHamming as a metric. Please "
"use nn-descent or the iterative CAGRA search build.");
RAFT_EXPECTS(
params.metric != cuvs::distance::DistanceType::CosineExpanded ||
std::holds_alternative<cagra::graph_build_params::ivf_pq_params>(knn_build_params) ||
std::holds_alternative<cagra::graph_build_params::brute_force_params>(knn_build_params) ||
std::holds_alternative<cagra::graph_build_params::nn_descent_params>(knn_build_params),
"CosineExpanded distance is not supported for iterative CAGRA graph build.");

Expand All @@ -997,7 +999,8 @@ inline void validate_cagra_knn_graph_build_constraints(index_params const& param
}

/**
* Iterative / IVF-PQ / NN-descent KNN graph construction and `optimize` → final host CAGRA graph.
* Iterative / IVF-PQ / NN-descent / brute-force KNN graph construction and `optimize` → final host
* CAGRA graph.
*
* @param knn_graph_dataset mdspan passed to IVF-PQ / NN-descent `build_knn_graph` (any stride).
*/
Expand Down Expand Up @@ -1026,6 +1029,10 @@ auto build_cagra_host_graph_from_knn_params(raft::resources const& res,
ivf_pq_params.build_params.metric = params.metric;
}
build_knn_graph(res, knn_graph_dataset, knn_graph->view(), ivf_pq_params);
} else if (std::holds_alternative<graph_build_params::brute_force_params>(knn_build_params)) {
auto brute_force_params = std::get<graph_build_params::brute_force_params>(knn_build_params);
brute_force_params.build_params.metric = params.metric;
build_knn_graph(res, knn_graph_dataset, knn_graph->view(), brute_force_params);
} else {
auto nn_descent_params =
std::get<cagra::graph_build_params::nn_descent_params>(knn_build_params);
Expand Down
Loading
Loading