faiss 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/faiss/version.rb +1 -1
  4. data/vendor/faiss/faiss/AutoTune.cpp +292 -291
  5. data/vendor/faiss/faiss/AutoTune.h +55 -56
  6. data/vendor/faiss/faiss/Clustering.cpp +334 -195
  7. data/vendor/faiss/faiss/Clustering.h +88 -35
  8. data/vendor/faiss/faiss/IVFlib.cpp +171 -195
  9. data/vendor/faiss/faiss/IVFlib.h +48 -51
  10. data/vendor/faiss/faiss/Index.cpp +85 -103
  11. data/vendor/faiss/faiss/Index.h +54 -48
  12. data/vendor/faiss/faiss/Index2Layer.cpp +139 -164
  13. data/vendor/faiss/faiss/Index2Layer.h +22 -22
  14. data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
  15. data/vendor/faiss/faiss/IndexBinary.h +140 -132
  16. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
  17. data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
  18. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
  19. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
  20. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
  21. data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
  22. data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
  23. data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
  24. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
  25. data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
  26. data/vendor/faiss/faiss/IndexFlat.cpp +116 -147
  27. data/vendor/faiss/faiss/IndexFlat.h +35 -46
  28. data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
  29. data/vendor/faiss/faiss/IndexHNSW.h +57 -41
  30. data/vendor/faiss/faiss/IndexIVF.cpp +474 -454
  31. data/vendor/faiss/faiss/IndexIVF.h +146 -113
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +248 -250
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +457 -516
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +74 -66
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
  37. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
  38. data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
  39. data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
  40. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +125 -133
  41. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +19 -21
  42. data/vendor/faiss/faiss/IndexLSH.cpp +75 -96
  43. data/vendor/faiss/faiss/IndexLSH.h +21 -26
  44. data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
  45. data/vendor/faiss/faiss/IndexLattice.h +11 -16
  46. data/vendor/faiss/faiss/IndexNNDescent.cpp +231 -0
  47. data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
  48. data/vendor/faiss/faiss/IndexNSG.cpp +303 -0
  49. data/vendor/faiss/faiss/IndexNSG.h +85 -0
  50. data/vendor/faiss/faiss/IndexPQ.cpp +405 -464
  51. data/vendor/faiss/faiss/IndexPQ.h +64 -67
  52. data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
  53. data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
  54. data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
  55. data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
  56. data/vendor/faiss/faiss/IndexRefine.cpp +115 -131
  57. data/vendor/faiss/faiss/IndexRefine.h +22 -23
  58. data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
  59. data/vendor/faiss/faiss/IndexReplicas.h +62 -56
  60. data/vendor/faiss/faiss/IndexResidual.cpp +291 -0
  61. data/vendor/faiss/faiss/IndexResidual.h +152 -0
  62. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +120 -155
  63. data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -45
  64. data/vendor/faiss/faiss/IndexShards.cpp +256 -240
  65. data/vendor/faiss/faiss/IndexShards.h +85 -73
  66. data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
  67. data/vendor/faiss/faiss/MatrixStats.h +7 -10
  68. data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
  69. data/vendor/faiss/faiss/MetaIndexes.h +40 -34
  70. data/vendor/faiss/faiss/MetricType.h +7 -7
  71. data/vendor/faiss/faiss/VectorTransform.cpp +652 -474
  72. data/vendor/faiss/faiss/VectorTransform.h +61 -89
  73. data/vendor/faiss/faiss/clone_index.cpp +77 -73
  74. data/vendor/faiss/faiss/clone_index.h +4 -9
  75. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
  76. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
  77. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +197 -170
  78. data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
  79. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
  80. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
  81. data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
  82. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
  83. data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
  84. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
  85. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
  86. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
  87. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
  88. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
  89. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
  90. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
  91. data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
  92. data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
  93. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
  94. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
  95. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
  96. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
  97. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
  98. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
  99. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
  100. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
  101. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
  102. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
  103. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
  104. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
  105. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
  106. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
  107. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
  108. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
  109. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
  110. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
  111. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
  112. data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
  113. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
  114. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
  115. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
  116. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
  117. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
  118. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
  119. data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
  120. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +270 -0
  121. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +115 -0
  122. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
  123. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
  124. data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
  125. data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
  126. data/vendor/faiss/faiss/impl/FaissException.h +41 -29
  127. data/vendor/faiss/faiss/impl/HNSW.cpp +595 -611
  128. data/vendor/faiss/faiss/impl/HNSW.h +179 -200
  129. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +672 -0
  130. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +172 -0
  131. data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
  132. data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
  133. data/vendor/faiss/faiss/impl/NSG.cpp +682 -0
  134. data/vendor/faiss/faiss/impl/NSG.h +199 -0
  135. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
  136. data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
  137. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
  138. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
  139. data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
  140. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +448 -0
  141. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +130 -0
  142. data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
  143. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +648 -701
  144. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
  145. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
  146. data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
  147. data/vendor/faiss/faiss/impl/index_read.cpp +547 -479
  148. data/vendor/faiss/faiss/impl/index_write.cpp +497 -407
  149. data/vendor/faiss/faiss/impl/io.cpp +75 -94
  150. data/vendor/faiss/faiss/impl/io.h +31 -41
  151. data/vendor/faiss/faiss/impl/io_macros.h +40 -29
  152. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
  153. data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
  154. data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
  155. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
  156. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
  157. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
  158. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
  159. data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
  160. data/vendor/faiss/faiss/index_factory.cpp +269 -218
  161. data/vendor/faiss/faiss/index_factory.h +6 -7
  162. data/vendor/faiss/faiss/index_io.h +23 -26
  163. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
  164. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
  165. data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
  166. data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
  167. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
  168. data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
  169. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
  170. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
  171. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
  172. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
  173. data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
  174. data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
  175. data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
  176. data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
  177. data/vendor/faiss/faiss/utils/Heap.h +186 -209
  178. data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
  179. data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
  180. data/vendor/faiss/faiss/utils/distances.cpp +301 -310
  181. data/vendor/faiss/faiss/utils/distances.h +133 -118
  182. data/vendor/faiss/faiss/utils/distances_simd.cpp +456 -516
  183. data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
  184. data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
  185. data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
  186. data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
  187. data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
  188. data/vendor/faiss/faiss/utils/hamming.h +62 -85
  189. data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
  190. data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
  191. data/vendor/faiss/faiss/utils/partitioning.h +26 -21
  192. data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
  193. data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
  194. data/vendor/faiss/faiss/utils/random.cpp +39 -63
  195. data/vendor/faiss/faiss/utils/random.h +13 -16
  196. data/vendor/faiss/faiss/utils/simdlib.h +4 -2
  197. data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
  198. data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
  199. data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
  200. data/vendor/faiss/faiss/utils/utils.cpp +304 -287
  201. data/vendor/faiss/faiss/utils/utils.h +53 -48
  202. metadata +20 -2
@@ -5,20 +5,20 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
-
9
8
  #pragma once
10
9
 
11
- #include <faiss/impl/FaissAssert.h>
12
10
  #include <faiss/Index.h>
11
+ #include <faiss/impl/FaissAssert.h>
13
12
  #include <faiss/invlists/InvertedLists.h>
14
- #include <initializer_list>
15
13
  #include <gtest/gtest.h>
16
14
  #include <cstring>
15
+ #include <initializer_list>
17
16
  #include <memory>
18
17
  #include <string>
19
18
  #include <vector>
20
19
 
21
- namespace faiss { namespace gpu {
20
+ namespace faiss {
21
+ namespace gpu {
22
22
 
23
23
  /// Generates and displays a new seed for the test
24
24
  void newTestSeed();
@@ -40,18 +40,18 @@ bool randBool();
40
40
  /// initializer_list
41
41
  template <typename T>
42
42
  T randSelect(std::initializer_list<T> vals) {
43
- FAISS_ASSERT(vals.size() > 0);
44
- int sel = randVal(0, vals.size());
45
-
46
- int i = 0;
47
- for (auto v : vals) {
48
- if (i++ == sel) {
49
- return v;
43
+ FAISS_ASSERT(vals.size() > 0);
44
+ int sel = randVal(0, vals.size());
45
+
46
+ int i = 0;
47
+ for (auto v : vals) {
48
+ if (i++ == sel) {
49
+ return v;
50
+ }
50
51
  }
51
- }
52
52
 
53
- // should not get here
54
- return *vals.begin();
53
+ // should not get here
54
+ return *vals.begin();
55
55
  }
56
56
 
57
57
  /// Generates a collection of random vectors in the range [0, 1]
@@ -62,65 +62,81 @@ std::vector<unsigned char> randBinaryVecs(size_t num, size_t dim);
62
62
 
63
63
  /// Compare two indices via query for similarity, with a user-specified set of
64
64
  /// query vectors
65
- void compareIndices(const std::vector<float>& queryVecs,
66
- faiss::Index& refIndex,
67
- faiss::Index& testIndex,
68
- int numQuery, int dim, int k,
69
- const std::string& configMsg,
70
- float maxRelativeError = 6e-5f,
71
- float pctMaxDiff1 = 0.1f,
72
- float pctMaxDiffN = 0.005f);
65
+ void compareIndices(
66
+ const std::vector<float>& queryVecs,
67
+ faiss::Index& refIndex,
68
+ faiss::Index& testIndex,
69
+ int numQuery,
70
+ int dim,
71
+ int k,
72
+ const std::string& configMsg,
73
+ float maxRelativeError = 6e-5f,
74
+ float pctMaxDiff1 = 0.1f,
75
+ float pctMaxDiffN = 0.005f);
73
76
 
74
77
  /// Compare two indices via query for similarity, generating random query
75
78
  /// vectors
76
- void compareIndices(faiss::Index& refIndex,
77
- faiss::Index& testIndex,
78
- int numQuery, int dim, int k,
79
- const std::string& configMsg,
80
- float maxRelativeError = 6e-5f,
81
- float pctMaxDiff1 = 0.1f,
82
- float pctMaxDiffN = 0.005f);
79
+ void compareIndices(
80
+ faiss::Index& refIndex,
81
+ faiss::Index& testIndex,
82
+ int numQuery,
83
+ int dim,
84
+ int k,
85
+ const std::string& configMsg,
86
+ float maxRelativeError = 6e-5f,
87
+ float pctMaxDiff1 = 0.1f,
88
+ float pctMaxDiffN = 0.005f);
83
89
 
84
90
  /// Display specific differences in the two (distance, index) lists
85
- void compareLists(const float* refDist,
86
- const faiss::Index::idx_t* refInd,
87
- const float* testDist,
88
- const faiss::Index::idx_t* testInd,
89
- int dim1, int dim2,
90
- const std::string& configMsg,
91
- bool printBasicStats, bool printDiffs, bool assertOnErr,
92
- float maxRelativeError = 6e-5f,
93
- float pctMaxDiff1 = 0.1f,
94
- float pctMaxDiffN = 0.005f);
91
+ void compareLists(
92
+ const float* refDist,
93
+ const faiss::Index::idx_t* refInd,
94
+ const float* testDist,
95
+ const faiss::Index::idx_t* testInd,
96
+ int dim1,
97
+ int dim2,
98
+ const std::string& configMsg,
99
+ bool printBasicStats,
100
+ bool printDiffs,
101
+ bool assertOnErr,
102
+ float maxRelativeError = 6e-5f,
103
+ float pctMaxDiff1 = 0.1f,
104
+ float pctMaxDiffN = 0.005f);
95
105
 
96
106
  /// Compare IVF lists between a CPU and GPU index
97
107
  template <typename A, typename B>
98
108
  void testIVFEquality(A& cpuIndex, B& gpuIndex) {
99
- // Ensure equality of the inverted lists
100
- EXPECT_EQ(cpuIndex.nlist, gpuIndex.nlist);
109
+ // Ensure equality of the inverted lists
110
+ EXPECT_EQ(cpuIndex.nlist, gpuIndex.nlist);
101
111
 
102
- for (int i = 0; i < cpuIndex.nlist; ++i) {
103
- auto cpuLists = cpuIndex.invlists;
112
+ for (int i = 0; i < cpuIndex.nlist; ++i) {
113
+ auto cpuLists = cpuIndex.invlists;
104
114
 
105
- // Code equality
106
- EXPECT_EQ(cpuLists->list_size(i), gpuIndex.getListLength(i));
107
- std::vector<uint8_t> cpuCodes(cpuLists->list_size(i) * cpuLists->code_size);
115
+ // Code equality
116
+ EXPECT_EQ(cpuLists->list_size(i), gpuIndex.getListLength(i));
117
+ std::vector<uint8_t> cpuCodes(
118
+ cpuLists->list_size(i) * cpuLists->code_size);
108
119
 
109
- auto sc = faiss::InvertedLists::ScopedCodes(cpuLists, i);
110
- std::memcpy(cpuCodes.data(), sc.get(),
120
+ auto sc = faiss::InvertedLists::ScopedCodes(cpuLists, i);
121
+ std::memcpy(
122
+ cpuCodes.data(),
123
+ sc.get(),
111
124
  cpuLists->list_size(i) * cpuLists->code_size);
112
125
 
113
- auto gpuCodes = gpuIndex.getListVectorData(i, false);
114
- EXPECT_EQ(cpuCodes, gpuCodes);
126
+ auto gpuCodes = gpuIndex.getListVectorData(i, false);
127
+ EXPECT_EQ(cpuCodes, gpuCodes);
115
128
 
116
- // Index equality
117
- std::vector<Index::idx_t> cpuIndices(cpuLists->list_size(i));
129
+ // Index equality
130
+ std::vector<Index::idx_t> cpuIndices(cpuLists->list_size(i));
118
131
 
119
- auto si = faiss::InvertedLists::ScopedIds(cpuLists, i);
120
- std::memcpy(cpuIndices.data(), si.get(),
132
+ auto si = faiss::InvertedLists::ScopedIds(cpuLists, i);
133
+ std::memcpy(
134
+ cpuIndices.data(),
135
+ si.get(),
121
136
  cpuLists->list_size(i) * sizeof(faiss::Index::idx_t));
122
- EXPECT_EQ(cpuIndices, gpuIndex.getListIndices(i));
123
- }
137
+ EXPECT_EQ(cpuIndices, gpuIndex.getListIndices(i));
138
+ }
124
139
  }
125
140
 
126
- } }
141
+ } // namespace gpu
142
+ } // namespace faiss
@@ -12,24 +12,19 @@
12
12
 
13
13
  #include <sys/time.h>
14
14
 
15
-
16
- #include <faiss/gpu/StandardGpuResources.h>
17
15
  #include <faiss/gpu/GpuIndexIVFPQ.h>
16
+ #include <faiss/gpu/StandardGpuResources.h>
18
17
 
19
18
  #include <faiss/gpu/GpuAutoTune.h>
20
19
  #include <faiss/index_io.h>
21
20
 
22
- double elapsed ()
23
- {
21
+ double elapsed() {
24
22
  struct timeval tv;
25
- gettimeofday (&tv, NULL);
26
- return tv.tv_sec + tv.tv_usec * 1e-6;
23
+ gettimeofday(&tv, NULL);
24
+ return tv.tv_sec + tv.tv_usec * 1e-6;
27
25
  }
28
26
 
29
-
30
- int main ()
31
- {
32
-
27
+ int main() {
33
28
  double t0 = elapsed();
34
29
 
35
30
  // dimension of the vectors to index
@@ -48,47 +43,48 @@ int main ()
48
43
  elapsed() - t0, d, nb, nt, dev_no);
49
44
  */
50
45
  // a reasonable number of centroids to index nb vectors
51
- int ncentroids = int (4 * sqrt (nb));
46
+ int ncentroids = int(4 * sqrt(nb));
52
47
 
53
48
  faiss::gpu::StandardGpuResources resources;
54
49
 
55
-
56
50
  // the coarse quantizer should not be dealloced before the index
57
51
  // 4 = nb of bytes per code (d must be a multiple of this)
58
52
  // 8 = nb of bits per sub-code (almost always 8)
59
53
  faiss::gpu::GpuIndexIVFPQConfig config;
60
54
  config.device = dev_no;
61
55
 
62
- faiss::gpu::GpuIndexIVFPQ index (
63
- &resources, d, ncentroids, 4, 8, faiss::METRIC_L2, config);
56
+ faiss::gpu::GpuIndexIVFPQ index(
57
+ &resources, d, ncentroids, 4, 8, faiss::METRIC_L2, config);
64
58
 
65
59
  std::mt19937 rng;
66
60
 
67
61
  { // training
68
- printf ("[%.3f s] Generating %ld vectors in %dD for training\n",
69
- elapsed() - t0, nt, d);
62
+ printf("[%.3f s] Generating %ld vectors in %dD for training\n",
63
+ elapsed() - t0,
64
+ nt,
65
+ d);
70
66
 
71
- std::vector <float> trainvecs (nt * d);
67
+ std::vector<float> trainvecs(nt * d);
72
68
  std::uniform_real_distribution<> distrib;
73
69
  for (size_t i = 0; i < nt * d; i++) {
74
70
  trainvecs[i] = distrib(rng);
75
71
  }
76
72
 
77
- printf ("[%.3f s] Training the index\n",
78
- elapsed() - t0);
73
+ printf("[%.3f s] Training the index\n", elapsed() - t0);
79
74
  index.verbose = true;
80
75
 
81
- index.train (nt, trainvecs.data());
76
+ index.train(nt, trainvecs.data());
82
77
  }
83
78
 
84
79
  { // I/O demo
85
- const char *outfilename = "/tmp/index_trained.faissindex";
86
- printf ("[%.3f s] storing the pre-trained index to %s\n",
87
- elapsed() - t0, outfilename);
80
+ const char* outfilename = "/tmp/index_trained.faissindex";
81
+ printf("[%.3f s] storing the pre-trained index to %s\n",
82
+ elapsed() - t0,
83
+ outfilename);
88
84
 
89
- faiss::Index * cpu_index = faiss::gpu::index_gpu_to_cpu (&index);
85
+ faiss::Index* cpu_index = faiss::gpu::index_gpu_to_cpu(&index);
90
86
 
91
- write_index (cpu_index, outfilename);
87
+ write_index(cpu_index, outfilename);
92
88
 
93
89
  delete cpu_index;
94
90
  }
@@ -97,64 +93,65 @@ int main ()
97
93
  std::vector<float> queries;
98
94
 
99
95
  { // populating the database
100
- printf ("[%.3f s] Building a dataset of %ld vectors to index\n",
101
- elapsed() - t0, nb);
96
+ printf("[%.3f s] Building a dataset of %ld vectors to index\n",
97
+ elapsed() - t0,
98
+ nb);
102
99
 
103
- std::vector <float> database (nb * d);
100
+ std::vector<float> database(nb * d);
104
101
  std::uniform_real_distribution<> distrib;
105
102
  for (size_t i = 0; i < nb * d; i++) {
106
103
  database[i] = distrib(rng);
107
104
  }
108
105
 
109
- printf ("[%.3f s] Adding the vectors to the index\n",
110
- elapsed() - t0);
106
+ printf("[%.3f s] Adding the vectors to the index\n", elapsed() - t0);
111
107
 
112
- index.add (nb, database.data());
108
+ index.add(nb, database.data());
113
109
 
114
- printf ("[%.3f s] done\n", elapsed() - t0);
110
+ printf("[%.3f s] done\n", elapsed() - t0);
115
111
 
116
112
  // remember a few elements from the database as queries
117
113
  int i0 = 1234;
118
114
  int i1 = 1243;
119
115
 
120
116
  nq = i1 - i0;
121
- queries.resize (nq * d);
117
+ queries.resize(nq * d);
122
118
  for (int i = i0; i < i1; i++) {
123
119
  for (int j = 0; j < d; j++) {
124
- queries [(i - i0) * d + j] = database [i * d + j];
120
+ queries[(i - i0) * d + j] = database[i * d + j];
125
121
  }
126
122
  }
127
-
128
123
  }
129
124
 
130
125
  { // searching the database
131
126
  int k = 5;
132
- printf ("[%.3f s] Searching the %d nearest neighbors "
133
- "of %ld vectors in the index\n",
134
- elapsed() - t0, k, nq);
127
+ printf("[%.3f s] Searching the %d nearest neighbors "
128
+ "of %ld vectors in the index\n",
129
+ elapsed() - t0,
130
+ k,
131
+ nq);
135
132
 
136
- std::vector<faiss::Index::idx_t> nns (k * nq);
137
- std::vector<float> dis (k * nq);
133
+ std::vector<faiss::Index::idx_t> nns(k * nq);
134
+ std::vector<float> dis(k * nq);
138
135
 
139
- index.search (nq, queries.data(), k, dis.data(), nns.data());
136
+ index.search(nq, queries.data(), k, dis.data(), nns.data());
140
137
 
141
- printf ("[%.3f s] Query results (vector ids, then distances):\n",
142
- elapsed() - t0);
138
+ printf("[%.3f s] Query results (vector ids, then distances):\n",
139
+ elapsed() - t0);
143
140
 
144
141
  for (int i = 0; i < nq; i++) {
145
- printf ("query %2d: ", i);
142
+ printf("query %2d: ", i);
146
143
  for (int j = 0; j < k; j++) {
147
- printf ("%7ld ", nns[j + i * k]);
144
+ printf("%7ld ", nns[j + i * k]);
148
145
  }
149
- printf ("\n dis: ");
146
+ printf("\n dis: ");
150
147
  for (int j = 0; j < k; j++) {
151
- printf ("%7g ", dis[j + i * k]);
148
+ printf("%7g ", dis[j + i * k]);
152
149
  }
153
- printf ("\n");
150
+ printf("\n");
154
151
  }
155
152
 
156
- printf ("note that the nearest neighbor is not at "
157
- "distance 0 due to quantization errors\n");
153
+ printf("note that the nearest neighbor is not at "
154
+ "distance 0 due to quantization errors\n");
158
155
  }
159
156
 
160
157
  return 0;
@@ -5,15 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
-
9
8
  #pragma once
10
9
 
11
- #include <faiss/impl/FaissAssert.h>
12
- #include <cuda_runtime.h>
13
10
  #include <cublas_v2.h>
11
+ #include <cuda_runtime.h>
12
+ #include <faiss/impl/FaissAssert.h>
14
13
  #include <vector>
15
14
 
16
- namespace faiss { namespace gpu {
15
+ namespace faiss {
16
+ namespace gpu {
17
17
 
18
18
  /// Returns the current thread-local GPU device
19
19
  int getCurrentDevice();
@@ -78,114 +78,121 @@ int getMaxKSelection();
78
78
  /// RAII object to set the current device, and restore the previous
79
79
  /// device upon destruction
80
80
  class DeviceScope {
81
- public:
82
- explicit DeviceScope(int device);
83
- ~DeviceScope();
81
+ public:
82
+ explicit DeviceScope(int device);
83
+ ~DeviceScope();
84
84
 
85
- private:
86
- int prevDevice_;
85
+ private:
86
+ int prevDevice_;
87
87
  };
88
88
 
89
89
  /// RAII object to manage a cublasHandle_t
90
90
  class CublasHandleScope {
91
- public:
92
- CublasHandleScope();
93
- ~CublasHandleScope();
91
+ public:
92
+ CublasHandleScope();
93
+ ~CublasHandleScope();
94
94
 
95
- cublasHandle_t get() { return blasHandle_; }
95
+ cublasHandle_t get() {
96
+ return blasHandle_;
97
+ }
96
98
 
97
- private:
98
- cublasHandle_t blasHandle_;
99
+ private:
100
+ cublasHandle_t blasHandle_;
99
101
  };
100
102
 
101
103
  // RAII object to manage a cudaEvent_t
102
104
  class CudaEvent {
103
- public:
104
- /// Creates an event and records it in this stream
105
- explicit CudaEvent(cudaStream_t stream, bool timer = false);
106
- CudaEvent(const CudaEvent& event) = delete;
107
- CudaEvent(CudaEvent&& event) noexcept;
108
- ~CudaEvent();
109
-
110
- inline cudaEvent_t get() { return event_; }
105
+ public:
106
+ /// Creates an event and records it in this stream
107
+ explicit CudaEvent(cudaStream_t stream, bool timer = false);
108
+ CudaEvent(const CudaEvent& event) = delete;
109
+ CudaEvent(CudaEvent&& event) noexcept;
110
+ ~CudaEvent();
111
+
112
+ inline cudaEvent_t get() {
113
+ return event_;
114
+ }
111
115
 
112
- /// Wait on this event in this stream
113
- void streamWaitOnEvent(cudaStream_t stream);
116
+ /// Wait on this event in this stream
117
+ void streamWaitOnEvent(cudaStream_t stream);
114
118
 
115
- /// Have the CPU wait for the completion of this event
116
- void cpuWaitOnEvent();
119
+ /// Have the CPU wait for the completion of this event
120
+ void cpuWaitOnEvent();
117
121
 
118
- CudaEvent& operator=(CudaEvent&& event) noexcept;
119
- CudaEvent& operator=(CudaEvent& event) = delete;
122
+ CudaEvent& operator=(CudaEvent&& event) noexcept;
123
+ CudaEvent& operator=(CudaEvent& event) = delete;
120
124
 
121
- private:
122
- cudaEvent_t event_;
125
+ private:
126
+ cudaEvent_t event_;
123
127
  };
124
128
 
125
129
  /// Wrapper to test return status of CUDA functions
126
- #define CUDA_VERIFY(X) \
127
- do { \
128
- auto err__ = (X); \
129
- FAISS_ASSERT_FMT(err__ == cudaSuccess, "CUDA error %d %s", \
130
- (int) err__, cudaGetErrorString(err__)); \
131
- } while (0)
130
+ #define CUDA_VERIFY(X) \
131
+ do { \
132
+ auto err__ = (X); \
133
+ FAISS_ASSERT_FMT( \
134
+ err__ == cudaSuccess, \
135
+ "CUDA error %d %s", \
136
+ (int)err__, \
137
+ cudaGetErrorString(err__)); \
138
+ } while (0)
132
139
 
133
140
  /// Wrapper to synchronously probe for CUDA errors
134
141
  // #define FAISS_GPU_SYNC_ERROR 1
135
142
 
136
143
  #ifdef FAISS_GPU_SYNC_ERROR
137
- #define CUDA_TEST_ERROR() \
138
- do { \
139
- CUDA_VERIFY(cudaDeviceSynchronize()); \
140
- } while (0)
144
+ #define CUDA_TEST_ERROR() \
145
+ do { \
146
+ CUDA_VERIFY(cudaDeviceSynchronize()); \
147
+ } while (0)
141
148
  #else
142
- #define CUDA_TEST_ERROR() \
143
- do { \
144
- CUDA_VERIFY(cudaGetLastError()); \
145
- } while (0)
149
+ #define CUDA_TEST_ERROR() \
150
+ do { \
151
+ CUDA_VERIFY(cudaGetLastError()); \
152
+ } while (0)
146
153
  #endif
147
154
 
148
155
  /// Call for a collection of streams to wait on
149
156
  template <typename L1, typename L2>
150
157
  void streamWaitBase(const L1& listWaiting, const L2& listWaitOn) {
151
- // For all the streams we are waiting on, create an event
152
- std::vector<cudaEvent_t> events;
153
- for (auto& stream : listWaitOn) {
154
- cudaEvent_t event;
155
- CUDA_VERIFY(cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
156
- CUDA_VERIFY(cudaEventRecord(event, stream));
157
- events.push_back(event);
158
- }
159
-
160
- // For all the streams that are waiting, issue a wait
161
- for (auto& stream : listWaiting) {
162
- for (auto& event : events) {
163
- CUDA_VERIFY(cudaStreamWaitEvent(stream, event, 0));
158
+ // For all the streams we are waiting on, create an event
159
+ std::vector<cudaEvent_t> events;
160
+ for (auto& stream : listWaitOn) {
161
+ cudaEvent_t event;
162
+ CUDA_VERIFY(cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
163
+ CUDA_VERIFY(cudaEventRecord(event, stream));
164
+ events.push_back(event);
165
+ }
166
+
167
+ // For all the streams that are waiting, issue a wait
168
+ for (auto& stream : listWaiting) {
169
+ for (auto& event : events) {
170
+ CUDA_VERIFY(cudaStreamWaitEvent(stream, event, 0));
171
+ }
164
172
  }
165
- }
166
173
 
167
- for (auto& event : events) {
168
- CUDA_VERIFY(cudaEventDestroy(event));
169
- }
174
+ for (auto& event : events) {
175
+ CUDA_VERIFY(cudaEventDestroy(event));
176
+ }
170
177
  }
171
178
 
172
179
  /// These versions allow usage of initializer_list as arguments, since
173
180
  /// otherwise {...} doesn't have a type
174
181
  template <typename L1>
175
- void streamWait(const L1& a,
176
- const std::initializer_list<cudaStream_t>& b) {
177
- streamWaitBase(a, b);
182
+ void streamWait(const L1& a, const std::initializer_list<cudaStream_t>& b) {
183
+ streamWaitBase(a, b);
178
184
  }
179
185
 
180
186
  template <typename L2>
181
- void streamWait(const std::initializer_list<cudaStream_t>& a,
182
- const L2& b) {
183
- streamWaitBase(a, b);
187
+ void streamWait(const std::initializer_list<cudaStream_t>& a, const L2& b) {
188
+ streamWaitBase(a, b);
184
189
  }
185
190
 
186
- inline void streamWait(const std::initializer_list<cudaStream_t>& a,
187
- const std::initializer_list<cudaStream_t>& b) {
188
- streamWaitBase(a, b);
191
+ inline void streamWait(
192
+ const std::initializer_list<cudaStream_t>& a,
193
+ const std::initializer_list<cudaStream_t>& b) {
194
+ streamWaitBase(a, b);
189
195
  }
190
196
 
191
- } } // namespace
197
+ } // namespace gpu
198
+ } // namespace faiss