| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c++ -*- |
| 9 |
|
| 10 |
#include "Index_c.h" |
| 11 |
#include <faiss/Index.h> |
| 12 |
#include <faiss/impl/IDSelector.h> |
| 13 |
#include "macros_impl.h" |
| 14 |
|
| 15 |
extern "C" { |
| 16 |
|
| 17 |
DEFINE_DESTRUCTOR(SearchParameters) |
| 18 |
|
| 19 |
int faiss_SearchParameters_new( |
| 20 |
FaissSearchParameters** p_sp, |
| 21 |
FaissIDSelector* sel) { |
| 22 |
try { |
| 23 |
faiss::SearchParameters* params = new faiss::SearchParameters; |
| 24 |
params->sel = reinterpret_cast<faiss::IDSelector*>(sel); |
| 25 |
*p_sp = reinterpret_cast<FaissSearchParameters*>(params); |
| 26 |
return 0; |
| 27 |
} |
| 28 |
CATCH_AND_HANDLE |
| 29 |
} |
| 30 |
|
| 31 |
DEFINE_DESTRUCTOR(Index) |
| 32 |
|
| 33 |
DEFINE_GETTER(Index, int, d) |
| 34 |
|
| 35 |
DEFINE_GETTER(Index, int, is_trained) |
| 36 |
|
| 37 |
DEFINE_GETTER(Index, idx_t, ntotal) |
| 38 |
|
| 39 |
DEFINE_GETTER(Index, FaissMetricType, metric_type) |
| 40 |
|
| 41 |
DEFINE_GETTER(Index, int, verbose); |
| 42 |
DEFINE_SETTER(Index, int, verbose); |
| 43 |
|
| 44 |
int faiss_Index_train(FaissIndex* index, idx_t n, const float* x) { |
| 45 |
try { |
| 46 |
reinterpret_cast<faiss::Index*>(index)->train(n, x); |
| 47 |
} |
| 48 |
CATCH_AND_HANDLE |
| 49 |
} |
| 50 |
|
| 51 |
int faiss_Index_add(FaissIndex* index, idx_t n, const float* x) { |
| 52 |
try { |
| 53 |
reinterpret_cast<faiss::Index*>(index)->add(n, x); |
| 54 |
} |
| 55 |
CATCH_AND_HANDLE |
| 56 |
} |
| 57 |
|
| 58 |
int faiss_Index_add_with_ids( |
| 59 |
FaissIndex* index, |
| 60 |
idx_t n, |
| 61 |
const float* x, |
| 62 |
const idx_t* xids) { |
| 63 |
try { |
| 64 |
reinterpret_cast<faiss::Index*>(index)->add_with_ids(n, x, xids); |
| 65 |
} |
| 66 |
CATCH_AND_HANDLE |
| 67 |
} |
| 68 |
|
| 69 |
int faiss_Index_search( |
| 70 |
const FaissIndex* index, |
| 71 |
idx_t n, |
| 72 |
const float* x, |
| 73 |
idx_t k, |
| 74 |
float* distances, |
| 75 |
idx_t* labels) { |
| 76 |
try { |
| 77 |
reinterpret_cast<const faiss::Index*>(index)->search( |
| 78 |
n, x, k, distances, labels); |
| 79 |
} |
| 80 |
CATCH_AND_HANDLE |
| 81 |
} |
| 82 |
|
| 83 |
int faiss_Index_search_with_params( |
| 84 |
const FaissIndex* index, |
| 85 |
idx_t n, |
| 86 |
const float* x, |
| 87 |
idx_t k, |
| 88 |
const FaissSearchParameters* params, |
| 89 |
float* distances, |
| 90 |
idx_t* labels) { |
| 91 |
try { |
| 92 |
reinterpret_cast<const faiss::Index*>(index)->search( |
| 93 |
n, |
| 94 |
x, |
| 95 |
k, |
| 96 |
distances, |
| 97 |
labels, |
| 98 |
reinterpret_cast<const faiss::SearchParameters*>(params)); |
| 99 |
} |
| 100 |
CATCH_AND_HANDLE |
| 101 |
} |
| 102 |
|
| 103 |
int faiss_Index_range_search( |
| 104 |
const FaissIndex* index, |
| 105 |
idx_t n, |
| 106 |
const float* x, |
| 107 |
float radius, |
| 108 |
FaissRangeSearchResult* result) { |
| 109 |
try { |
| 110 |
reinterpret_cast<const faiss::Index*>(index)->range_search( |
| 111 |
n, |
| 112 |
x, |
| 113 |
radius, |
| 114 |
reinterpret_cast<faiss::RangeSearchResult*>(result)); |
| 115 |
} |
| 116 |
CATCH_AND_HANDLE |
| 117 |
} |
| 118 |
|
| 119 |
int faiss_Index_assign( |
| 120 |
FaissIndex* index, |
| 121 |
idx_t n, |
| 122 |
const float* x, |
| 123 |
idx_t* labels, |
| 124 |
idx_t k) { |
| 125 |
try { |
| 126 |
reinterpret_cast<faiss::Index*>(index)->assign(n, x, labels, k); |
| 127 |
} |
| 128 |
CATCH_AND_HANDLE |
| 129 |
} |
| 130 |
|
| 131 |
int faiss_Index_reset(FaissIndex* index) { |
| 132 |
try { |
| 133 |
reinterpret_cast<faiss::Index*>(index)->reset(); |
| 134 |
} |
| 135 |
CATCH_AND_HANDLE |
| 136 |
} |
| 137 |
|
| 138 |
int faiss_Index_remove_ids( |
| 139 |
FaissIndex* index, |
| 140 |
const FaissIDSelector* sel, |
| 141 |
size_t* n_removed) { |
| 142 |
try { |
| 143 |
size_t n{reinterpret_cast<faiss::Index*>(index)->remove_ids( |
| 144 |
*reinterpret_cast<const faiss::IDSelector*>(sel))}; |
| 145 |
if (n_removed) { |
| 146 |
*n_removed = n; |
| 147 |
} |
| 148 |
} |
| 149 |
CATCH_AND_HANDLE |
| 150 |
} |
| 151 |
|
| 152 |
int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons) { |
| 153 |
try { |
| 154 |
reinterpret_cast<const faiss::Index*>(index)->reconstruct(key, recons); |
| 155 |
} |
| 156 |
CATCH_AND_HANDLE |
| 157 |
} |
| 158 |
|
| 159 |
int faiss_Index_reconstruct_n( |
| 160 |
const FaissIndex* index, |
| 161 |
idx_t i0, |
| 162 |
idx_t ni, |
| 163 |
float* recons) { |
| 164 |
try { |
| 165 |
reinterpret_cast<const faiss::Index*>(index)->reconstruct_n( |
| 166 |
i0, ni, recons); |
| 167 |
} |
| 168 |
CATCH_AND_HANDLE |
| 169 |
} |
| 170 |
|
| 171 |
int faiss_Index_compute_residual( |
| 172 |
const FaissIndex* index, |
| 173 |
const float* x, |
| 174 |
float* residual, |
| 175 |
idx_t key) { |
| 176 |
try { |
| 177 |
reinterpret_cast<const faiss::Index*>(index)->compute_residual( |
| 178 |
x, residual, key); |
| 179 |
} |
| 180 |
CATCH_AND_HANDLE |
| 181 |
} |
| 182 |
|
| 183 |
int faiss_Index_compute_residual_n( |
| 184 |
const FaissIndex* index, |
| 185 |
idx_t n, |
| 186 |
const float* x, |
| 187 |
float* residuals, |
| 188 |
const idx_t* keys) { |
| 189 |
try { |
| 190 |
reinterpret_cast<const faiss::Index*>(index)->compute_residual_n( |
| 191 |
n, x, residuals, keys); |
| 192 |
} |
| 193 |
CATCH_AND_HANDLE |
| 194 |
} |
| 195 |
|
| 196 |
int faiss_Index_sa_code_size(const FaissIndex* index, size_t* size) { |
| 197 |
try { |
| 198 |
*size = reinterpret_cast<const faiss::Index*>(index)->sa_code_size(); |
| 199 |
} |
| 200 |
CATCH_AND_HANDLE |
| 201 |
} |
| 202 |
|
| 203 |
int faiss_Index_sa_encode( |
| 204 |
const FaissIndex* index, |
| 205 |
idx_t n, |
| 206 |
const float* x, |
| 207 |
uint8_t* bytes) { |
| 208 |
try { |
| 209 |
reinterpret_cast<const faiss::Index*>(index)->sa_encode(n, x, bytes); |
| 210 |
} |
| 211 |
CATCH_AND_HANDLE |
| 212 |
} |
| 213 |
|
| 214 |
int faiss_Index_sa_decode( |
| 215 |
const FaissIndex* index, |
| 216 |
idx_t n, |
| 217 |
const uint8_t* bytes, |
| 218 |
float* x) { |
| 219 |
try { |
| 220 |
reinterpret_cast<const faiss::Index*>(index)->sa_decode(n, bytes, x); |
| 221 |
} |
| 222 |
CATCH_AND_HANDLE |
| 223 |
} |
| 224 |
} |