faiss 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -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 +0 -1
  12. data/vendor/faiss/faiss/Clustering.cpp +4 -18
  13. data/vendor/faiss/faiss/Clustering.h +31 -21
  14. data/vendor/faiss/faiss/IVFlib.cpp +22 -11
  15. data/vendor/faiss/faiss/Index.cpp +1 -1
  16. data/vendor/faiss/faiss/Index.h +20 -5
  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/IndexBinaryHash.cpp +25 -50
  25. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +106 -187
  26. data/vendor/faiss/faiss/IndexFastScan.cpp +90 -159
  27. data/vendor/faiss/faiss/IndexFastScan.h +9 -8
  28. data/vendor/faiss/faiss/IndexFlat.cpp +195 -3
  29. data/vendor/faiss/faiss/IndexFlat.h +20 -1
  30. data/vendor/faiss/faiss/IndexFlatCodes.cpp +11 -0
  31. data/vendor/faiss/faiss/IndexFlatCodes.h +3 -1
  32. data/vendor/faiss/faiss/IndexHNSW.cpp +112 -316
  33. data/vendor/faiss/faiss/IndexHNSW.h +12 -48
  34. data/vendor/faiss/faiss/IndexIDMap.cpp +69 -28
  35. data/vendor/faiss/faiss/IndexIDMap.h +24 -2
  36. data/vendor/faiss/faiss/IndexIVF.cpp +159 -53
  37. data/vendor/faiss/faiss/IndexIVF.h +37 -5
  38. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +18 -26
  39. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +3 -2
  40. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +19 -46
  41. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +4 -3
  42. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +433 -405
  43. data/vendor/faiss/faiss/IndexIVFFastScan.h +56 -26
  44. data/vendor/faiss/faiss/IndexIVFFlat.cpp +15 -5
  45. data/vendor/faiss/faiss/IndexIVFFlat.h +3 -2
  46. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +172 -0
  47. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +56 -0
  48. data/vendor/faiss/faiss/IndexIVFPQ.cpp +78 -122
  49. data/vendor/faiss/faiss/IndexIVFPQ.h +6 -7
  50. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +18 -50
  51. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +4 -3
  52. data/vendor/faiss/faiss/IndexIVFPQR.cpp +45 -29
  53. data/vendor/faiss/faiss/IndexIVFPQR.h +5 -2
  54. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +25 -27
  55. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +6 -6
  56. data/vendor/faiss/faiss/IndexLSH.cpp +14 -16
  57. data/vendor/faiss/faiss/IndexNNDescent.cpp +3 -4
  58. data/vendor/faiss/faiss/IndexNSG.cpp +11 -27
  59. data/vendor/faiss/faiss/IndexNSG.h +10 -10
  60. data/vendor/faiss/faiss/IndexPQ.cpp +72 -88
  61. data/vendor/faiss/faiss/IndexPQ.h +1 -4
  62. data/vendor/faiss/faiss/IndexPQFastScan.cpp +1 -1
  63. data/vendor/faiss/faiss/IndexPreTransform.cpp +25 -31
  64. data/vendor/faiss/faiss/IndexRefine.cpp +49 -19
  65. data/vendor/faiss/faiss/IndexRefine.h +7 -0
  66. data/vendor/faiss/faiss/IndexReplicas.cpp +23 -26
  67. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +22 -16
  68. data/vendor/faiss/faiss/IndexScalarQuantizer.h +6 -4
  69. data/vendor/faiss/faiss/IndexShards.cpp +21 -29
  70. data/vendor/faiss/faiss/IndexShardsIVF.cpp +1 -2
  71. data/vendor/faiss/faiss/MatrixStats.cpp +17 -32
  72. data/vendor/faiss/faiss/MatrixStats.h +21 -9
  73. data/vendor/faiss/faiss/MetaIndexes.cpp +35 -35
  74. data/vendor/faiss/faiss/VectorTransform.cpp +13 -26
  75. data/vendor/faiss/faiss/VectorTransform.h +7 -7
  76. data/vendor/faiss/faiss/clone_index.cpp +15 -10
  77. data/vendor/faiss/faiss/clone_index.h +3 -0
  78. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +87 -4
  79. data/vendor/faiss/faiss/gpu/GpuCloner.h +22 -0
  80. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +7 -0
  81. data/vendor/faiss/faiss/gpu/GpuDistance.h +46 -38
  82. data/vendor/faiss/faiss/gpu/GpuIndex.h +28 -4
  83. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +4 -4
  84. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +8 -9
  85. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +18 -3
  86. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +22 -11
  87. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +1 -3
  88. data/vendor/faiss/faiss/gpu/GpuResources.cpp +24 -3
  89. data/vendor/faiss/faiss/gpu/GpuResources.h +39 -11
  90. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +117 -17
  91. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +57 -3
  92. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +1 -1
  93. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +25 -0
  94. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +129 -9
  95. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +267 -40
  96. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +299 -208
  97. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +1 -0
  98. data/vendor/faiss/faiss/gpu/utils/RaftUtils.h +75 -0
  99. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +3 -1
  100. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +5 -5
  101. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
  102. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -2
  103. data/vendor/faiss/faiss/impl/DistanceComputer.h +24 -1
  104. data/vendor/faiss/faiss/impl/FaissException.h +13 -34
  105. data/vendor/faiss/faiss/impl/HNSW.cpp +321 -70
  106. data/vendor/faiss/faiss/impl/HNSW.h +9 -8
  107. data/vendor/faiss/faiss/impl/IDSelector.h +4 -4
  108. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +3 -1
  109. data/vendor/faiss/faiss/impl/NNDescent.cpp +29 -19
  110. data/vendor/faiss/faiss/impl/NSG.h +1 -1
  111. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +14 -12
  112. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +1 -1
  113. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +24 -22
  114. data/vendor/faiss/faiss/impl/ProductQuantizer.h +1 -1
  115. data/vendor/faiss/faiss/impl/Quantizer.h +1 -1
  116. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +27 -1015
  117. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +5 -63
  118. data/vendor/faiss/faiss/impl/ResultHandler.h +232 -176
  119. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +444 -104
  120. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +0 -8
  121. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +280 -42
  122. data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +21 -14
  123. data/vendor/faiss/faiss/impl/code_distance/code_distance.h +22 -12
  124. data/vendor/faiss/faiss/impl/index_read.cpp +45 -19
  125. data/vendor/faiss/faiss/impl/index_write.cpp +60 -41
  126. data/vendor/faiss/faiss/impl/io.cpp +10 -10
  127. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -1
  128. data/vendor/faiss/faiss/impl/platform_macros.h +18 -1
  129. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +3 -0
  130. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +7 -6
  131. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +52 -38
  132. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +40 -49
  133. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +960 -0
  134. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +176 -0
  135. data/vendor/faiss/faiss/impl/simd_result_handlers.h +374 -202
  136. data/vendor/faiss/faiss/index_factory.cpp +10 -7
  137. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  138. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +27 -9
  139. data/vendor/faiss/faiss/invlists/InvertedLists.h +12 -3
  140. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +3 -3
  141. data/vendor/faiss/faiss/python/python_callbacks.cpp +1 -1
  142. data/vendor/faiss/faiss/utils/Heap.cpp +3 -1
  143. data/vendor/faiss/faiss/utils/WorkerThread.h +1 -0
  144. data/vendor/faiss/faiss/utils/distances.cpp +128 -74
  145. data/vendor/faiss/faiss/utils/distances.h +81 -4
  146. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +5 -5
  147. data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
  148. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
  149. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +1 -1
  150. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +5 -5
  151. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +1 -1
  152. data/vendor/faiss/faiss/utils/distances_simd.cpp +428 -70
  153. data/vendor/faiss/faiss/utils/fp16-arm.h +29 -0
  154. data/vendor/faiss/faiss/utils/fp16.h +2 -0
  155. data/vendor/faiss/faiss/utils/hamming.cpp +162 -110
  156. data/vendor/faiss/faiss/utils/hamming.h +58 -0
  157. data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +16 -89
  158. data/vendor/faiss/faiss/utils/hamming_distance/common.h +1 -0
  159. data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +15 -87
  160. data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +57 -0
  161. data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +14 -104
  162. data/vendor/faiss/faiss/utils/partitioning.cpp +3 -4
  163. data/vendor/faiss/faiss/utils/prefetch.h +77 -0
  164. data/vendor/faiss/faiss/utils/quantize_lut.cpp +0 -14
  165. data/vendor/faiss/faiss/utils/simdlib_avx2.h +0 -6
  166. data/vendor/faiss/faiss/utils/simdlib_neon.h +72 -77
  167. data/vendor/faiss/faiss/utils/sorting.cpp +140 -5
  168. data/vendor/faiss/faiss/utils/sorting.h +27 -0
  169. data/vendor/faiss/faiss/utils/utils.cpp +112 -6
  170. data/vendor/faiss/faiss/utils/utils.h +57 -20
  171. metadata +10 -3
@@ -0,0 +1,75 @@
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
+ * Copyright (c) 2023, NVIDIA CORPORATION.
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ #pragma once
24
+
25
+ #include <faiss/MetricType.h>
26
+ #include <faiss/gpu/GpuResources.h>
27
+ #include <faiss/gpu/utils/Tensor.cuh>
28
+
29
+ #include <raft/distance/distance_types.hpp>
30
+
31
+ #pragma GCC visibility push(default)
32
+ namespace faiss {
33
+ namespace gpu {
34
+
35
+ inline raft::distance::DistanceType metricFaissToRaft(
36
+ MetricType metric,
37
+ bool exactDistance) {
38
+ switch (metric) {
39
+ case MetricType::METRIC_INNER_PRODUCT:
40
+ return raft::distance::DistanceType::InnerProduct;
41
+ case MetricType::METRIC_L2:
42
+ return raft::distance::DistanceType::L2Expanded;
43
+ case MetricType::METRIC_L1:
44
+ return raft::distance::DistanceType::L1;
45
+ case MetricType::METRIC_Linf:
46
+ return raft::distance::DistanceType::Linf;
47
+ case MetricType::METRIC_Lp:
48
+ return raft::distance::DistanceType::LpUnexpanded;
49
+ case MetricType::METRIC_Canberra:
50
+ return raft::distance::DistanceType::Canberra;
51
+ case MetricType::METRIC_BrayCurtis:
52
+ return raft::distance::DistanceType::BrayCurtis;
53
+ case MetricType::METRIC_JensenShannon:
54
+ return raft::distance::DistanceType::JensenShannon;
55
+ default:
56
+ RAFT_FAIL("Distance type not supported");
57
+ }
58
+ }
59
+
60
+ /// Identify matrix rows containing non NaN values. validRows[i] is false if row
61
+ /// i contains a NaN value and true otherwise.
62
+ void validRowIndices(
63
+ GpuResources* res,
64
+ Tensor<float, 2, true>& vecs,
65
+ bool* validRows);
66
+
67
+ /// Filter out matrix rows containing NaN values. The vectors and indices are
68
+ /// updated in-place.
69
+ idx_t inplaceGatherFilteredRows(
70
+ GpuResources* res,
71
+ Tensor<float, 2, true>& vecs,
72
+ Tensor<idx_t, 1, true>& indices);
73
+ } // namespace gpu
74
+ } // namespace faiss
75
+ #pragma GCC visibility pop
@@ -261,7 +261,7 @@ void AdditiveQuantizer::decode(const uint8_t* code, float* x, size_t n) const {
261
261
  is_trained, "The additive quantizer is not trained yet.");
262
262
 
263
263
  // standard additive quantizer decoding
264
- #pragma omp parallel for if (n > 1000)
264
+ #pragma omp parallel for if (n > 100)
265
265
  for (int64_t i = 0; i < n; i++) {
266
266
  BitstringReader bsr(code + i * code_size, code_size);
267
267
  float* xi = x + i * d;
@@ -370,6 +370,8 @@ void AdditiveQuantizer::compute_LUT(
370
370
 
371
371
  namespace {
372
372
 
373
+ /* compute inner products of one query with all centroids, given a look-up
374
+ * table of all inner producst with codebook entries */
373
375
  void compute_inner_prod_with_LUT(
374
376
  const AdditiveQuantizer& aq,
375
377
  const float* LUT,
@@ -49,11 +49,11 @@ struct AdditiveQuantizer : Quantizer {
49
49
  /// encode a norm into norm_bits bits
50
50
  uint64_t encode_norm(float norm) const;
51
51
 
52
- uint32_t encode_qcint(
53
- float x) const; ///< encode norm by non-uniform scalar quantization
52
+ /// encode norm by non-uniform scalar quantization
53
+ uint32_t encode_qcint(float x) const;
54
54
 
55
- float decode_qcint(uint32_t c)
56
- const; ///< decode norm by non-uniform scalar quantization
55
+ /// decode norm by non-uniform scalar quantization
56
+ float decode_qcint(uint32_t c) const;
57
57
 
58
58
  /// Encodes how search is performed and how vectors are encoded
59
59
  enum Search_type_t {
@@ -203,4 +203,4 @@ struct AdditiveQuantizer : Quantizer {
203
203
  virtual ~AdditiveQuantizer();
204
204
  };
205
205
 
206
- }; // namespace faiss
206
+ } // namespace faiss
@@ -230,7 +230,7 @@ bool InterruptCallback::is_interrupted() {
230
230
 
231
231
  size_t InterruptCallback::get_period_hint(size_t flops) {
232
232
  if (!instance.get()) {
233
- return 1L << 30; // never check
233
+ return (size_t)1 << 30; // never check
234
234
  }
235
235
  // for 10M flops, it is reasonable to check once every 10 iterations
236
236
  return std::max((size_t)10 * 10 * 1000 * 1000 / (flops + 1), (size_t)1);
@@ -41,7 +41,6 @@ struct RangeSearchResult {
41
41
 
42
42
  /// called when lims contains the nb of elements result entries
43
43
  /// for each query
44
-
45
44
  virtual void do_allocation();
46
45
 
47
46
  virtual ~RangeSearchResult();
@@ -165,7 +164,7 @@ struct FAISS_API InterruptCallback {
165
164
  /// set implementation optimized for fast access.
166
165
  struct VisitedTable {
167
166
  std::vector<uint8_t> visited;
168
- int visno;
167
+ uint8_t visno;
169
168
 
170
169
  explicit VisitedTable(int size) : visited(size), visno(1) {}
171
170
 
@@ -30,6 +30,29 @@ struct DistanceComputer {
30
30
  /// compute distance of vector i to current query
31
31
  virtual float operator()(idx_t i) = 0;
32
32
 
33
+ /// compute distances of current query to 4 stored vectors.
34
+ /// certain DistanceComputer implementations may benefit
35
+ /// heavily from this.
36
+ virtual void distances_batch_4(
37
+ const idx_t idx0,
38
+ const idx_t idx1,
39
+ const idx_t idx2,
40
+ const idx_t idx3,
41
+ float& dis0,
42
+ float& dis1,
43
+ float& dis2,
44
+ float& dis3) {
45
+ // compute first, assign next
46
+ const float d0 = this->operator()(idx0);
47
+ const float d1 = this->operator()(idx1);
48
+ const float d2 = this->operator()(idx2);
49
+ const float d3 = this->operator()(idx3);
50
+ dis0 = d0;
51
+ dis1 = d1;
52
+ dis2 = d2;
53
+ dis3 = d3;
54
+ }
55
+
33
56
  /// compute distance between two stored vectors
34
57
  virtual float symmetric_dis(idx_t i, idx_t j) = 0;
35
58
 
@@ -49,7 +72,7 @@ struct FlatCodesDistanceComputer : DistanceComputer {
49
72
 
50
73
  FlatCodesDistanceComputer() : codes(nullptr), code_size(0) {}
51
74
 
52
- float operator()(idx_t i) final {
75
+ float operator()(idx_t i) override {
53
76
  return distance_to_code(codes + i * code_size);
54
77
  }
55
78
 
@@ -1,3 +1,4 @@
1
+
1
2
  /**
2
3
  * Copyright (c) Facebook, Inc. and its affiliates.
3
4
  *
@@ -40,42 +41,20 @@ class FaissException : public std::exception {
40
41
  void handleExceptions(
41
42
  std::vector<std::pair<int, std::exception_ptr>>& exceptions);
42
43
 
43
- /** bare-bones unique_ptr
44
- * this one deletes with delete [] */
45
- template <class T>
46
- struct ScopeDeleter {
47
- const T* ptr;
48
- explicit ScopeDeleter(const T* ptr = nullptr) : ptr(ptr) {}
49
- void release() {
50
- ptr = nullptr;
51
- }
52
- void set(const T* ptr_in) {
53
- ptr = ptr_in;
54
- }
55
- void swap(ScopeDeleter<T>& other) {
56
- std::swap(ptr, other.ptr);
57
- }
58
- ~ScopeDeleter() {
59
- delete[] ptr;
44
+ /** RAII object for a set of possibly transformed vectors (deallocated only if
45
+ * they are indeed transformed)
46
+ */
47
+ struct TransformedVectors {
48
+ const float* x;
49
+ bool own_x;
50
+ TransformedVectors(const float* x_orig, const float* x) : x(x) {
51
+ own_x = x_orig != x;
60
52
  }
61
- };
62
53
 
63
- /** same but deletes with the simple delete (least common case) */
64
- template <class T>
65
- struct ScopeDeleter1 {
66
- const T* ptr;
67
- explicit ScopeDeleter1(const T* ptr = nullptr) : ptr(ptr) {}
68
- void release() {
69
- ptr = nullptr;
70
- }
71
- void set(const T* ptr_in) {
72
- ptr = ptr_in;
73
- }
74
- void swap(ScopeDeleter1<T>& other) {
75
- std::swap(ptr, other.ptr);
76
- }
77
- ~ScopeDeleter1() {
78
- delete ptr;
54
+ ~TransformedVectors() {
55
+ if (own_x) {
56
+ delete[] x;
57
+ }
79
58
  }
80
59
  };
81
60