1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// -*- c++ -*-
#include "IndexIVF_c.h"
#include <faiss/IndexIVF.h>
#include "Clustering_c.h"
#include "Index_c.h"
#include "impl/AuxIndexStructures_c.h"
#include "macros_impl.h"
using faiss::IndexIVF;
using faiss::IndexIVFStats;
using faiss::SearchParametersIVF;
/// SearchParametersIVF definitions
DEFINE_DESTRUCTOR(SearchParametersIVF)
DEFINE_SEARCH_PARAMETERS_DOWNCAST(SearchParametersIVF)
int faiss_SearchParametersIVF_new(FaissSearchParametersIVF** p_sp) {
try {
SearchParametersIVF* sp = new SearchParametersIVF;
*p_sp = reinterpret_cast<FaissSearchParametersIVF*>(sp);
}
CATCH_AND_HANDLE
}
int faiss_SearchParametersIVF_new_with(
FaissSearchParametersIVF** p_sp,
FaissIDSelector* sel,
size_t nprobe,
size_t max_codes) {
try {
SearchParametersIVF* sp = new SearchParametersIVF;
sp->sel = reinterpret_cast<faiss::IDSelector*>(sel);
sp->nprobe = nprobe;
sp->max_codes = max_codes;
*p_sp = reinterpret_cast<FaissSearchParametersIVF*>(sp);
}
CATCH_AND_HANDLE
}
DEFINE_GETTER_PERMISSIVE(SearchParametersIVF, const FaissIDSelector*, sel)
DEFINE_GETTER(SearchParametersIVF, size_t, nprobe)
DEFINE_SETTER(SearchParametersIVF, size_t, nprobe)
DEFINE_GETTER(SearchParametersIVF, size_t, max_codes)
DEFINE_SETTER(SearchParametersIVF, size_t, max_codes)
/// IndexIVF definitions
DEFINE_DESTRUCTOR(IndexIVF)
DEFINE_INDEX_DOWNCAST(IndexIVF)
/// number of possible key values
DEFINE_GETTER(IndexIVF, size_t, nlist)
/// number of probes at query time
DEFINE_GETTER(IndexIVF, size_t, nprobe)
DEFINE_SETTER(IndexIVF, size_t, nprobe)
/// quantizer that maps vectors to inverted lists
DEFINE_GETTER_PERMISSIVE(IndexIVF, FaissIndex*, quantizer)
/**
* = 0: use the quantizer as index in a kmeans training
* = 1: just pass on the training set to the train() of the quantizer
* = 2: kmeans training on a flat index + add the centroids to the quantizer
*/
DEFINE_GETTER(IndexIVF, char, quantizer_trains_alone)
/// whether object owns the quantizer
DEFINE_GETTER(IndexIVF, int, own_fields)
DEFINE_SETTER(IndexIVF, int, own_fields)
using faiss::IndexIVF;
int faiss_IndexIVF_merge_from(
FaissIndexIVF* index,
FaissIndexIVF* other,
idx_t add_id) {
try {
reinterpret_cast<IndexIVF*>(index)->merge_from(
*reinterpret_cast<IndexIVF*>(other), add_id);
}
CATCH_AND_HANDLE
}
int faiss_IndexIVF_copy_subset_to(
const FaissIndexIVF* index,
FaissIndexIVF* other,
int subset_type,
idx_t a1,
idx_t a2) {
try {
reinterpret_cast<const IndexIVF*>(index)->copy_subset_to(
*reinterpret_cast<IndexIVF*>(other),
static_cast<faiss::InvertedLists::subset_type_t>(subset_type),
a1,
a2);
}
CATCH_AND_HANDLE
}
int faiss_IndexIVF_search_preassigned(
const FaissIndexIVF* index,
idx_t n,
const float* x,
idx_t k,
const idx_t* assign,
const float* centroid_dis,
float* distances,
idx_t* labels,
int store_pairs) {
try {
reinterpret_cast<const IndexIVF*>(index)->search_preassigned(
n, x, k, assign, centroid_dis, distances, labels, store_pairs);
}
CATCH_AND_HANDLE
}
size_t faiss_IndexIVF_get_list_size(
const FaissIndexIVF* index,
size_t list_no) {
return reinterpret_cast<const IndexIVF*>(index)->get_list_size(list_no);
}
int faiss_IndexIVF_make_direct_map(
FaissIndexIVF* index,
int new_maintain_direct_map) {
try {
reinterpret_cast<IndexIVF*>(index)->make_direct_map(
static_cast<bool>(new_maintain_direct_map));
}
CATCH_AND_HANDLE
}
double faiss_IndexIVF_imbalance_factor(const FaissIndexIVF* index) {
return reinterpret_cast<const IndexIVF*>(index)
->invlists->imbalance_factor();
}
/// display some stats about the inverted lists
void faiss_IndexIVF_print_stats(const FaissIndexIVF* index) {
reinterpret_cast<const IndexIVF*>(index)->invlists->print_stats();
}
/// get inverted lists ids
void faiss_IndexIVF_invlists_get_ids(
const FaissIndexIVF* index,
size_t list_no,
idx_t* invlist) {
const idx_t* list =
reinterpret_cast<const IndexIVF*>(index)->invlists->get_ids(
list_no);
size_t list_size =
reinterpret_cast<const IndexIVF*>(index)->get_list_size(list_no);
memcpy(invlist, list, list_size * sizeof(idx_t));
}
int faiss_IndexIVF_train_encoder(
FaissIndexIVF* index,
idx_t n,
const float* x,
const idx_t* assign) {
try {
reinterpret_cast<IndexIVF*>(index)->train_encoder(n, x, assign);
}
CATCH_AND_HANDLE
}
void faiss_IndexIVFStats_reset(FaissIndexIVFStats* stats) {
reinterpret_cast<IndexIVFStats*>(stats)->reset();
}
FaissIndexIVFStats* faiss_get_indexIVF_stats() {
return reinterpret_cast<FaissIndexIVFStats*>(&faiss::indexIVF_stats);
}
|