Debian Codegraph @

c_api/IndexIVF_c.cpp

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