| 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 "IndexBinaryIVF_c.h" |
| 11 |
#include <faiss/IndexBinaryIVF.h> |
| 12 |
#include <faiss/IndexIVF.h> |
| 13 |
#include "macros_impl.h" |
| 14 |
|
| 15 |
extern "C" { |
| 16 |
|
| 17 |
using faiss::IndexBinaryIVF; |
| 18 |
|
| 19 |
DEFINE_DESTRUCTOR(IndexBinaryIVF) |
| 20 |
DEFINE_INDEX_BINARY_DOWNCAST(IndexBinaryIVF) |
| 21 |
|
| 22 |
/// number of possible key values |
| 23 |
DEFINE_GETTER(IndexBinaryIVF, size_t, nlist) |
| 24 |
|
| 25 |
/// number of probes at query time |
| 26 |
DEFINE_GETTER(IndexBinaryIVF, size_t, nprobe) |
| 27 |
DEFINE_SETTER(IndexBinaryIVF, size_t, nprobe) |
| 28 |
|
| 29 |
/// quantizer that maps vectors to inverted lists |
| 30 |
DEFINE_GETTER_PERMISSIVE(IndexBinaryIVF, FaissIndexBinary*, quantizer) |
| 31 |
|
| 32 |
/// whether object owns the quantizer |
| 33 |
DEFINE_GETTER(IndexBinaryIVF, int, own_fields) |
| 34 |
DEFINE_SETTER(IndexBinaryIVF, int, own_fields) |
| 35 |
|
| 36 |
/// max nb of codes to visit to do a query |
| 37 |
DEFINE_GETTER(IndexBinaryIVF, size_t, max_codes) |
| 38 |
DEFINE_SETTER(IndexBinaryIVF, size_t, max_codes) |
| 39 |
|
| 40 |
/** Select between using a heap or counting to select the k smallest values |
| 41 |
* when scanning inverted lists. |
| 42 |
*/ |
| 43 |
DEFINE_GETTER(IndexBinaryIVF, int, use_heap) |
| 44 |
DEFINE_SETTER(IndexBinaryIVF, int, use_heap) |
| 45 |
|
| 46 |
/// collect computations per batch |
| 47 |
DEFINE_GETTER(IndexBinaryIVF, int, per_invlist_search) |
| 48 |
DEFINE_SETTER(IndexBinaryIVF, int, per_invlist_search) |
| 49 |
|
| 50 |
int faiss_IndexBinaryIVF_merge_from( |
| 51 |
FaissIndexBinaryIVF* index, |
| 52 |
FaissIndexBinaryIVF* other, |
| 53 |
idx_t add_id) { |
| 54 |
try { |
| 55 |
reinterpret_cast<IndexBinaryIVF*>(index)->merge_from( |
| 56 |
*reinterpret_cast<IndexBinaryIVF*>(other), add_id); |
| 57 |
} |
| 58 |
CATCH_AND_HANDLE |
| 59 |
} |
| 60 |
|
| 61 |
int faiss_IndexBinaryIVF_search_preassigned( |
| 62 |
const FaissIndexBinaryIVF* index, |
| 63 |
idx_t n, |
| 64 |
const uint8_t* x, |
| 65 |
idx_t k, |
| 66 |
const idx_t* cidx, |
| 67 |
const int32_t* cdis, |
| 68 |
int32_t* dis, |
| 69 |
idx_t* idx, |
| 70 |
int store_pairs, |
| 71 |
const FaissSearchParametersIVF* params) { |
| 72 |
try { |
| 73 |
const faiss::SearchParametersIVF* sp = |
| 74 |
reinterpret_cast<const faiss::SearchParametersIVF*>(params); |
| 75 |
reinterpret_cast<const IndexBinaryIVF*>(index)->search_preassigned( |
| 76 |
n, x, k, cidx, cdis, dis, idx, store_pairs, sp); |
| 77 |
} |
| 78 |
CATCH_AND_HANDLE |
| 79 |
} |
| 80 |
|
| 81 |
size_t faiss_IndexBinaryIVF_get_list_size( |
| 82 |
const FaissIndexBinaryIVF* index, |
| 83 |
size_t list_no) { |
| 84 |
return reinterpret_cast<const IndexBinaryIVF*>(index)->get_list_size( |
| 85 |
list_no); |
| 86 |
} |
| 87 |
|
| 88 |
int faiss_IndexBinaryIVF_make_direct_map( |
| 89 |
FaissIndexBinaryIVF* index, |
| 90 |
int new_maintain_direct_map) { |
| 91 |
try { |
| 92 |
reinterpret_cast<IndexBinaryIVF*>(index)->make_direct_map( |
| 93 |
static_cast<bool>(new_maintain_direct_map)); |
| 94 |
} |
| 95 |
CATCH_AND_HANDLE |
| 96 |
} |
| 97 |
|
| 98 |
double faiss_IndexBinaryIVF_imbalance_factor(const FaissIndexBinaryIVF* index) { |
| 99 |
return reinterpret_cast<const IndexBinaryIVF*>(index) |
| 100 |
->invlists->imbalance_factor(); |
| 101 |
} |
| 102 |
|
| 103 |
/// display some stats about the inverted lists |
| 104 |
void faiss_IndexBinaryIVF_print_stats(const FaissIndexBinaryIVF* index) { |
| 105 |
reinterpret_cast<const IndexBinaryIVF*>(index)->invlists->print_stats(); |
| 106 |
} |
| 107 |
|
| 108 |
/// get inverted lists ids |
| 109 |
void faiss_IndexBinaryIVF_invlists_get_ids( |
| 110 |
const FaissIndexBinaryIVF* index, |
| 111 |
size_t list_no, |
| 112 |
idx_t* invlist) { |
| 113 |
const idx_t* list = |
| 114 |
reinterpret_cast<const IndexBinaryIVF*>(index)->invlists->get_ids( |
| 115 |
list_no); |
| 116 |
size_t list_size = |
| 117 |
reinterpret_cast<const IndexBinaryIVF*>(index)->get_list_size( |
| 118 |
list_no); |
| 119 |
memcpy(invlist, list, list_size * sizeof(idx_t)); |
| 120 |
} |
| 121 |
} |