| 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 "AuxIndexStructures_c.h" |
| 11 |
#include <faiss/impl/AuxIndexStructures.h> |
| 12 |
#include <faiss/impl/DistanceComputer.h> |
| 13 |
#include <faiss/impl/IDSelector.h> |
| 14 |
#include <iostream> |
| 15 |
#include "../macros_impl.h" |
| 16 |
|
| 17 |
using faiss::BufferList; |
| 18 |
using faiss::DistanceComputer; |
| 19 |
using faiss::IDSelector; |
| 20 |
using faiss::IDSelectorAnd; |
| 21 |
using faiss::IDSelectorBatch; |
| 22 |
using faiss::IDSelectorBitmap; |
| 23 |
using faiss::IDSelectorNot; |
| 24 |
using faiss::IDSelectorOr; |
| 25 |
using faiss::IDSelectorRange; |
| 26 |
using faiss::IDSelectorXOr; |
| 27 |
using faiss::RangeQueryResult; |
| 28 |
using faiss::RangeSearchPartialResult; |
| 29 |
using faiss::RangeSearchResult; |
| 30 |
|
| 31 |
DEFINE_GETTER(RangeSearchResult, size_t, nq) |
| 32 |
|
| 33 |
int faiss_RangeSearchResult_new(FaissRangeSearchResult** p_rsr, idx_t nq) { |
| 34 |
try { |
| 35 |
*p_rsr = reinterpret_cast<FaissRangeSearchResult*>( |
| 36 |
new RangeSearchResult(nq)); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
CATCH_AND_HANDLE |
| 40 |
} |
| 41 |
|
| 42 |
int faiss_RangeSearchResult_new_with( |
| 43 |
FaissRangeSearchResult** p_rsr, |
| 44 |
idx_t nq, |
| 45 |
int alloc_lims) { |
| 46 |
try { |
| 47 |
*p_rsr = reinterpret_cast<FaissRangeSearchResult*>( |
| 48 |
new RangeSearchResult(nq, static_cast<bool>(alloc_lims))); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
CATCH_AND_HANDLE |
| 52 |
} |
| 53 |
|
| 54 |
/// called when lims contains the nb of elements result entries |
| 55 |
/// for each query |
| 56 |
int faiss_RangeSearchResult_do_allocation(FaissRangeSearchResult* rsr) { |
| 57 |
try { |
| 58 |
reinterpret_cast<RangeSearchResult*>(rsr)->do_allocation(); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
CATCH_AND_HANDLE |
| 62 |
} |
| 63 |
|
| 64 |
DEFINE_DESTRUCTOR(RangeSearchResult) |
| 65 |
|
| 66 |
/// getter for buffer_size |
| 67 |
DEFINE_GETTER(RangeSearchResult, size_t, buffer_size) |
| 68 |
|
| 69 |
/// getter for lims: size (nq + 1) |
| 70 |
void faiss_RangeSearchResult_lims(FaissRangeSearchResult* rsr, size_t** lims) { |
| 71 |
*lims = reinterpret_cast<RangeSearchResult*>(rsr)->lims; |
| 72 |
} |
| 73 |
|
| 74 |
/// getter for labels and respective distances (not sorted): |
| 75 |
/// result for query i is labels[lims[i]:lims[i+1]] |
| 76 |
void faiss_RangeSearchResult_labels( |
| 77 |
FaissRangeSearchResult* rsr, |
| 78 |
idx_t** labels, |
| 79 |
float** distances) { |
| 80 |
auto sr = reinterpret_cast<RangeSearchResult*>(rsr); |
| 81 |
*labels = sr->labels; |
| 82 |
*distances = sr->distances; |
| 83 |
} |
| 84 |
|
| 85 |
DEFINE_DESTRUCTOR(IDSelector) |
| 86 |
|
| 87 |
int faiss_IDSelector_is_member(const FaissIDSelector* sel, idx_t id) { |
| 88 |
return reinterpret_cast<const IDSelector*>(sel)->is_member(id); |
| 89 |
} |
| 90 |
|
| 91 |
DEFINE_DESTRUCTOR(IDSelectorRange) |
| 92 |
|
| 93 |
DEFINE_GETTER(IDSelectorRange, idx_t, imin) |
| 94 |
DEFINE_GETTER(IDSelectorRange, idx_t, imax) |
| 95 |
|
| 96 |
int faiss_IDSelectorRange_new( |
| 97 |
FaissIDSelectorRange** p_sel, |
| 98 |
idx_t imin, |
| 99 |
idx_t imax) { |
| 100 |
try { |
| 101 |
*p_sel = reinterpret_cast<FaissIDSelectorRange*>( |
| 102 |
new IDSelectorRange(imin, imax)); |
| 103 |
return 0; |
| 104 |
} |
| 105 |
CATCH_AND_HANDLE |
| 106 |
} |
| 107 |
|
| 108 |
DEFINE_GETTER(IDSelectorBatch, int, nbits) |
| 109 |
DEFINE_GETTER(IDSelectorBatch, idx_t, mask) |
| 110 |
|
| 111 |
int faiss_IDSelectorBatch_new( |
| 112 |
FaissIDSelectorBatch** p_sel, |
| 113 |
size_t n, |
| 114 |
const idx_t* indices) { |
| 115 |
try { |
| 116 |
*p_sel = reinterpret_cast<FaissIDSelectorBatch*>( |
| 117 |
new IDSelectorBatch(n, indices)); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
CATCH_AND_HANDLE |
| 121 |
} |
| 122 |
|
| 123 |
DEFINE_DESTRUCTOR(IDSelectorBitmap) |
| 124 |
|
| 125 |
DEFINE_GETTER(IDSelectorBitmap, size_t, n) |
| 126 |
DEFINE_GETTER(IDSelectorBitmap, const uint8_t*, bitmap) |
| 127 |
|
| 128 |
int faiss_IDSelectorBitmap_new( |
| 129 |
FaissIDSelectorBitmap** p_sel, |
| 130 |
size_t n, |
| 131 |
const uint8_t* bitmap) { |
| 132 |
try { |
| 133 |
*p_sel = reinterpret_cast<FaissIDSelectorBitmap*>( |
| 134 |
new IDSelectorBitmap(n, bitmap)); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
CATCH_AND_HANDLE |
| 138 |
} |
| 139 |
|
| 140 |
int faiss_IDSelectorNot_new( |
| 141 |
FaissIDSelectorNot** p_sel, |
| 142 |
const FaissIDSelector* sel) { |
| 143 |
try { |
| 144 |
*p_sel = reinterpret_cast<FaissIDSelectorNot*>( |
| 145 |
new IDSelectorNot(reinterpret_cast<const IDSelector*>(sel))); |
| 146 |
} |
| 147 |
CATCH_AND_HANDLE |
| 148 |
} |
| 149 |
|
| 150 |
int faiss_IDSelectorAnd_new( |
| 151 |
FaissIDSelectorAnd** p_sel, |
| 152 |
const FaissIDSelector* lhs_sel, |
| 153 |
const FaissIDSelector* rhs_sel) { |
| 154 |
try { |
| 155 |
*p_sel = reinterpret_cast<FaissIDSelectorAnd*>(new IDSelectorAnd( |
| 156 |
reinterpret_cast<const IDSelector*>(lhs_sel), |
| 157 |
reinterpret_cast<const IDSelector*>(rhs_sel))); |
| 158 |
} |
| 159 |
CATCH_AND_HANDLE |
| 160 |
} |
| 161 |
|
| 162 |
int faiss_IDSelectorOr_new( |
| 163 |
FaissIDSelectorOr** p_sel, |
| 164 |
const FaissIDSelector* lhs_sel, |
| 165 |
const FaissIDSelector* rhs_sel) { |
| 166 |
try { |
| 167 |
*p_sel = reinterpret_cast<FaissIDSelectorOr*>(new IDSelectorOr( |
| 168 |
reinterpret_cast<const IDSelector*>(lhs_sel), |
| 169 |
reinterpret_cast<const IDSelector*>(rhs_sel))); |
| 170 |
} |
| 171 |
CATCH_AND_HANDLE |
| 172 |
} |
| 173 |
|
| 174 |
int faiss_IDSelectorXOr_new( |
| 175 |
FaissIDSelectorXOr** p_sel, |
| 176 |
const FaissIDSelector* lhs_sel, |
| 177 |
const FaissIDSelector* rhs_sel) { |
| 178 |
try { |
| 179 |
*p_sel = reinterpret_cast<FaissIDSelectorXOr*>(new IDSelectorXOr( |
| 180 |
reinterpret_cast<const IDSelector*>(lhs_sel), |
| 181 |
reinterpret_cast<const IDSelector*>(rhs_sel))); |
| 182 |
} |
| 183 |
CATCH_AND_HANDLE |
| 184 |
} |
| 185 |
|
| 186 |
// Below are structures used only by Index implementations |
| 187 |
|
| 188 |
DEFINE_DESTRUCTOR(BufferList) |
| 189 |
|
| 190 |
DEFINE_GETTER(BufferList, size_t, buffer_size) |
| 191 |
DEFINE_GETTER(BufferList, size_t, wp) |
| 192 |
|
| 193 |
int faiss_BufferList_append_buffer(FaissBufferList* bl) { |
| 194 |
try { |
| 195 |
reinterpret_cast<BufferList*>(bl)->append_buffer(); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
CATCH_AND_HANDLE |
| 199 |
} |
| 200 |
|
| 201 |
int faiss_BufferList_new(FaissBufferList** p_bl, size_t buffer_size) { |
| 202 |
try { |
| 203 |
*p_bl = reinterpret_cast<FaissBufferList*>(new BufferList(buffer_size)); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
CATCH_AND_HANDLE |
| 207 |
} |
| 208 |
|
| 209 |
int faiss_BufferList_add(FaissBufferList* bl, idx_t id, float dis) { |
| 210 |
try { |
| 211 |
reinterpret_cast<BufferList*>(bl)->add(id, dis); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
CATCH_AND_HANDLE |
| 215 |
} |
| 216 |
|
| 217 |
/// copy elemnts ofs:ofs+n-1 seen as linear data in the buffers to |
| 218 |
/// tables dest_ids, dest_dis |
| 219 |
int faiss_BufferList_copy_range( |
| 220 |
FaissBufferList* bl, |
| 221 |
size_t ofs, |
| 222 |
size_t n, |
| 223 |
idx_t* dest_ids, |
| 224 |
float* dest_dis) { |
| 225 |
try { |
| 226 |
reinterpret_cast<BufferList*>(bl)->copy_range( |
| 227 |
ofs, n, dest_ids, dest_dis); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
CATCH_AND_HANDLE |
| 231 |
} |
| 232 |
|
| 233 |
DEFINE_GETTER(RangeQueryResult, idx_t, qno) |
| 234 |
DEFINE_GETTER(RangeQueryResult, size_t, nres) |
| 235 |
DEFINE_GETTER_PERMISSIVE(RangeQueryResult, FaissRangeSearchPartialResult*, pres) |
| 236 |
|
| 237 |
int faiss_RangeQueryResult_add(FaissRangeQueryResult* qr, float dis, idx_t id) { |
| 238 |
try { |
| 239 |
reinterpret_cast<RangeQueryResult*>(qr)->add(dis, id); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
CATCH_AND_HANDLE |
| 243 |
} |
| 244 |
|
| 245 |
DEFINE_GETTER_PERMISSIVE(RangeSearchPartialResult, FaissRangeSearchResult*, res) |
| 246 |
|
| 247 |
int faiss_RangeSearchPartialResult_new( |
| 248 |
FaissRangeSearchPartialResult** p_res, |
| 249 |
FaissRangeSearchResult* res_in) { |
| 250 |
try { |
| 251 |
*p_res = reinterpret_cast<FaissRangeSearchPartialResult*>( |
| 252 |
new RangeSearchPartialResult( |
| 253 |
reinterpret_cast<RangeSearchResult*>(res_in))); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
CATCH_AND_HANDLE |
| 257 |
} |
| 258 |
|
| 259 |
int faiss_RangeSearchPartialResult_finalize( |
| 260 |
FaissRangeSearchPartialResult* res) { |
| 261 |
try { |
| 262 |
reinterpret_cast<RangeSearchPartialResult*>(res)->finalize(); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
CATCH_AND_HANDLE |
| 266 |
} |
| 267 |
|
| 268 |
/// called by range_search before do_allocation |
| 269 |
int faiss_RangeSearchPartialResult_set_lims( |
| 270 |
FaissRangeSearchPartialResult* res) { |
| 271 |
try { |
| 272 |
reinterpret_cast<RangeSearchPartialResult*>(res)->set_lims(); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
CATCH_AND_HANDLE |
| 276 |
} |
| 277 |
|
| 278 |
int faiss_RangeSearchPartialResult_new_result( |
| 279 |
FaissRangeSearchPartialResult* res, |
| 280 |
idx_t qno, |
| 281 |
FaissRangeQueryResult** qr) { |
| 282 |
try { |
| 283 |
auto q = &reinterpret_cast<RangeSearchPartialResult*>(res)->new_result( |
| 284 |
qno); |
| 285 |
if (qr) { |
| 286 |
*qr = reinterpret_cast<FaissRangeQueryResult*>(&q); |
| 287 |
} |
| 288 |
return 0; |
| 289 |
} |
| 290 |
CATCH_AND_HANDLE |
| 291 |
} |
| 292 |
|
| 293 |
DEFINE_DESTRUCTOR(DistanceComputer) |
| 294 |
|
| 295 |
int faiss_DistanceComputer_set_query( |
| 296 |
FaissDistanceComputer* dc, |
| 297 |
const float* x) { |
| 298 |
try { |
| 299 |
reinterpret_cast<DistanceComputer*>(dc)->set_query(x); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
CATCH_AND_HANDLE |
| 303 |
} |
| 304 |
|
| 305 |
int faiss_DistanceComputer_vector_to_query_dis( |
| 306 |
FaissDistanceComputer* dc, |
| 307 |
idx_t i, |
| 308 |
float* qd) { |
| 309 |
try { |
| 310 |
*qd = reinterpret_cast<DistanceComputer*>(dc)->operator()(i); |
| 311 |
return 0; |
| 312 |
} |
| 313 |
CATCH_AND_HANDLE |
| 314 |
} |
| 315 |
|
| 316 |
int faiss_DistanceComputer_symmetric_dis( |
| 317 |
FaissDistanceComputer* dc, |
| 318 |
idx_t i, |
| 319 |
idx_t j, |
| 320 |
float* vd) { |
| 321 |
try { |
| 322 |
*vd = reinterpret_cast<DistanceComputer*>(dc)->symmetric_dis(i, j); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
CATCH_AND_HANDLE |
| 326 |
} |