Skip to content
Merged
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
3 changes: 1 addition & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# =============================================================================
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
# Copyright (c) 2020-2025, 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
Expand Down Expand Up @@ -416,7 +416,6 @@ if(BUILD_SHARED_LIBS)
src/neighbors/iface/iface_pq_half_int64_t.cu
src/neighbors/iface/iface_pq_int8_t_int64_t.cu
src/neighbors/iface/iface_pq_uint8_t_int64_t.cu
src/neighbors/detail/cagra/cagra_build.cpp
src/neighbors/detail/cagra/topk_for_cagra/topk.cu
src/neighbors/dynamic_batching.cu
src/neighbors/cagra_index_wrapper.cu
Expand Down
17 changes: 1 addition & 16 deletions cpp/include/cuvs/neighbors/all_neighbors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <cuvs/neighbors/graph_build_types.hpp>
#include <cuvs/neighbors/ivf_pq.hpp>
#include <cuvs/neighbors/nn_descent.hpp>

Expand All @@ -27,22 +28,6 @@ namespace cuvs::neighbors::all_neighbors {
* @{
*/

/**
* @brief Parameters used to build an all-neighbors knn graph (find nearest neighbors for all the
* training vectors)
*/
namespace graph_build_params {

/** Specialized parameters utilizing IVF-PQ to build knn graph */
struct ivf_pq_params {
cuvs::neighbors::ivf_pq::index_params build_params;
cuvs::neighbors::ivf_pq::search_params search_params;
float refinement_rate = 2.0;
};

using nn_descent_params = cuvs::neighbors::nn_descent::index_params;
} // namespace graph_build_params

using GraphBuildParams =
std::variant<graph_build_params::ivf_pq_params, graph_build_params::nn_descent_params>;

Expand Down
43 changes: 4 additions & 39 deletions cpp/include/cuvs/neighbors/cagra.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023-2024, NVIDIA CORPORATION.
* Copyright (c) 2023-2025, 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 All @@ -19,6 +19,7 @@
#include "common.hpp"
#include <cuvs/distance/distance.hpp>
#include <cuvs/neighbors/common.hpp>
#include <cuvs/neighbors/graph_build_types.hpp>
#include <cuvs/neighbors/ivf_pq.hpp>
#include <cuvs/neighbors/nn_descent.hpp>
#include <raft/core/device_mdspan.hpp>
Expand All @@ -35,49 +36,13 @@
#include <variant>

namespace cuvs::neighbors::cagra {
// For re-exporting into cagra namespace
namespace graph_build_params = cuvs::neighbors::graph_build_params;
/**
* @defgroup cagra_cpp_index_params CAGRA index build parameters
* @{
*/

/**
* @brief ANN parameters used by CAGRA to build knn graph
*
*/
namespace graph_build_params {

/** Specialized parameters utilizing IVF-PQ to build knn graph */
struct ivf_pq_params {
cuvs::neighbors::ivf_pq::index_params build_params;
cuvs::neighbors::ivf_pq::search_params search_params;
float refinement_rate;

ivf_pq_params() = default;
/**
* Set default parameters based on shape of the input dataset.
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* raft::resources res;
* // create index_params for a [N. D] dataset
* auto dataset = raft::make_device_matrix<float, int64_t>(res, N, D);
* auto pq_params =
* cagra::graph_build_params::ivf_pq_params(dataset.extents());
* // modify/update index_params as needed
* pq_params.kmeans_trainset_fraction = 0.1;
* @endcode
*/
ivf_pq_params(raft::matrix_extent<int64_t> dataset_extents,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded);
};

using nn_descent_params = cuvs::neighbors::nn_descent::index_params;

// **** Experimental ****
using iterative_search_params = cuvs::neighbors::search_params;

} // namespace graph_build_params

struct index_params : cuvs::neighbors::index_params {
/** Degree of input graph for pruning. */
size_t intermediate_graph_degree = 128;
Expand Down
78 changes: 78 additions & 0 deletions cpp/include/cuvs/neighbors/graph_build_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cuVS isn't header-only, so the header files are very lightweight. Instead of introducing new headers for structs and enums, can we just reuse common.hpp?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.hpp has the base index_params (link), so declaring graph_build_params in that file results in a circular dependency (because we make use of ivf_pq::index_params and nn_descent::index_params inside `graph_build_params)

* Copyright (c) 2025, 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 <cuvs/neighbors/ivf_pq.hpp>
#include <cuvs/neighbors/nn_descent.hpp>

namespace cuvs::neighbors {

/**
* @defgroup neighbors_build_algo Graph build algorithm types
* @{
*/

enum GRAPH_BUILD_ALGO { BRUTE_FORCE = 0, IVF_PQ = 1, NN_DESCENT = 1 };

namespace graph_build_params {

/** Specialized parameters utilizing IVF-PQ to build knn graph */
struct ivf_pq_params {
cuvs::neighbors::ivf_pq::index_params build_params;
cuvs::neighbors::ivf_pq::search_params search_params;
float refinement_rate = 1.0;

ivf_pq_params() = default;

/**
* Set default parameters based on shape of the input dataset.
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* raft::resources res;
* // create index_params for a [N. D] dataset
* auto dataset = raft::make_device_matrix<float, int64_t>(res, N, D);
* auto pq_params =
* graph_build_params::ivf_pq_params(dataset.extents());
* // modify/update index_params as needed
* pq_params.kmeans_trainset_fraction = 0.1;
* @endcode
*/
ivf_pq_params(raft::matrix_extent<int64_t> dataset_extents,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded)
{
build_params = cuvs::neighbors::ivf_pq::index_params::from_dataset(dataset_extents, metric);

search_params = cuvs::neighbors::ivf_pq::search_params{};
search_params.n_probes = std::max<uint32_t>(10, build_params.n_lists * 0.01);
search_params.lut_dtype = CUDA_R_16F;
search_params.internal_distance_dtype = CUDA_R_16F;
search_params.coarse_search_dtype = CUDA_R_16F;
search_params.max_internal_batch_size = 128 * 1024;

refinement_rate = 1;
}
};

using nn_descent_params = cuvs::neighbors::nn_descent::index_params;

// **** Experimental ****
using iterative_search_params = cuvs::neighbors::search_params;
} // namespace graph_build_params

/** @} */ // end group neighbors_build_algo
} // namespace cuvs::neighbors
6 changes: 3 additions & 3 deletions cpp/src/neighbors/all_neighbors/all_neighbors_builder.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct all_neighbors_builder_ivfpq : public all_neighbors_builder<T, IdxT> {
size_t min_cluster_size,
size_t max_cluster_size,
size_t k,
all_neighbors::graph_build_params::ivf_pq_params& params,
graph_build_params::ivf_pq_params& params,
std::optional<raft::device_matrix_view<IdxT, IdxT, row_major>> indices = std::nullopt,
std::optional<raft::device_matrix_view<T, IdxT, row_major>> distances = std::nullopt)
: all_neighbors_builder<T, IdxT>(
Expand Down Expand Up @@ -287,7 +287,7 @@ struct all_neighbors_builder_ivfpq : public all_neighbors_builder<T, IdxT> {
dataset_h.data_handle(), dataset.extent(0), dataset.extent(1)));
}

all_neighbors::graph_build_params::ivf_pq_params all_ivf_pq_params;
graph_build_params::ivf_pq_params all_ivf_pq_params;
size_t candidate_k;

std::optional<raft::device_matrix<T, IdxT>> data_d;
Expand All @@ -308,7 +308,7 @@ struct all_neighbors_builder_nn_descent : public all_neighbors_builder<T, IdxT>
size_t min_cluster_size,
size_t max_cluster_size,
size_t k,
all_neighbors::graph_build_params::nn_descent_params& params,
graph_build_params::nn_descent_params& params,
std::optional<raft::device_matrix_view<IdxT, IdxT, row_major>> indices = std::nullopt,
std::optional<raft::device_matrix_view<T, IdxT, row_major>> distances = std::nullopt)
: all_neighbors_builder<T, IdxT>(
Expand Down
37 changes: 0 additions & 37 deletions cpp/src/neighbors/detail/cagra/cagra_build.cpp

This file was deleted.

9 changes: 5 additions & 4 deletions cpp/tests/neighbors/all_neighbors.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ void get_graphs(raft::resources& handle,
nn_descent_params.metric = params.metric;
params.graph_build_params = nn_descent_params;
} else if (build_algo == IVF_PQ) {
auto ivfq_build_params = graph_build_params::ivf_pq_params{};
ivfq_build_params.build_params.metric = params.metric;
auto ivfpq_build_params = graph_build_params::ivf_pq_params{};
ivfpq_build_params.build_params.metric = params.metric;
ivfpq_build_params.refinement_rate = 2.0;
// heuristically good ivfpq n_lists
ivfq_build_params.build_params.n_lists = std::max(
ivfpq_build_params.build_params.n_lists = std::max(
5u, static_cast<uint32_t>(ps.n_rows * params.overlap_factor / (5000 * params.n_clusters)));
params.graph_build_params = ivfq_build_params;
params.graph_build_params = ivfpq_build_params;
}

auto cuda_stream = raft::resource::get_cuda_stream(handle);
Expand Down