faiss 0.3.0 → 0.3.2

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 (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ext/faiss/extconf.rb +9 -2
  6. data/ext/faiss/index.cpp +1 -1
  7. data/ext/faiss/index_binary.cpp +2 -2
  8. data/ext/faiss/product_quantizer.cpp +1 -1
  9. data/lib/faiss/version.rb +1 -1
  10. data/vendor/faiss/faiss/AutoTune.cpp +7 -7
  11. data/vendor/faiss/faiss/AutoTune.h +1 -2
  12. data/vendor/faiss/faiss/Clustering.cpp +39 -22
  13. data/vendor/faiss/faiss/Clustering.h +40 -21
  14. data/vendor/faiss/faiss/IVFlib.cpp +26 -12
  15. data/vendor/faiss/faiss/Index.cpp +1 -1
  16. data/vendor/faiss/faiss/Index.h +40 -10
  17. data/vendor/faiss/faiss/Index2Layer.cpp +7 -7
  18. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +176 -166
  19. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +15 -15
  20. data/vendor/faiss/faiss/IndexBinary.cpp +9 -4
  21. data/vendor/faiss/faiss/IndexBinary.h +8 -19
  22. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -1
  23. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +24 -31
  24. data/vendor/faiss/faiss/IndexBinaryHNSW.h +1 -1
  25. data/vendor/faiss/faiss/IndexBinaryHash.cpp +25 -50
  26. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +107 -188
  27. data/vendor/faiss/faiss/IndexFastScan.cpp +95 -146
  28. data/vendor/faiss/faiss/IndexFastScan.h +9 -8
  29. data/vendor/faiss/faiss/IndexFlat.cpp +206 -10
  30. data/vendor/faiss/faiss/IndexFlat.h +20 -1
  31. data/vendor/faiss/faiss/IndexFlatCodes.cpp +170 -5
  32. data/vendor/faiss/faiss/IndexFlatCodes.h +23 -4
  33. data/vendor/faiss/faiss/IndexHNSW.cpp +231 -382
  34. data/vendor/faiss/faiss/IndexHNSW.h +62 -49
  35. data/vendor/faiss/faiss/IndexIDMap.cpp +69 -28
  36. data/vendor/faiss/faiss/IndexIDMap.h +24 -2
  37. data/vendor/faiss/faiss/IndexIVF.cpp +162 -56
  38. data/vendor/faiss/faiss/IndexIVF.h +46 -6
  39. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +33 -26
  40. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +6 -2
  41. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +19 -46
  42. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +4 -3
  43. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +502 -401
  44. data/vendor/faiss/faiss/IndexIVFFastScan.h +63 -26
  45. data/vendor/faiss/faiss/IndexIVFFlat.cpp +15 -5
  46. data/vendor/faiss/faiss/IndexIVFFlat.h +3 -2
  47. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +172 -0
  48. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +56 -0
  49. data/vendor/faiss/faiss/IndexIVFPQ.cpp +79 -125
  50. data/vendor/faiss/faiss/IndexIVFPQ.h +6 -7
  51. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +39 -52
  52. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +4 -3
  53. data/vendor/faiss/faiss/IndexIVFPQR.cpp +45 -29
  54. data/vendor/faiss/faiss/IndexIVFPQR.h +5 -2
  55. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +25 -27
  56. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +6 -6
  57. data/vendor/faiss/faiss/IndexLSH.cpp +14 -16
  58. data/vendor/faiss/faiss/IndexLattice.cpp +1 -19
  59. data/vendor/faiss/faiss/IndexLattice.h +3 -22
  60. data/vendor/faiss/faiss/IndexNNDescent.cpp +3 -33
  61. data/vendor/faiss/faiss/IndexNNDescent.h +1 -1
  62. data/vendor/faiss/faiss/IndexNSG.cpp +11 -27
  63. data/vendor/faiss/faiss/IndexNSG.h +11 -11
  64. data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +56 -0
  65. data/vendor/faiss/faiss/IndexNeuralNetCodec.h +49 -0
  66. data/vendor/faiss/faiss/IndexPQ.cpp +72 -88
  67. data/vendor/faiss/faiss/IndexPQ.h +1 -4
  68. data/vendor/faiss/faiss/IndexPQFastScan.cpp +1 -1
  69. data/vendor/faiss/faiss/IndexPreTransform.cpp +25 -31
  70. data/vendor/faiss/faiss/IndexPreTransform.h +1 -1
  71. data/vendor/faiss/faiss/IndexRefine.cpp +54 -24
  72. data/vendor/faiss/faiss/IndexRefine.h +7 -0
  73. data/vendor/faiss/faiss/IndexReplicas.cpp +23 -26
  74. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +25 -17
  75. data/vendor/faiss/faiss/IndexScalarQuantizer.h +6 -4
  76. data/vendor/faiss/faiss/IndexShards.cpp +21 -29
  77. data/vendor/faiss/faiss/IndexShardsIVF.cpp +1 -2
  78. data/vendor/faiss/faiss/MatrixStats.cpp +17 -32
  79. data/vendor/faiss/faiss/MatrixStats.h +21 -9
  80. data/vendor/faiss/faiss/MetaIndexes.cpp +35 -35
  81. data/vendor/faiss/faiss/MetricType.h +7 -2
  82. data/vendor/faiss/faiss/VectorTransform.cpp +13 -26
  83. data/vendor/faiss/faiss/VectorTransform.h +7 -7
  84. data/vendor/faiss/faiss/clone_index.cpp +15 -10
  85. data/vendor/faiss/faiss/clone_index.h +3 -0
  86. data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +95 -17
  87. data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +152 -0
  88. data/vendor/faiss/faiss/cppcontrib/factory_tools.h +24 -0
  89. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +83 -30
  90. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +123 -8
  91. data/vendor/faiss/faiss/gpu/GpuCloner.h +22 -0
  92. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +13 -0
  93. data/vendor/faiss/faiss/gpu/GpuDistance.h +46 -38
  94. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -1
  95. data/vendor/faiss/faiss/gpu/GpuIndex.h +30 -12
  96. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +282 -0
  97. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +4 -4
  98. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +14 -9
  99. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +20 -3
  100. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +22 -11
  101. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +1 -3
  102. data/vendor/faiss/faiss/gpu/GpuResources.cpp +24 -3
  103. data/vendor/faiss/faiss/gpu/GpuResources.h +39 -11
  104. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +142 -17
  105. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +57 -3
  106. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +26 -21
  107. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +7 -1
  108. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +8 -5
  109. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +25 -0
  110. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +129 -9
  111. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +332 -40
  112. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +299 -208
  113. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +1 -0
  114. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +1 -1
  115. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +6 -0
  116. data/vendor/faiss/faiss/gpu/utils/RaftUtils.h +75 -0
  117. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +4 -1
  118. data/vendor/faiss/faiss/gpu/utils/Timer.h +1 -1
  119. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +3 -1
  120. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +5 -5
  121. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +26 -1
  122. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +10 -3
  123. data/vendor/faiss/faiss/impl/DistanceComputer.h +70 -1
  124. data/vendor/faiss/faiss/impl/FaissAssert.h +4 -2
  125. data/vendor/faiss/faiss/impl/FaissException.h +13 -34
  126. data/vendor/faiss/faiss/impl/HNSW.cpp +605 -186
  127. data/vendor/faiss/faiss/impl/HNSW.h +52 -30
  128. data/vendor/faiss/faiss/impl/IDSelector.h +4 -4
  129. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +11 -9
  130. data/vendor/faiss/faiss/impl/LookupTableScaler.h +34 -0
  131. data/vendor/faiss/faiss/impl/NNDescent.cpp +42 -27
  132. data/vendor/faiss/faiss/impl/NSG.cpp +0 -29
  133. data/vendor/faiss/faiss/impl/NSG.h +1 -1
  134. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +14 -12
  135. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +1 -1
  136. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +25 -22
  137. data/vendor/faiss/faiss/impl/ProductQuantizer.h +6 -2
  138. data/vendor/faiss/faiss/impl/Quantizer.h +1 -1
  139. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +27 -1015
  140. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +5 -63
  141. data/vendor/faiss/faiss/impl/ResultHandler.h +347 -172
  142. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +1104 -147
  143. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +3 -8
  144. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +285 -42
  145. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +248 -0
  146. data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +21 -14
  147. data/vendor/faiss/faiss/impl/code_distance/code_distance.h +22 -12
  148. data/vendor/faiss/faiss/impl/index_read.cpp +74 -34
  149. data/vendor/faiss/faiss/impl/index_read_utils.h +37 -0
  150. data/vendor/faiss/faiss/impl/index_write.cpp +88 -51
  151. data/vendor/faiss/faiss/impl/io.cpp +23 -15
  152. data/vendor/faiss/faiss/impl/io.h +4 -4
  153. data/vendor/faiss/faiss/impl/io_macros.h +6 -0
  154. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -1
  155. data/vendor/faiss/faiss/impl/platform_macros.h +40 -1
  156. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +14 -0
  157. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +7 -6
  158. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +52 -38
  159. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +487 -49
  160. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +960 -0
  161. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +176 -0
  162. data/vendor/faiss/faiss/impl/simd_result_handlers.h +481 -225
  163. data/vendor/faiss/faiss/index_factory.cpp +41 -20
  164. data/vendor/faiss/faiss/index_io.h +12 -5
  165. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +28 -8
  166. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +3 -0
  167. data/vendor/faiss/faiss/invlists/DirectMap.cpp +10 -2
  168. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +73 -17
  169. data/vendor/faiss/faiss/invlists/InvertedLists.h +26 -8
  170. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +24 -9
  171. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +2 -1
  172. data/vendor/faiss/faiss/python/python_callbacks.cpp +4 -4
  173. data/vendor/faiss/faiss/utils/Heap.cpp +3 -1
  174. data/vendor/faiss/faiss/utils/Heap.h +105 -0
  175. data/vendor/faiss/faiss/utils/NeuralNet.cpp +342 -0
  176. data/vendor/faiss/faiss/utils/NeuralNet.h +147 -0
  177. data/vendor/faiss/faiss/utils/WorkerThread.h +1 -0
  178. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  179. data/vendor/faiss/faiss/utils/distances.cpp +147 -123
  180. data/vendor/faiss/faiss/utils/distances.h +86 -9
  181. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +5 -5
  182. data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
  183. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
  184. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +1 -1
  185. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +5 -5
  186. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +1 -1
  187. data/vendor/faiss/faiss/utils/distances_simd.cpp +1589 -243
  188. data/vendor/faiss/faiss/utils/extra_distances-inl.h +70 -0
  189. data/vendor/faiss/faiss/utils/extra_distances.cpp +85 -137
  190. data/vendor/faiss/faiss/utils/extra_distances.h +3 -2
  191. data/vendor/faiss/faiss/utils/fp16-arm.h +29 -0
  192. data/vendor/faiss/faiss/utils/fp16.h +2 -0
  193. data/vendor/faiss/faiss/utils/hamming.cpp +163 -111
  194. data/vendor/faiss/faiss/utils/hamming.h +58 -0
  195. data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +16 -89
  196. data/vendor/faiss/faiss/utils/hamming_distance/common.h +1 -0
  197. data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +19 -88
  198. data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +58 -0
  199. data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +14 -104
  200. data/vendor/faiss/faiss/utils/partitioning.cpp +3 -4
  201. data/vendor/faiss/faiss/utils/prefetch.h +77 -0
  202. data/vendor/faiss/faiss/utils/quantize_lut.cpp +0 -14
  203. data/vendor/faiss/faiss/utils/random.cpp +43 -0
  204. data/vendor/faiss/faiss/utils/random.h +25 -0
  205. data/vendor/faiss/faiss/utils/simdlib.h +10 -1
  206. data/vendor/faiss/faiss/utils/simdlib_avx2.h +0 -6
  207. data/vendor/faiss/faiss/utils/simdlib_avx512.h +296 -0
  208. data/vendor/faiss/faiss/utils/simdlib_neon.h +77 -79
  209. data/vendor/faiss/faiss/utils/simdlib_ppc64.h +1084 -0
  210. data/vendor/faiss/faiss/utils/sorting.cpp +140 -5
  211. data/vendor/faiss/faiss/utils/sorting.h +27 -0
  212. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +176 -0
  213. data/vendor/faiss/faiss/utils/utils.cpp +120 -7
  214. data/vendor/faiss/faiss/utils/utils.h +60 -20
  215. metadata +23 -4
  216. data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +0 -102
@@ -20,11 +20,11 @@ namespace faiss {
20
20
  * IO functions
21
21
  ***********************************************************************/
22
22
 
23
- int IOReader::fileno() {
23
+ int IOReader::filedescriptor() {
24
24
  FAISS_THROW_MSG("IOReader does not support memory mapping");
25
25
  }
26
26
 
27
- int IOWriter::fileno() {
27
+ int IOWriter::filedescriptor() {
28
28
  FAISS_THROW_MSG("IOWriter does not support memory mapping");
29
29
  }
30
30
 
@@ -85,8 +85,12 @@ size_t FileIOReader::operator()(void* ptr, size_t size, size_t nitems) {
85
85
  return fread(ptr, size, nitems, f);
86
86
  }
87
87
 
88
- int FileIOReader::fileno() {
88
+ int FileIOReader::filedescriptor() {
89
+ #ifdef _AIX
90
+ return fileno(f);
91
+ #else
89
92
  return ::fileno(f);
93
+ #endif
90
94
  }
91
95
 
92
96
  FileIOWriter::FileIOWriter(FILE* wf) : f(wf) {}
@@ -116,8 +120,12 @@ size_t FileIOWriter::operator()(const void* ptr, size_t size, size_t nitems) {
116
120
  return fwrite(ptr, size, nitems, f);
117
121
  }
118
122
 
119
- int FileIOWriter::fileno() {
123
+ int FileIOWriter::filedescriptor() {
124
+ #ifdef _AIX
125
+ return fileno(f);
126
+ #else
120
127
  return ::fileno(f);
128
+ #endif
121
129
  }
122
130
 
123
131
  /***********************************************************************
@@ -196,13 +204,13 @@ size_t BufferedIOWriter::operator()(
196
204
  while (size > 0) {
197
205
  assert(b0 == bsz);
198
206
  // now we need to flush to add more bytes
199
- size_t ofs = 0;
207
+ size_t ofs_2 = 0;
200
208
  do {
201
- assert(ofs < 10000000);
202
- size_t written = (*writer)(buffer.data() + ofs, 1, bsz - ofs);
209
+ assert(ofs_2 < 10000000);
210
+ size_t written = (*writer)(buffer.data() + ofs_2, 1, bsz - ofs_2);
203
211
  FAISS_THROW_IF_NOT(written > 0);
204
- ofs += written;
205
- } while (ofs != bsz);
212
+ ofs_2 += written;
213
+ } while (ofs_2 != bsz);
206
214
 
207
215
  // copy src to buffer
208
216
  size_t nb1 = std::min(bsz, size);
@@ -217,12 +225,12 @@ size_t BufferedIOWriter::operator()(
217
225
  }
218
226
 
219
227
  BufferedIOWriter::~BufferedIOWriter() {
220
- size_t ofs = 0;
221
- while (ofs != b0) {
222
- // printf("Destructor write %zd \n", b0 - ofs);
223
- size_t written = (*writer)(buffer.data() + ofs, 1, b0 - ofs);
228
+ size_t ofs_2 = 0;
229
+ while (ofs_2 != b0) {
230
+ // printf("Destructor write %zd \n", b0 - ofs_2);
231
+ size_t written = (*writer)(buffer.data() + ofs_2, 1, b0 - ofs_2);
224
232
  FAISS_THROW_IF_NOT(written > 0);
225
- ofs += written;
233
+ ofs_2 += written;
226
234
  }
227
235
  }
228
236
 
@@ -259,7 +267,7 @@ std::string fourcc_inv_printable(uint32_t x) {
259
267
  str += c;
260
268
  } else {
261
269
  char buf[10];
262
- sprintf(buf, "\\x%02x", c);
270
+ snprintf(buf, sizeof(buf), "\\x%02x", c);
263
271
  str += buf;
264
272
  }
265
273
  }
@@ -32,7 +32,7 @@ struct IOReader {
32
32
  virtual size_t operator()(void* ptr, size_t size, size_t nitems) = 0;
33
33
 
34
34
  // return a file number that can be memory-mapped
35
- virtual int fileno();
35
+ virtual int filedescriptor();
36
36
 
37
37
  virtual ~IOReader() {}
38
38
  };
@@ -45,7 +45,7 @@ struct IOWriter {
45
45
  virtual size_t operator()(const void* ptr, size_t size, size_t nitems) = 0;
46
46
 
47
47
  // return a file number that can be memory-mapped
48
- virtual int fileno();
48
+ virtual int filedescriptor();
49
49
 
50
50
  virtual ~IOWriter() noexcept(false) {}
51
51
  };
@@ -73,7 +73,7 @@ struct FileIOReader : IOReader {
73
73
 
74
74
  size_t operator()(void* ptr, size_t size, size_t nitems) override;
75
75
 
76
- int fileno() override;
76
+ int filedescriptor() override;
77
77
  };
78
78
 
79
79
  struct FileIOWriter : IOWriter {
@@ -88,7 +88,7 @@ struct FileIOWriter : IOWriter {
88
88
 
89
89
  size_t operator()(const void* ptr, size_t size, size_t nitems) override;
90
90
 
91
- int fileno() override;
91
+ int filedescriptor() override;
92
92
  };
93
93
 
94
94
  /*******************************************************
@@ -29,6 +29,12 @@
29
29
 
30
30
  #define READ1(x) READANDCHECK(&(x), 1)
31
31
 
32
+ #define READ1_DUMMY(x_type) \
33
+ { \
34
+ x_type x = {}; \
35
+ READ1(x); \
36
+ }
37
+
32
38
  // will fail if we write 256G of data at once...
33
39
  #define READVECTOR(vec) \
34
40
  { \
@@ -455,7 +455,7 @@ void ZnSphereCodec::decode(uint64_t code, float* c) const {
455
455
  int nnz = 0;
456
456
  for (int i = 0; i < dim; i++) {
457
457
  if (c[i] != 0) {
458
- if (signs & (1UL << nnz)) {
458
+ if (signs & (uint64_t(1) << nnz)) {
459
459
  c[i] = -c[i];
460
460
  }
461
461
  nnz++;
@@ -40,11 +40,13 @@
40
40
 
41
41
  #include <intrin.h>
42
42
 
43
+ #ifndef __clang__
43
44
  inline int __builtin_ctzll(uint64_t x) {
44
45
  unsigned long ret;
45
46
  _BitScanForward64(&ret, x);
46
47
  return (int)ret;
47
48
  }
49
+ #endif
48
50
 
49
51
  // cudatoolkit provides __builtin_ctz for NVCC >= 11.0
50
52
  #if !defined(__CUDACC__) || __CUDACC_VER_MAJOR__ < 11
@@ -55,13 +57,20 @@ inline int __builtin_ctz(unsigned long x) {
55
57
  }
56
58
  #endif
57
59
 
60
+ #ifndef __clang__
58
61
  inline int __builtin_clzll(uint64_t x) {
59
62
  return (int)__lzcnt64(x);
60
63
  }
64
+ #endif
61
65
 
62
66
  #define __builtin_popcount __popcnt
63
67
  #define __builtin_popcountl __popcnt64
64
68
 
69
+ #ifndef __clang__
70
+ #define __m128i_u __m128i
71
+ #define __m256i_u __m256i
72
+ #endif
73
+
65
74
  // MSVC does not define __SSEx__, and _M_IX86_FP is only defined on 32-bit
66
75
  // processors cf.
67
76
  // https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
@@ -82,6 +91,8 @@ inline int __builtin_clzll(uint64_t x) {
82
91
  #define __F16C__ 1
83
92
  #endif
84
93
 
94
+ #define FAISS_ALWAYS_INLINE __forceinline
95
+
85
96
  #else
86
97
  /*******************************************************
87
98
  * Linux and OSX
@@ -92,9 +103,15 @@ inline int __builtin_clzll(uint64_t x) {
92
103
 
93
104
  // aligned should be *in front* of the declaration, for compatibility with
94
105
  // windows
106
+ #ifdef SWIG
107
+ #define ALIGNED(x)
108
+ #else
95
109
  #define ALIGNED(x) __attribute__((aligned(x)))
110
+ #endif
96
111
 
97
- #endif // _MSC_VER
112
+ #define FAISS_ALWAYS_INLINE __attribute__((always_inline)) inline
113
+
114
+ #endif
98
115
 
99
116
  #if defined(__GNUC__) || defined(__clang__)
100
117
  #define FAISS_DEPRECATED(msg) __attribute__((deprecated(msg)))
@@ -110,6 +127,13 @@ inline int __builtin_clzll(uint64_t x) {
110
127
  __pragma(float_control(precise, off, push))
111
128
  #define FAISS_PRAGMA_IMPRECISE_FUNCTION_END __pragma(float_control(pop))
112
129
  #elif defined(__clang__)
130
+ #if defined(__PPC__)
131
+ #define FAISS_PRAGMA_IMPRECISE_LOOP \
132
+ _Pragma("clang loop vectorize_width(4) interleave_count(8)")
133
+ #define FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN \
134
+ _Pragma("float_control(precise, off, push)")
135
+ #define FAISS_PRAGMA_IMPRECISE_FUNCTION_END _Pragma("float_control(pop)")
136
+ #else
113
137
  #define FAISS_PRAGMA_IMPRECISE_LOOP \
114
138
  _Pragma("clang loop vectorize(enable) interleave(enable)")
115
139
 
@@ -127,6 +151,7 @@ inline int __builtin_clzll(uint64_t x) {
127
151
  #define FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
128
152
  #define FAISS_PRAGMA_IMPRECISE_FUNCTION_END
129
153
  #endif
154
+ #endif
130
155
  #elif defined(__GNUC__)
131
156
  // Unfortunately, GCC does not provide a pragma for detecting it.
132
157
  // So, we have to stick to GNUC, which is defined by MANY compilers.
@@ -148,3 +173,17 @@ inline int __builtin_clzll(uint64_t x) {
148
173
  #endif
149
174
 
150
175
  // clang-format on
176
+
177
+ /*******************************************************
178
+ * BIGENDIAN specific macros
179
+ *******************************************************/
180
+ #if !defined(_MSC_VER) && \
181
+ (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
182
+ #define FAISS_BIG_ENDIAN
183
+ #endif
184
+
185
+ #define Swap2Bytes(val) ((((val) >> 8) & 0x00FF) | (((val) << 8) & 0xFF00))
186
+
187
+ #define Swap4Bytes(val) \
188
+ ((((val) >> 24) & 0x000000FF) | (((val) >> 8) & 0x0000FF00) | \
189
+ (((val) << 8) & 0x00FF0000) | (((val) << 24) & 0xFF000000))
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  #include <faiss/impl/FaissAssert.h>
9
+ #include <faiss/impl/platform_macros.h>
9
10
  #include <faiss/impl/pq4_fast_scan.h>
10
11
  #include <faiss/impl/simd_result_handlers.h>
11
12
 
@@ -54,9 +55,17 @@ void pq4_pack_codes(
54
55
  FAISS_THROW_IF_NOT(nb % bbs == 0);
55
56
  FAISS_THROW_IF_NOT(nsq % 2 == 0);
56
57
 
58
+ if (nb == 0) {
59
+ return;
60
+ }
57
61
  memset(blocks, 0, nb * nsq / 2);
62
+ #ifdef FAISS_BIG_ENDIAN
63
+ const uint8_t perm0[16] = {
64
+ 8, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7};
65
+ #else
58
66
  const uint8_t perm0[16] = {
59
67
  0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15};
68
+ #endif
60
69
 
61
70
  uint8_t* codes2 = blocks;
62
71
  for (size_t i0 = 0; i0 < nb; i0 += bbs) {
@@ -90,8 +99,13 @@ void pq4_pack_codes_range(
90
99
  size_t bbs,
91
100
  size_t nsq,
92
101
  uint8_t* blocks) {
102
+ #ifdef FAISS_BIG_ENDIAN
103
+ const uint8_t perm0[16] = {
104
+ 8, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7};
105
+ #else
93
106
  const uint8_t perm0[16] = {
94
107
  0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15};
108
+ #endif
95
109
 
96
110
  // range of affected blocks
97
111
  size_t block0 = i0 / bbs;
@@ -24,6 +24,9 @@
24
24
 
25
25
  namespace faiss {
26
26
 
27
+ struct NormTableScaler;
28
+ struct SIMDResultHandler;
29
+
27
30
  /** Pack codes for consumption by the SIMD kernels.
28
31
  * The unused bytes are set to 0.
29
32
  *
@@ -117,7 +120,6 @@ void pq4_pack_LUT(int nq, int nsq, const uint8_t* src, uint8_t* dest);
117
120
  * @param LUT packed look-up table
118
121
  * @param scaler scaler to scale the encoded norm
119
122
  */
120
- template <class ResultHandler, class Scaler>
121
123
  void pq4_accumulate_loop(
122
124
  int nq,
123
125
  size_t nb,
@@ -125,8 +127,8 @@ void pq4_accumulate_loop(
125
127
  int nsq,
126
128
  const uint8_t* codes,
127
129
  const uint8_t* LUT,
128
- ResultHandler& res,
129
- const Scaler& scaler);
130
+ SIMDResultHandler& res,
131
+ const NormTableScaler* scaler);
130
132
 
131
133
  /* qbs versions, supported only for bbs=32.
132
134
  *
@@ -178,14 +180,13 @@ int pq4_pack_LUT_qbs_q_map(
178
180
  * @param res call-back for the resutls
179
181
  * @param scaler scaler to scale the encoded norm
180
182
  */
181
- template <class ResultHandler, class Scaler>
182
183
  void pq4_accumulate_loop_qbs(
183
184
  int qbs,
184
185
  size_t nb,
185
186
  int nsq,
186
187
  const uint8_t* codes,
187
188
  const uint8_t* LUT,
188
- ResultHandler& res,
189
- const Scaler& scaler);
189
+ SIMDResultHandler& res,
190
+ const NormTableScaler* scaler = nullptr);
190
191
 
191
192
  } // namespace faiss
@@ -125,7 +125,7 @@ void accumulate_fixed_blocks(
125
125
  ResultHandler& res,
126
126
  const Scaler& scaler) {
127
127
  constexpr int bbs = 32 * BB;
128
- for (int64_t j0 = 0; j0 < nb; j0 += bbs) {
128
+ for (size_t j0 = 0; j0 < nb; j0 += bbs) {
129
129
  FixedStorageHandler<NQ, 2 * BB> res2;
130
130
  kernel_accumulate_block<NQ, BB>(nsq, codes, LUT, res2, scaler);
131
131
  res.set_block_origin(0, j0);
@@ -134,10 +134,8 @@ void accumulate_fixed_blocks(
134
134
  }
135
135
  }
136
136
 
137
- } // anonymous namespace
138
-
139
137
  template <class ResultHandler, class Scaler>
140
- void pq4_accumulate_loop(
138
+ void pq4_accumulate_loop_fixed_scaler(
141
139
  int nq,
142
140
  size_t nb,
143
141
  int bbs,
@@ -172,39 +170,55 @@ void pq4_accumulate_loop(
172
170
  #undef DISPATCH
173
171
  }
174
172
 
175
- // explicit template instantiations
176
-
177
- #define INSTANTIATE_ACCUMULATE(TH, C, with_id_map, S) \
178
- template void pq4_accumulate_loop<TH<C, with_id_map>, S>( \
179
- int, \
180
- size_t, \
181
- int, \
182
- int, \
183
- const uint8_t*, \
184
- const uint8_t*, \
185
- TH<C, with_id_map>&, \
186
- const S&);
187
-
188
- using DS = DummyScaler;
189
- using NS = NormTableScaler;
190
-
191
- #define INSTANTIATE_3(C, with_id_map) \
192
- INSTANTIATE_ACCUMULATE(SingleResultHandler, C, with_id_map, DS) \
193
- INSTANTIATE_ACCUMULATE(HeapHandler, C, with_id_map, DS) \
194
- INSTANTIATE_ACCUMULATE(ReservoirHandler, C, with_id_map, DS) \
195
- \
196
- INSTANTIATE_ACCUMULATE(SingleResultHandler, C, with_id_map, NS) \
197
- INSTANTIATE_ACCUMULATE(HeapHandler, C, with_id_map, NS) \
198
- INSTANTIATE_ACCUMULATE(ReservoirHandler, C, with_id_map, NS)
199
-
200
- using Csi = CMax<uint16_t, int>;
201
- INSTANTIATE_3(Csi, false);
202
- using CsiMin = CMin<uint16_t, int>;
203
- INSTANTIATE_3(CsiMin, false);
204
-
205
- using Csl = CMax<uint16_t, int64_t>;
206
- INSTANTIATE_3(Csl, true);
207
- using CslMin = CMin<uint16_t, int64_t>;
208
- INSTANTIATE_3(CslMin, true);
173
+ template <class ResultHandler>
174
+ void pq4_accumulate_loop_fixed_handler(
175
+ int nq,
176
+ size_t nb,
177
+ int bbs,
178
+ int nsq,
179
+ const uint8_t* codes,
180
+ const uint8_t* LUT,
181
+ ResultHandler& res,
182
+ const NormTableScaler* scaler) {
183
+ if (scaler) {
184
+ pq4_accumulate_loop_fixed_scaler(
185
+ nq, nb, bbs, nsq, codes, LUT, res, *scaler);
186
+ } else {
187
+ DummyScaler dscaler;
188
+ pq4_accumulate_loop_fixed_scaler(
189
+ nq, nb, bbs, nsq, codes, LUT, res, dscaler);
190
+ }
191
+ }
192
+
193
+ struct Run_pq4_accumulate_loop {
194
+ template <class ResultHandler>
195
+ void f(ResultHandler& res,
196
+ int nq,
197
+ size_t nb,
198
+ int bbs,
199
+ int nsq,
200
+ const uint8_t* codes,
201
+ const uint8_t* LUT,
202
+ const NormTableScaler* scaler) {
203
+ pq4_accumulate_loop_fixed_handler(
204
+ nq, nb, bbs, nsq, codes, LUT, res, scaler);
205
+ }
206
+ };
207
+
208
+ } // anonymous namespace
209
+
210
+ void pq4_accumulate_loop(
211
+ int nq,
212
+ size_t nb,
213
+ int bbs,
214
+ int nsq,
215
+ const uint8_t* codes,
216
+ const uint8_t* LUT,
217
+ SIMDResultHandler& res,
218
+ const NormTableScaler* scaler) {
219
+ Run_pq4_accumulate_loop consumer;
220
+ dispatch_SIMDResultHandler(
221
+ res, consumer, nq, nb, bbs, nsq, codes, LUT, scaler);
222
+ }
209
223
 
210
224
  } // namespace faiss