Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WriteIndex.cpp
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 
10 #include "../../IndexIVFFlat.h"
11 #include "../../IndexIVFPQ.h"
12 #include "../../IndexFlat.h"
13 #include "../../index_io.h"
14 #include "../test/TestUtils.h"
15 #include <vector>
16 #include <gflags/gflags.h>
17 
18 // For IVFPQ:
19 DEFINE_bool(ivfpq, false, "use IVFPQ encoding");
20 DEFINE_int32(codes, 4, "number of PQ codes per vector");
21 DEFINE_int32(bits_per_code, 8, "number of bits per PQ code");
22 
23 // For IVFFlat:
24 DEFINE_bool(l2, true, "use L2 metric (versus IP metric)");
25 DEFINE_bool(ivfflat, false, "use IVF flat encoding");
26 
27 // For both:
28 DEFINE_string(out, "/home/jhj/local/index.out", "index file for output");
29 DEFINE_int32(dim, 128, "vector dimension");
30 DEFINE_int32(num_coarse, 100, "number of coarse centroids");
31 DEFINE_int32(num, 100000, "total database size");
32 DEFINE_int32(num_train, -1, "number of database vecs to train on");
33 
34 template <typename T>
35 void fillAndSave(T& index, int numTrain, int num, int dim) {
36  auto trainVecs = faiss::gpu::randVecs(numTrain, dim);
37  index.train(numTrain, trainVecs.data());
38 
39  constexpr int kAddChunk = 1000000;
40 
41  for (int i = 0; i < num; i += kAddChunk) {
42  int numRemaining = (num - i) < kAddChunk ? (num - i) : kAddChunk;
43  auto vecs = faiss::gpu::randVecs(numRemaining, dim);
44 
45  printf("adding at %d: %d\n", i, numRemaining);
46  index.add(numRemaining, vecs.data());
47  }
48 
49  faiss::write_index(&index, FLAGS_out.c_str());
50 }
51 
52 int main(int argc, char** argv) {
53  gflags::ParseCommandLineFlags(&argc, &argv, true);
54 
55  // Either ivfpq or ivfflat must be set
56  if ((FLAGS_ivfpq && FLAGS_ivfflat) ||
57  (!FLAGS_ivfpq && !FLAGS_ivfflat)) {
58  printf("must specify either ivfpq or ivfflat\n");
59  return 1;
60  }
61 
62  auto dim = FLAGS_dim;
63  auto numCentroids = FLAGS_num_coarse;
64  auto num = FLAGS_num;
65  auto numTrain = FLAGS_num_train;
66  numTrain = numTrain == -1 ? std::max((num / 4), 1) : numTrain;
67  numTrain = std::min(num, numTrain);
68 
69  if (FLAGS_ivfpq) {
70  faiss::IndexFlatL2 quantizer(dim);
71  faiss::IndexIVFPQ index(&quantizer, dim, numCentroids,
72  FLAGS_codes, FLAGS_bits_per_code);
73  index.verbose = true;
74 
75  printf("IVFPQ: codes %d bits per code %d\n",
76  FLAGS_codes, FLAGS_bits_per_code);
77  printf("Lists: %d\n", numCentroids);
78  printf("Database: dim %d num vecs %d trained on %d\n", dim, num, numTrain);
79  printf("output file: %s\n", FLAGS_out.c_str());
80 
81  fillAndSave(index, numTrain, num, dim);
82  } else if (FLAGS_ivfflat) {
83  faiss::IndexFlatL2 quantizerL2(dim);
84  faiss::IndexFlatIP quantizerIP(dim);
85 
86  faiss::IndexFlat* quantizer = FLAGS_l2 ?
87  (faiss::IndexFlat*) &quantizerL2 :
88  (faiss::IndexFlat*) &quantizerIP;
89 
90  faiss::IndexIVFFlat index(quantizer, dim, numCentroids,
91  FLAGS_l2 ? faiss::METRIC_L2 :
92  faiss::METRIC_INNER_PRODUCT);
93 
94  printf("IVFFlat: metric %s\n", FLAGS_l2 ? "L2" : "IP");
95  printf("Lists: %d\n", numCentroids);
96  printf("Database: dim %d num vecs %d trained on %d\n", dim, num, numTrain);
97  printf("output file: %s\n", FLAGS_out.c_str());
98 
99  fillAndSave(index, numTrain, num, dim);
100  }
101 
102  return 0;
103 }