faiss 0.1.5 → 0.2.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 (219) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/README.md +12 -0
  4. data/ext/faiss/ext.cpp +1 -1
  5. data/ext/faiss/extconf.rb +6 -2
  6. data/ext/faiss/index.cpp +114 -43
  7. data/ext/faiss/index_binary.cpp +24 -30
  8. data/ext/faiss/kmeans.cpp +20 -16
  9. data/ext/faiss/numo.hpp +867 -0
  10. data/ext/faiss/pca_matrix.cpp +13 -14
  11. data/ext/faiss/product_quantizer.cpp +23 -24
  12. data/ext/faiss/utils.cpp +10 -37
  13. data/ext/faiss/utils.h +2 -13
  14. data/lib/faiss.rb +0 -5
  15. data/lib/faiss/version.rb +1 -1
  16. data/vendor/faiss/faiss/AutoTune.cpp +292 -291
  17. data/vendor/faiss/faiss/AutoTune.h +55 -56
  18. data/vendor/faiss/faiss/Clustering.cpp +334 -195
  19. data/vendor/faiss/faiss/Clustering.h +88 -35
  20. data/vendor/faiss/faiss/IVFlib.cpp +171 -195
  21. data/vendor/faiss/faiss/IVFlib.h +48 -51
  22. data/vendor/faiss/faiss/Index.cpp +85 -103
  23. data/vendor/faiss/faiss/Index.h +54 -48
  24. data/vendor/faiss/faiss/Index2Layer.cpp +139 -164
  25. data/vendor/faiss/faiss/Index2Layer.h +22 -22
  26. data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
  27. data/vendor/faiss/faiss/IndexBinary.h +140 -132
  28. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
  29. data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
  30. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
  31. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
  32. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
  33. data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
  34. data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
  35. data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
  36. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
  37. data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
  38. data/vendor/faiss/faiss/IndexFlat.cpp +116 -147
  39. data/vendor/faiss/faiss/IndexFlat.h +35 -46
  40. data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
  41. data/vendor/faiss/faiss/IndexHNSW.h +57 -41
  42. data/vendor/faiss/faiss/IndexIVF.cpp +474 -454
  43. data/vendor/faiss/faiss/IndexIVF.h +146 -113
  44. data/vendor/faiss/faiss/IndexIVFFlat.cpp +248 -250
  45. data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
  46. data/vendor/faiss/faiss/IndexIVFPQ.cpp +457 -516
  47. data/vendor/faiss/faiss/IndexIVFPQ.h +74 -66
  48. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
  49. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
  50. data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
  51. data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
  52. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +125 -133
  53. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +19 -21
  54. data/vendor/faiss/faiss/IndexLSH.cpp +75 -96
  55. data/vendor/faiss/faiss/IndexLSH.h +21 -26
  56. data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
  57. data/vendor/faiss/faiss/IndexLattice.h +11 -16
  58. data/vendor/faiss/faiss/IndexNNDescent.cpp +231 -0
  59. data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
  60. data/vendor/faiss/faiss/IndexNSG.cpp +303 -0
  61. data/vendor/faiss/faiss/IndexNSG.h +85 -0
  62. data/vendor/faiss/faiss/IndexPQ.cpp +405 -464
  63. data/vendor/faiss/faiss/IndexPQ.h +64 -67
  64. data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
  65. data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
  66. data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
  67. data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
  68. data/vendor/faiss/faiss/IndexRefine.cpp +115 -131
  69. data/vendor/faiss/faiss/IndexRefine.h +22 -23
  70. data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
  71. data/vendor/faiss/faiss/IndexReplicas.h +62 -56
  72. data/vendor/faiss/faiss/IndexResidual.cpp +291 -0
  73. data/vendor/faiss/faiss/IndexResidual.h +152 -0
  74. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +120 -155
  75. data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -45
  76. data/vendor/faiss/faiss/IndexShards.cpp +256 -240
  77. data/vendor/faiss/faiss/IndexShards.h +85 -73
  78. data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
  79. data/vendor/faiss/faiss/MatrixStats.h +7 -10
  80. data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
  81. data/vendor/faiss/faiss/MetaIndexes.h +40 -34
  82. data/vendor/faiss/faiss/MetricType.h +7 -7
  83. data/vendor/faiss/faiss/VectorTransform.cpp +652 -474
  84. data/vendor/faiss/faiss/VectorTransform.h +61 -89
  85. data/vendor/faiss/faiss/clone_index.cpp +77 -73
  86. data/vendor/faiss/faiss/clone_index.h +4 -9
  87. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
  88. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
  89. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +197 -170
  90. data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
  91. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
  92. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
  93. data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
  94. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
  95. data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
  96. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
  97. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
  98. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
  99. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
  100. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
  101. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
  102. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
  103. data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
  104. data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
  105. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
  106. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
  107. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
  108. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
  109. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
  110. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
  111. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
  112. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
  113. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
  114. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
  115. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
  116. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
  117. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
  118. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
  119. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
  120. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
  121. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
  122. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
  123. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
  124. data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
  125. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
  126. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
  127. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
  128. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
  129. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
  130. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
  131. data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
  132. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +270 -0
  133. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +115 -0
  134. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
  135. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
  136. data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
  137. data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
  138. data/vendor/faiss/faiss/impl/FaissException.h +41 -29
  139. data/vendor/faiss/faiss/impl/HNSW.cpp +595 -611
  140. data/vendor/faiss/faiss/impl/HNSW.h +179 -200
  141. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +672 -0
  142. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +172 -0
  143. data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
  144. data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
  145. data/vendor/faiss/faiss/impl/NSG.cpp +682 -0
  146. data/vendor/faiss/faiss/impl/NSG.h +199 -0
  147. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
  148. data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
  149. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
  150. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
  151. data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
  152. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +448 -0
  153. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +130 -0
  154. data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
  155. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +648 -701
  156. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
  157. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
  158. data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
  159. data/vendor/faiss/faiss/impl/index_read.cpp +547 -479
  160. data/vendor/faiss/faiss/impl/index_write.cpp +497 -407
  161. data/vendor/faiss/faiss/impl/io.cpp +75 -94
  162. data/vendor/faiss/faiss/impl/io.h +31 -41
  163. data/vendor/faiss/faiss/impl/io_macros.h +40 -29
  164. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
  165. data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
  166. data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
  167. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
  168. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
  169. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
  170. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
  171. data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
  172. data/vendor/faiss/faiss/index_factory.cpp +269 -218
  173. data/vendor/faiss/faiss/index_factory.h +6 -7
  174. data/vendor/faiss/faiss/index_io.h +23 -26
  175. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
  176. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
  177. data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
  178. data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
  179. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
  180. data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
  181. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
  182. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
  183. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
  184. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
  185. data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
  186. data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
  187. data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
  188. data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
  189. data/vendor/faiss/faiss/utils/Heap.h +186 -209
  190. data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
  191. data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
  192. data/vendor/faiss/faiss/utils/distances.cpp +301 -310
  193. data/vendor/faiss/faiss/utils/distances.h +133 -118
  194. data/vendor/faiss/faiss/utils/distances_simd.cpp +456 -516
  195. data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
  196. data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
  197. data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
  198. data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
  199. data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
  200. data/vendor/faiss/faiss/utils/hamming.h +62 -85
  201. data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
  202. data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
  203. data/vendor/faiss/faiss/utils/partitioning.h +26 -21
  204. data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
  205. data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
  206. data/vendor/faiss/faiss/utils/random.cpp +39 -63
  207. data/vendor/faiss/faiss/utils/random.h +13 -16
  208. data/vendor/faiss/faiss/utils/simdlib.h +4 -2
  209. data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
  210. data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
  211. data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
  212. data/vendor/faiss/faiss/utils/utils.cpp +304 -287
  213. data/vendor/faiss/faiss/utils/utils.h +53 -48
  214. metadata +24 -10
  215. data/lib/faiss/index.rb +0 -20
  216. data/lib/faiss/index_binary.rb +0 -20
  217. data/lib/faiss/kmeans.rb +0 -15
  218. data/lib/faiss/pca_matrix.rb +0 -15
  219. data/lib/faiss/product_quantizer.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d260b6585de456d0df620c322f5366a9ae29a1d275c0200c726233f5c5156aa
4
- data.tar.gz: 1f463792926335213ae59a56c7271593fa9eaf93d4705fa936da02d74127a571
3
+ metadata.gz: 01c09476fa378a49da82beddeab101d2c4ac124346260f56fe4dab0312b9c1ba
4
+ data.tar.gz: 587c6213b479cf979fca6c21c9f4a08cc56825e9403b84fff98e33b6a8759e5b
5
5
  SHA512:
6
- metadata.gz: 70960a3c2244b8d8dceaa7a2cc98a04b8f765c783ff2d1879d61e2303f7a7337490b67a9a6a56d3f0a4e19939812418122394633f2a68c198a6e0776a3a2b29a
7
- data.tar.gz: 0bc2edfdc86a7d3894923dfd98fe6db7dfa8a15cf640340198f92f7ef967b9548ed7a18e80d886018d8f621da01f73c1b061a85cf892318327144209d1a4ed87
6
+ metadata.gz: 38f65e098d1ac5ff8217bcdbafb68673dc6a37804175f00c0076cfad56534dcd6776dd294d5bb5178cf87b749fb342447255aa35fc39c1afbcf2011499285981
7
+ data.tar.gz: 0f62e4e4c7ead6503e935a26d2ae6d3a4fc3d83db19b648337be5847c598ad82235968aed368a0d75b64aad161518006cf91836e9315c00d7e4c67250b6a1bad
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## 0.2.2 (2021-06-08)
2
+
3
+ - Fixed installation error
4
+
5
+ ## 0.2.1 (2021-06-07)
6
+
7
+ - Updated Faiss to 1.7.1
8
+
9
+ ## 0.2.0 (2021-05-23)
10
+
11
+ - Improved performance
12
+ - Updated to Rice 4
13
+ - Dropped support for Ruby < 2.6
14
+
15
+ ## 0.1.7 (2021-03-26)
16
+
17
+ - Added `IndexScalarQuantizer` and `IndexIVFScalarQuantizer`
18
+
19
+ ## 0.1.6 (2021-03-21)
20
+
21
+ - Added support for inner product to `IndexIVFFlat` and `IndexIVFPQ`
22
+ - Added `ParameterSpace`
23
+ - Added `nprobe=` method
24
+
1
25
  ## 0.1.5 (2021-03-14)
2
26
 
3
27
  - Added `--with-optflags` option
data/README.md CHANGED
@@ -97,12 +97,24 @@ Locality-sensitive hashing
97
97
  Faiss::IndexLSH.new(d, nbits)
98
98
  ```
99
99
 
100
+ Scalar quantizer (SQ) in flat mode
101
+
102
+ ```rb
103
+ Faiss::IndexScalarQuantizer.new(d, qtype)
104
+ ```
105
+
100
106
  Product quantizer (PQ) in flat mode
101
107
 
102
108
  ```rb
103
109
  Faiss::IndexPQ.new(d, m, nbits)
104
110
  ```
105
111
 
112
+ IVF and scalar quantizer
113
+
114
+ ```rb
115
+ Faiss::IndexIVFScalarQuantizer.new(quantizer, d, nlists, qtype)
116
+ ```
117
+
106
118
  IVFADC (coarse quantizer+PQ on residuals)
107
119
 
108
120
  ```rb
data/ext/faiss/ext.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <rice/Module.hpp>
1
+ #include "utils.h"
2
2
 
3
3
  void init_index(Rice::Module& m);
4
4
  void init_index_binary(Rice::Module& m);
data/ext/faiss/extconf.rb CHANGED
@@ -1,17 +1,21 @@
1
1
  require "mkmf-rice"
2
+ require "numo/narray"
2
3
 
3
4
  abort "BLAS not found" unless have_library("blas")
4
5
  abort "LAPACK not found" unless have_library("lapack")
5
6
  abort "OpenMP not found" unless have_library("omp") || have_library("gomp")
6
7
 
7
- $CXXFLAGS << " -std=c++11 -DFINTEGER=int " << with_config("optflags", "-march=native")
8
+ numo = $LOAD_PATH.find { |v| File.exist?("#{v}/numo/numo/narray.h") }
9
+ abort "Numo not found" unless numo && find_header("numo/narray.h", "#{numo}/numo")
10
+
11
+ $CXXFLAGS << " -std=c++17 $(optflags) -DFINTEGER=int " << with_config("optflags", "-march=native")
8
12
 
9
13
  ext = File.expand_path(".", __dir__)
10
14
  vendor = File.expand_path("../../vendor/faiss", __dir__)
11
15
 
12
16
  $srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/{impl,invlists,utils}}/*.{cpp}"]
13
17
  $objs = $srcs.map { |v| v.sub(/cpp\z/, "o") }
14
- $INCFLAGS << " -I#{vendor}"
18
+ abort "Faiss not found" unless find_header("faiss/Index.h", vendor)
15
19
  $VPATH << vendor
16
20
 
17
21
  create_makefile("faiss/ext")
data/ext/faiss/index.cpp CHANGED
@@ -8,81 +8,144 @@
8
8
  #include <faiss/IndexIVFPQ.h>
9
9
  #include <faiss/IndexIVFPQR.h>
10
10
  #include <faiss/index_io.h>
11
-
12
- #include <rice/Array.hpp>
13
- #include <rice/Constructor.hpp>
14
- #include <rice/Module.hpp>
11
+ #include <faiss/AutoTune.h>
15
12
 
16
13
  #include "utils.h"
17
14
 
18
- template<>
19
- faiss::MetricType from_ruby<faiss::MetricType>(Rice::Object x)
20
- {
21
- auto s = x.to_s().str();
22
- if (s == "inner_product") {
23
- return faiss::METRIC_INNER_PRODUCT;
24
- } else if (s == "l2") {
25
- return faiss::METRIC_L2;
26
- } else {
27
- // TODO throw argument error
28
- throw std::runtime_error("Invalid metric: " + s);
29
- }
15
+ namespace Rice::detail {
16
+ template<>
17
+ struct Type<faiss::MetricType>
18
+ {
19
+ static bool verify()
20
+ {
21
+ return true;
22
+ }
23
+ };
24
+
25
+ template<>
26
+ class From_Ruby<faiss::MetricType>
27
+ {
28
+ public:
29
+ From_Ruby() = default;
30
+
31
+ From_Ruby(Arg* arg) : arg_(arg)
32
+ {
33
+ }
34
+
35
+ faiss::MetricType convert(VALUE x)
36
+ {
37
+ if (x == Qnil && this->arg_ && this->arg_->hasDefaultValue()) {
38
+ return this->arg_->defaultValue<faiss::MetricType>();
39
+ }
40
+
41
+ auto s = Object(x).to_s().str();
42
+ if (s == "inner_product") {
43
+ return faiss::METRIC_INNER_PRODUCT;
44
+ } else if (s == "l2") {
45
+ return faiss::METRIC_L2;
46
+ } else {
47
+ throw Rice::Exception(rb_eArgError, "Invalid metric: %s", s.c_str());
48
+ }
49
+ }
50
+
51
+ private:
52
+ Arg* arg_;
53
+ };
54
+
55
+ template<>
56
+ struct Type<faiss::ScalarQuantizer::QuantizerType>
57
+ {
58
+ static bool verify()
59
+ {
60
+ return true;
61
+ }
62
+ };
63
+
64
+ template<>
65
+ class From_Ruby<faiss::ScalarQuantizer::QuantizerType>
66
+ {
67
+ public:
68
+ faiss::ScalarQuantizer::QuantizerType convert(VALUE x)
69
+ {
70
+ auto s = Object(x).to_s().str();
71
+ if (s == "qt_8bit") {
72
+ return faiss::ScalarQuantizer::QT_8bit;
73
+ } else if (s == "qt_4bit") {
74
+ return faiss::ScalarQuantizer::QT_4bit;
75
+ } else if (s == "qt_8bit_uniform") {
76
+ return faiss::ScalarQuantizer::QT_8bit_uniform;
77
+ } else if (s == "qt_4bit_uniform") {
78
+ return faiss::ScalarQuantizer::QT_4bit_uniform;
79
+ } else if (s == "qt_fp16") {
80
+ return faiss::ScalarQuantizer::QT_fp16;
81
+ } else if (s == "qt_8bit_direct") {
82
+ return faiss::ScalarQuantizer::QT_8bit_direct;
83
+ } else if (s == "qt_6bit") {
84
+ return faiss::ScalarQuantizer::QT_6bit;
85
+ } else {
86
+ throw Rice::Exception(rb_eArgError, "Invalid quantizer type: %s", s.c_str());
87
+ }
88
+ }
89
+ };
30
90
  }
31
91
 
32
92
  void init_index(Rice::Module& m) {
33
93
  Rice::define_class_under<faiss::Index>(m, "Index")
34
94
  .define_method(
35
95
  "d",
36
- *[](faiss::Index &self) {
96
+ [](faiss::Index &self) {
37
97
  return self.d;
38
98
  })
39
99
  .define_method(
40
100
  "trained?",
41
- *[](faiss::Index &self) {
101
+ [](faiss::Index &self) {
42
102
  return self.is_trained;
43
103
  })
44
104
  .define_method(
45
105
  "ntotal",
46
- *[](faiss::Index &self) {
106
+ [](faiss::Index &self) {
47
107
  return self.ntotal;
48
108
  })
49
109
  .define_method(
50
- "_train",
51
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
52
- const float *x = float_array(o);
53
- self.train(n, x);
110
+ "train",
111
+ [](faiss::Index &self, numo::SFloat objects) {
112
+ auto n = check_shape(objects, self.d);
113
+ self.train(n, objects.read_ptr());
54
114
  })
55
115
  .define_method(
56
- "_add",
57
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
58
- const float *x = float_array(o);
59
- self.add(n, x);
116
+ "add",
117
+ [](faiss::Index &self, numo::SFloat objects) {
118
+ auto n = check_shape(objects, self.d);
119
+ self.add(n, objects.read_ptr());
60
120
  })
61
121
  .define_method(
62
- "_search",
63
- *[](faiss::Index &self, int64_t n, Rice::Object o, int64_t k) {
64
- const float *x = float_array(o);
65
- float *distances = new float[k * n];
66
- int64_t *labels = new int64_t[k * n];
122
+ "search",
123
+ [](faiss::Index &self, numo::SFloat objects, size_t k) {
124
+ auto n = check_shape(objects, self.d);
67
125
 
68
- self.search(n, x, k, distances, labels);
126
+ auto distances = numo::SFloat({n, k});
127
+ auto labels = numo::Int64({n, k});
69
128
 
70
- auto dstr = result(distances, k * n);
71
- auto lstr = result(labels, k * n);
129
+ self.search(n, objects.read_ptr(), k, distances.write_ptr(), labels.write_ptr());
72
130
 
73
131
  Rice::Array ret;
74
- ret.push(dstr);
75
- ret.push(lstr);
132
+ ret.push(distances);
133
+ ret.push(labels);
76
134
  return ret;
77
135
  })
136
+ .define_method(
137
+ "nprobe=",
138
+ [](faiss::Index &self, double val) {
139
+ faiss::ParameterSpace().set_index_parameter(&self, "nprobe", val);
140
+ })
78
141
  .define_method(
79
142
  "save",
80
- *[](faiss::Index &self, const char *fname) {
143
+ [](faiss::Index &self, const char *fname) {
81
144
  faiss::write_index(&self, fname);
82
145
  })
83
- .define_singleton_method(
146
+ .define_singleton_function(
84
147
  "load",
85
- *[](const char *fname) {
148
+ [](const char *fname) {
86
149
  return faiss::read_index(fname);
87
150
  });
88
151
 
@@ -93,10 +156,10 @@ void init_index(Rice::Module& m) {
93
156
  .define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
94
157
 
95
158
  Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(m, "IndexHNSWFlat")
96
- .define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int, faiss::MetricType>(), (Rice::Arg("d"), Rice::Arg("M"), Rice::Arg("metric") = faiss::METRIC_L2));
159
+ .define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int, faiss::MetricType>(), Rice::Arg("d"), Rice::Arg("M"), Rice::Arg("metric") = faiss::METRIC_L2);
97
160
 
98
161
  Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(m, "IndexIVFFlat")
99
- .define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t>());
162
+ .define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t, faiss::MetricType>(), Rice::Arg("quantizer"), Rice::Arg("d"), Rice::Arg("nlist"), Rice::Arg("metric") = faiss::METRIC_L2);
100
163
 
101
164
  Rice::define_class_under<faiss::IndexLSH, faiss::Index>(m, "IndexLSH")
102
165
  .define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
@@ -111,8 +174,16 @@ void init_index(Rice::Module& m) {
111
174
  .define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
112
175
 
113
176
  Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(m, "IndexIVFPQ")
114
- .define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t>());
177
+ .define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t, faiss::MetricType>(), Rice::Arg("quantizer"), Rice::Arg("d"), Rice::Arg("nlist"), Rice::Arg("M"), Rice::Arg("nbits_per_idx"), Rice::Arg("metric") = faiss::METRIC_L2);
115
178
 
116
179
  Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(m, "IndexIVFPQR")
117
180
  .define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
181
+
182
+ Rice::define_class_under<faiss::ParameterSpace>(m, "ParameterSpace")
183
+ .define_constructor(Rice::Constructor<faiss::ParameterSpace>())
184
+ .define_method(
185
+ "set_index_parameter",
186
+ [](faiss::ParameterSpace& self, faiss::Index* index, const std::string& name, double val) {
187
+ self.set_index_parameter(index, name, val);
188
+ });
118
189
  }
@@ -4,66 +4,60 @@
4
4
  #include <faiss/index_factory.h>
5
5
  #include <faiss/index_io.h>
6
6
 
7
- #include <rice/Array.hpp>
8
- #include <rice/Constructor.hpp>
9
- #include <rice/Module.hpp>
10
-
11
7
  #include "utils.h"
12
8
 
13
9
  void init_index_binary(Rice::Module& m) {
14
10
  Rice::define_class_under<faiss::IndexBinary>(m, "IndexBinary")
15
11
  .define_method(
16
12
  "d",
17
- *[](faiss::IndexBinary &self) {
13
+ [](faiss::IndexBinary &self) {
18
14
  return self.d;
19
15
  })
20
16
  .define_method(
21
17
  "trained?",
22
- *[](faiss::IndexBinary &self) {
18
+ [](faiss::IndexBinary &self) {
23
19
  return self.is_trained;
24
20
  })
25
21
  .define_method(
26
22
  "ntotal",
27
- *[](faiss::IndexBinary &self) {
23
+ [](faiss::IndexBinary &self) {
28
24
  return self.ntotal;
29
25
  })
30
26
  .define_method(
31
- "_train",
32
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
33
- const uint8_t *x = uint8_array(o);
34
- self.train(n, x);
27
+ "train",
28
+ [](faiss::IndexBinary &self, numo::UInt8 objects) {
29
+ auto n = check_shape(objects, self.d / 8);
30
+ self.train(n, objects.read_ptr());
35
31
  })
36
32
  .define_method(
37
- "_add",
38
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
39
- const uint8_t *x = uint8_array(o);
40
- self.add(n, x);
33
+ "add",
34
+ [](faiss::IndexBinary &self, numo::UInt8 objects) {
35
+ auto n = check_shape(objects, self.d / 8);
36
+ self.add(n, objects.read_ptr());
41
37
  })
42
38
  .define_method(
43
- "_search",
44
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o, int64_t k) {
45
- const uint8_t *x = uint8_array(o);
46
- int32_t *distances = new int32_t[k * n];
47
- int64_t *labels = new int64_t[k * n];
39
+ "search",
40
+ [](faiss::IndexBinary &self, numo::UInt8 objects, size_t k) {
41
+ auto n = check_shape(objects, self.d / 8);
48
42
 
49
- self.search(n, x, k, distances, labels);
43
+ auto distances = numo::Int32({n, k});
44
+ auto labels = numo::Int64({n, k});
50
45
 
51
- auto dstr = result(distances, k * n);
52
- auto lstr = result(labels, k * n);
46
+ self.search(n, objects.read_ptr(), k, distances.write_ptr(), labels.write_ptr());
53
47
 
54
48
  Rice::Array ret;
55
- ret.push(dstr);
56
- ret.push(lstr);
49
+ ret.push(distances);
50
+ ret.push(labels);
57
51
  return ret;
58
52
  })
59
53
  .define_method(
60
54
  "save",
61
- *[](faiss::IndexBinary &self, const char *fname) {
55
+ [](faiss::IndexBinary &self, const char *fname) {
62
56
  faiss::write_index_binary(&self, fname);
63
57
  })
64
- .define_singleton_method(
58
+ .define_singleton_function(
65
59
  "load",
66
- *[](const char *fname) {
60
+ [](const char *fname) {
67
61
  return faiss::read_index_binary(fname);
68
62
  });
69
63
 
@@ -73,9 +67,9 @@ void init_index_binary(Rice::Module& m) {
73
67
  Rice::define_class_under<faiss::IndexBinaryIVF, faiss::IndexBinary>(m, "IndexBinaryIVF")
74
68
  .define_constructor(Rice::Constructor<faiss::IndexBinaryIVF, faiss::IndexBinary*, size_t, size_t>());
75
69
 
76
- m.define_singleton_method(
70
+ m.define_singleton_function(
77
71
  "index_binary_factory",
78
- *[](int d, const char *description) {
72
+ [](int d, const char *description) {
79
73
  return faiss::index_binary_factory(d, description);
80
74
  });
81
75
  }
data/ext/faiss/kmeans.cpp CHANGED
@@ -1,36 +1,40 @@
1
1
  #include <faiss/Clustering.h>
2
-
3
- #include <rice/Constructor.hpp>
4
- #include <rice/Module.hpp>
2
+ #include <faiss/IndexFlat.h>
5
3
 
6
4
  #include "utils.h"
7
5
 
8
6
  void init_kmeans(Rice::Module& m) {
9
7
  Rice::define_class_under<faiss::Clustering>(m, "Kmeans")
10
8
  .define_constructor(Rice::Constructor<faiss::Clustering, int, int>())
9
+ .define_method(
10
+ "index",
11
+ [](Rice::Object self) {
12
+ return self.attr_get("@index");
13
+ })
11
14
  .define_method(
12
15
  "d",
13
- *[](faiss::Clustering &self) {
16
+ [](faiss::Clustering &self) {
14
17
  return self.d;
15
18
  })
16
19
  .define_method(
17
20
  "k",
18
- *[](faiss::Clustering &self) {
21
+ [](faiss::Clustering &self) {
19
22
  return self.k;
20
23
  })
21
24
  .define_method(
22
- "_centroids",
23
- *[](faiss::Clustering &self) {
24
- float *centroids = new float[self.k * self.d];
25
- for (size_t i = 0; i < self.centroids.size(); i++) {
26
- centroids[i] = self.centroids[i];
27
- }
28
- return result(centroids, self.k * self.d);
25
+ "centroids",
26
+ [](faiss::Clustering &self) {
27
+ auto centroids = numo::SFloat({self.k, self.d});
28
+ memcpy(centroids.write_ptr(), self.centroids.data(), self.centroids.size() * sizeof(float));
29
+ return centroids;
29
30
  })
30
31
  .define_method(
31
- "_train",
32
- *[](faiss::Clustering &self, int64_t n, Rice::Object o, faiss::Index & index) {
33
- const float *x = float_array(o);
34
- self.train(n, x, index);
32
+ "train",
33
+ [](Rice::Object self, numo::SFloat objects) {
34
+ auto self_ptr = Rice::detail::From_Ruby<faiss::Clustering*>().convert(self.value());
35
+ auto n = check_shape(objects, self_ptr->d);
36
+ auto index = faiss::IndexFlatL2(self_ptr->d);
37
+ self.iv_set("@index", Rice::Object(Rice::detail::To_Ruby<faiss::IndexFlatL2>().convert(index)));
38
+ self_ptr->train(n, objects.read_ptr(), index);
35
39
  });
36
40
  }