faiss 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (201) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/LICENSE.txt +18 -18
  4. data/README.md +105 -5
  5. data/ext/faiss/ext.cpp +12 -241
  6. data/ext/faiss/extconf.rb +12 -2
  7. data/ext/faiss/index.cpp +118 -0
  8. data/ext/faiss/index_binary.cpp +81 -0
  9. data/ext/faiss/kmeans.cpp +36 -0
  10. data/ext/faiss/pca_matrix.cpp +34 -0
  11. data/ext/faiss/product_quantizer.cpp +54 -0
  12. data/ext/faiss/utils.cpp +40 -0
  13. data/ext/faiss/utils.h +16 -0
  14. data/lib/faiss/index.rb +3 -3
  15. data/lib/faiss/index_binary.rb +3 -3
  16. data/lib/faiss/kmeans.rb +1 -1
  17. data/lib/faiss/pca_matrix.rb +2 -2
  18. data/lib/faiss/product_quantizer.rb +3 -3
  19. data/lib/faiss/version.rb +1 -1
  20. data/vendor/faiss/LICENSE +21 -0
  21. data/vendor/faiss/faiss/AutoTune.cpp +722 -0
  22. data/vendor/faiss/faiss/AutoTune.h +215 -0
  23. data/vendor/faiss/faiss/Clustering.cpp +531 -0
  24. data/vendor/faiss/faiss/Clustering.h +129 -0
  25. data/vendor/faiss/faiss/IVFlib.cpp +434 -0
  26. data/vendor/faiss/faiss/IVFlib.h +154 -0
  27. data/vendor/faiss/faiss/Index.cpp +170 -0
  28. data/vendor/faiss/faiss/Index.h +242 -0
  29. data/vendor/faiss/faiss/Index2Layer.cpp +437 -0
  30. data/vendor/faiss/faiss/Index2Layer.h +85 -0
  31. data/vendor/faiss/faiss/IndexBinary.cpp +77 -0
  32. data/vendor/faiss/faiss/IndexBinary.h +167 -0
  33. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +88 -0
  34. data/vendor/faiss/faiss/IndexBinaryFlat.h +57 -0
  35. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +79 -0
  36. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +52 -0
  37. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +324 -0
  38. data/vendor/faiss/faiss/IndexBinaryHNSW.h +56 -0
  39. data/vendor/faiss/faiss/IndexBinaryHash.cpp +491 -0
  40. data/vendor/faiss/faiss/IndexBinaryHash.h +117 -0
  41. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +717 -0
  42. data/vendor/faiss/faiss/IndexBinaryIVF.h +221 -0
  43. data/vendor/faiss/faiss/IndexFlat.cpp +360 -0
  44. data/vendor/faiss/faiss/IndexFlat.h +125 -0
  45. data/vendor/faiss/faiss/IndexHNSW.cpp +1172 -0
  46. data/vendor/faiss/faiss/IndexHNSW.h +170 -0
  47. data/vendor/faiss/faiss/IndexIVF.cpp +1075 -0
  48. data/vendor/faiss/faiss/IndexIVF.h +383 -0
  49. data/vendor/faiss/faiss/IndexIVFFlat.cpp +468 -0
  50. data/vendor/faiss/faiss/IndexIVFFlat.h +109 -0
  51. data/vendor/faiss/faiss/IndexIVFPQ.cpp +1255 -0
  52. data/vendor/faiss/faiss/IndexIVFPQ.h +178 -0
  53. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  54. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  55. data/vendor/faiss/faiss/IndexIVFPQR.cpp +220 -0
  56. data/vendor/faiss/faiss/IndexIVFPQR.h +66 -0
  57. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +330 -0
  58. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +75 -0
  59. data/vendor/faiss/faiss/IndexLSH.cpp +225 -0
  60. data/vendor/faiss/faiss/IndexLSH.h +90 -0
  61. data/vendor/faiss/faiss/IndexLattice.cpp +143 -0
  62. data/vendor/faiss/faiss/IndexLattice.h +68 -0
  63. data/vendor/faiss/faiss/IndexPQ.cpp +1204 -0
  64. data/vendor/faiss/faiss/IndexPQ.h +201 -0
  65. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  66. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  67. data/vendor/faiss/faiss/IndexPreTransform.cpp +334 -0
  68. data/vendor/faiss/faiss/IndexPreTransform.h +93 -0
  69. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  70. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  71. data/vendor/faiss/faiss/IndexReplicas.cpp +215 -0
  72. data/vendor/faiss/faiss/IndexReplicas.h +82 -0
  73. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +324 -0
  74. data/vendor/faiss/faiss/IndexScalarQuantizer.h +127 -0
  75. data/vendor/faiss/faiss/IndexShards.cpp +342 -0
  76. data/vendor/faiss/faiss/IndexShards.h +99 -0
  77. data/vendor/faiss/faiss/MatrixStats.cpp +252 -0
  78. data/vendor/faiss/faiss/MatrixStats.h +62 -0
  79. data/vendor/faiss/faiss/MetaIndexes.cpp +352 -0
  80. data/vendor/faiss/faiss/MetaIndexes.h +126 -0
  81. data/vendor/faiss/faiss/MetricType.h +36 -0
  82. data/vendor/faiss/faiss/VectorTransform.cpp +1158 -0
  83. data/vendor/faiss/faiss/VectorTransform.h +322 -0
  84. data/vendor/faiss/faiss/clone_index.cpp +147 -0
  85. data/vendor/faiss/faiss/clone_index.h +38 -0
  86. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +95 -0
  87. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +27 -0
  88. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +407 -0
  89. data/vendor/faiss/faiss/gpu/GpuCloner.h +81 -0
  90. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +28 -0
  91. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +53 -0
  92. data/vendor/faiss/faiss/gpu/GpuDistance.h +153 -0
  93. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +29 -0
  94. data/vendor/faiss/faiss/gpu/GpuIndex.h +156 -0
  95. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +96 -0
  96. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +205 -0
  97. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +106 -0
  98. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +112 -0
  99. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +170 -0
  100. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +128 -0
  101. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +30 -0
  102. data/vendor/faiss/faiss/gpu/GpuResources.cpp +200 -0
  103. data/vendor/faiss/faiss/gpu/GpuResources.h +268 -0
  104. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +605 -0
  105. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +204 -0
  106. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  107. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  108. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +43 -0
  109. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +25 -0
  110. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +71 -0
  111. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +39 -0
  112. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +115 -0
  113. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +139 -0
  114. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +102 -0
  115. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  116. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +130 -0
  117. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +393 -0
  118. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +555 -0
  119. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +651 -0
  120. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  121. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +84 -0
  122. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +317 -0
  123. data/vendor/faiss/faiss/gpu/test/TestUtils.h +126 -0
  124. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +161 -0
  125. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +191 -0
  126. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +214 -0
  127. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +114 -0
  128. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +89 -0
  129. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +59 -0
  130. data/vendor/faiss/faiss/gpu/utils/Timer.h +52 -0
  131. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +323 -0
  132. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +260 -0
  133. data/vendor/faiss/faiss/impl/FaissAssert.h +96 -0
  134. data/vendor/faiss/faiss/impl/FaissException.cpp +92 -0
  135. data/vendor/faiss/faiss/impl/FaissException.h +75 -0
  136. data/vendor/faiss/faiss/impl/HNSW.cpp +815 -0
  137. data/vendor/faiss/faiss/impl/HNSW.h +283 -0
  138. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +953 -0
  139. data/vendor/faiss/faiss/impl/PolysemousTraining.h +158 -0
  140. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +137 -0
  141. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +774 -0
  142. data/vendor/faiss/faiss/impl/ProductQuantizer.h +239 -0
  143. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  144. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +1662 -0
  145. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +126 -0
  146. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +192 -0
  147. data/vendor/faiss/faiss/impl/ThreadedIndex.h +80 -0
  148. data/vendor/faiss/faiss/impl/index_read.cpp +840 -0
  149. data/vendor/faiss/faiss/impl/index_write.cpp +628 -0
  150. data/vendor/faiss/faiss/impl/io.cpp +288 -0
  151. data/vendor/faiss/faiss/impl/io.h +155 -0
  152. data/vendor/faiss/faiss/impl/io_macros.h +57 -0
  153. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +714 -0
  154. data/vendor/faiss/faiss/impl/lattice_Zn.h +199 -0
  155. data/vendor/faiss/faiss/impl/platform_macros.h +68 -0
  156. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  157. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  158. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  159. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  160. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  161. data/vendor/faiss/faiss/index_factory.cpp +517 -0
  162. data/vendor/faiss/faiss/index_factory.h +25 -0
  163. data/vendor/faiss/faiss/index_io.h +82 -0
  164. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  165. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  166. data/vendor/faiss/faiss/invlists/DirectMap.cpp +267 -0
  167. data/vendor/faiss/faiss/invlists/DirectMap.h +120 -0
  168. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +692 -0
  169. data/vendor/faiss/faiss/invlists/InvertedLists.h +366 -0
  170. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  171. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  172. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +827 -0
  173. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +150 -0
  174. data/vendor/faiss/faiss/python/python_callbacks.cpp +112 -0
  175. data/vendor/faiss/faiss/python/python_callbacks.h +52 -0
  176. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  177. data/vendor/faiss/faiss/utils/Heap.cpp +120 -0
  178. data/vendor/faiss/faiss/utils/Heap.h +504 -0
  179. data/vendor/faiss/faiss/utils/WorkerThread.cpp +126 -0
  180. data/vendor/faiss/faiss/utils/WorkerThread.h +61 -0
  181. data/vendor/faiss/faiss/utils/distances.cpp +624 -0
  182. data/vendor/faiss/faiss/utils/distances.h +252 -0
  183. data/vendor/faiss/faiss/utils/distances_simd.cpp +1036 -0
  184. data/vendor/faiss/faiss/utils/extra_distances.cpp +336 -0
  185. data/vendor/faiss/faiss/utils/extra_distances.h +54 -0
  186. data/vendor/faiss/faiss/utils/hamming-inl.h +470 -0
  187. data/vendor/faiss/faiss/utils/hamming.cpp +872 -0
  188. data/vendor/faiss/faiss/utils/hamming.h +241 -0
  189. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  190. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  191. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  192. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  193. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  194. data/vendor/faiss/faiss/utils/random.cpp +192 -0
  195. data/vendor/faiss/faiss/utils/random.h +60 -0
  196. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  197. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  198. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  199. data/vendor/faiss/faiss/utils/utils.cpp +686 -0
  200. data/vendor/faiss/faiss/utils/utils.h +170 -0
  201. metadata +196 -64
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47b973803ea179379ab292d5d7b8350c12a383387e90b96f38eae4e90e20dad3
4
- data.tar.gz: 5697b1ec26dbbb0794e6c11563b0cf76d564391882e7c0c0148f3893e1bf6b59
3
+ metadata.gz: 9d260b6585de456d0df620c322f5366a9ae29a1d275c0200c726233f5c5156aa
4
+ data.tar.gz: 1f463792926335213ae59a56c7271593fa9eaf93d4705fa936da02d74127a571
5
5
  SHA512:
6
- metadata.gz: dccf5a8ddfd4030e70308c3e71b16bb6742839b8b1a263d3a010a381729af869c1755164e25a3941a3f475376e783ec42b43409fede77010e97949092a424e18
7
- data.tar.gz: 57c933bc9d3ffbec37be0edbf641bd004e8dbfa13c04d52ca970e8ff03f719f65f27016892b06cd66755f36afc8d3ff505e6062656f2c48fa20d58cf64c2b6fd
6
+ metadata.gz: 70960a3c2244b8d8dceaa7a2cc98a04b8f765c783ff2d1879d61e2303f7a7337490b67a9a6a56d3f0a4e19939812418122394633f2a68c198a6e0776a3a2b29a
7
+ data.tar.gz: 0bc2edfdc86a7d3894923dfd98fe6db7dfa8a15cf640340198f92f7ef967b9548ed7a18e80d886018d8f621da01f73c1b061a85cf892318327144209d1a4ed87
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ ## 0.1.5 (2021-03-14)
2
+
3
+ - Added `--with-optflags` option
4
+ - Added support for inner product to `IndexHNSWFlat`
5
+
6
+ ## 0.1.4 (2021-02-04)
7
+
8
+ - Updated Faiss to 1.7.0
9
+
10
+ ## 0.1.3 (2020-10-22)
11
+
12
+ - Updated Faiss to 1.6.4
13
+
14
+ ## 0.1.2 (2020-08-17)
15
+
16
+ - Updated Faiss to 1.6.3
17
+
18
+ ## 0.1.1 (2020-03-09)
19
+
20
+ - Vendored library
21
+ - Added `save` and `load` methods
22
+
1
23
  ## 0.1.0 (2020-03-08)
2
24
 
3
25
  - First release
data/LICENSE.txt CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2020 Andrew Kane
2
-
3
1
  MIT License
4
2
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
3
+ Copyright (c) Facebook, Inc. and its affiliates.
4
+ Copyright (c) 2020-2021 Andrew Kane
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
12
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
15
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md CHANGED
@@ -2,12 +2,20 @@
2
2
 
3
3
  [Faiss](https://github.com/facebookresearch/faiss) - efficient similarity search and clustering - for Ruby
4
4
 
5
+ Learn more about [Faiss](https://engineering.fb.com/data-infrastructure/faiss-a-library-for-efficient-similarity-search/)
6
+
7
+ [![Build Status](https://github.com/ankane/faiss/workflows/build/badge.svg?branch=master)](https://github.com/ankane/faiss/actions)
8
+
5
9
  ## Installation
6
10
 
7
- First, install the [Faiss C++ library](https://github.com/facebookresearch/faiss/blob/master/INSTALL.md). For Homebrew, use:
11
+ First, install BLAS, LAPACK, and OpenMP:
8
12
 
9
13
  ```sh
10
- brew install faiss
14
+ # Mac
15
+ brew install openblas lapack libomp
16
+
17
+ # Ubuntu
18
+ sudo apt install libblas-dev liblapack-dev
11
19
  ```
12
20
 
13
21
  Add this line to your application’s Gemfile:
@@ -16,6 +24,8 @@ Add this line to your application’s Gemfile:
16
24
  gem 'faiss'
17
25
  ```
18
26
 
27
+ It can take a few minutes to compile the gem. Faiss is not available for Windows.
28
+
19
29
  ## Getting Started
20
30
 
21
31
  Prep your data
@@ -41,6 +51,84 @@ Search
41
51
  distances, ids = index.search(objects, 3)
42
52
  ```
43
53
 
54
+ Save an index
55
+
56
+ ```ruby
57
+ index.save("index.bin")
58
+ ```
59
+
60
+ Load an index
61
+
62
+ ```ruby
63
+ index = Faiss::Index.load("index.bin")
64
+ ```
65
+
66
+ > Use `Faiss::IndexBinary` to load binary indexes
67
+
68
+ ## Basic Indexes
69
+
70
+ Exact search for L2
71
+
72
+ ```rb
73
+ Faiss::IndexFlatL2.new(d)
74
+ ```
75
+
76
+ Exact search for inner product
77
+
78
+ ```rb
79
+ Faiss::IndexFlatIP.new(d)
80
+ ```
81
+
82
+ Hierarchical navigable small world graph exploration
83
+
84
+ ```rb
85
+ Faiss::IndexHNSWFlat.new(d, m)
86
+ ```
87
+
88
+ Inverted file with exact post-verification
89
+
90
+ ```rb
91
+ Faiss::IndexIVFFlat.new(quantizer, d, nlists)
92
+ ```
93
+
94
+ Locality-sensitive hashing
95
+
96
+ ```rb
97
+ Faiss::IndexLSH.new(d, nbits)
98
+ ```
99
+
100
+ Product quantizer (PQ) in flat mode
101
+
102
+ ```rb
103
+ Faiss::IndexPQ.new(d, m, nbits)
104
+ ```
105
+
106
+ IVFADC (coarse quantizer+PQ on residuals)
107
+
108
+ ```rb
109
+ Faiss::IndexIVFPQ.new(quantizer, d, nlists, m, nbits)
110
+ ```
111
+
112
+ IVFADC+R (same as IVFADC with re-ranking based on codes)
113
+
114
+ ```rb
115
+ Faiss::IndexIVFPQR.new(quantizer, d, nlists, m, nbits, m_refine, nbits_refine)
116
+ ```
117
+
118
+ ## Binary Indexes
119
+
120
+ Index binary vectors
121
+
122
+ ```rb
123
+ Faiss::IndexBinaryFlat.new(d)
124
+ ```
125
+
126
+ Speed up search with an inverse vector file
127
+
128
+ ```rb
129
+ Faiss::IndexBinaryIVF.new(quantizer, d, nlists)
130
+ ```
131
+
44
132
  ## K-means Clustering
45
133
 
46
134
  Train
@@ -92,6 +180,18 @@ Decode
92
180
  pq.decode(codes)
93
181
  ```
94
182
 
183
+ Save a quantizer
184
+
185
+ ```ruby
186
+ pq.save("pq.bin")
187
+ ```
188
+
189
+ Load a quantizer
190
+
191
+ ```ruby
192
+ pq = Faiss::ProductQuantizer.load("pq.bin")
193
+ ```
194
+
95
195
  ## Data
96
196
 
97
197
  Data can be an array of arrays
@@ -100,10 +200,10 @@ Data can be an array of arrays
100
200
  [[1, 2, 3], [4, 5, 6]]
101
201
  ```
102
202
 
103
- Or a Numo NArray
203
+ Or a Numo array
104
204
 
105
205
  ```ruby
106
- Numo::SFloat.new(3, 2).seq
206
+ Numo::NArray.cast([[1, 2, 3], [4, 5, 6]])
107
207
  ```
108
208
 
109
209
  ## History
@@ -122,7 +222,7 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
122
222
  To get started with development:
123
223
 
124
224
  ```sh
125
- git clone https://github.com/ankane/faiss.git
225
+ git clone --recursive https://github.com/ankane/faiss.git
126
226
  cd faiss
127
227
  bundle install
128
228
  bundle exec rake compile
data/ext/faiss/ext.cpp CHANGED
@@ -1,248 +1,19 @@
1
- #include <faiss/IndexFlat.h>
2
- #include <faiss/IndexHNSW.h>
3
- #include <faiss/IndexIVFFlat.h>
4
- #include <faiss/IndexLSH.h>
5
- #include <faiss/IndexScalarQuantizer.h>
6
- #include <faiss/IndexPQ.h>
7
- #include <faiss/IndexIVFPQ.h>
8
- #include <faiss/IndexIVFPQR.h>
9
-
10
- #include <faiss/IndexBinaryFlat.h>
11
- #include <faiss/IndexBinaryIVF.h>
12
- #include <faiss/index_factory.h>
13
-
14
- #include <faiss/Clustering.h>
15
- #include <faiss/VectorTransform.h>
16
-
17
- #include <rice/Array.hpp>
18
- #include <rice/Class.hpp>
19
- #include <rice/Constructor.hpp>
20
1
  #include <rice/Module.hpp>
21
2
 
3
+ void init_index(Rice::Module& m);
4
+ void init_index_binary(Rice::Module& m);
5
+ void init_kmeans(Rice::Module& m);
6
+ void init_pca_matrix(Rice::Module& m);
7
+ void init_product_quantizer(Rice::Module& m);
8
+
22
9
  extern "C"
23
10
  void Init_ext()
24
11
  {
25
- Rice::Module rb_mFaiss = Rice::define_module("Faiss")
26
- .define_singleton_method(
27
- "index_binary_factory",
28
- *[](int d, const char *description) {
29
- return faiss::index_binary_factory(d, description);
30
- });
31
-
32
- Rice::define_class_under<faiss::Index>(rb_mFaiss, "Index")
33
- .define_method(
34
- "d",
35
- *[](faiss::Index &self) {
36
- return self.d;
37
- })
38
- .define_method(
39
- "trained?",
40
- *[](faiss::Index &self) {
41
- return self.is_trained;
42
- })
43
- .define_method(
44
- "ntotal",
45
- *[](faiss::Index &self) {
46
- return self.ntotal;
47
- })
48
- .define_method(
49
- "_train",
50
- *[](faiss::Index &self, int64_t n, Rice::String s) {
51
- const float *x = (float*) s.c_str();
52
- self.train(n, x);
53
- })
54
- .define_method(
55
- "_add",
56
- *[](faiss::Index &self, int64_t n, Rice::String s) {
57
- const float *x = (float*) s.c_str();
58
- self.add(n, x);
59
- })
60
- .define_method(
61
- "_search",
62
- *[](faiss::Index &self, int64_t n, Rice::String s, int64_t k) {
63
- const float *x = (float*) s.c_str();
64
- float *distances = new float[k * n];
65
- int64_t *labels = new int64_t[k * n];
66
-
67
- self.search(n, x, k, distances, labels);
68
-
69
- auto dstr = std::string((char*) distances, k * n * sizeof(float));
70
- auto lstr = std::string((char*) labels, k * n * sizeof(int64_t));
71
-
72
- Rice::Array ret;
73
- ret.push(dstr);
74
- ret.push(lstr);
75
- return ret;
76
- });
77
-
78
- Rice::define_class_under<faiss::IndexBinary>(rb_mFaiss, "IndexBinary")
79
- .define_method(
80
- "d",
81
- *[](faiss::Index &self) {
82
- return self.d;
83
- })
84
- .define_method(
85
- "trained?",
86
- *[](faiss::IndexBinary &self) {
87
- return self.is_trained;
88
- })
89
- .define_method(
90
- "ntotal",
91
- *[](faiss::IndexBinary &self) {
92
- return self.ntotal;
93
- })
94
- .define_method(
95
- "_train",
96
- *[](faiss::IndexBinary &self, int64_t n, Rice::String s) {
97
- const uint8_t *x = (uint8_t*) s.c_str();
98
- self.train(n, x);
99
- })
100
- .define_method(
101
- "_add",
102
- *[](faiss::IndexBinary &self, int64_t n, Rice::String s) {
103
- const uint8_t *x = (uint8_t*) s.c_str();
104
- self.add(n, x);
105
- })
106
- .define_method(
107
- "_search",
108
- *[](faiss::IndexBinary &self, int64_t n, Rice::String s, int64_t k) {
109
- const uint8_t *x = (uint8_t*) s.c_str();
110
- int32_t *distances = new int32_t[k * n];
111
- int64_t *labels = new int64_t[k * n];
112
-
113
- self.search(n, x, k, distances, labels);
114
-
115
- auto dstr = std::string((char*) distances, k * n * sizeof(int32_t));
116
- auto lstr = std::string((char*) labels, k * n * sizeof(int64_t));
117
-
118
- Rice::Array ret;
119
- ret.push(dstr);
120
- ret.push(lstr);
121
- return ret;
122
- });
123
-
124
- Rice::define_class_under<faiss::IndexFlatL2, faiss::Index>(rb_mFaiss, "IndexFlatL2")
125
- .define_constructor(Rice::Constructor<faiss::IndexFlatL2, int64_t>());
126
-
127
- Rice::define_class_under<faiss::IndexFlatIP, faiss::Index>(rb_mFaiss, "IndexFlatIP")
128
- .define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
129
-
130
- Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(rb_mFaiss, "IndexHNSWFlat")
131
- .define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int>());
132
-
133
- Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(rb_mFaiss, "IndexIVFFlat")
134
- .define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t>());
135
-
136
- Rice::define_class_under<faiss::IndexLSH, faiss::Index>(rb_mFaiss, "IndexLSH")
137
- .define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
138
-
139
- Rice::define_class_under<faiss::IndexScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexScalarQuantizer")
140
- .define_constructor(Rice::Constructor<faiss::IndexScalarQuantizer, int, faiss::ScalarQuantizer::QuantizerType>());
141
-
142
- Rice::define_class_under<faiss::IndexPQ, faiss::Index>(rb_mFaiss, "IndexPQ")
143
- .define_constructor(Rice::Constructor<faiss::IndexPQ, int, size_t, size_t>());
144
-
145
- Rice::define_class_under<faiss::IndexIVFScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexIVFScalarQuantizer")
146
- .define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
147
-
148
- Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(rb_mFaiss, "IndexIVFPQ")
149
- .define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t>());
150
-
151
- Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(rb_mFaiss, "IndexIVFPQR")
152
- .define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
153
-
154
- Rice::define_class_under<faiss::IndexBinaryFlat, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryFlat")
155
- .define_constructor(Rice::Constructor<faiss::IndexBinaryFlat, int64_t>());
156
-
157
- Rice::define_class_under<faiss::IndexBinaryIVF, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryIVF")
158
- .define_constructor(Rice::Constructor<faiss::IndexBinaryIVF, faiss::IndexBinary*, size_t, size_t>());
159
-
160
- Rice::define_class_under<faiss::Clustering>(rb_mFaiss, "Kmeans")
161
- .define_constructor(Rice::Constructor<faiss::Clustering, int, int>())
162
- .define_method(
163
- "d",
164
- *[](faiss::Clustering &self) {
165
- return self.d;
166
- })
167
- .define_method(
168
- "k",
169
- *[](faiss::Clustering &self) {
170
- return self.k;
171
- })
172
- .define_method(
173
- "_centroids",
174
- *[](faiss::Clustering &self) {
175
- float *centroids = new float[self.k * self.d];
176
- for (size_t i = 0; i < self.centroids.size(); i++) {
177
- centroids[i] = self.centroids[i];
178
- }
179
- return std::string((char*) centroids, self.k * self.d * sizeof(float));
180
- })
181
- .define_method(
182
- "_train",
183
- *[](faiss::Clustering &self, int64_t n, Rice::String s, faiss::Index & index) {
184
- const float *x = (float*) s.c_str();
185
- self.train(n, x, index);
186
- });
187
-
188
- Rice::define_class_under<faiss::PCAMatrix>(rb_mFaiss, "PCAMatrix")
189
- .define_constructor(Rice::Constructor<faiss::PCAMatrix, int, int>())
190
- .define_method(
191
- "d_in",
192
- *[](faiss::PCAMatrix &self) {
193
- return self.d_in;
194
- })
195
- .define_method(
196
- "d_out",
197
- *[](faiss::PCAMatrix &self) {
198
- return self.d_out;
199
- })
200
- .define_method(
201
- "_train",
202
- *[](faiss::PCAMatrix &self, int64_t n, Rice::String s) {
203
- const float *x = (float*) s.c_str();
204
- self.train(n, x);
205
- })
206
- .define_method(
207
- "_apply",
208
- *[](faiss::PCAMatrix &self, int64_t n, Rice::String s) {
209
- const float *x = (float*) s.c_str();
210
- float* res = self.apply(n, x);
211
- return std::string((char*) res, n * self.d_out * sizeof(float));
212
- });
12
+ auto m = Rice::define_module("Faiss");
213
13
 
214
- Rice::define_class_under<faiss::ProductQuantizer>(rb_mFaiss, "ProductQuantizer")
215
- .define_constructor(Rice::Constructor<faiss::ProductQuantizer, size_t, size_t, size_t>())
216
- .define_method(
217
- "d",
218
- *[](faiss::ProductQuantizer &self) {
219
- return self.d;
220
- })
221
- .define_method(
222
- "m",
223
- *[](faiss::ProductQuantizer &self) {
224
- return self.M;
225
- })
226
- .define_method(
227
- "_train",
228
- *[](faiss::ProductQuantizer &self, int n, Rice::String s) {
229
- const float *x = (float*) s.c_str();
230
- self.train(n, x);
231
- })
232
- .define_method(
233
- "_compute_codes",
234
- *[](faiss::ProductQuantizer &self, int n, Rice::String s) {
235
- const float *x = (float*) s.c_str();
236
- uint8_t *codes = new uint8_t[n * self.M];
237
- self.compute_codes(x, codes, n);
238
- return std::string((char*) codes, n * self.M * sizeof(uint8_t));
239
- })
240
- .define_method(
241
- "_decode",
242
- *[](faiss::ProductQuantizer &self, int n, Rice::String s) {
243
- const uint8_t *codes = (uint8_t*) s.c_str();
244
- float *x = new float[n * self.d];
245
- self.decode(codes, x, n);
246
- return std::string((char*) x, n * self.d * sizeof(float));
247
- });
14
+ init_index(m);
15
+ init_index_binary(m);
16
+ init_kmeans(m);
17
+ init_pca_matrix(m);
18
+ init_product_quantizer(m);
248
19
  }