knn.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
20 
21 #include <raft/spatial/knn/detail/processing.hpp> // MetricProcessor
22 
23 #include <cstdint>
24 #include <memory>
25 #include <vector>
26 
27 namespace raft {
28 class handle_t;
29 }
30 
31 namespace ML {
32 
56 void brute_force_knn(const raft::handle_t& handle,
57  std::vector<float*>& input,
58  std::vector<int>& sizes,
59  int D,
60  float* search_items,
61  int n,
62  int64_t* res_I,
63  float* res_D,
64  int k,
65  bool rowMajorIndex = false,
66  bool rowMajorQuery = false,
68  float metric_arg = 2.0f,
69  std::vector<int64_t>* translations = nullptr);
70 
71 void rbc_build_index(const raft::handle_t& handle,
72  std::uintptr_t& rbc_index,
73  float* X,
74  int64_t n_rows,
75  int64_t n_cols,
77 
78 void rbc_knn_query(const raft::handle_t& handle,
79  const std::uintptr_t& rbc_index,
80  uint32_t k,
81  const float* search_items,
82  uint32_t n_search_items,
83  int64_t dim,
84  int64_t* out_inds,
85  float* out_dists);
86 
92 void rbc_free_index(std::uintptr_t rbc_index);
93 
94 struct knnIndexImpl;
95 
96 struct knnIndex {
99 
101  float metricArg;
102  int nprobe;
103  std::unique_ptr<raft::spatial::knn::MetricProcessor<float>> metric_processor;
104  int device;
105 
106  std::unique_ptr<knnIndexImpl> pimpl;
107 };
108 
110  virtual ~knnIndexParam() {}
111 };
112 
114  int nlist;
115  int nprobe;
116 };
117 
118 struct IVFFlatParam : IVFParam {};
119 
121  int M;
122  int n_bits;
124 };
125 
139 void approx_knn_build_index(raft::handle_t& handle,
140  knnIndex* index,
143  float metricArg,
144  float* index_array,
145  int n,
146  int D);
147 
161 void approx_knn_search(raft::handle_t& handle,
162  float* distances,
163  int64_t* indices,
164  knnIndex* index,
165  int k,
166  float* query_array,
167  int n);
168 
183 void knn_classify(raft::handle_t& handle,
184  int* out,
185  int64_t* knn_indices,
186  std::vector<int*>& y,
187  size_t n_index_rows,
188  size_t n_query_rows,
189  int k);
190 
205 void knn_regress(raft::handle_t& handle,
206  float* out,
207  int64_t* knn_indices,
208  std::vector<float*>& y,
209  size_t n_index_rows,
210  size_t n_query_rows,
211  int k);
212 
227 void knn_class_proba(raft::handle_t& handle,
228  std::vector<float*>& out,
229  int64_t* knn_indices,
230  std::vector<int*>& y,
231  size_t n_index_rows,
232  size_t n_query_rows,
233  int k);
234 }; // namespace ML
Definition: params.hpp:34
DistanceType
Definition: distance_type.hpp:21
Definition: dbscan.hpp:29
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)
Flat C++ API function to perform a knn classification using a given a vector of label arrays....
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 rbc_free_index(std::uintptr_t rbc_index)
Free the RBC index.
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)
Flat C++ API function to compute knn class probabilities using a vector of device arrays containing d...
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)
Flat C++ API function to perform a knn regression using a given a vector of label arrays....
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 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...
Definition: dbscan.hpp:25
Definition: knn.hpp:118
Definition: knn.hpp:120
int M
Definition: knn.hpp:121
int n_bits
Definition: knn.hpp:122
bool usePrecomputedTables
Definition: knn.hpp:123
Definition: knn.hpp:113
int nprobe
Definition: knn.hpp:115
int nlist
Definition: knn.hpp:114
Definition: knn.hpp:109
virtual ~knnIndexParam()
Definition: knn.hpp:110
Definition: knn.hpp:96
int nprobe
Definition: knn.hpp:102
std::unique_ptr< knnIndexImpl > pimpl
Definition: knn.hpp:106
int device
Definition: knn.hpp:104
float metricArg
Definition: knn.hpp:101
std::unique_ptr< raft::spatial::knn::MetricProcessor< float > > metric_processor
Definition: knn.hpp:103
ML::distance::DistanceType metric
Definition: knn.hpp:100