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
15 changes: 15 additions & 0 deletions c/include/cuvs/neighbors/nn_descent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
extern "C" {
#endif

/**
* @brief Dtype to use for distance computation
* - `NND_DIST_COMP_AUTO`: Automatically determine the best dtype for distance computation based on the dataset dimensions.
* - `NND_DIST_COMP_FP32`: Use fp32 distance computation for better precision at the cost of performance and memory usage.
* - `NND_DIST_COMP_FP16`: Use fp16 distance computation.
*/
typedef enum {
NND_DIST_COMP_AUTO = 0,
NND_DIST_COMP_FP32 = 1,
NND_DIST_COMP_FP16 = 2
} cuvsNNDescentDistCompDtype;

/**
* @defgroup nn_descent_c_index_params The nn-descent algorithm parameters.
* @{
Expand All @@ -34,6 +46,8 @@ extern "C" {
* `max_iterations`: The number of iterations that nn-descent will refine
* the graph for. More iterations produce a better quality graph at cost of performance
* `termination_threshold`: The delta at which nn-descent will terminate its iterations
* `return_distances`: Boolean to decide whether to return distances array
* `dist_comp_dtype`: dtype to use for distance computation. Defaults to `NND_DIST_COMP_AUTO` which automatically determines the best dtype for distance computation based on the dataset dimensions. Use `NND_DIST_COMP_FP32` for better precision at the cost of performance and memory usage. This option is only valid when data type is fp32. Use `NND_DIST_COMP_FP16` for better performance and memory usage at the cost of precision.
*/
struct cuvsNNDescentIndexParams {
cuvsDistanceType metric;
Expand All @@ -43,6 +57,7 @@ struct cuvsNNDescentIndexParams {
size_t max_iterations;
float termination_threshold;
bool return_distances;
cuvsNNDescentDistCompDtype dist_comp_dtype;
};

typedef struct cuvsNNDescentIndexParams* cuvsNNDescentIndexParams_t;
Expand Down
4 changes: 3 additions & 1 deletion c/src/neighbors/nn_descent.cpp
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.

Please default initialize the index creation with this new parameter.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void* _build(cuvsResources_t res,
build_params.max_iterations = params.max_iterations;
build_params.termination_threshold = params.termination_threshold;
build_params.return_distances = params.return_distances;
build_params.dist_comp_dtype = static_cast<cuvs::neighbors::nn_descent::DIST_COMP_DTYPE>(static_cast<int>(params.dist_comp_dtype));

using graph_type = raft::host_matrix_view<IdxT, int64_t, raft::row_major>;
std::optional<graph_type> graph;
Expand Down Expand Up @@ -177,7 +178,8 @@ extern "C" cuvsError_t cuvsNNDescentIndexParamsCreate(cuvsNNDescentIndexParams_t
.intermediate_graph_degree = cpp_params.intermediate_graph_degree,
.max_iterations = cpp_params.max_iterations,
.termination_threshold = cpp_params.termination_threshold,
.return_distances = cpp_params.return_distances};
.return_distances = cpp_params.return_distances,
.dist_comp_dtype = static_cast<cuvsNNDescentDistCompDtype>(static_cast<int>(cpp_params.dist_comp_dtype))};
});
}

Expand Down
16 changes: 16 additions & 0 deletions cpp/include/cuvs/neighbors/nn_descent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ namespace cuvs::neighbors::nn_descent {
* @{
*/

/**
* @brief Dtype to use for distance computation
* - `AUTO`: Automatically determine the best dtype for distance computation based on the dataset
* dimensions.
* - `FP32`: Use fp32 distance computation for better precision at the cost of performance and
* memory usage.
* - `FP16`: Use fp16 distance computation.
*/
enum class DIST_COMP_DTYPE { AUTO = 0, FP32 = 1, FP16 = 2 };

/**
* @brief Parameters used to build an nn-descent index
* - `graph_degree`: For an input dataset of dimensions (N, D),
Expand All @@ -37,13 +47,19 @@ namespace cuvs::neighbors::nn_descent {
* the graph for. More iterations produce a better quality graph at cost of performance
* - `termination_threshold`: The delta at which nn-descent will terminate its iterations
* - `return_distances`: Boolean to decide whether to return distances array
* - `dist_comp_dtype`: dtype to use for distance computation. Defaults to `AUTO` which
* automatically determines the best dtype for distance computation based on the dataset dimensions.
* Use `FP32` for better precision at the cost of performance and memory usage. This option is only
* valid when data type is fp32. Use `FP16` for better performance and memory usage at the cost of
* precision.
*/
struct index_params : cuvs::neighbors::index_params {
size_t graph_degree = 64;
size_t intermediate_graph_degree = 128;
size_t max_iterations = 20;
float termination_threshold = 0.0001;
bool return_distances = true;
DIST_COMP_DTYPE dist_comp_dtype = DIST_COMP_DTYPE::AUTO;
Copy link
Copy Markdown
Contributor Author

@jinsolp jinsolp Dec 10, 2025

Choose a reason for hiding this comment

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

@divyegala It already defaults to AUTO here, which would be taken in nn_descent.cpp

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.

That file is the source of C API, and this default is for the C++ API.

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.

I believe all the defaults of cpp are passed when we call cuvsNNDescentIndexParamsCreate

extern "C" cuvsError_t cuvsNNDescentIndexParamsCreate(cuvsNNDescentIndexParams_t* params)
{
return cuvs::core::translate_exceptions([=] {
// get defaults from cpp parameters struct
cuvs::neighbors::nn_descent::index_params cpp_params;
*params = new cuvsNNDescentIndexParams{
.metric = static_cast<cuvsDistanceType>((int)cpp_params.metric),
.metric_arg = cpp_params.metric_arg,
.graph_degree = cpp_params.graph_degree,
.intermediate_graph_degree = cpp_params.intermediate_graph_degree,
.max_iterations = cpp_params.max_iterations,
.termination_threshold = cpp_params.termination_threshold,
.return_distances = cpp_params.return_distances};
});
}

C doesn't support default struct init and so nothing is initialized in the c struct:

struct cuvsNNDescentIndexParams {
cuvsDistanceType metric;
float metric_arg;
size_t graph_degree;
size_t intermediate_graph_degree;
size_t max_iterations;
float termination_threshold;
bool return_distances;
};

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.

I haven't kept up with the changes to the C API, my apologies. We used to explicitly default initialize in the create function previously.


/** @brief Construct NN descent parameters for a specific kNN graph degree
*
Expand Down
Loading