faiss 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/LICENSE.txt +1 -1
- data/README.md +16 -4
- data/ext/faiss/ext.cpp +12 -308
- data/ext/faiss/extconf.rb +6 -3
- data/ext/faiss/index.cpp +189 -0
- data/ext/faiss/index_binary.cpp +75 -0
- data/ext/faiss/kmeans.cpp +40 -0
- data/ext/faiss/numo.hpp +867 -0
- data/ext/faiss/pca_matrix.cpp +33 -0
- data/ext/faiss/product_quantizer.cpp +53 -0
- data/ext/faiss/utils.cpp +13 -0
- data/ext/faiss/utils.h +5 -0
- data/lib/faiss.rb +0 -5
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +36 -33
- data/vendor/faiss/faiss/AutoTune.h +6 -3
- data/vendor/faiss/faiss/Clustering.cpp +16 -12
- data/vendor/faiss/faiss/Index.cpp +3 -4
- data/vendor/faiss/faiss/Index.h +3 -3
- data/vendor/faiss/faiss/IndexBinary.cpp +3 -4
- data/vendor/faiss/faiss/IndexBinary.h +1 -1
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -12
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +1 -2
- data/vendor/faiss/faiss/IndexFlat.cpp +0 -148
- data/vendor/faiss/faiss/IndexFlat.h +0 -51
- data/vendor/faiss/faiss/IndexHNSW.cpp +4 -5
- data/vendor/faiss/faiss/IndexIVF.cpp +118 -31
- data/vendor/faiss/faiss/IndexIVF.h +22 -15
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -3
- data/vendor/faiss/faiss/IndexIVFFlat.h +2 -1
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +39 -15
- data/vendor/faiss/faiss/IndexIVFPQ.h +25 -9
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +8 -9
- data/vendor/faiss/faiss/IndexIVFPQR.h +2 -1
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -2
- data/vendor/faiss/faiss/IndexPQ.cpp +34 -18
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
- data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
- data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -0
- data/vendor/faiss/faiss/IndexPreTransform.h +2 -0
- data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
- data/vendor/faiss/faiss/IndexRefine.h +73 -0
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -2
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +1 -1
- data/vendor/faiss/faiss/gpu/GpuDistance.h +1 -1
- data/vendor/faiss/faiss/gpu/GpuIndex.h +16 -9
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +8 -1
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -11
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +19 -2
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +28 -2
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +24 -14
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +29 -2
- data/vendor/faiss/faiss/gpu/GpuResources.h +4 -0
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +60 -27
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +28 -6
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +3 -3
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +3 -2
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +7 -2
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +5 -1
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +33 -0
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -0
- data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +6 -0
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +5 -6
- data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +5 -4
- data/vendor/faiss/faiss/impl/HNSW.cpp +2 -4
- data/vendor/faiss/faiss/impl/PolysemousTraining.h +4 -4
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +22 -12
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -0
- data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -19
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +6 -0
- data/vendor/faiss/faiss/impl/index_read.cpp +64 -96
- data/vendor/faiss/faiss/impl/index_write.cpp +34 -25
- data/vendor/faiss/faiss/impl/io.cpp +33 -2
- data/vendor/faiss/faiss/impl/io.h +7 -2
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -15
- data/vendor/faiss/faiss/impl/platform_macros.h +44 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
- data/vendor/faiss/faiss/index_factory.cpp +112 -7
- data/vendor/faiss/faiss/index_io.h +1 -48
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
- data/vendor/faiss/faiss/{DirectMap.cpp → invlists/DirectMap.cpp} +1 -1
- data/vendor/faiss/faiss/{DirectMap.h → invlists/DirectMap.h} +1 -1
- data/vendor/faiss/faiss/{InvertedLists.cpp → invlists/InvertedLists.cpp} +72 -1
- data/vendor/faiss/faiss/{InvertedLists.h → invlists/InvertedLists.h} +32 -1
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
- data/vendor/faiss/faiss/{OnDiskInvertedLists.cpp → invlists/OnDiskInvertedLists.cpp} +21 -6
- data/vendor/faiss/faiss/{OnDiskInvertedLists.h → invlists/OnDiskInvertedLists.h} +5 -2
- data/vendor/faiss/faiss/python/python_callbacks.h +8 -1
- data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
- data/vendor/faiss/faiss/utils/Heap.cpp +2 -4
- data/vendor/faiss/faiss/utils/Heap.h +61 -50
- data/vendor/faiss/faiss/utils/distances.cpp +164 -319
- data/vendor/faiss/faiss/utils/distances.h +28 -20
- data/vendor/faiss/faiss/utils/distances_simd.cpp +277 -49
- data/vendor/faiss/faiss/utils/extra_distances.cpp +1 -2
- data/vendor/faiss/faiss/utils/hamming-inl.h +4 -4
- data/vendor/faiss/faiss/utils/hamming.cpp +3 -6
- data/vendor/faiss/faiss/utils/hamming.h +2 -7
- data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
- data/vendor/faiss/faiss/utils/partitioning.h +69 -0
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
- data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
- data/vendor/faiss/faiss/utils/simdlib.h +31 -0
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
- data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
- metadata +54 -149
- 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
- data/vendor/faiss/benchs/bench_6bit_codec.cpp +0 -80
- data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
- data/vendor/faiss/c_api/AutoTune_c.h +0 -66
- data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
- data/vendor/faiss/c_api/Clustering_c.h +0 -123
- data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
- data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
- data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
- data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
- data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
- data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
- data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
- data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
- data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
- data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
- data/vendor/faiss/c_api/IndexShards_c.cpp +0 -38
- data/vendor/faiss/c_api/IndexShards_c.h +0 -39
- data/vendor/faiss/c_api/Index_c.cpp +0 -105
- data/vendor/faiss/c_api/Index_c.h +0 -183
- data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
- data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
- data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
- data/vendor/faiss/c_api/clone_index_c.h +0 -32
- data/vendor/faiss/c_api/error_c.h +0 -42
- data/vendor/faiss/c_api/error_impl.cpp +0 -27
- data/vendor/faiss/c_api/error_impl.h +0 -16
- data/vendor/faiss/c_api/faiss_c.h +0 -58
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -98
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
- data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
- data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
- data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
- data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
- data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
- data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
- data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
- data/vendor/faiss/c_api/index_factory_c.h +0 -30
- data/vendor/faiss/c_api/index_io_c.cpp +0 -42
- data/vendor/faiss/c_api/index_io_c.h +0 -50
- data/vendor/faiss/c_api/macros_impl.h +0 -110
- data/vendor/faiss/demos/demo_imi_flat.cpp +0 -154
- data/vendor/faiss/demos/demo_imi_pq.cpp +0 -203
- data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -151
- data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
- data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
- data/vendor/faiss/misc/test_blas.cpp +0 -87
- data/vendor/faiss/tests/test_binary_flat.cpp +0 -62
- data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -188
- data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -70
- data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -100
- data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -573
- data/vendor/faiss/tests/test_merge.cpp +0 -260
- data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
- data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -225
- data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -193
- data/vendor/faiss/tests/test_params_override.cpp +0 -236
- data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
- data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -246
- data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
- data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
- data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -104
- data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -85
- data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -98
- data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -122
- data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +0 -104
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04246f7b765dc5680223f91ecc5c0951f6328411dcaf443c4ae88819c17b26a7
|
4
|
+
data.tar.gz: 81698c65e8cca4d7794253775ef06f6e84af005b8dac2872d5d3057a868638ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ad4dcb525a55f0b6743fddeb856d6132c8a518eb92be3dc2ce57302ff05b921fb3ba517aecc76bde78aa6e8bf11d9199fa97cbfe4f3a00e4d466d47f0cabe14
|
7
|
+
data.tar.gz: b333df087e863c8ee828b66c1611d8576976064419403710b0b238346d46cc58e69f1c2c708e35bdbc995754b02f50a976389bb0eb102273e7b9f92d771ac395
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,28 @@
|
|
1
|
+
## 0.2.0 (2021-05-23)
|
2
|
+
|
3
|
+
- Improved performance
|
4
|
+
- Updated to Rice 4
|
5
|
+
- Dropped support for Ruby < 2.6
|
6
|
+
|
7
|
+
## 0.1.7 (2021-03-26)
|
8
|
+
|
9
|
+
- Added `IndexScalarQuantizer` and `IndexIVFScalarQuantizer`
|
10
|
+
|
11
|
+
## 0.1.6 (2021-03-21)
|
12
|
+
|
13
|
+
- Added support for inner product to `IndexIVFFlat` and `IndexIVFPQ`
|
14
|
+
- Added `ParameterSpace`
|
15
|
+
- Added `nprobe=` method
|
16
|
+
|
17
|
+
## 0.1.5 (2021-03-14)
|
18
|
+
|
19
|
+
- Added `--with-optflags` option
|
20
|
+
- Added support for inner product to `IndexHNSWFlat`
|
21
|
+
|
22
|
+
## 0.1.4 (2021-02-04)
|
23
|
+
|
24
|
+
- Updated Faiss to 1.7.0
|
25
|
+
|
1
26
|
## 0.1.3 (2020-10-22)
|
2
27
|
|
3
28
|
- Updated Faiss to 1.6.4
|
data/LICENSE.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
MIT License
|
2
2
|
|
3
3
|
Copyright (c) Facebook, Inc. and its affiliates.
|
4
|
-
Copyright (c) 2020 Andrew Kane
|
4
|
+
Copyright (c) 2020-2021 Andrew Kane
|
5
5
|
|
6
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
7
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
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://
|
7
|
+
[![Build Status](https://github.com/ankane/faiss/workflows/build/badge.svg?branch=master)](https://github.com/ankane/faiss/actions)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -24,7 +24,7 @@ Add this line to your application’s Gemfile:
|
|
24
24
|
gem 'faiss'
|
25
25
|
```
|
26
26
|
|
27
|
-
Faiss is not available for Windows
|
27
|
+
It can take a few minutes to compile the gem. Faiss is not available for Windows.
|
28
28
|
|
29
29
|
## Getting Started
|
30
30
|
|
@@ -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
|
@@ -200,10 +212,10 @@ Data can be an array of arrays
|
|
200
212
|
[[1, 2, 3], [4, 5, 6]]
|
201
213
|
```
|
202
214
|
|
203
|
-
Or a Numo
|
215
|
+
Or a Numo array
|
204
216
|
|
205
217
|
```ruby
|
206
|
-
Numo::
|
218
|
+
Numo::NArray.cast([[1, 2, 3], [4, 5, 6]])
|
207
219
|
```
|
208
220
|
|
209
221
|
## History
|
data/ext/faiss/ext.cpp
CHANGED
@@ -1,315 +1,19 @@
|
|
1
|
-
#include
|
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>
|
1
|
+
#include "utils.h"
|
9
2
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#include <faiss/Clustering.h>
|
16
|
-
#include <faiss/VectorTransform.h>
|
17
|
-
|
18
|
-
#include <rice/Array.hpp>
|
19
|
-
#include <rice/Class.hpp>
|
20
|
-
#include <rice/Constructor.hpp>
|
21
|
-
#include <rice/Module.hpp>
|
22
|
-
|
23
|
-
float* float_array(Rice::Object o)
|
24
|
-
{
|
25
|
-
Rice::String s = o.call("to_binary");
|
26
|
-
return (float*) s.c_str();
|
27
|
-
}
|
28
|
-
|
29
|
-
uint8_t* uint8_array(Rice::Object o)
|
30
|
-
{
|
31
|
-
Rice::String s = o.call("to_binary");
|
32
|
-
return (uint8_t*) s.c_str();
|
33
|
-
}
|
34
|
-
|
35
|
-
// TODO return Numo::SFloat
|
36
|
-
Rice::String result(float* ptr, int64_t length)
|
37
|
-
{
|
38
|
-
return Rice::String(std::string((char*) ptr, length * sizeof(float)));
|
39
|
-
}
|
40
|
-
|
41
|
-
// TODO return Numo::UInt8
|
42
|
-
Rice::String result(uint8_t* ptr, int64_t length)
|
43
|
-
{
|
44
|
-
return Rice::String(std::string((char*) ptr, length * sizeof(uint8_t)));
|
45
|
-
}
|
46
|
-
|
47
|
-
// TODO return Numo::Int32
|
48
|
-
Rice::String result(int32_t* ptr, int64_t length)
|
49
|
-
{
|
50
|
-
return Rice::String(std::string((char*) ptr, length * sizeof(int32_t)));
|
51
|
-
}
|
52
|
-
|
53
|
-
// TODO return Numo::Int64
|
54
|
-
Rice::String result(int64_t* ptr, int64_t length)
|
55
|
-
{
|
56
|
-
return Rice::String(std::string((char*) ptr, length * sizeof(int64_t)));
|
57
|
-
}
|
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);
|
58
8
|
|
59
9
|
extern "C"
|
60
10
|
void Init_ext()
|
61
11
|
{
|
62
|
-
|
63
|
-
.define_singleton_method(
|
64
|
-
"index_binary_factory",
|
65
|
-
*[](int d, const char *description) {
|
66
|
-
return faiss::index_binary_factory(d, description);
|
67
|
-
});
|
68
|
-
|
69
|
-
Rice::define_class_under<faiss::Index>(rb_mFaiss, "Index")
|
70
|
-
.define_method(
|
71
|
-
"d",
|
72
|
-
*[](faiss::Index &self) {
|
73
|
-
return self.d;
|
74
|
-
})
|
75
|
-
.define_method(
|
76
|
-
"trained?",
|
77
|
-
*[](faiss::Index &self) {
|
78
|
-
return self.is_trained;
|
79
|
-
})
|
80
|
-
.define_method(
|
81
|
-
"ntotal",
|
82
|
-
*[](faiss::Index &self) {
|
83
|
-
return self.ntotal;
|
84
|
-
})
|
85
|
-
.define_method(
|
86
|
-
"_train",
|
87
|
-
*[](faiss::Index &self, int64_t n, Rice::Object o) {
|
88
|
-
const float *x = float_array(o);
|
89
|
-
self.train(n, x);
|
90
|
-
})
|
91
|
-
.define_method(
|
92
|
-
"_add",
|
93
|
-
*[](faiss::Index &self, int64_t n, Rice::Object o) {
|
94
|
-
const float *x = float_array(o);
|
95
|
-
self.add(n, x);
|
96
|
-
})
|
97
|
-
.define_method(
|
98
|
-
"_search",
|
99
|
-
*[](faiss::Index &self, int64_t n, Rice::Object o, int64_t k) {
|
100
|
-
const float *x = float_array(o);
|
101
|
-
float *distances = new float[k * n];
|
102
|
-
int64_t *labels = new int64_t[k * n];
|
103
|
-
|
104
|
-
self.search(n, x, k, distances, labels);
|
105
|
-
|
106
|
-
auto dstr = result(distances, k * n);
|
107
|
-
auto lstr = result(labels, k * n);
|
108
|
-
|
109
|
-
Rice::Array ret;
|
110
|
-
ret.push(dstr);
|
111
|
-
ret.push(lstr);
|
112
|
-
return ret;
|
113
|
-
})
|
114
|
-
.define_method(
|
115
|
-
"save",
|
116
|
-
*[](faiss::Index &self, const char *fname) {
|
117
|
-
faiss::write_index(&self, fname);
|
118
|
-
})
|
119
|
-
.define_singleton_method(
|
120
|
-
"load",
|
121
|
-
*[](const char *fname) {
|
122
|
-
return faiss::read_index(fname);
|
123
|
-
});
|
124
|
-
|
125
|
-
Rice::define_class_under<faiss::IndexBinary>(rb_mFaiss, "IndexBinary")
|
126
|
-
.define_method(
|
127
|
-
"d",
|
128
|
-
*[](faiss::Index &self) {
|
129
|
-
return self.d;
|
130
|
-
})
|
131
|
-
.define_method(
|
132
|
-
"trained?",
|
133
|
-
*[](faiss::IndexBinary &self) {
|
134
|
-
return self.is_trained;
|
135
|
-
})
|
136
|
-
.define_method(
|
137
|
-
"ntotal",
|
138
|
-
*[](faiss::IndexBinary &self) {
|
139
|
-
return self.ntotal;
|
140
|
-
})
|
141
|
-
.define_method(
|
142
|
-
"_train",
|
143
|
-
*[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
|
144
|
-
const uint8_t *x = uint8_array(o);
|
145
|
-
self.train(n, x);
|
146
|
-
})
|
147
|
-
.define_method(
|
148
|
-
"_add",
|
149
|
-
*[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
|
150
|
-
const uint8_t *x = uint8_array(o);
|
151
|
-
self.add(n, x);
|
152
|
-
})
|
153
|
-
.define_method(
|
154
|
-
"_search",
|
155
|
-
*[](faiss::IndexBinary &self, int64_t n, Rice::Object o, int64_t k) {
|
156
|
-
const uint8_t *x = uint8_array(o);
|
157
|
-
int32_t *distances = new int32_t[k * n];
|
158
|
-
int64_t *labels = new int64_t[k * n];
|
159
|
-
|
160
|
-
self.search(n, x, k, distances, labels);
|
161
|
-
|
162
|
-
auto dstr = result(distances, k * n);
|
163
|
-
auto lstr = result(labels, k * n);
|
164
|
-
|
165
|
-
Rice::Array ret;
|
166
|
-
ret.push(dstr);
|
167
|
-
ret.push(lstr);
|
168
|
-
return ret;
|
169
|
-
})
|
170
|
-
.define_method(
|
171
|
-
"save",
|
172
|
-
*[](faiss::IndexBinary &self, const char *fname) {
|
173
|
-
faiss::write_index_binary(&self, fname);
|
174
|
-
})
|
175
|
-
.define_singleton_method(
|
176
|
-
"load",
|
177
|
-
*[](const char *fname) {
|
178
|
-
return faiss::read_index_binary(fname);
|
179
|
-
});
|
180
|
-
|
181
|
-
Rice::define_class_under<faiss::IndexFlatL2, faiss::Index>(rb_mFaiss, "IndexFlatL2")
|
182
|
-
.define_constructor(Rice::Constructor<faiss::IndexFlatL2, int64_t>());
|
183
|
-
|
184
|
-
Rice::define_class_under<faiss::IndexFlatIP, faiss::Index>(rb_mFaiss, "IndexFlatIP")
|
185
|
-
.define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
|
186
|
-
|
187
|
-
Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(rb_mFaiss, "IndexHNSWFlat")
|
188
|
-
.define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int>());
|
189
|
-
|
190
|
-
Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(rb_mFaiss, "IndexIVFFlat")
|
191
|
-
.define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t>());
|
192
|
-
|
193
|
-
Rice::define_class_under<faiss::IndexLSH, faiss::Index>(rb_mFaiss, "IndexLSH")
|
194
|
-
.define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
|
195
|
-
|
196
|
-
Rice::define_class_under<faiss::IndexScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexScalarQuantizer")
|
197
|
-
.define_constructor(Rice::Constructor<faiss::IndexScalarQuantizer, int, faiss::ScalarQuantizer::QuantizerType>());
|
198
|
-
|
199
|
-
Rice::define_class_under<faiss::IndexPQ, faiss::Index>(rb_mFaiss, "IndexPQ")
|
200
|
-
.define_constructor(Rice::Constructor<faiss::IndexPQ, int, size_t, size_t>());
|
201
|
-
|
202
|
-
Rice::define_class_under<faiss::IndexIVFScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexIVFScalarQuantizer")
|
203
|
-
.define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
|
204
|
-
|
205
|
-
Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(rb_mFaiss, "IndexIVFPQ")
|
206
|
-
.define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t>());
|
207
|
-
|
208
|
-
Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(rb_mFaiss, "IndexIVFPQR")
|
209
|
-
.define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
|
210
|
-
|
211
|
-
Rice::define_class_under<faiss::IndexBinaryFlat, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryFlat")
|
212
|
-
.define_constructor(Rice::Constructor<faiss::IndexBinaryFlat, int64_t>());
|
213
|
-
|
214
|
-
Rice::define_class_under<faiss::IndexBinaryIVF, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryIVF")
|
215
|
-
.define_constructor(Rice::Constructor<faiss::IndexBinaryIVF, faiss::IndexBinary*, size_t, size_t>());
|
216
|
-
|
217
|
-
Rice::define_class_under<faiss::Clustering>(rb_mFaiss, "Kmeans")
|
218
|
-
.define_constructor(Rice::Constructor<faiss::Clustering, int, int>())
|
219
|
-
.define_method(
|
220
|
-
"d",
|
221
|
-
*[](faiss::Clustering &self) {
|
222
|
-
return self.d;
|
223
|
-
})
|
224
|
-
.define_method(
|
225
|
-
"k",
|
226
|
-
*[](faiss::Clustering &self) {
|
227
|
-
return self.k;
|
228
|
-
})
|
229
|
-
.define_method(
|
230
|
-
"_centroids",
|
231
|
-
*[](faiss::Clustering &self) {
|
232
|
-
float *centroids = new float[self.k * self.d];
|
233
|
-
for (size_t i = 0; i < self.centroids.size(); i++) {
|
234
|
-
centroids[i] = self.centroids[i];
|
235
|
-
}
|
236
|
-
return result(centroids, self.k * self.d);
|
237
|
-
})
|
238
|
-
.define_method(
|
239
|
-
"_train",
|
240
|
-
*[](faiss::Clustering &self, int64_t n, Rice::Object o, faiss::Index & index) {
|
241
|
-
const float *x = float_array(o);
|
242
|
-
self.train(n, x, index);
|
243
|
-
});
|
244
|
-
|
245
|
-
Rice::define_class_under<faiss::PCAMatrix>(rb_mFaiss, "PCAMatrix")
|
246
|
-
.define_constructor(Rice::Constructor<faiss::PCAMatrix, int, int>())
|
247
|
-
.define_method(
|
248
|
-
"d_in",
|
249
|
-
*[](faiss::PCAMatrix &self) {
|
250
|
-
return self.d_in;
|
251
|
-
})
|
252
|
-
.define_method(
|
253
|
-
"d_out",
|
254
|
-
*[](faiss::PCAMatrix &self) {
|
255
|
-
return self.d_out;
|
256
|
-
})
|
257
|
-
.define_method(
|
258
|
-
"_train",
|
259
|
-
*[](faiss::PCAMatrix &self, int64_t n, Rice::Object o) {
|
260
|
-
const float *x = float_array(o);
|
261
|
-
self.train(n, x);
|
262
|
-
})
|
263
|
-
.define_method(
|
264
|
-
"_apply",
|
265
|
-
*[](faiss::PCAMatrix &self, int64_t n, Rice::Object o) {
|
266
|
-
const float *x = float_array(o);
|
267
|
-
float* res = self.apply(n, x);
|
268
|
-
return result(res, n * self.d_out);
|
269
|
-
});
|
12
|
+
auto m = Rice::define_module("Faiss");
|
270
13
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
return self.d;
|
277
|
-
})
|
278
|
-
.define_method(
|
279
|
-
"m",
|
280
|
-
*[](faiss::ProductQuantizer &self) {
|
281
|
-
return self.M;
|
282
|
-
})
|
283
|
-
.define_method(
|
284
|
-
"_train",
|
285
|
-
*[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
|
286
|
-
const float *x = float_array(o);
|
287
|
-
self.train(n, x);
|
288
|
-
})
|
289
|
-
.define_method(
|
290
|
-
"_compute_codes",
|
291
|
-
*[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
|
292
|
-
const float *x = float_array(o);
|
293
|
-
uint8_t *codes = new uint8_t[n * self.M];
|
294
|
-
self.compute_codes(x, codes, n);
|
295
|
-
return result(codes, n * self.M);
|
296
|
-
})
|
297
|
-
.define_method(
|
298
|
-
"_decode",
|
299
|
-
*[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
|
300
|
-
const uint8_t *codes = uint8_array(o);
|
301
|
-
float *x = new float[n * self.d];
|
302
|
-
self.decode(codes, x, n);
|
303
|
-
return result(x, n * self.d);
|
304
|
-
})
|
305
|
-
.define_method(
|
306
|
-
"save",
|
307
|
-
*[](faiss::ProductQuantizer &self, const char *fname) {
|
308
|
-
faiss::write_ProductQuantizer(&self, fname);
|
309
|
-
})
|
310
|
-
.define_singleton_method(
|
311
|
-
"load",
|
312
|
-
*[](const char *fname) {
|
313
|
-
return faiss::read_ProductQuantizer(fname);
|
314
|
-
});
|
14
|
+
init_index(m);
|
15
|
+
init_index_binary(m);
|
16
|
+
init_kmeans(m);
|
17
|
+
init_pca_matrix(m);
|
18
|
+
init_product_quantizer(m);
|
315
19
|
}
|
data/ext/faiss/extconf.rb
CHANGED
@@ -4,14 +4,17 @@ abort "BLAS not found" unless have_library("blas")
|
|
4
4
|
abort "LAPACK not found" unless have_library("lapack")
|
5
5
|
abort "OpenMP not found" unless have_library("omp") || have_library("gomp")
|
6
6
|
|
7
|
-
|
7
|
+
numo = $LOAD_PATH.find { |v| File.exist?("#{v}/numo/numo/narray.h") }
|
8
|
+
abort "Numo not found" unless numo && find_header("numo/narray.h", "#{numo}/numo")
|
9
|
+
|
10
|
+
$CXXFLAGS << " -std=c++17 $(optflags) -DFINTEGER=int " << with_config("optflags", "-march=native")
|
8
11
|
|
9
12
|
ext = File.expand_path(".", __dir__)
|
10
13
|
vendor = File.expand_path("../../vendor/faiss", __dir__)
|
11
14
|
|
12
|
-
$srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/impl
|
15
|
+
$srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/{impl,invlists,utils}}/*.{cpp}"]
|
13
16
|
$objs = $srcs.map { |v| v.sub(/cpp\z/, "o") }
|
14
|
-
|
17
|
+
abort "Faiss not found" unless find_header("faiss/Index.h", vendor)
|
15
18
|
$VPATH << vendor
|
16
19
|
|
17
20
|
create_makefile("faiss/ext")
|