13 #include "IndexIVFPQ.h"
24 #include "Clustering.h"
25 #include "IndexFlat.h"
29 #include "FaissAssert.h"
31 #include "AuxIndexStructures.h"
43 IndexIVFPQ::IndexIVFPQ (Index * quantizer,
size_t d,
size_t nlist,
44 size_t M,
size_t nbits_per_idx):
45 IndexIVF (quantizer, d, nlist, 0, METRIC_L2),
46 pq (d, M, nbits_per_idx)
48 FAISS_THROW_IF_NOT (nbits_per_idx <= 8);
53 use_precomputed_table = 0;
54 scan_table_threshold = 0;
56 polysemous_training =
nullptr;
57 do_polysemous_training =
false;
74 const float * x_in = x;
82 const float *trainset;
85 if(
verbose) printf(
"computing residuals\n");
89 float *residuals =
new float [n *
d];
90 del_residuals.set (residuals);
91 for (
idx_t i = 0; i < n; i++)
99 printf (
"training %zdx%zd product quantizer on %ld vectors in %dD\n",
102 pq.train (n, trainset);
106 printf(
"doing polysemous training for PQ\n");
109 if (!pt) pt = &default_pt;
115 uint8_t *train_codes =
new uint8_t [
pq.
code_size * n];
119 for (
idx_t i = 0; i < n; i++) {
120 const float *xx = trainset + i *
d;
121 float * res = residuals_2 + i *
d;
123 for (
int j = 0; j <
d; j++)
124 res[j] = xx[j] - res[j];
145 void IndexIVFPQ::encode (
long key,
const float * x, uint8_t * code)
const
148 float residual_vec[
d];
156 const float * x, uint8_t * xcodes,
157 bool compute_keys)
const
163 float *residuals =
new float [n *
d];
166 for (
size_t i = 0; i < n; i++)
175 const uint8_t * xcodes,
float * x)
const
179 std::vector<float> centroid (
d);
180 for (
size_t i = 0; i < n; i++) {
182 float *xi = x + i *
d;
183 for (
size_t j = 0; j <
d; j++) {
184 xi [j] += centroid [j];
204 float *residuals_2,
const long *precomputed_idx)
209 for (
idx_t i0 = 0; i0 < n; i0 += bs) {
210 idx_t i1 = std::min(i0 + bs, n);
212 printf(
"IndexIVFPQ::add_core_o: adding %ld:%ld / %ld\n",
216 xids ? xids + i0 :
nullptr,
217 residuals_2 ? residuals_2 + i0 * d :
nullptr,
218 precomputed_idx ? precomputed_idx + i0 :
nullptr);
228 if (precomputed_idx) {
229 idx = precomputed_idx;
231 long * idx0 =
new long [n];
238 uint8_t * xcodes =
new uint8_t [n *
code_size];
241 const float *to_encode =
nullptr;
245 float *residuals =
new float [n *
d];
247 for (
size_t i = 0; i < n; i++) {
249 memset (residuals + i *
d, 0,
sizeof(*residuals) * d);
252 x + i * d, residuals + i * d, idx[i]);
254 to_encode = residuals;
255 del_to_encode.set (to_encode);
264 for (
size_t i = 0; i < n; i++) {
269 memset (residuals_2, 0,
sizeof(*residuals_2) *
d);
278 float *res2 = residuals_2 + i *
d;
279 const float *xi = to_encode + i *
d;
281 for (
int j = 0; j <
d; j++)
282 res2[j] = xi[j] - res2[j];
286 direct_map.push_back (key << 32 | offset);
292 char comment[100] = {0};
294 snprintf (comment, 100,
"(%ld vectors ignored)", n_ignore);
295 printf(
" add_core times: %.3f %.3f %.3f %s\n",
296 t1 - t0, t2 - t1, t3 - t2, comment);
308 std::vector<float> centroid(
d);
312 for (
int i = 0; i <
d; ++i) {
313 recons[i] += centroid[i];
359 fprintf(stderr,
"IndexIVFPQ::precompute_table: WARN precomputed "
360 "tables not needed for inner product quantizers\n");
365 if (miq &&
pq.
M % miq->pq.
M == 0)
372 printf (
"precomputing IVFPQ tables type %d\n",
377 std::vector<float> r_norms (
pq.
M *
pq.
ksub, NAN);
378 for (
int m = 0; m <
pq.
M; m++)
379 for (
int j = 0; j <
pq.
ksub; j++)
380 r_norms [m *
pq.
ksub + j] =
386 std::vector<float> centroid (
d);
388 for (
size_t i = 0; i <
nlist; i++) {
392 pq.compute_inner_prod_table (centroid.data(), tab);
398 FAISS_THROW_IF_NOT (miq);
400 FAISS_THROW_IF_NOT (
pq.
M % cpq.
M == 0);
405 std::vector<float> centroids (
d * cpq.
ksub, NAN);
407 for (
int m = 0; m < cpq.
M; m++) {
408 for (
size_t i = 0; i < cpq.
ksub; i++) {
409 memcpy (centroids.data() + i *
d + m * cpq.
dsub,
411 sizeof (*centroids.data()) * cpq.
dsub);
415 pq.compute_inner_prod_tables (cpq.
ksub, centroids.data (),
418 for (
size_t i = 0; i < cpq.
ksub; i++) {
428 static uint64_t get_cycles () {
430 asm volatile(
"rdtsc \n\t"
433 return ((uint64_t)high << 32) | (low);
436 #define TIC t0 = get_cycles()
437 #define TOC get_cycles () - t0
454 const IndexIVFPQ & ivfpq;
458 const ProductQuantizer & pq;
461 int use_precomputed_table;
464 float * sim_table, * sim_table_2;
465 float * residual_vec, *decoded_vec;
468 std::vector<float> mem;
471 std::vector<const float *> sim_table_ptrs;
473 explicit QueryTables (
const IndexIVFPQ & ivfpq):
477 metric_type (ivfpq.metric_type),
478 by_residual (ivfpq.by_residual),
479 use_precomputed_table (ivfpq.use_precomputed_table)
481 mem.resize (pq.ksub * pq.M * 2 + d * 2);
482 sim_table = mem.data ();
483 sim_table_2 = sim_table + pq.ksub * pq.M;
484 residual_vec = sim_table_2 + pq.ksub * pq.M;
485 decoded_vec = residual_vec + d;
488 if (ivfpq.polysemous_ht != 0) {
489 q_code.resize (pq.code_size);
491 init_list_cycles = 0;
492 sim_table_ptrs.resize (pq.M);
503 void init_query (
const float * qi) {
505 if (metric_type == METRIC_INNER_PRODUCT)
509 if (!by_residual && ivfpq.polysemous_ht != 0)
510 pq.compute_code (qi, q_code.data());
513 void init_query_IP () {
515 pq.compute_inner_prod_table (qi, sim_table);
517 for (
int i = 0; i < pq.ksub * pq.M; i++) {
518 sim_table[i] = - sim_table[i];
522 void init_query_L2 () {
524 pq.compute_distance_table (qi, sim_table);
525 }
else if (use_precomputed_table) {
526 pq.compute_inner_prod_table (qi, sim_table_2);
537 std::vector<uint8_t> q_code;
539 uint64_t init_list_cycles;
544 float precompute_list_tables () {
548 if (metric_type == METRIC_INNER_PRODUCT)
549 dis0 = precompute_list_tables_IP ();
551 dis0 = precompute_list_tables_L2 ();
553 init_list_cycles += TOC;
557 float precompute_list_table_pointers () {
561 if (metric_type == METRIC_INNER_PRODUCT)
562 FAISS_THROW_MSG (
"not implemented");
564 dis0 = precompute_list_table_pointers_L2 ();
566 init_list_cycles += TOC;
574 float precompute_list_tables_IP ()
578 ivfpq.quantizer->reconstruct (key, decoded_vec);
580 float dis0 = -fvec_inner_product (qi, decoded_vec, d);
582 if (ivfpq.polysemous_ht) {
583 for (
int i = 0; i < d; i++) {
584 residual_vec [i] = qi[i] - decoded_vec[i];
586 pq.compute_code (residual_vec, q_code.data());
596 float precompute_list_tables_L2 ()
600 if (use_precomputed_table == 0) {
601 ivfpq.quantizer->compute_residual (qi, residual_vec, key);
602 pq.compute_distance_table (residual_vec, sim_table);
603 }
else if (use_precomputed_table == 1) {
607 &ivfpq.precomputed_table [key * pq.ksub * pq.M],
610 }
else if (use_precomputed_table == 2) {
613 const MultiIndexQuantizer *miq =
614 dynamic_cast<const MultiIndexQuantizer *
> (ivfpq.quantizer);
615 FAISS_THROW_IF_NOT (miq);
616 const ProductQuantizer &cpq = miq->pq;
617 int Mf = pq.M / cpq.M;
619 const float *qtab = sim_table_2;
620 float *ltab = sim_table;
623 for (
int cm = 0; cm < cpq.M; cm++) {
625 int ki = k & ((uint64_t(1) << cpq.nbits) - 1);
629 const float *pc = &ivfpq.precomputed_table
630 [(ki * pq.M + cm * Mf) * pq.ksub];
632 if (ivfpq.polysemous_ht == 0) {
639 ltab += Mf * pq.ksub;
640 qtab += Mf * pq.ksub;
642 for (
int m = cm * Mf; m < (cm + 1) * Mf; m++) {
644 (pq.ksub, pc, -2, qtab, ltab);
657 float precompute_list_table_pointers_L2 ()
661 if (use_precomputed_table == 1) {
664 const float * s = &ivfpq.precomputed_table [key * pq.ksub * pq.M];
665 for (
int m = 0; m < pq.M; m++) {
666 sim_table_ptrs [m] = s;
669 }
else if (use_precomputed_table == 2) {
672 const MultiIndexQuantizer *miq =
673 dynamic_cast<const MultiIndexQuantizer *
> (ivfpq.quantizer);
674 FAISS_THROW_IF_NOT (miq);
675 const ProductQuantizer &cpq = miq->pq;
676 int Mf = pq.M / cpq.M;
680 for (
int cm = 0; cm < cpq.M; cm++) {
681 int ki = k & ((uint64_t(1) << cpq.nbits) - 1);
684 const float *pc = &ivfpq.precomputed_table
685 [(ki * pq.M + cm * Mf) * pq.ksub];
687 for (
int m = m0; m < m0 + Mf; m++) {
688 sim_table_ptrs [m] = pc;
694 FAISS_THROW_MSG (
"need precomputed tables");
697 if (ivfpq.polysemous_ht) {
698 FAISS_THROW_MSG (
"not implemented");
716 template <
typename IDType>
717 struct InvertedListScanner: QueryTables {
719 const uint8_t * __restrict list_codes;
720 const IDType * list_ids;
723 explicit InvertedListScanner (
const IndexIVFPQ & ivfpq):
726 FAISS_THROW_IF_NOT (pq.byte_per_idx == 1);
732 size_t list_size_in,
const IDType *list_ids_in,
733 const uint8_t *list_codes_in) {
735 this->coarse_dis = coarse_dis;
736 list_size = list_size_in;
737 list_codes = list_codes_in;
738 list_ids = list_ids_in;
746 void scan_list_with_table (
747 size_t k,
float * heap_sim,
long * heap_ids,
bool store_pairs)
749 float dis0 = precompute_list_tables ();
751 for (
size_t j = 0; j < list_size; j++) {
754 const float *tab = sim_table;
756 for (
size_t m = 0; m < pq.M; m++) {
757 dis += tab[*list_codes++];
761 if (dis < heap_sim[0]) {
762 maxheap_pop (k, heap_sim, heap_ids);
763 long id = store_pairs ? (key << 32 | j) : list_ids[j];
764 maxheap_push (k, heap_sim, heap_ids, dis,
id);
772 void scan_list_with_pointer (
773 size_t k,
float * heap_sim,
long * heap_ids,
bool store_pairs)
776 float dis0 = precompute_list_table_pointers ();
778 for (
size_t j = 0; j < list_size; j++) {
781 const float *tab = sim_table_2;
783 for (
size_t m = 0; m < pq.M; m++) {
784 int ci = *list_codes++;
785 dis += sim_table_ptrs [m][ci] - 2 * tab [ci];
789 if (dis < heap_sim[0]) {
790 maxheap_pop (k, heap_sim, heap_ids);
791 long id = store_pairs ? (key << 32 | j) : list_ids[j];
792 maxheap_push (k, heap_sim, heap_ids, dis,
id);
799 void scan_on_the_fly_dist (
800 size_t k,
float * heap_sim,
long * heap_ids,
bool store_pairs)
803 if (by_residual && use_precomputed_table) {
804 scan_list_with_pointer (k, heap_sim, heap_ids, store_pairs);
812 if (metric_type == METRIC_INNER_PRODUCT) {
813 ivfpq.quantizer->reconstruct (key, residual_vec);
814 dis0 = fvec_inner_product (residual_vec, qi, d);
816 ivfpq.quantizer->compute_residual (qi, residual_vec, key);
824 for (
size_t j = 0; j < list_size; j++) {
826 pq.decode (list_codes, decoded_vec);
827 list_codes += pq.code_size;
830 if (metric_type == METRIC_INNER_PRODUCT) {
831 dis = -dis0 - fvec_inner_product (decoded_vec, qi, d);
836 if (dis < heap_sim[0]) {
837 maxheap_pop (k, heap_sim, heap_ids);
838 long id = store_pairs ? (key << 32 | j) : list_ids[j];
839 maxheap_push (k, heap_sim, heap_ids, dis,
id);
849 size_t n_hamming_pass;
852 template <
class HammingComputer>
853 void scan_list_polysemous_hc (
854 size_t k,
float * heap_sim,
long * heap_ids,
bool store_pairs)
856 float dis0 = precompute_list_tables ();
857 int ht = ivfpq.polysemous_ht;
859 int code_size = pq.code_size;
861 HammingComputer hc (q_code.data(), code_size);
863 for (
size_t j = 0; j < list_size; j++) {
864 const uint8_t *b_code = list_codes;
865 int hd = hc.hamming (b_code);
870 const float *tab = sim_table;
872 for (
size_t m = 0; m < pq.M; m++) {
873 dis += tab[*b_code++];
877 if (dis < heap_sim[0]) {
878 maxheap_pop (k, heap_sim, heap_ids);
879 long id = store_pairs ? (key << 32 | j) : list_ids[j];
880 maxheap_push (k, heap_sim, heap_ids, dis,
id);
883 list_codes += code_size;
887 void scan_list_polysemous (
888 size_t k,
float * heap_sim,
long * heap_ids,
bool store_pairs)
890 switch (pq.code_size) {
891 #define HANDLE_CODE_SIZE(cs) \
893 scan_list_polysemous_hc <HammingComputer ## cs> \
894 (k, heap_sim, heap_ids, store_pairs); \
898 HANDLE_CODE_SIZE(16);
899 HANDLE_CODE_SIZE(20);
900 HANDLE_CODE_SIZE(32);
901 HANDLE_CODE_SIZE(64);
902 #undef HANDLE_CODE_SIZE
904 if (pq.code_size % 8 == 0)
905 scan_list_polysemous_hc <HammingComputerM8>
906 (k, heap_sim, heap_ids, store_pairs);
908 scan_list_polysemous_hc <HammingComputerM4>
909 (k, heap_sim, heap_ids, store_pairs);
922 IndexIVFPQStats indexIVFPQ_stats;
924 void IndexIVFPQStats::reset () {
925 memset (
this, 0,
sizeof (*
this));
932 const float *coarse_dis,
933 float *distances,
idx_t *labels,
934 bool store_pairs)
const
937 size_t(nx), size_t(k),
943 InvertedListScanner<long> qt (*
this);
944 size_t stats_nlist = 0;
945 size_t stats_ncode = 0;
946 uint64_t init_query_cycles = 0;
947 uint64_t scan_cycles = 0;
948 uint64_t heap_cycles = 0;
951 for (
size_t i = 0; i < nx; i++) {
952 const float *qi = qx + i *
d;
953 const long * keysi = keys + i *
nprobe;
954 const float *coarse_dis_i = coarse_dis + i *
nprobe;
955 float * heap_sim = res.
get_val (i);
956 long * heap_ids = res.
get_ids (i);
960 maxheap_heapify (k, heap_sim, heap_ids);
965 init_query_cycles += TOC;
969 for (
size_t ik = 0; ik <
nprobe; ik++) {
970 long key = keysi[ik];
980 if (list_size == 0)
continue;
982 qt.init_list (key, coarse_dis_i[ik],
988 qt.scan_list_polysemous
989 (k, heap_sim, heap_ids, store_pairs);
991 qt.scan_list_with_table (k, heap_sim, heap_ids, store_pairs);
993 qt.scan_on_the_fly_dist (k, heap_sim, heap_ids, store_pairs);
999 stats_ncode += nscan;
1001 maxheap_reorder (k, heap_sim, heap_ids);
1004 for (
size_t j = 0; j < k; j++)
1005 heap_sim[j] = -heap_sim[j];
1010 #pragma omp critical
1012 indexIVFPQ_stats.n_hamming_pass += qt.n_hamming_pass;
1013 indexIVFPQ_stats.nlist += stats_nlist;
1014 indexIVFPQ_stats.ncode += stats_ncode;
1016 indexIVFPQ_stats.init_query_cycles += init_query_cycles;
1017 indexIVFPQ_stats.init_list_cycles += qt.init_list_cycles;
1018 indexIVFPQ_stats.scan_cycles += scan_cycles - qt.init_list_cycles;
1019 indexIVFPQ_stats.heap_cycles += heap_cycles;
1023 indexIVFPQ_stats.nq += nx;
1027 IndexIVFPQ::IndexIVFPQ ()
1041 bool operator () (
int a,
int b)
const {
1042 return cmp (a, b) > 0;
1044 int cmp (
int a,
int b)
const {
1045 return memcmp (tab + a * code_size, tab + b * code_size,
1055 for (
size_t list_no = 0; list_no <
nlist; list_no++) {
1057 std::vector<int> ord (n);
1058 for (
int i = 0; i < n; i++) ord[i] = i;
1060 std::sort (ord.begin(), ord.end(), cs);
1064 for (
int i = 0; i < n; i++) {
1065 if (prev >= 0 && cs.cmp (ord [prev], ord [i]) == 0) {
1067 if (prev + 1 == i) {
1069 lims[ngroup] = lims[ngroup - 1];
1070 dup_ids [lims [ngroup]++] = list_ids [ord [prev]];
1072 dup_ids [lims [ngroup]++] = list_ids [ord [i]];
1088 IndexIVFPQR::IndexIVFPQR (
1089 Index * quantizer,
size_t d,
size_t nlist,
1090 size_t M,
size_t nbits_per_idx,
1091 size_t M_refine,
size_t nbits_per_idx_refine):
1092 IndexIVFPQ (quantizer, d, nlist, M, nbits_per_idx),
1093 refine_pq (d, M_refine, nbits_per_idx_refine),
1099 IndexIVFPQR::IndexIVFPQR ():
1119 float * residual_2 =
new float [n *
d];
1125 printf (
"training %zdx%zd 2nd level PQ quantizer on %ld %dD-vectors\n",
1141 const long *precomputed_idx) {
1143 float * residual_2 =
new float [n *
d];
1148 add_core_o (n, x, xids, residual_2, precomputed_idx);
1161 const float *L1_dis,
1162 float *distances,
idx_t *labels,
1163 bool store_pairs)
const
1167 size_t k_coarse = long(k *
k_factor);
1168 idx_t *coarse_labels =
new idx_t [k_coarse * n];
1171 float *coarse_distances =
new float [k_coarse * n];
1175 idx, L1_dis, coarse_distances, coarse_labels,
1180 indexIVFPQ_stats.search_cycles += TOC;
1185 size_t n_refine = 0;
1186 #pragma omp parallel reduction(+ : n_refine)
1189 float *residual_1 =
new float [2 *
d];
1191 float *residual_2 = residual_1 +
d;
1193 for (
idx_t i = 0; i < n; i++) {
1194 const float *xq = x + i *
d;
1195 const long * shortlist = coarse_labels + k_coarse * i;
1196 float * heap_sim = distances + k * i;
1197 long * heap_ids = labels + k * i;
1198 maxheap_heapify (k, heap_sim, heap_ids);
1200 for (
int j = 0; j < k_coarse; j++) {
1201 long sl = shortlist[j];
1203 if (sl == -1)
continue;
1205 int list_no = sl >> 32;
1206 int ofs = sl & 0xffffffff;
1208 assert (list_no >= 0 && list_no <
nlist);
1209 assert (ofs >= 0 && ofs < invlists->list_size (list_no));
1215 const uint8_t * l2code =
1219 for (
int l = 0; l <
d; l++)
1220 residual_2[l] = residual_1[l] - residual_2[l];
1224 assert (0 <=
id &&
id <
ntotal);
1228 float dis =
fvec_L2sqr (residual_1, residual_2, d);
1230 if (dis < heap_sim[0]) {
1231 maxheap_pop (k, heap_sim, heap_ids);
1232 long id_or_pair = store_pairs ? sl : id;
1233 maxheap_push (k, heap_sim, heap_ids, dis, id_or_pair);
1237 maxheap_reorder (k, heap_sim, heap_ids);
1240 indexIVFPQ_stats.nrefine += n_refine;
1241 indexIVFPQ_stats.refine_cycles += TOC;
1245 float* recons)
const
1250 assert (0 <=
id &&
id <
ntotal);
1252 std::vector<float> r3(d);
1254 for (
int i = 0; i <
d; ++i) {
1262 FAISS_THROW_IF_NOT(other);
1273 FAISS_THROW_MSG(
"not implemented");
1282 Index2Layer::Index2Layer (
Index * quantizer,
size_t nlist,
1285 Index (quantizer->d, metric),
1286 q1 (quantizer, nlist),
1287 pq (quantizer->d, M, 8)
1290 for (
int nbyte = 0; nbyte < 7; nbyte++) {
1291 if ((1L << (8 * nbyte)) >= nlist) {
1292 code_size_1 = nbyte;
1297 code_size = code_size_1 + code_size_2;
1300 Index2Layer::Index2Layer ()
1305 Index2Layer::~Index2Layer ()
1311 printf (
"training level-1 quantizer %ld vectors in %dD\n",
1318 printf(
"computing residuals\n");
1321 const float * x_in = x;
1329 std::vector<idx_t>
assign(n);
1331 std::vector<float> residuals(n * d);
1332 for (
idx_t i = 0; i < n; i++) {
1334 x + i * d, residuals.data() + i *
d, assign[i]);
1338 printf (
"training %zdx%zd product quantizer on %ld vectors in %dD\n",
1341 pq.train (n, residuals.data());
1350 for (
idx_t i0 = 0; i0 < n; i0 += bs) {
1351 idx_t i1 = std::min(i0 + bs, n);
1353 printf(
"Index2Layer::add: adding %ld:%ld / %ld\n",
1356 add (i1 - i0, x + i0 * d);
1361 std::vector<idx_t> codes1 (n);
1363 std::vector<float> residuals(n * d);
1364 for (
idx_t i = 0; i < n; i++) {
1366 x + i * d, residuals.data() + i *
d, codes1[i]);
1377 const char *ip = (
char*)&i;
1378 FAISS_THROW_IF_NOT_MSG (ip[0] == 0x44,
1379 "works only on a little-endian CPU");
1383 for (
idx_t i = 0; i < n; i++) {
1386 memcpy (wp, &codes2[i * code_size_2], code_size_2);
1399 idx_t* labels)
const
1401 FAISS_THROW_MSG (
"not implemented");
1408 FAISS_THROW_IF_NOT (i0 >= 0 && i0 + ni <=
ntotal);
1411 for (
idx_t i = 0; i < ni; i++) {
1417 for (
idx_t j = 0; j <
d; j++) {
1418 recons[j] += recons1[j];
1429 FAISS_THROW_IF_NOT (other.
ntotal == 0);
1431 const uint8_t *rp =
codes.data();
void precompute_table()
build precomputed table
void merge_from(IndexIVF &other, idx_t add_id) override
void transfer_to_IVFPQ(IndexIVFPQ &other) const
transfer the flat codes to an IVFPQ index
size_t code_size_2
size of the code for the second level
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
void search_preassigned(idx_t n, const float *x, idx_t k, const idx_t *assign, const float *centroid_dis, float *distances, idx_t *labels, bool store_pairs) const override
void decode(const uint8_t *code, float *x) const
decode a vector from a given code (or n vectors if third argument)
ProductQuantizer refine_pq
3rd level quantizer
float fvec_L2sqr(const float *x, const float *y, size_t d)
Squared L2 distance between two vectors.
PolysemousTraining * polysemous_training
if NULL, use default
virtual const idx_t * get_ids(size_t list_no) const =0
T * get_val(size_t key)
Return the list of values for a heap.
void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
size_t code_size
code_size_1 + code_size_2
const float * fvecs_maybe_subsample(size_t d, size_t *n, size_t nmax, const float *x, bool verbose, long seed)
size_t nprobe
number of probes at query time
void reset() override
removes all elements from the database.
void assign(idx_t n, const float *x, idx_t *labels, idx_t k=1)
virtual size_t list_size(size_t list_no) const =0
get the size of a list
void decode_multiple(size_t n, const long *keys, const uint8_t *xcodes, float *x) const
inverse of encode_multiple
void train_residual_o(idx_t n, const float *x, float *residuals_2)
same as train_residual, also output 2nd level residuals
bool do_polysemous_training
reorder PQ centroids after training?
size_t scan_table_threshold
use table computation or on-the-fly?
void train_residual(idx_t n, const float *x) override
trains the two product quantizers
void add_core(idx_t n, const float *x, const long *xids, const long *precomputed_idx=nullptr)
same as add_with_ids, but optionally use the precomputed list ids
size_t dsub
dimensionality of each subvector
int seed
seed for the random number generator
std::vector< float > precomputed_table
Level1Quantizer q1
first level quantizer
void fvec_madd(size_t n, const float *a, float bf, const float *b, float *c)
int polysemous_ht
Hamming thresh for polysemous filtering.
void reset() override
removes all elements from the database.
std::vector< uint8_t > codes
Codes. Size ntotal * code_size.
void add_with_ids(idx_t n, const float *x, const long *xids=nullptr) override
void compute_codes(const float *x, uint8_t *codes, size_t n) const
same as compute_code for several vectors
virtual idx_t get_single_id(size_t list_no, size_t offset) const
size_t code_size
code size per vector in bytes
virtual const uint8_t * get_single_code(size_t list_no, size_t offset) const
std::vector< uint8_t > refine_codes
corresponding codes
size_t code_size
byte per indexed vector
void train_residual(idx_t n, const float *x) override
trains the product quantizer
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
void encode_multiple(size_t n, long *keys, const float *x, uint8_t *codes, bool compute_keys=false) const
virtual size_t add_entry(size_t list_no, idx_t theid, const uint8_t *code)
add one entry to an inverted list
size_t ksub
number of centroids for each subquantizer
long idx_t
all indices are this type
void compute_code(const float *x, uint8_t *code) const
Quantize one vector with the product quantizer.
idx_t ntotal
total nb of indexed vectors
void train(idx_t n, const float *x) override
bool verbose
verbosity level
void reset() override
removes all elements from the database.
double getmillisecs()
ms elapsed since some arbitrary epoch
optimizes the order of indices in a ProductQuantizer
void add(idx_t n, const float *x) override
float fvec_norm_L2sqr(const float *x, size_t d)
void train_q1(size_t n, const float *x, bool verbose, MetricType metric_type)
Trains the quantizer and calls train_residual to train sub-quantizers.
ClusteringParameters cp
parameters used during clustering
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
not implemented
bool by_residual
Encode residual or plain vector?
TI * get_ids(size_t key)
Correspponding identifiers.
MetricType metric_type
type of metric this index uses for search
ProductQuantizer pq
produces the codes
InvertedLists * invlists
Acess to the actual data.
size_t M
number of subquantizers
size_t code_size_1
size of the code for the first level (ceil(log8(q1.nlist)))
void add_core_o(idx_t n, const float *x, const long *xids, float *residuals_2, const long *precomputed_idx=nullptr)
int fvec_madd_and_argmin(size_t n, const float *a, float bf, const float *b, float *c)
virtual const uint8_t * get_codes(size_t list_no) const =0
long remove_ids(const IDSelector &sel) override
Dataset manipulation functions.
void search_preassigned(idx_t n, const float *x, idx_t k, const idx_t *assign, const float *centroid_dis, float *distances, idx_t *labels, bool store_pairs) const override
Index * quantizer
quantizer that maps vectors to inverted lists
bool is_trained
set if the Index does not require training, or if training is done already
void compute_residual(const float *x, float *residual, idx_t key) const
float * get_centroids(size_t m, size_t i)
return the centroids associated with subvector m
size_t max_codes
max nb of codes to visit to do a query
virtual void reconstruct(idx_t key, float *recons) const
ProductQuantizer pq
second level quantizer is always a PQ
bool maintain_direct_map
map for direct access to the elements. Enables reconstruct().
void optimize_pq_for_hamming(ProductQuantizer &pq, size_t n, const float *x) const
int max_points_per_centroid
to limit size of dataset
bool verbose
verbose during training?
void add_with_ids(idx_t n, const float *x, const long *xids) override
void reconstruct(idx_t key, float *recons) const override
virtual void merge_from(IndexIVF &other, idx_t add_id)
size_t nlist
number of possible key values
size_t code_size
code size per vector in bytes
size_t find_duplicates(idx_t *ids, size_t *lims) const
MetricType
Some algorithms support both an inner product version and a L2 search version.
float k_factor
factor between k requested in search and the k requested from the IVFPQ
int use_precomputed_table
if by_residual, build precompute tables