knn.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
9 
10 #include <raft/spatial/knn/detail/processing.hpp> // MetricProcessor
11 
12 #include <cstdint>
13 #include <memory>
14 #include <vector>
15 
16 namespace raft {
17 class handle_t;
18 }
19 
20 namespace ML {
21 
45 void brute_force_knn(const raft::handle_t& handle,
46  std::vector<float*>& input,
47  std::vector<int>& sizes,
48  int D,
49  float* search_items,
50  int n,
51  int64_t* res_I,
52  float* res_D,
53  int k,
54  bool rowMajorIndex = false,
55  bool rowMajorQuery = false,
57  float metric_arg = 2.0f,
58  std::vector<int64_t>* translations = nullptr);
59 
60 void rbc_build_index(const raft::handle_t& handle,
61  std::uintptr_t& rbc_index,
62  float* X,
63  int64_t n_rows,
64  int64_t n_cols,
66 
67 void rbc_knn_query(const raft::handle_t& handle,
68  const std::uintptr_t& rbc_index,
69  uint32_t k,
70  const float* search_items,
71  uint32_t n_search_items,
72  int64_t dim,
73  int64_t* out_inds,
74  float* out_dists);
75 
81 void rbc_free_index(std::uintptr_t rbc_index);
82 
83 struct knnIndexImpl;
84 
85 struct knnIndex {
88 
90  float metricArg;
91  int nprobe;
92  std::unique_ptr<raft::spatial::knn::MetricProcessor<float>> metric_processor;
93  int device;
94 
95  std::unique_ptr<knnIndexImpl> pimpl;
96 };
97 
98 struct knnIndexParam {
99  virtual ~knnIndexParam() {}
100 };
101 
103  int nlist;
104  int nprobe;
105 };
106 
107 struct IVFFlatParam : IVFParam {};
108 
110  int M;
111  int n_bits;
113 };
114 
128 void approx_knn_build_index(raft::handle_t& handle,
129  knnIndex* index,
132  float metricArg,
133  float* index_array,
134  int n,
135  int D);
136 
150 void approx_knn_search(raft::handle_t& handle,
151  float* distances,
152  int64_t* indices,
153  knnIndex* index,
154  int k,
155  float* query_array,
156  int n);
157 
174 void knn_classify(raft::handle_t& handle,
175  int* out,
176  int64_t* knn_indices,
177  std::vector<int*>& y,
178  size_t n_index_rows,
179  size_t n_query_rows,
180  int k,
181  float* sample_weight = nullptr);
182 
199 void knn_regress(raft::handle_t& handle,
200  float* out,
201  int64_t* knn_indices,
202  std::vector<float*>& y,
203  size_t n_index_rows,
204  size_t n_query_rows,
205  int k,
206  float* sample_weight = nullptr);
207 
224 void knn_class_proba(raft::handle_t& handle,
225  std::vector<float*>& out,
226  int64_t* knn_indices,
227  std::vector<int*>& y,
228  size_t n_index_rows,
229  size_t n_query_rows,
230  int k,
231  float* sample_weight = nullptr);
232 }; // namespace ML
Definition: params.hpp:23
DistanceType
Definition: distance_type.hpp:10
Definition: dbscan.hpp:18
void rbc_knn_query(const raft::handle_t &handle, const std::uintptr_t &rbc_index, uint32_t k, const float *search_items, uint32_t n_search_items, int64_t dim, int64_t *out_inds, float *out_dists)
void knn_class_proba(raft::handle_t &handle, std::vector< float * > &out, int64_t *knn_indices, std::vector< int * > &y, size_t n_index_rows, size_t n_query_rows, int k, float *sample_weight=nullptr)
Flat C++ API function to compute knn class probabilities using a vector of device arrays containing d...
void rbc_free_index(std::uintptr_t rbc_index)
Free the RBC index.
void brute_force_knn(const raft::handle_t &handle, std::vector< float * > &input, std::vector< int > &sizes, int D, float *search_items, int n, int64_t *res_I, float *res_D, int k, bool rowMajorIndex=false, bool rowMajorQuery=false, ML::distance::DistanceType metric=ML::distance::DistanceType::L2Expanded, float metric_arg=2.0f, std::vector< int64_t > *translations=nullptr)
Flat C++ API function to perform a brute force knn on a series of input arrays and combine the result...
void rbc_build_index(const raft::handle_t &handle, std::uintptr_t &rbc_index, float *X, int64_t n_rows, int64_t n_cols, ML::distance::DistanceType metric)
void approx_knn_search(raft::handle_t &handle, float *distances, int64_t *indices, knnIndex *index, int k, float *query_array, int n)
Flat C++ API function to perform an approximate nearest neighbors search from previously built index ...
void knn_regress(raft::handle_t &handle, float *out, int64_t *knn_indices, std::vector< float * > &y, size_t n_index_rows, size_t n_query_rows, int k, float *sample_weight=nullptr)
Flat C++ API function to perform a knn regression using a given a vector of label arrays....
void approx_knn_build_index(raft::handle_t &handle, knnIndex *index, knnIndexParam *params, ML::distance::DistanceType metric, float metricArg, float *index_array, int n, int D)
Flat C++ API function to build an approximate nearest neighbors index from an index array and a set o...
void knn_classify(raft::handle_t &handle, int *out, int64_t *knn_indices, std::vector< int * > &y, size_t n_index_rows, size_t n_query_rows, int k, float *sample_weight=nullptr)
Flat C++ API function to perform a knn classification using a given a vector of label arrays....
Definition: dbscan.hpp:14
Definition: knn.hpp:107
Definition: knn.hpp:109
int M
Definition: knn.hpp:110
int n_bits
Definition: knn.hpp:111
bool usePrecomputedTables
Definition: knn.hpp:112
Definition: knn.hpp:102
int nprobe
Definition: knn.hpp:104
int nlist
Definition: knn.hpp:103
Definition: knn.hpp:98
virtual ~knnIndexParam()
Definition: knn.hpp:99
Definition: knn.hpp:85
int nprobe
Definition: knn.hpp:91
std::unique_ptr< knnIndexImpl > pimpl
Definition: knn.hpp:95
int device
Definition: knn.hpp:93
float metricArg
Definition: knn.hpp:90
std::unique_ptr< raft::spatial::knn::MetricProcessor< float > > metric_processor
Definition: knn.hpp:92
ML::distance::DistanceType metric
Definition: knn.hpp:89