faiss 0.1.5 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +12 -0
- data/ext/faiss/ext.cpp +1 -1
- data/ext/faiss/extconf.rb +6 -2
- data/ext/faiss/index.cpp +114 -43
- 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.rb +0 -5
- data/lib/faiss/version.rb +1 -1
- 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 +24 -10
- 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: 01c09476fa378a49da82beddeab101d2c4ac124346260f56fe4dab0312b9c1ba
|
4
|
+
data.tar.gz: 587c6213b479cf979fca6c21c9f4a08cc56825e9403b84fff98e33b6a8759e5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
-
|
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
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
}
|
27
|
-
|
28
|
-
|
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
|
-
|
96
|
+
[](faiss::Index &self) {
|
37
97
|
return self.d;
|
38
98
|
})
|
39
99
|
.define_method(
|
40
100
|
"trained?",
|
41
|
-
|
101
|
+
[](faiss::Index &self) {
|
42
102
|
return self.is_trained;
|
43
103
|
})
|
44
104
|
.define_method(
|
45
105
|
"ntotal",
|
46
|
-
|
106
|
+
[](faiss::Index &self) {
|
47
107
|
return self.ntotal;
|
48
108
|
})
|
49
109
|
.define_method(
|
50
|
-
"
|
51
|
-
|
52
|
-
|
53
|
-
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());
|
54
114
|
})
|
55
115
|
.define_method(
|
56
|
-
"
|
57
|
-
|
58
|
-
|
59
|
-
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());
|
60
120
|
})
|
61
121
|
.define_method(
|
62
|
-
"
|
63
|
-
|
64
|
-
|
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
|
-
|
126
|
+
auto distances = numo::SFloat({n, k});
|
127
|
+
auto labels = numo::Int64({n, k});
|
69
128
|
|
70
|
-
|
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(
|
75
|
-
ret.push(
|
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
|
-
|
143
|
+
[](faiss::Index &self, const char *fname) {
|
81
144
|
faiss::write_index(&self, fname);
|
82
145
|
})
|
83
|
-
.
|
146
|
+
.define_singleton_function(
|
84
147
|
"load",
|
85
|
-
|
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>(),
|
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
|
}
|
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
|
}
|