faiss 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ext/faiss/extconf.rb +1 -1
  6. data/lib/faiss/version.rb +1 -1
  7. data/vendor/faiss/faiss/AutoTune.cpp +36 -33
  8. data/vendor/faiss/faiss/AutoTune.h +6 -3
  9. data/vendor/faiss/faiss/Clustering.cpp +16 -12
  10. data/vendor/faiss/faiss/Index.cpp +3 -4
  11. data/vendor/faiss/faiss/Index.h +3 -3
  12. data/vendor/faiss/faiss/IndexBinary.cpp +3 -4
  13. data/vendor/faiss/faiss/IndexBinary.h +1 -1
  14. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -12
  15. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +1 -2
  16. data/vendor/faiss/faiss/IndexFlat.cpp +0 -148
  17. data/vendor/faiss/faiss/IndexFlat.h +0 -51
  18. data/vendor/faiss/faiss/IndexHNSW.cpp +4 -5
  19. data/vendor/faiss/faiss/IndexIVF.cpp +118 -31
  20. data/vendor/faiss/faiss/IndexIVF.h +22 -15
  21. data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -3
  22. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -1
  23. data/vendor/faiss/faiss/IndexIVFPQ.cpp +39 -15
  24. data/vendor/faiss/faiss/IndexIVFPQ.h +25 -9
  25. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  26. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  27. data/vendor/faiss/faiss/IndexIVFPQR.cpp +8 -9
  28. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -1
  29. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -2
  30. data/vendor/faiss/faiss/IndexPQ.cpp +34 -18
  31. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  32. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  33. data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -0
  34. data/vendor/faiss/faiss/IndexPreTransform.h +2 -0
  35. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  36. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  37. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -2
  38. data/vendor/faiss/faiss/IndexScalarQuantizer.h +1 -1
  39. data/vendor/faiss/faiss/gpu/GpuDistance.h +1 -1
  40. data/vendor/faiss/faiss/gpu/GpuIndex.h +16 -9
  41. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +8 -1
  42. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -11
  43. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +19 -2
  44. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +28 -2
  45. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +24 -14
  46. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +29 -2
  47. data/vendor/faiss/faiss/gpu/GpuResources.h +4 -0
  48. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +60 -27
  49. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +28 -6
  50. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  51. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  52. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +3 -3
  53. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +3 -2
  54. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  55. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +7 -2
  56. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +5 -1
  57. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  58. data/vendor/faiss/faiss/gpu/test/TestUtils.h +33 -0
  59. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -0
  60. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +6 -0
  61. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +5 -6
  62. data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
  63. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +5 -4
  64. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -4
  65. data/vendor/faiss/faiss/impl/PolysemousTraining.h +4 -4
  66. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +22 -12
  67. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -0
  68. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  69. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -19
  70. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +6 -0
  71. data/vendor/faiss/faiss/impl/index_read.cpp +64 -96
  72. data/vendor/faiss/faiss/impl/index_write.cpp +34 -25
  73. data/vendor/faiss/faiss/impl/io.cpp +33 -2
  74. data/vendor/faiss/faiss/impl/io.h +7 -2
  75. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -15
  76. data/vendor/faiss/faiss/impl/platform_macros.h +44 -0
  77. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  78. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  79. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  80. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  81. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  82. data/vendor/faiss/faiss/index_factory.cpp +112 -7
  83. data/vendor/faiss/faiss/index_io.h +1 -48
  84. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  85. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  86. data/vendor/faiss/faiss/{DirectMap.cpp → invlists/DirectMap.cpp} +1 -1
  87. data/vendor/faiss/faiss/{DirectMap.h → invlists/DirectMap.h} +1 -1
  88. data/vendor/faiss/faiss/{InvertedLists.cpp → invlists/InvertedLists.cpp} +72 -1
  89. data/vendor/faiss/faiss/{InvertedLists.h → invlists/InvertedLists.h} +32 -1
  90. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  91. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  92. data/vendor/faiss/faiss/{OnDiskInvertedLists.cpp → invlists/OnDiskInvertedLists.cpp} +21 -6
  93. data/vendor/faiss/faiss/{OnDiskInvertedLists.h → invlists/OnDiskInvertedLists.h} +5 -2
  94. data/vendor/faiss/faiss/python/python_callbacks.h +8 -1
  95. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  96. data/vendor/faiss/faiss/utils/Heap.cpp +2 -4
  97. data/vendor/faiss/faiss/utils/Heap.h +61 -50
  98. data/vendor/faiss/faiss/utils/distances.cpp +164 -319
  99. data/vendor/faiss/faiss/utils/distances.h +28 -20
  100. data/vendor/faiss/faiss/utils/distances_simd.cpp +277 -49
  101. data/vendor/faiss/faiss/utils/extra_distances.cpp +1 -2
  102. data/vendor/faiss/faiss/utils/hamming-inl.h +4 -4
  103. data/vendor/faiss/faiss/utils/hamming.cpp +3 -6
  104. data/vendor/faiss/faiss/utils/hamming.h +2 -7
  105. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  106. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  107. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  108. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  109. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  110. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  111. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  112. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  113. metadata +43 -141
  114. data/vendor/faiss/benchs/bench_6bit_codec.cpp +0 -80
  115. data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
  116. data/vendor/faiss/c_api/AutoTune_c.h +0 -66
  117. data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
  118. data/vendor/faiss/c_api/Clustering_c.h +0 -123
  119. data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
  120. data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
  121. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
  122. data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
  123. data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
  124. data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
  125. data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
  126. data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
  127. data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
  128. data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
  129. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -38
  130. data/vendor/faiss/c_api/IndexShards_c.h +0 -39
  131. data/vendor/faiss/c_api/Index_c.cpp +0 -105
  132. data/vendor/faiss/c_api/Index_c.h +0 -183
  133. data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
  134. data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
  135. data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
  136. data/vendor/faiss/c_api/clone_index_c.h +0 -32
  137. data/vendor/faiss/c_api/error_c.h +0 -42
  138. data/vendor/faiss/c_api/error_impl.cpp +0 -27
  139. data/vendor/faiss/c_api/error_impl.h +0 -16
  140. data/vendor/faiss/c_api/faiss_c.h +0 -58
  141. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -98
  142. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
  143. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
  144. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
  145. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
  146. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
  147. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
  148. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
  149. data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
  150. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
  151. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
  152. data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
  153. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
  154. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
  155. data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
  156. data/vendor/faiss/c_api/index_factory_c.h +0 -30
  157. data/vendor/faiss/c_api/index_io_c.cpp +0 -42
  158. data/vendor/faiss/c_api/index_io_c.h +0 -50
  159. data/vendor/faiss/c_api/macros_impl.h +0 -110
  160. data/vendor/faiss/demos/demo_imi_flat.cpp +0 -154
  161. data/vendor/faiss/demos/demo_imi_pq.cpp +0 -203
  162. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -151
  163. data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
  164. data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
  165. data/vendor/faiss/misc/test_blas.cpp +0 -87
  166. data/vendor/faiss/tests/test_binary_flat.cpp +0 -62
  167. data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -188
  168. data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -70
  169. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -100
  170. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -573
  171. data/vendor/faiss/tests/test_merge.cpp +0 -260
  172. data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
  173. data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -225
  174. data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -193
  175. data/vendor/faiss/tests/test_params_override.cpp +0 -236
  176. data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
  177. data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -246
  178. data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
  179. data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
  180. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -104
  181. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -85
  182. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -98
  183. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -122
  184. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +0 -104
@@ -1,159 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- #include <memory>
9
- #include <cstdio>
10
- #include <cstdlib>
11
-
12
- #include <gtest/gtest.h>
13
-
14
- #include <faiss/IndexIVFFlat.h>
15
- #include <faiss/index_io.h>
16
- #include <faiss/impl/io.h>
17
- #include <faiss/AutoTune.h>
18
- #include <faiss/index_factory.h>
19
- #include <faiss/clone_index.h>
20
- #include <faiss/VectorTransform.h>
21
- #include <faiss/utils/random.h>
22
- #include <faiss/IVFlib.h>
23
-
24
-
25
- namespace {
26
-
27
- // parameters to use for the test
28
- int d = 64;
29
- size_t nb = 1000;
30
- size_t nq = 100;
31
- size_t nt = 500;
32
- int k = 10;
33
- int nlist = 40;
34
-
35
- using namespace faiss;
36
-
37
- typedef faiss::Index::idx_t idx_t;
38
-
39
-
40
- std::vector<float> get_data (size_t nb, int seed) {
41
- std::vector<float> x (nb * d);
42
- float_randn (x.data(), nb * d, seed);
43
- return x;
44
- }
45
-
46
-
47
- void test_index_type(const char *factory_string) {
48
-
49
- // transfer inverted lists in nslice slices
50
- int nslice = 3;
51
-
52
- /****************************************************************
53
- * trained reference index
54
- ****************************************************************/
55
-
56
- std::unique_ptr<Index> trained (index_factory (d, factory_string));
57
-
58
- {
59
- auto xt = get_data (nt, 123);
60
- trained->train (nt, xt.data());
61
- }
62
-
63
- // sample nq query vectors to check if results are the same
64
- auto xq = get_data (nq, 818);
65
-
66
-
67
- /****************************************************************
68
- * source index
69
- ***************************************************************/
70
- std::unique_ptr<Index> src_index (clone_index (trained.get()));
71
-
72
- { // add some data to source index
73
- auto xb = get_data (nb, 245);
74
- src_index->add (nb, xb.data());
75
- }
76
-
77
- ParameterSpace().set_index_parameter (src_index.get(), "nprobe", 4);
78
-
79
- // remember reference search result on source index
80
- std::vector<idx_t> Iref (nq * k);
81
- std::vector<float> Dref (nq * k);
82
- src_index->search (nq, xq.data(), k, Dref.data(), Iref.data());
83
-
84
- /****************************************************************
85
- * destination index -- should be replaced by source index
86
- ***************************************************************/
87
-
88
- std::unique_ptr<Index> dst_index (clone_index (trained.get()));
89
-
90
- { // initial state: filled in with some garbage
91
- int nb2 = nb + 10;
92
- auto xb = get_data (nb2, 366);
93
- dst_index->add (nb2, xb.data());
94
- }
95
-
96
- std::vector<idx_t> Inew (nq * k);
97
- std::vector<float> Dnew (nq * k);
98
-
99
- ParameterSpace().set_index_parameter (dst_index.get(), "nprobe", 4);
100
-
101
- // transfer from source to destination in nslice slices
102
- for (int sl = 0; sl < nslice; sl++) {
103
-
104
- // so far, the indexes are different
105
- dst_index->search (nq, xq.data(), k, Dnew.data(), Inew.data());
106
- EXPECT_TRUE (Iref != Inew);
107
- EXPECT_TRUE (Dref != Dnew);
108
-
109
- // range of inverted list indices to transfer
110
- long i0 = sl * nlist / nslice;
111
- long i1 = (sl + 1) * nlist / nslice;
112
-
113
- std::vector<uint8_t> data_to_transfer;
114
- {
115
- std::unique_ptr<ArrayInvertedLists> il
116
- (ivflib::get_invlist_range (src_index.get(), i0, i1));
117
- // serialize inverted lists
118
- VectorIOWriter wr;
119
- write_InvertedLists (il.get(), &wr);
120
- data_to_transfer.swap (wr.data);
121
- }
122
-
123
- // transfer data here from source machine to dest machine
124
-
125
- {
126
- VectorIOReader reader;
127
- reader.data.swap (data_to_transfer);
128
-
129
- // deserialize inverted lists
130
- std::unique_ptr<ArrayInvertedLists> il
131
- (dynamic_cast<ArrayInvertedLists *>
132
- (read_InvertedLists (&reader)));
133
-
134
- // swap inverted lists. Block searches here!
135
- {
136
- ivflib::set_invlist_range (dst_index.get(), i0, i1, il.get());
137
- }
138
- }
139
-
140
- }
141
- EXPECT_EQ (dst_index->ntotal, src_index->ntotal);
142
-
143
- // now, the indexes are the same
144
- dst_index->search (nq, xq.data(), k, Dnew.data(), Inew.data());
145
- EXPECT_TRUE (Iref == Inew);
146
- EXPECT_TRUE (Dref == Dnew);
147
-
148
- }
149
-
150
- } // namespace
151
-
152
-
153
- TEST(TRANS, IVFFlat) {
154
- test_index_type ("IVF40,Flat");
155
- }
156
-
157
- TEST(TRANS, IVFFlatPreproc) {
158
- test_index_type ("PCAR32,IVF40,Flat");
159
- }
@@ -1,104 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- #include <cstdio>
9
- #include <cstdlib>
10
- #include <random>
11
-
12
- #include <faiss/IndexFlat.h>
13
-
14
- // 64-bit int
15
- using idx_t = faiss::Index::idx_t;
16
-
17
- int main() {
18
- int d = 64; // dimension
19
- int nb = 100000; // database size
20
- int nq = 10000; // nb of queries
21
-
22
- std::mt19937 rng;
23
- std::uniform_real_distribution<> distrib;
24
-
25
- float *xb = new float[d * nb];
26
- float *xq = new float[d * nq];
27
-
28
- for(int i = 0; i < nb; i++) {
29
- for(int j = 0; j < d; j++)
30
- xb[d * i + j] = distrib(rng);
31
- xb[d * i] += i / 1000.;
32
- }
33
-
34
- for(int i = 0; i < nq; i++) {
35
- for(int j = 0; j < d; j++)
36
- xq[d * i + j] = distrib(rng);
37
- xq[d * i] += i / 1000.;
38
- }
39
-
40
- faiss::IndexFlatL2 index(d); // call constructor
41
- printf("is_trained = %s\n", index.is_trained ? "true" : "false");
42
- index.add(nb, xb); // add vectors to the index
43
- printf("ntotal = %zd\n", index.ntotal);
44
-
45
- int k = 4;
46
-
47
- { // sanity check: search 5 first vectors of xb
48
- idx_t *I = new idx_t[k * 5];
49
- float *D = new float[k * 5];
50
-
51
- index.search(5, xb, k, D, I);
52
-
53
- // print results
54
- printf("I=\n");
55
- for(int i = 0; i < 5; i++) {
56
- for(int j = 0; j < k; j++)
57
- printf("%5zd ", I[i * k + j]);
58
- printf("\n");
59
- }
60
-
61
- printf("D=\n");
62
- for(int i = 0; i < 5; i++) {
63
- for(int j = 0; j < k; j++)
64
- printf("%7g ", D[i * k + j]);
65
- printf("\n");
66
- }
67
-
68
- delete [] I;
69
- delete [] D;
70
- }
71
-
72
-
73
- { // search xq
74
- idx_t *I = new idx_t[k * nq];
75
- float *D = new float[k * nq];
76
-
77
- index.search(nq, xq, k, D, I);
78
-
79
- // print results
80
- printf("I (5 first results)=\n");
81
- for(int i = 0; i < 5; i++) {
82
- for(int j = 0; j < k; j++)
83
- printf("%5zd ", I[i * k + j]);
84
- printf("\n");
85
- }
86
-
87
- printf("I (5 last results)=\n");
88
- for(int i = nq - 5; i < nq; i++) {
89
- for(int j = 0; j < k; j++)
90
- printf("%5zd ", I[i * k + j]);
91
- printf("\n");
92
- }
93
-
94
- delete [] I;
95
- delete [] D;
96
- }
97
-
98
-
99
-
100
- delete [] xb;
101
- delete [] xq;
102
-
103
- return 0;
104
- }
@@ -1,85 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- #include <cstdio>
9
- #include <cstdlib>
10
- #include <cassert>
11
- #include <random>
12
-
13
- #include <faiss/IndexFlat.h>
14
- #include <faiss/IndexIVFFlat.h>
15
-
16
- using idx_t = faiss::Index::idx_t;
17
-
18
- int main() {
19
- int d = 64; // dimension
20
- int nb = 100000; // database size
21
- int nq = 10000; // nb of queries
22
-
23
- std::mt19937 rng;
24
- std::uniform_real_distribution<> distrib;
25
-
26
- float *xb = new float[d * nb];
27
- float *xq = new float[d * nq];
28
-
29
- for(int i = 0; i < nb; i++) {
30
- for(int j = 0; j < d; j++)
31
- xb[d * i + j] = distrib(rng);
32
- xb[d * i] += i / 1000.;
33
- }
34
-
35
- for(int i = 0; i < nq; i++) {
36
- for(int j = 0; j < d; j++)
37
- xq[d * i + j] = distrib(rng);
38
- xq[d * i] += i / 1000.;
39
- }
40
-
41
-
42
- int nlist = 100;
43
- int k = 4;
44
-
45
- faiss::IndexFlatL2 quantizer(d); // the other index
46
- faiss::IndexIVFFlat index(&quantizer, d, nlist);
47
- assert(!index.is_trained);
48
- index.train(nb, xb);
49
- assert(index.is_trained);
50
- index.add(nb, xb);
51
-
52
- { // search xq
53
- idx_t *I = new idx_t[k * nq];
54
- float *D = new float[k * nq];
55
-
56
- index.search(nq, xq, k, D, I);
57
-
58
- printf("I=\n");
59
- for(int i = nq - 5; i < nq; i++) {
60
- for(int j = 0; j < k; j++)
61
- printf("%5zd ", I[i * k + j]);
62
- printf("\n");
63
- }
64
-
65
- index.nprobe = 10;
66
- index.search(nq, xq, k, D, I);
67
-
68
- printf("I=\n");
69
- for(int i = nq - 5; i < nq; i++) {
70
- for(int j = 0; j < k; j++)
71
- printf("%5zd ", I[i * k + j]);
72
- printf("\n");
73
- }
74
-
75
- delete [] I;
76
- delete [] D;
77
- }
78
-
79
-
80
-
81
- delete [] xb;
82
- delete [] xq;
83
-
84
- return 0;
85
- }
@@ -1,98 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- #include <cstdio>
9
- #include <cstdlib>
10
- #include <random>
11
-
12
- #include <faiss/IndexFlat.h>
13
- #include <faiss/IndexIVFPQ.h>
14
-
15
- using idx_t = faiss::Index::idx_t;
16
-
17
- int main() {
18
- int d = 64; // dimension
19
- int nb = 100000; // database size
20
- int nq = 10000; // nb of queries
21
-
22
- std::mt19937 rng;
23
- std::uniform_real_distribution<> distrib;
24
-
25
- float *xb = new float[d * nb];
26
- float *xq = new float[d * nq];
27
-
28
- for(int i = 0; i < nb; i++) {
29
- for(int j = 0; j < d; j++)
30
- xb[d * i + j] = distrib(rng);
31
- xb[d * i] += i / 1000.;
32
- }
33
-
34
- for(int i = 0; i < nq; i++) {
35
- for(int j = 0; j < d; j++)
36
- xq[d * i + j] = distrib(rng);
37
- xq[d * i] += i / 1000.;
38
- }
39
-
40
-
41
- int nlist = 100;
42
- int k = 4;
43
- int m = 8; // bytes per vector
44
- faiss::IndexFlatL2 quantizer(d); // the other index
45
- faiss::IndexIVFPQ index(&quantizer, d, nlist, m, 8);
46
-
47
- index.train(nb, xb);
48
- index.add(nb, xb);
49
-
50
- { // sanity check
51
- idx_t *I = new idx_t[k * 5];
52
- float *D = new float[k * 5];
53
-
54
- index.search(5, xb, k, D, I);
55
-
56
- printf("I=\n");
57
- for(int i = 0; i < 5; i++) {
58
- for(int j = 0; j < k; j++)
59
- printf("%5zd ", I[i * k + j]);
60
- printf("\n");
61
- }
62
-
63
- printf("D=\n");
64
- for(int i = 0; i < 5; i++) {
65
- for(int j = 0; j < k; j++)
66
- printf("%7g ", D[i * k + j]);
67
- printf("\n");
68
- }
69
-
70
- delete [] I;
71
- delete [] D;
72
- }
73
-
74
- { // search xq
75
- idx_t *I = new idx_t[k * nq];
76
- float *D = new float[k * nq];
77
-
78
- index.nprobe = 10;
79
- index.search(nq, xq, k, D, I);
80
-
81
- printf("I=\n");
82
- for(int i = nq - 5; i < nq; i++) {
83
- for(int j = 0; j < k; j++)
84
- printf("%5zd ", I[i * k + j]);
85
- printf("\n");
86
- }
87
-
88
- delete [] I;
89
- delete [] D;
90
- }
91
-
92
-
93
-
94
- delete [] xb;
95
- delete [] xq;
96
-
97
- return 0;
98
- }