faiss 0.1.7 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +7 -7
- data/ext/faiss/ext.cpp +1 -1
- data/ext/faiss/extconf.rb +8 -2
- data/ext/faiss/index.cpp +102 -69
- data/ext/faiss/index_binary.cpp +24 -30
- data/ext/faiss/kmeans.cpp +20 -16
- data/ext/faiss/numo.hpp +867 -0
- data/ext/faiss/pca_matrix.cpp +13 -14
- data/ext/faiss/product_quantizer.cpp +23 -24
- data/ext/faiss/utils.cpp +10 -37
- data/ext/faiss/utils.h +2 -13
- data/lib/faiss/version.rb +1 -1
- data/lib/faiss.rb +0 -5
- data/vendor/faiss/faiss/AutoTune.cpp +292 -291
- data/vendor/faiss/faiss/AutoTune.h +55 -56
- data/vendor/faiss/faiss/Clustering.cpp +334 -195
- data/vendor/faiss/faiss/Clustering.h +88 -35
- data/vendor/faiss/faiss/IVFlib.cpp +171 -195
- data/vendor/faiss/faiss/IVFlib.h +48 -51
- data/vendor/faiss/faiss/Index.cpp +85 -103
- data/vendor/faiss/faiss/Index.h +54 -48
- data/vendor/faiss/faiss/Index2Layer.cpp +139 -164
- data/vendor/faiss/faiss/Index2Layer.h +22 -22
- data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
- data/vendor/faiss/faiss/IndexBinary.h +140 -132
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
- data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
- data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
- data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
- data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
- data/vendor/faiss/faiss/IndexFlat.cpp +116 -147
- data/vendor/faiss/faiss/IndexFlat.h +35 -46
- data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
- data/vendor/faiss/faiss/IndexHNSW.h +57 -41
- data/vendor/faiss/faiss/IndexIVF.cpp +474 -454
- data/vendor/faiss/faiss/IndexIVF.h +146 -113
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +248 -250
- data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +457 -516
- data/vendor/faiss/faiss/IndexIVFPQ.h +74 -66
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
- data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +125 -133
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +19 -21
- data/vendor/faiss/faiss/IndexLSH.cpp +75 -96
- data/vendor/faiss/faiss/IndexLSH.h +21 -26
- data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
- data/vendor/faiss/faiss/IndexLattice.h +11 -16
- data/vendor/faiss/faiss/IndexNNDescent.cpp +231 -0
- data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
- data/vendor/faiss/faiss/IndexNSG.cpp +303 -0
- data/vendor/faiss/faiss/IndexNSG.h +85 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +405 -464
- data/vendor/faiss/faiss/IndexPQ.h +64 -67
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
- data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
- data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
- data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
- data/vendor/faiss/faiss/IndexRefine.cpp +115 -131
- data/vendor/faiss/faiss/IndexRefine.h +22 -23
- data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
- data/vendor/faiss/faiss/IndexReplicas.h +62 -56
- data/vendor/faiss/faiss/IndexResidual.cpp +291 -0
- data/vendor/faiss/faiss/IndexResidual.h +152 -0
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +120 -155
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -45
- data/vendor/faiss/faiss/IndexShards.cpp +256 -240
- data/vendor/faiss/faiss/IndexShards.h +85 -73
- data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
- data/vendor/faiss/faiss/MatrixStats.h +7 -10
- data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
- data/vendor/faiss/faiss/MetaIndexes.h +40 -34
- data/vendor/faiss/faiss/MetricType.h +7 -7
- data/vendor/faiss/faiss/VectorTransform.cpp +652 -474
- data/vendor/faiss/faiss/VectorTransform.h +61 -89
- data/vendor/faiss/faiss/clone_index.cpp +77 -73
- data/vendor/faiss/faiss/clone_index.h +4 -9
- data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
- data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +197 -170
- data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
- data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
- data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
- data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
- data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
- data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
- data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
- data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
- data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
- data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
- data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
- data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +270 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +115 -0
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
- data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
- data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
- data/vendor/faiss/faiss/impl/FaissException.h +41 -29
- data/vendor/faiss/faiss/impl/HNSW.cpp +595 -611
- data/vendor/faiss/faiss/impl/HNSW.h +179 -200
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +672 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +172 -0
- data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
- data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
- data/vendor/faiss/faiss/impl/NSG.cpp +682 -0
- data/vendor/faiss/faiss/impl/NSG.h +199 -0
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
- data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
- data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +448 -0
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +130 -0
- data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +648 -701
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
- data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
- data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
- data/vendor/faiss/faiss/impl/index_read.cpp +547 -479
- data/vendor/faiss/faiss/impl/index_write.cpp +497 -407
- data/vendor/faiss/faiss/impl/io.cpp +75 -94
- data/vendor/faiss/faiss/impl/io.h +31 -41
- data/vendor/faiss/faiss/impl/io_macros.h +40 -29
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
- data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
- data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
- data/vendor/faiss/faiss/index_factory.cpp +269 -218
- data/vendor/faiss/faiss/index_factory.h +6 -7
- data/vendor/faiss/faiss/index_io.h +23 -26
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
- data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
- data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
- data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
- data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
- data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
- data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
- data/vendor/faiss/faiss/utils/Heap.h +186 -209
- data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
- data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
- data/vendor/faiss/faiss/utils/distances.cpp +301 -310
- data/vendor/faiss/faiss/utils/distances.h +133 -118
- data/vendor/faiss/faiss/utils/distances_simd.cpp +456 -516
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
- data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
- data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
- data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
- data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
- data/vendor/faiss/faiss/utils/hamming.h +62 -85
- data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
- data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
- data/vendor/faiss/faiss/utils/partitioning.h +26 -21
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
- data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
- data/vendor/faiss/faiss/utils/random.cpp +39 -63
- data/vendor/faiss/faiss/utils/random.h +13 -16
- data/vendor/faiss/faiss/utils/simdlib.h +4 -2
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
- data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
- data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
- data/vendor/faiss/faiss/utils/utils.cpp +304 -287
- data/vendor/faiss/faiss/utils/utils.h +53 -48
- metadata +26 -12
- data/lib/faiss/index.rb +0 -20
- data/lib/faiss/index_binary.rb +0 -20
- data/lib/faiss/kmeans.rb +0 -15
- data/lib/faiss/pca_matrix.rb +0 -15
- data/lib/faiss/product_quantizer.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 106966f6d5e7f6a3f5237a2ebb59912304bbe319ba2891c45166c753fc0e9df1
|
4
|
+
data.tar.gz: 3d72777c777d75beb15c09cb223c4e63f188b7e89fab06d6fa9268bc2be4ff59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
[](https://github.com/ankane/faiss/actions)
|
7
|
+
[](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
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
|
-
|
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
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
96
|
+
[](faiss::Index &self) {
|
62
97
|
return self.d;
|
63
98
|
})
|
64
99
|
.define_method(
|
65
100
|
"trained?",
|
66
|
-
|
101
|
+
[](faiss::Index &self) {
|
67
102
|
return self.is_trained;
|
68
103
|
})
|
69
104
|
.define_method(
|
70
105
|
"ntotal",
|
71
|
-
|
106
|
+
[](faiss::Index &self) {
|
72
107
|
return self.ntotal;
|
73
108
|
})
|
74
109
|
.define_method(
|
75
|
-
"
|
76
|
-
|
77
|
-
|
78
|
-
self.train(n,
|
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
|
-
"
|
82
|
-
|
83
|
-
|
84
|
-
self.add(n,
|
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
|
-
"
|
88
|
-
|
89
|
-
|
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
|
-
|
126
|
+
auto distances = numo::SFloat({n, k});
|
127
|
+
auto labels = numo::Int64({n, k});
|
94
128
|
|
95
|
-
|
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(
|
100
|
-
ret.push(
|
132
|
+
ret.push(distances);
|
133
|
+
ret.push(labels);
|
101
134
|
return ret;
|
102
135
|
})
|
103
136
|
.define_method(
|
104
137
|
"nprobe=",
|
105
|
-
|
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
|
-
|
143
|
+
[](faiss::Index &self, const char *fname) {
|
111
144
|
faiss::write_index(&self, fname);
|
112
145
|
})
|
113
|
-
.
|
146
|
+
.define_singleton_function(
|
114
147
|
"load",
|
115
|
-
|
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>(),
|
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>(),
|
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>(),
|
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
|
-
|
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
|
}
|
data/ext/faiss/index_binary.cpp
CHANGED
@@ -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
|
-
|
13
|
+
[](faiss::IndexBinary &self) {
|
18
14
|
return self.d;
|
19
15
|
})
|
20
16
|
.define_method(
|
21
17
|
"trained?",
|
22
|
-
|
18
|
+
[](faiss::IndexBinary &self) {
|
23
19
|
return self.is_trained;
|
24
20
|
})
|
25
21
|
.define_method(
|
26
22
|
"ntotal",
|
27
|
-
|
23
|
+
[](faiss::IndexBinary &self) {
|
28
24
|
return self.ntotal;
|
29
25
|
})
|
30
26
|
.define_method(
|
31
|
-
"
|
32
|
-
|
33
|
-
|
34
|
-
self.train(n,
|
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
|
-
"
|
38
|
-
|
39
|
-
|
40
|
-
self.add(n,
|
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
|
-
"
|
44
|
-
|
45
|
-
|
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
|
-
|
43
|
+
auto distances = numo::Int32({n, k});
|
44
|
+
auto labels = numo::Int64({n, k});
|
50
45
|
|
51
|
-
|
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(
|
56
|
-
ret.push(
|
49
|
+
ret.push(distances);
|
50
|
+
ret.push(labels);
|
57
51
|
return ret;
|
58
52
|
})
|
59
53
|
.define_method(
|
60
54
|
"save",
|
61
|
-
|
55
|
+
[](faiss::IndexBinary &self, const char *fname) {
|
62
56
|
faiss::write_index_binary(&self, fname);
|
63
57
|
})
|
64
|
-
.
|
58
|
+
.define_singleton_function(
|
65
59
|
"load",
|
66
|
-
|
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.
|
70
|
+
m.define_singleton_function(
|
77
71
|
"index_binary_factory",
|
78
|
-
|
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
|
-
|
16
|
+
[](faiss::Clustering &self) {
|
14
17
|
return self.d;
|
15
18
|
})
|
16
19
|
.define_method(
|
17
20
|
"k",
|
18
|
-
|
21
|
+
[](faiss::Clustering &self) {
|
19
22
|
return self.k;
|
20
23
|
})
|
21
24
|
.define_method(
|
22
|
-
"
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
"
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
}
|