faiss 0.1.7 → 0.2.3

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 +18 -0
  3. data/README.md +7 -7
  4. data/ext/faiss/ext.cpp +1 -1
  5. data/ext/faiss/extconf.rb +8 -2
  6. data/ext/faiss/index.cpp +102 -69
  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/version.rb +1 -1
  15. data/lib/faiss.rb +0 -5
  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 +26 -12
  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: f6fb14299c0b91d6ddaf74ca2fa2d6895e8621249a5351683b109f35866ab541
4
- data.tar.gz: 2ec9d11faf8f3f1af7790d1f39e31cb92d8a79d2539fa7c935dbda63f3052d8c
3
+ metadata.gz: 106966f6d5e7f6a3f5237a2ebb59912304bbe319ba2891c45166c753fc0e9df1
4
+ data.tar.gz: 3d72777c777d75beb15c09cb223c4e63f188b7e89fab06d6fa9268bc2be4ff59
5
5
  SHA512:
6
- metadata.gz: 5afa3a7ca6947fad6f5a4f7693b524411875d212079621bc72bda73df36fdf090fcb566b83248302a83ecb38ee04189c57c812eaad4ffc99f94840eaf3abfd28
7
- data.tar.gz: c29cf727d683f82e530ba1788630ae546ddb4470f3145f0a4dd1b6afe31555bb9363dfa52cc2a5e005a077f6e2aae24b3dc1aa44d1ff9c50fc9269987df0ec87
6
+ metadata.gz: b1d822fd4e0850dd667aba09a02f81c43e2afde90a6d88fff1e631539a8d47f42b4dfe0705980a6ff8be88b021af805998d1e70b09cd1010302702a1c1363cac
7
+ data.tar.gz: 24e4febd81142150541199523f0b1e19a0be7c658e504472778173e38006234ea47f7e362c1396c3135b4449b7ab5229dc027c4bcea59c880902e1fa23e3c956
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.2.3 (2021-12-17)
2
+
3
+ - Fixed installation error with ARM Mac
4
+
5
+ ## 0.2.2 (2021-06-08)
6
+
7
+ - Fixed installation error
8
+
9
+ ## 0.2.1 (2021-06-07)
10
+
11
+ - Updated Faiss to 1.7.1
12
+
13
+ ## 0.2.0 (2021-05-23)
14
+
15
+ - Improved performance
16
+ - Updated to Rice 4
17
+ - Dropped support for Ruby < 2.6
18
+
1
19
  ## 0.1.7 (2021-03-26)
2
20
 
3
21
  - Added `IndexScalarQuantizer` and `IndexIVFScalarQuantizer`
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # Faiss
1
+ # Faiss Ruby
2
2
 
3
3
  [Faiss](https://github.com/facebookresearch/faiss) - efficient similarity search and clustering - for Ruby
4
4
 
5
5
  Learn more about [Faiss](https://engineering.fb.com/data-infrastructure/faiss-a-library-for-efficient-similarity-search/)
6
6
 
7
- [![Build Status](https://github.com/ankane/faiss/workflows/build/badge.svg?branch=master)](https://github.com/ankane/faiss/actions)
7
+ [![Build Status](https://github.com/ankane/faiss-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/faiss-ruby/actions)
8
8
 
9
9
  ## Installation
10
10
 
@@ -220,22 +220,22 @@ Numo::NArray.cast([[1, 2, 3], [4, 5, 6]])
220
220
 
221
221
  ## History
222
222
 
223
- View the [changelog](https://github.com/ankane/faiss/blob/master/CHANGELOG.md)
223
+ View the [changelog](https://github.com/ankane/faiss-ruby/blob/master/CHANGELOG.md)
224
224
 
225
225
  ## Contributing
226
226
 
227
227
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
228
228
 
229
- - [Report bugs](https://github.com/ankane/faiss/issues)
230
- - Fix bugs and [submit pull requests](https://github.com/ankane/faiss/pulls)
229
+ - [Report bugs](https://github.com/ankane/faiss-ruby/issues)
230
+ - Fix bugs and [submit pull requests](https://github.com/ankane/faiss-ruby/pulls)
231
231
  - Write, clarify, or fix documentation
232
232
  - Suggest or add new features
233
233
 
234
234
  To get started with development:
235
235
 
236
236
  ```sh
237
- git clone --recursive https://github.com/ankane/faiss.git
238
- cd faiss
237
+ git clone --recursive https://github.com/ankane/faiss-ruby.git
238
+ cd faiss-ruby
239
239
  bundle install
240
240
  bundle exec rake compile
241
241
  bundle exec rake test
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,23 @@
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 = File.join(Gem.loaded_specs["numo-narray"].require_path, "numo")
9
+ abort "Numo not found" unless find_header("numo/narray.h", numo)
10
+
11
+ # -march=native not supported with ARM Mac
12
+ default_optflags = RbConfig::CONFIG["host_os"] =~ /darwin/i && RbConfig::CONFIG["host_cpu"] =~ /arm|aarch64/i ? "" : "-march=native"
13
+ $CXXFLAGS << " -std=c++17 $(optflags) -DFINTEGER=int " << with_config("optflags", default_optflags)
8
14
 
9
15
  ext = File.expand_path(".", __dir__)
10
16
  vendor = File.expand_path("../../vendor/faiss", __dir__)
11
17
 
12
18
  $srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/{impl,invlists,utils}}/*.{cpp}"]
13
19
  $objs = $srcs.map { |v| v.sub(/cpp\z/, "o") }
14
- $INCFLAGS << " -I#{vendor}"
20
+ abort "Faiss not found" unless find_header("faiss/Index.h", vendor)
15
21
  $VPATH << vendor
16
22
 
17
23
  create_makefile("faiss/ext")
data/ext/faiss/index.cpp CHANGED
@@ -10,109 +10,142 @@
10
10
  #include <faiss/index_io.h>
11
11
  #include <faiss/AutoTune.h>
12
12
 
13
- #include <rice/Array.hpp>
14
- #include <rice/Constructor.hpp>
15
- #include <rice/Module.hpp>
16
-
17
13
  #include "utils.h"
18
14
 
19
- template<>
20
- faiss::MetricType from_ruby<faiss::MetricType>(Rice::Object x)
21
- {
22
- auto s = x.to_s().str();
23
- if (s == "inner_product") {
24
- return faiss::METRIC_INNER_PRODUCT;
25
- } else if (s == "l2") {
26
- return faiss::METRIC_L2;
27
- } else {
28
- // TODO throw argument error
29
- throw std::runtime_error("Invalid metric: " + s);
30
- }
31
- }
32
-
33
- template<>
34
- faiss::ScalarQuantizer::QuantizerType from_ruby<faiss::ScalarQuantizer::QuantizerType>(Rice::Object x)
35
- {
36
- auto s = x.to_s().str();
37
- if (s == "qt_8bit") {
38
- return faiss::ScalarQuantizer::QT_8bit;
39
- } else if (s == "qt_4bit") {
40
- return faiss::ScalarQuantizer::QT_4bit;
41
- } else if (s == "qt_8bit_uniform") {
42
- return faiss::ScalarQuantizer::QT_8bit_uniform;
43
- } else if (s == "qt_4bit_uniform") {
44
- return faiss::ScalarQuantizer::QT_4bit_uniform;
45
- } else if (s == "qt_fp16") {
46
- return faiss::ScalarQuantizer::QT_fp16;
47
- } else if (s == "qt_8bit_direct") {
48
- return faiss::ScalarQuantizer::QT_8bit_direct;
49
- } else if (s == "qt_6bit") {
50
- return faiss::ScalarQuantizer::QT_6bit;
51
- } else {
52
- // TODO throw argument error
53
- throw std::runtime_error("Invalid quantizer type: " + s);
54
- }
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
+ };
55
90
  }
56
91
 
57
92
  void init_index(Rice::Module& m) {
58
93
  Rice::define_class_under<faiss::Index>(m, "Index")
59
94
  .define_method(
60
95
  "d",
61
- *[](faiss::Index &self) {
96
+ [](faiss::Index &self) {
62
97
  return self.d;
63
98
  })
64
99
  .define_method(
65
100
  "trained?",
66
- *[](faiss::Index &self) {
101
+ [](faiss::Index &self) {
67
102
  return self.is_trained;
68
103
  })
69
104
  .define_method(
70
105
  "ntotal",
71
- *[](faiss::Index &self) {
106
+ [](faiss::Index &self) {
72
107
  return self.ntotal;
73
108
  })
74
109
  .define_method(
75
- "_train",
76
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
77
- const float *x = float_array(o);
78
- 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());
79
114
  })
80
115
  .define_method(
81
- "_add",
82
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
83
- const float *x = float_array(o);
84
- 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());
85
120
  })
86
121
  .define_method(
87
- "_search",
88
- *[](faiss::Index &self, int64_t n, Rice::Object o, int64_t k) {
89
- const float *x = float_array(o);
90
- float *distances = new float[k * n];
91
- 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);
92
125
 
93
- self.search(n, x, k, distances, labels);
126
+ auto distances = numo::SFloat({n, k});
127
+ auto labels = numo::Int64({n, k});
94
128
 
95
- auto dstr = result(distances, k * n);
96
- auto lstr = result(labels, k * n);
129
+ self.search(n, objects.read_ptr(), k, distances.write_ptr(), labels.write_ptr());
97
130
 
98
131
  Rice::Array ret;
99
- ret.push(dstr);
100
- ret.push(lstr);
132
+ ret.push(distances);
133
+ ret.push(labels);
101
134
  return ret;
102
135
  })
103
136
  .define_method(
104
137
  "nprobe=",
105
- *[](faiss::Index &self, double val) {
138
+ [](faiss::Index &self, double val) {
106
139
  faiss::ParameterSpace().set_index_parameter(&self, "nprobe", val);
107
140
  })
108
141
  .define_method(
109
142
  "save",
110
- *[](faiss::Index &self, const char *fname) {
143
+ [](faiss::Index &self, const char *fname) {
111
144
  faiss::write_index(&self, fname);
112
145
  })
113
- .define_singleton_method(
146
+ .define_singleton_function(
114
147
  "load",
115
- *[](const char *fname) {
148
+ [](const char *fname) {
116
149
  return faiss::read_index(fname);
117
150
  });
118
151
 
@@ -123,10 +156,10 @@ void init_index(Rice::Module& m) {
123
156
  .define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
124
157
 
125
158
  Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(m, "IndexHNSWFlat")
126
- .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);
127
160
 
128
161
  Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(m, "IndexIVFFlat")
129
- .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));
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);
130
163
 
131
164
  Rice::define_class_under<faiss::IndexLSH, faiss::Index>(m, "IndexLSH")
132
165
  .define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
@@ -141,7 +174,7 @@ void init_index(Rice::Module& m) {
141
174
  .define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
142
175
 
143
176
  Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(m, "IndexIVFPQ")
144
- .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));
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);
145
178
 
146
179
  Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(m, "IndexIVFPQR")
147
180
  .define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
@@ -150,7 +183,7 @@ void init_index(Rice::Module& m) {
150
183
  .define_constructor(Rice::Constructor<faiss::ParameterSpace>())
151
184
  .define_method(
152
185
  "set_index_parameter",
153
- *[](faiss::ParameterSpace& self, faiss::Index* index, const std::string& name, double val) {
186
+ [](faiss::ParameterSpace& self, faiss::Index* index, const std::string& name, double val) {
154
187
  self.set_index_parameter(index, name, val);
155
188
  });
156
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
  }