faiss 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +16 -4
  5. data/ext/faiss/ext.cpp +12 -308
  6. data/ext/faiss/extconf.rb +6 -3
  7. data/ext/faiss/index.cpp +189 -0
  8. data/ext/faiss/index_binary.cpp +75 -0
  9. data/ext/faiss/kmeans.cpp +40 -0
  10. data/ext/faiss/numo.hpp +867 -0
  11. data/ext/faiss/pca_matrix.cpp +33 -0
  12. data/ext/faiss/product_quantizer.cpp +53 -0
  13. data/ext/faiss/utils.cpp +13 -0
  14. data/ext/faiss/utils.h +5 -0
  15. data/lib/faiss.rb +0 -5
  16. data/lib/faiss/version.rb +1 -1
  17. data/vendor/faiss/faiss/AutoTune.cpp +36 -33
  18. data/vendor/faiss/faiss/AutoTune.h +6 -3
  19. data/vendor/faiss/faiss/Clustering.cpp +16 -12
  20. data/vendor/faiss/faiss/Index.cpp +3 -4
  21. data/vendor/faiss/faiss/Index.h +3 -3
  22. data/vendor/faiss/faiss/IndexBinary.cpp +3 -4
  23. data/vendor/faiss/faiss/IndexBinary.h +1 -1
  24. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -12
  25. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +1 -2
  26. data/vendor/faiss/faiss/IndexFlat.cpp +0 -148
  27. data/vendor/faiss/faiss/IndexFlat.h +0 -51
  28. data/vendor/faiss/faiss/IndexHNSW.cpp +4 -5
  29. data/vendor/faiss/faiss/IndexIVF.cpp +118 -31
  30. data/vendor/faiss/faiss/IndexIVF.h +22 -15
  31. data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -3
  32. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -1
  33. data/vendor/faiss/faiss/IndexIVFPQ.cpp +39 -15
  34. data/vendor/faiss/faiss/IndexIVFPQ.h +25 -9
  35. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  37. data/vendor/faiss/faiss/IndexIVFPQR.cpp +8 -9
  38. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -1
  39. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -2
  40. data/vendor/faiss/faiss/IndexPQ.cpp +34 -18
  41. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  42. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  43. data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -0
  44. data/vendor/faiss/faiss/IndexPreTransform.h +2 -0
  45. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  46. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  47. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -2
  48. data/vendor/faiss/faiss/IndexScalarQuantizer.h +1 -1
  49. data/vendor/faiss/faiss/gpu/GpuDistance.h +1 -1
  50. data/vendor/faiss/faiss/gpu/GpuIndex.h +16 -9
  51. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +8 -1
  52. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -11
  53. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +19 -2
  54. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +28 -2
  55. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +24 -14
  56. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +29 -2
  57. data/vendor/faiss/faiss/gpu/GpuResources.h +4 -0
  58. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +60 -27
  59. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +28 -6
  60. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  61. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  62. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +3 -3
  63. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +3 -2
  64. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  65. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +7 -2
  66. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +5 -1
  67. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  68. data/vendor/faiss/faiss/gpu/test/TestUtils.h +33 -0
  69. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -0
  70. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +6 -0
  71. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +5 -6
  72. data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
  73. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +5 -4
  74. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -4
  75. data/vendor/faiss/faiss/impl/PolysemousTraining.h +4 -4
  76. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +22 -12
  77. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -0
  78. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  79. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -19
  80. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +6 -0
  81. data/vendor/faiss/faiss/impl/index_read.cpp +64 -96
  82. data/vendor/faiss/faiss/impl/index_write.cpp +34 -25
  83. data/vendor/faiss/faiss/impl/io.cpp +33 -2
  84. data/vendor/faiss/faiss/impl/io.h +7 -2
  85. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -15
  86. data/vendor/faiss/faiss/impl/platform_macros.h +44 -0
  87. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  88. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  89. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  90. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  91. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  92. data/vendor/faiss/faiss/index_factory.cpp +112 -7
  93. data/vendor/faiss/faiss/index_io.h +1 -48
  94. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  95. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  96. data/vendor/faiss/faiss/{DirectMap.cpp → invlists/DirectMap.cpp} +1 -1
  97. data/vendor/faiss/faiss/{DirectMap.h → invlists/DirectMap.h} +1 -1
  98. data/vendor/faiss/faiss/{InvertedLists.cpp → invlists/InvertedLists.cpp} +72 -1
  99. data/vendor/faiss/faiss/{InvertedLists.h → invlists/InvertedLists.h} +32 -1
  100. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  101. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  102. data/vendor/faiss/faiss/{OnDiskInvertedLists.cpp → invlists/OnDiskInvertedLists.cpp} +21 -6
  103. data/vendor/faiss/faiss/{OnDiskInvertedLists.h → invlists/OnDiskInvertedLists.h} +5 -2
  104. data/vendor/faiss/faiss/python/python_callbacks.h +8 -1
  105. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  106. data/vendor/faiss/faiss/utils/Heap.cpp +2 -4
  107. data/vendor/faiss/faiss/utils/Heap.h +61 -50
  108. data/vendor/faiss/faiss/utils/distances.cpp +164 -319
  109. data/vendor/faiss/faiss/utils/distances.h +28 -20
  110. data/vendor/faiss/faiss/utils/distances_simd.cpp +277 -49
  111. data/vendor/faiss/faiss/utils/extra_distances.cpp +1 -2
  112. data/vendor/faiss/faiss/utils/hamming-inl.h +4 -4
  113. data/vendor/faiss/faiss/utils/hamming.cpp +3 -6
  114. data/vendor/faiss/faiss/utils/hamming.h +2 -7
  115. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  116. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  117. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  118. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  119. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  120. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  121. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  122. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  123. metadata +54 -149
  124. data/lib/faiss/index.rb +0 -20
  125. data/lib/faiss/index_binary.rb +0 -20
  126. data/lib/faiss/kmeans.rb +0 -15
  127. data/lib/faiss/pca_matrix.rb +0 -15
  128. data/lib/faiss/product_quantizer.rb +0 -22
  129. data/vendor/faiss/benchs/bench_6bit_codec.cpp +0 -80
  130. data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
  131. data/vendor/faiss/c_api/AutoTune_c.h +0 -66
  132. data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
  133. data/vendor/faiss/c_api/Clustering_c.h +0 -123
  134. data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
  135. data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
  136. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
  137. data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
  138. data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
  139. data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
  140. data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
  141. data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
  142. data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
  143. data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
  144. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -38
  145. data/vendor/faiss/c_api/IndexShards_c.h +0 -39
  146. data/vendor/faiss/c_api/Index_c.cpp +0 -105
  147. data/vendor/faiss/c_api/Index_c.h +0 -183
  148. data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
  149. data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
  150. data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
  151. data/vendor/faiss/c_api/clone_index_c.h +0 -32
  152. data/vendor/faiss/c_api/error_c.h +0 -42
  153. data/vendor/faiss/c_api/error_impl.cpp +0 -27
  154. data/vendor/faiss/c_api/error_impl.h +0 -16
  155. data/vendor/faiss/c_api/faiss_c.h +0 -58
  156. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -98
  157. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
  158. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
  159. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
  160. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
  161. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
  162. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
  163. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
  164. data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
  165. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
  166. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
  167. data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
  168. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
  169. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
  170. data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
  171. data/vendor/faiss/c_api/index_factory_c.h +0 -30
  172. data/vendor/faiss/c_api/index_io_c.cpp +0 -42
  173. data/vendor/faiss/c_api/index_io_c.h +0 -50
  174. data/vendor/faiss/c_api/macros_impl.h +0 -110
  175. data/vendor/faiss/demos/demo_imi_flat.cpp +0 -154
  176. data/vendor/faiss/demos/demo_imi_pq.cpp +0 -203
  177. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -151
  178. data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
  179. data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
  180. data/vendor/faiss/misc/test_blas.cpp +0 -87
  181. data/vendor/faiss/tests/test_binary_flat.cpp +0 -62
  182. data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -188
  183. data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -70
  184. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -100
  185. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -573
  186. data/vendor/faiss/tests/test_merge.cpp +0 -260
  187. data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
  188. data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -225
  189. data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -193
  190. data/vendor/faiss/tests/test_params_override.cpp +0 -236
  191. data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
  192. data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -246
  193. data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
  194. data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
  195. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -104
  196. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -85
  197. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -98
  198. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -122
  199. 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
- }