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
data/ext/faiss/index.cpp
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
#include <faiss/Index.h>
|
2
|
+
#include <faiss/IndexFlat.h>
|
3
|
+
#include <faiss/IndexHNSW.h>
|
4
|
+
#include <faiss/IndexIVFFlat.h>
|
5
|
+
#include <faiss/IndexLSH.h>
|
6
|
+
#include <faiss/IndexScalarQuantizer.h>
|
7
|
+
#include <faiss/IndexPQ.h>
|
8
|
+
#include <faiss/IndexIVFPQ.h>
|
9
|
+
#include <faiss/IndexIVFPQR.h>
|
10
|
+
#include <faiss/index_io.h>
|
11
|
+
#include <faiss/AutoTune.h>
|
12
|
+
|
13
|
+
#include "utils.h"
|
14
|
+
|
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
|
+
};
|
90
|
+
}
|
91
|
+
|
92
|
+
void init_index(Rice::Module& m) {
|
93
|
+
Rice::define_class_under<faiss::Index>(m, "Index")
|
94
|
+
.define_method(
|
95
|
+
"d",
|
96
|
+
[](faiss::Index &self) {
|
97
|
+
return self.d;
|
98
|
+
})
|
99
|
+
.define_method(
|
100
|
+
"trained?",
|
101
|
+
[](faiss::Index &self) {
|
102
|
+
return self.is_trained;
|
103
|
+
})
|
104
|
+
.define_method(
|
105
|
+
"ntotal",
|
106
|
+
[](faiss::Index &self) {
|
107
|
+
return self.ntotal;
|
108
|
+
})
|
109
|
+
.define_method(
|
110
|
+
"train",
|
111
|
+
[](faiss::Index &self, numo::SFloat objects) {
|
112
|
+
auto n = check_shape(objects, self.d);
|
113
|
+
self.train(n, objects.read_ptr());
|
114
|
+
})
|
115
|
+
.define_method(
|
116
|
+
"add",
|
117
|
+
[](faiss::Index &self, numo::SFloat objects) {
|
118
|
+
auto n = check_shape(objects, self.d);
|
119
|
+
self.add(n, objects.read_ptr());
|
120
|
+
})
|
121
|
+
.define_method(
|
122
|
+
"search",
|
123
|
+
[](faiss::Index &self, numo::SFloat objects, size_t k) {
|
124
|
+
auto n = check_shape(objects, self.d);
|
125
|
+
|
126
|
+
auto distances = numo::SFloat({n, k});
|
127
|
+
auto labels = numo::Int64({n, k});
|
128
|
+
|
129
|
+
self.search(n, objects.read_ptr(), k, distances.write_ptr(), labels.write_ptr());
|
130
|
+
|
131
|
+
Rice::Array ret;
|
132
|
+
ret.push(distances);
|
133
|
+
ret.push(labels);
|
134
|
+
return ret;
|
135
|
+
})
|
136
|
+
.define_method(
|
137
|
+
"nprobe=",
|
138
|
+
[](faiss::Index &self, double val) {
|
139
|
+
faiss::ParameterSpace().set_index_parameter(&self, "nprobe", val);
|
140
|
+
})
|
141
|
+
.define_method(
|
142
|
+
"save",
|
143
|
+
[](faiss::Index &self, const char *fname) {
|
144
|
+
faiss::write_index(&self, fname);
|
145
|
+
})
|
146
|
+
.define_singleton_function(
|
147
|
+
"load",
|
148
|
+
[](const char *fname) {
|
149
|
+
return faiss::read_index(fname);
|
150
|
+
});
|
151
|
+
|
152
|
+
Rice::define_class_under<faiss::IndexFlatL2, faiss::Index>(m, "IndexFlatL2")
|
153
|
+
.define_constructor(Rice::Constructor<faiss::IndexFlatL2, int64_t>());
|
154
|
+
|
155
|
+
Rice::define_class_under<faiss::IndexFlatIP, faiss::Index>(m, "IndexFlatIP")
|
156
|
+
.define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
|
157
|
+
|
158
|
+
Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(m, "IndexHNSWFlat")
|
159
|
+
.define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int, faiss::MetricType>(), Rice::Arg("d"), Rice::Arg("M"), Rice::Arg("metric") = faiss::METRIC_L2);
|
160
|
+
|
161
|
+
Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(m, "IndexIVFFlat")
|
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);
|
163
|
+
|
164
|
+
Rice::define_class_under<faiss::IndexLSH, faiss::Index>(m, "IndexLSH")
|
165
|
+
.define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
|
166
|
+
|
167
|
+
Rice::define_class_under<faiss::IndexScalarQuantizer, faiss::Index>(m, "IndexScalarQuantizer")
|
168
|
+
.define_constructor(Rice::Constructor<faiss::IndexScalarQuantizer, int, faiss::ScalarQuantizer::QuantizerType>());
|
169
|
+
|
170
|
+
Rice::define_class_under<faiss::IndexPQ, faiss::Index>(m, "IndexPQ")
|
171
|
+
.define_constructor(Rice::Constructor<faiss::IndexPQ, int, size_t, size_t>());
|
172
|
+
|
173
|
+
Rice::define_class_under<faiss::IndexIVFScalarQuantizer, faiss::Index>(m, "IndexIVFScalarQuantizer")
|
174
|
+
.define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
|
175
|
+
|
176
|
+
Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(m, "IndexIVFPQ")
|
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);
|
178
|
+
|
179
|
+
Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(m, "IndexIVFPQR")
|
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
|
+
});
|
189
|
+
}
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#include <faiss/IndexBinary.h>
|
2
|
+
#include <faiss/IndexBinaryFlat.h>
|
3
|
+
#include <faiss/IndexBinaryIVF.h>
|
4
|
+
#include <faiss/index_factory.h>
|
5
|
+
#include <faiss/index_io.h>
|
6
|
+
|
7
|
+
#include "utils.h"
|
8
|
+
|
9
|
+
void init_index_binary(Rice::Module& m) {
|
10
|
+
Rice::define_class_under<faiss::IndexBinary>(m, "IndexBinary")
|
11
|
+
.define_method(
|
12
|
+
"d",
|
13
|
+
[](faiss::IndexBinary &self) {
|
14
|
+
return self.d;
|
15
|
+
})
|
16
|
+
.define_method(
|
17
|
+
"trained?",
|
18
|
+
[](faiss::IndexBinary &self) {
|
19
|
+
return self.is_trained;
|
20
|
+
})
|
21
|
+
.define_method(
|
22
|
+
"ntotal",
|
23
|
+
[](faiss::IndexBinary &self) {
|
24
|
+
return self.ntotal;
|
25
|
+
})
|
26
|
+
.define_method(
|
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());
|
31
|
+
})
|
32
|
+
.define_method(
|
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());
|
37
|
+
})
|
38
|
+
.define_method(
|
39
|
+
"search",
|
40
|
+
[](faiss::IndexBinary &self, numo::UInt8 objects, size_t k) {
|
41
|
+
auto n = check_shape(objects, self.d / 8);
|
42
|
+
|
43
|
+
auto distances = numo::Int32({n, k});
|
44
|
+
auto labels = numo::Int64({n, k});
|
45
|
+
|
46
|
+
self.search(n, objects.read_ptr(), k, distances.write_ptr(), labels.write_ptr());
|
47
|
+
|
48
|
+
Rice::Array ret;
|
49
|
+
ret.push(distances);
|
50
|
+
ret.push(labels);
|
51
|
+
return ret;
|
52
|
+
})
|
53
|
+
.define_method(
|
54
|
+
"save",
|
55
|
+
[](faiss::IndexBinary &self, const char *fname) {
|
56
|
+
faiss::write_index_binary(&self, fname);
|
57
|
+
})
|
58
|
+
.define_singleton_function(
|
59
|
+
"load",
|
60
|
+
[](const char *fname) {
|
61
|
+
return faiss::read_index_binary(fname);
|
62
|
+
});
|
63
|
+
|
64
|
+
Rice::define_class_under<faiss::IndexBinaryFlat, faiss::IndexBinary>(m, "IndexBinaryFlat")
|
65
|
+
.define_constructor(Rice::Constructor<faiss::IndexBinaryFlat, int64_t>());
|
66
|
+
|
67
|
+
Rice::define_class_under<faiss::IndexBinaryIVF, faiss::IndexBinary>(m, "IndexBinaryIVF")
|
68
|
+
.define_constructor(Rice::Constructor<faiss::IndexBinaryIVF, faiss::IndexBinary*, size_t, size_t>());
|
69
|
+
|
70
|
+
m.define_singleton_function(
|
71
|
+
"index_binary_factory",
|
72
|
+
[](int d, const char *description) {
|
73
|
+
return faiss::index_binary_factory(d, description);
|
74
|
+
});
|
75
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#include <faiss/Clustering.h>
|
2
|
+
#include <faiss/IndexFlat.h>
|
3
|
+
|
4
|
+
#include "utils.h"
|
5
|
+
|
6
|
+
void init_kmeans(Rice::Module& m) {
|
7
|
+
Rice::define_class_under<faiss::Clustering>(m, "Kmeans")
|
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
|
+
})
|
14
|
+
.define_method(
|
15
|
+
"d",
|
16
|
+
[](faiss::Clustering &self) {
|
17
|
+
return self.d;
|
18
|
+
})
|
19
|
+
.define_method(
|
20
|
+
"k",
|
21
|
+
[](faiss::Clustering &self) {
|
22
|
+
return self.k;
|
23
|
+
})
|
24
|
+
.define_method(
|
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;
|
30
|
+
})
|
31
|
+
.define_method(
|
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);
|
39
|
+
});
|
40
|
+
}
|
data/ext/faiss/numo.hpp
ADDED
@@ -0,0 +1,867 @@
|
|
1
|
+
/*!
|
2
|
+
* Numo.hpp v0.1.0
|
3
|
+
* https://github.com/ankane/numo.hpp
|
4
|
+
* BSD-2-Clause License
|
5
|
+
*/
|
6
|
+
|
7
|
+
#pragma once
|
8
|
+
|
9
|
+
#include <rice/rice.hpp>
|
10
|
+
#include <numo/narray.h>
|
11
|
+
|
12
|
+
namespace numo {
|
13
|
+
class NArray {
|
14
|
+
public:
|
15
|
+
NArray(VALUE v) {
|
16
|
+
construct_value(this->dtype(), v);
|
17
|
+
}
|
18
|
+
|
19
|
+
NArray(Rice::Object o) {
|
20
|
+
construct_value(this->dtype(), o.value());
|
21
|
+
}
|
22
|
+
|
23
|
+
VALUE value() const {
|
24
|
+
return this->_value;
|
25
|
+
}
|
26
|
+
|
27
|
+
size_t ndim() {
|
28
|
+
return RNARRAY_NDIM(this->_value);
|
29
|
+
}
|
30
|
+
|
31
|
+
size_t* shape() {
|
32
|
+
return RNARRAY_SHAPE(this->_value);
|
33
|
+
}
|
34
|
+
|
35
|
+
size_t size() {
|
36
|
+
return RNARRAY_SIZE(this->_value);
|
37
|
+
}
|
38
|
+
|
39
|
+
bool is_contiguous() {
|
40
|
+
return nary_check_contiguous(this->_value) == Qtrue;
|
41
|
+
}
|
42
|
+
|
43
|
+
operator Rice::Object() const {
|
44
|
+
return Rice::Object(this->_value);
|
45
|
+
}
|
46
|
+
|
47
|
+
const void* read_ptr() {
|
48
|
+
if (!is_contiguous()) {
|
49
|
+
this->_value = nary_dup(this->_value);
|
50
|
+
}
|
51
|
+
return nary_get_pointer_for_read(this->_value) + nary_get_offset(this->_value);
|
52
|
+
}
|
53
|
+
|
54
|
+
void* write_ptr() {
|
55
|
+
return nary_get_pointer_for_write(this->_value);
|
56
|
+
}
|
57
|
+
|
58
|
+
protected:
|
59
|
+
NArray() { }
|
60
|
+
|
61
|
+
void construct_value(VALUE dtype, VALUE v) {
|
62
|
+
this->_value = rb_funcall(dtype, rb_intern("cast"), 1, v);
|
63
|
+
}
|
64
|
+
|
65
|
+
void construct_shape(VALUE dtype, std::initializer_list<size_t> shape) {
|
66
|
+
// rb_narray_new doesn't modify shape, but not marked as const
|
67
|
+
this->_value = rb_narray_new(dtype, shape.size(), const_cast<size_t*>(shape.begin()));
|
68
|
+
}
|
69
|
+
|
70
|
+
VALUE _value;
|
71
|
+
|
72
|
+
private:
|
73
|
+
VALUE dtype() {
|
74
|
+
return numo_cNArray;
|
75
|
+
}
|
76
|
+
};
|
77
|
+
|
78
|
+
class SFloat: public NArray {
|
79
|
+
public:
|
80
|
+
SFloat(VALUE v) {
|
81
|
+
construct_value(this->dtype(), v);
|
82
|
+
}
|
83
|
+
|
84
|
+
SFloat(Rice::Object o) {
|
85
|
+
construct_value(this->dtype(), o.value());
|
86
|
+
}
|
87
|
+
|
88
|
+
SFloat(std::initializer_list<size_t> shape) {
|
89
|
+
construct_shape(this->dtype(), shape);
|
90
|
+
}
|
91
|
+
|
92
|
+
const float* read_ptr() {
|
93
|
+
return reinterpret_cast<const float*>(NArray::read_ptr());
|
94
|
+
}
|
95
|
+
|
96
|
+
float* write_ptr() {
|
97
|
+
return reinterpret_cast<float*>(NArray::write_ptr());
|
98
|
+
}
|
99
|
+
|
100
|
+
private:
|
101
|
+
VALUE dtype() {
|
102
|
+
return numo_cSFloat;
|
103
|
+
}
|
104
|
+
};
|
105
|
+
|
106
|
+
class DFloat: public NArray {
|
107
|
+
public:
|
108
|
+
DFloat(VALUE v) {
|
109
|
+
construct_value(this->dtype(), v);
|
110
|
+
}
|
111
|
+
|
112
|
+
DFloat(Rice::Object o) {
|
113
|
+
construct_value(this->dtype(), o.value());
|
114
|
+
}
|
115
|
+
|
116
|
+
DFloat(std::initializer_list<size_t> shape) {
|
117
|
+
construct_shape(this->dtype(), shape);
|
118
|
+
}
|
119
|
+
|
120
|
+
const double* read_ptr() {
|
121
|
+
return reinterpret_cast<const double*>(NArray::read_ptr());
|
122
|
+
}
|
123
|
+
|
124
|
+
double* write_ptr() {
|
125
|
+
return reinterpret_cast<double*>(NArray::write_ptr());
|
126
|
+
}
|
127
|
+
|
128
|
+
private:
|
129
|
+
VALUE dtype() {
|
130
|
+
return numo_cDFloat;
|
131
|
+
}
|
132
|
+
};
|
133
|
+
|
134
|
+
class Int8: public NArray {
|
135
|
+
public:
|
136
|
+
Int8(VALUE v) {
|
137
|
+
construct_value(this->dtype(), v);
|
138
|
+
}
|
139
|
+
|
140
|
+
Int8(Rice::Object o) {
|
141
|
+
construct_value(this->dtype(), o.value());
|
142
|
+
}
|
143
|
+
|
144
|
+
Int8(std::initializer_list<size_t> shape) {
|
145
|
+
construct_shape(this->dtype(), shape);
|
146
|
+
}
|
147
|
+
|
148
|
+
const int8_t* read_ptr() {
|
149
|
+
return reinterpret_cast<const int8_t*>(NArray::read_ptr());
|
150
|
+
}
|
151
|
+
|
152
|
+
int8_t* write_ptr() {
|
153
|
+
return reinterpret_cast<int8_t*>(NArray::write_ptr());
|
154
|
+
}
|
155
|
+
|
156
|
+
private:
|
157
|
+
VALUE dtype() {
|
158
|
+
return numo_cInt8;
|
159
|
+
}
|
160
|
+
};
|
161
|
+
|
162
|
+
class Int16: public NArray {
|
163
|
+
public:
|
164
|
+
Int16(VALUE v) {
|
165
|
+
construct_value(this->dtype(), v);
|
166
|
+
}
|
167
|
+
|
168
|
+
Int16(Rice::Object o) {
|
169
|
+
construct_value(this->dtype(), o.value());
|
170
|
+
}
|
171
|
+
|
172
|
+
Int16(std::initializer_list<size_t> shape) {
|
173
|
+
construct_shape(this->dtype(), shape);
|
174
|
+
}
|
175
|
+
|
176
|
+
const int16_t* read_ptr() {
|
177
|
+
return reinterpret_cast<const int16_t*>(NArray::read_ptr());
|
178
|
+
}
|
179
|
+
|
180
|
+
int16_t* write_ptr() {
|
181
|
+
return reinterpret_cast<int16_t*>(NArray::write_ptr());
|
182
|
+
}
|
183
|
+
|
184
|
+
private:
|
185
|
+
VALUE dtype() {
|
186
|
+
return numo_cInt16;
|
187
|
+
}
|
188
|
+
};
|
189
|
+
|
190
|
+
class Int32: public NArray {
|
191
|
+
public:
|
192
|
+
Int32(VALUE v) {
|
193
|
+
construct_value(this->dtype(), v);
|
194
|
+
}
|
195
|
+
|
196
|
+
Int32(Rice::Object o) {
|
197
|
+
construct_value(this->dtype(), o.value());
|
198
|
+
}
|
199
|
+
|
200
|
+
Int32(std::initializer_list<size_t> shape) {
|
201
|
+
construct_shape(this->dtype(), shape);
|
202
|
+
}
|
203
|
+
|
204
|
+
const int32_t* read_ptr() {
|
205
|
+
return reinterpret_cast<const int32_t*>(NArray::read_ptr());
|
206
|
+
}
|
207
|
+
|
208
|
+
int32_t* write_ptr() {
|
209
|
+
return reinterpret_cast<int32_t*>(NArray::write_ptr());
|
210
|
+
}
|
211
|
+
|
212
|
+
private:
|
213
|
+
VALUE dtype() {
|
214
|
+
return numo_cInt32;
|
215
|
+
}
|
216
|
+
};
|
217
|
+
|
218
|
+
class Int64: public NArray {
|
219
|
+
public:
|
220
|
+
Int64(VALUE v) {
|
221
|
+
construct_value(this->dtype(), v);
|
222
|
+
}
|
223
|
+
|
224
|
+
Int64(Rice::Object o) {
|
225
|
+
construct_value(this->dtype(), o.value());
|
226
|
+
}
|
227
|
+
|
228
|
+
Int64(std::initializer_list<size_t> shape) {
|
229
|
+
construct_shape(this->dtype(), shape);
|
230
|
+
}
|
231
|
+
|
232
|
+
const int64_t* read_ptr() {
|
233
|
+
return reinterpret_cast<const int64_t*>(NArray::read_ptr());
|
234
|
+
}
|
235
|
+
|
236
|
+
int64_t* write_ptr() {
|
237
|
+
return reinterpret_cast<int64_t*>(NArray::write_ptr());
|
238
|
+
}
|
239
|
+
|
240
|
+
private:
|
241
|
+
VALUE dtype() {
|
242
|
+
return numo_cInt64;
|
243
|
+
}
|
244
|
+
};
|
245
|
+
|
246
|
+
class UInt8: public NArray {
|
247
|
+
public:
|
248
|
+
UInt8(VALUE v) {
|
249
|
+
construct_value(this->dtype(), v);
|
250
|
+
}
|
251
|
+
|
252
|
+
UInt8(Rice::Object o) {
|
253
|
+
construct_value(this->dtype(), o.value());
|
254
|
+
}
|
255
|
+
|
256
|
+
UInt8(std::initializer_list<size_t> shape) {
|
257
|
+
construct_shape(this->dtype(), shape);
|
258
|
+
}
|
259
|
+
|
260
|
+
const uint8_t* read_ptr() {
|
261
|
+
return reinterpret_cast<const uint8_t*>(NArray::read_ptr());
|
262
|
+
}
|
263
|
+
|
264
|
+
uint8_t* write_ptr() {
|
265
|
+
return reinterpret_cast<uint8_t*>(NArray::write_ptr());
|
266
|
+
}
|
267
|
+
|
268
|
+
private:
|
269
|
+
VALUE dtype() {
|
270
|
+
return numo_cUInt8;
|
271
|
+
}
|
272
|
+
};
|
273
|
+
|
274
|
+
class UInt16: public NArray {
|
275
|
+
public:
|
276
|
+
UInt16(VALUE v) {
|
277
|
+
construct_value(this->dtype(), v);
|
278
|
+
}
|
279
|
+
|
280
|
+
UInt16(Rice::Object o) {
|
281
|
+
construct_value(this->dtype(), o.value());
|
282
|
+
}
|
283
|
+
|
284
|
+
UInt16(std::initializer_list<size_t> shape) {
|
285
|
+
construct_shape(this->dtype(), shape);
|
286
|
+
}
|
287
|
+
|
288
|
+
const uint16_t* read_ptr() {
|
289
|
+
return reinterpret_cast<const uint16_t*>(NArray::read_ptr());
|
290
|
+
}
|
291
|
+
|
292
|
+
uint16_t* write_ptr() {
|
293
|
+
return reinterpret_cast<uint16_t*>(NArray::write_ptr());
|
294
|
+
}
|
295
|
+
|
296
|
+
private:
|
297
|
+
VALUE dtype() {
|
298
|
+
return numo_cUInt16;
|
299
|
+
}
|
300
|
+
};
|
301
|
+
|
302
|
+
class UInt32: public NArray {
|
303
|
+
public:
|
304
|
+
UInt32(VALUE v) {
|
305
|
+
construct_value(this->dtype(), v);
|
306
|
+
}
|
307
|
+
|
308
|
+
UInt32(Rice::Object o) {
|
309
|
+
construct_value(this->dtype(), o.value());
|
310
|
+
}
|
311
|
+
|
312
|
+
UInt32(std::initializer_list<size_t> shape) {
|
313
|
+
construct_shape(this->dtype(), shape);
|
314
|
+
}
|
315
|
+
|
316
|
+
const uint32_t* read_ptr() {
|
317
|
+
return reinterpret_cast<const uint32_t*>(NArray::read_ptr());
|
318
|
+
}
|
319
|
+
|
320
|
+
uint32_t* write_ptr() {
|
321
|
+
return reinterpret_cast<uint32_t*>(NArray::write_ptr());
|
322
|
+
}
|
323
|
+
|
324
|
+
private:
|
325
|
+
VALUE dtype() {
|
326
|
+
return numo_cUInt32;
|
327
|
+
}
|
328
|
+
};
|
329
|
+
|
330
|
+
class UInt64: public NArray {
|
331
|
+
public:
|
332
|
+
UInt64(VALUE v) {
|
333
|
+
construct_value(this->dtype(), v);
|
334
|
+
}
|
335
|
+
|
336
|
+
UInt64(Rice::Object o) {
|
337
|
+
construct_value(this->dtype(), o.value());
|
338
|
+
}
|
339
|
+
|
340
|
+
UInt64(std::initializer_list<size_t> shape) {
|
341
|
+
construct_shape(this->dtype(), shape);
|
342
|
+
}
|
343
|
+
|
344
|
+
const uint64_t* read_ptr() {
|
345
|
+
return reinterpret_cast<const uint64_t*>(NArray::read_ptr());
|
346
|
+
}
|
347
|
+
|
348
|
+
uint64_t* write_ptr() {
|
349
|
+
return reinterpret_cast<uint64_t*>(NArray::write_ptr());
|
350
|
+
}
|
351
|
+
|
352
|
+
private:
|
353
|
+
VALUE dtype() {
|
354
|
+
return numo_cUInt64;
|
355
|
+
}
|
356
|
+
};
|
357
|
+
|
358
|
+
class SComplex: public NArray {
|
359
|
+
public:
|
360
|
+
SComplex(VALUE v) {
|
361
|
+
construct_value(this->dtype(), v);
|
362
|
+
}
|
363
|
+
|
364
|
+
SComplex(Rice::Object o) {
|
365
|
+
construct_value(this->dtype(), o.value());
|
366
|
+
}
|
367
|
+
|
368
|
+
SComplex(std::initializer_list<size_t> shape) {
|
369
|
+
construct_shape(this->dtype(), shape);
|
370
|
+
}
|
371
|
+
|
372
|
+
private:
|
373
|
+
VALUE dtype() {
|
374
|
+
return numo_cSComplex;
|
375
|
+
}
|
376
|
+
};
|
377
|
+
|
378
|
+
class DComplex: public NArray {
|
379
|
+
public:
|
380
|
+
DComplex(VALUE v) {
|
381
|
+
construct_value(this->dtype(), v);
|
382
|
+
}
|
383
|
+
|
384
|
+
DComplex(Rice::Object o) {
|
385
|
+
construct_value(this->dtype(), o.value());
|
386
|
+
}
|
387
|
+
|
388
|
+
DComplex(std::initializer_list<size_t> shape) {
|
389
|
+
construct_shape(this->dtype(), shape);
|
390
|
+
}
|
391
|
+
|
392
|
+
private:
|
393
|
+
VALUE dtype() {
|
394
|
+
return numo_cDComplex;
|
395
|
+
}
|
396
|
+
};
|
397
|
+
|
398
|
+
class Bit: public NArray {
|
399
|
+
public:
|
400
|
+
Bit(VALUE v) {
|
401
|
+
construct_value(this->dtype(), v);
|
402
|
+
}
|
403
|
+
|
404
|
+
Bit(Rice::Object o) {
|
405
|
+
construct_value(this->dtype(), o.value());
|
406
|
+
}
|
407
|
+
|
408
|
+
Bit(std::initializer_list<size_t> shape) {
|
409
|
+
construct_shape(this->dtype(), shape);
|
410
|
+
}
|
411
|
+
|
412
|
+
private:
|
413
|
+
VALUE dtype() {
|
414
|
+
return numo_cBit;
|
415
|
+
}
|
416
|
+
};
|
417
|
+
|
418
|
+
class RObject: public NArray {
|
419
|
+
public:
|
420
|
+
RObject(VALUE v) {
|
421
|
+
construct_value(this->dtype(), v);
|
422
|
+
}
|
423
|
+
|
424
|
+
RObject(Rice::Object o) {
|
425
|
+
construct_value(this->dtype(), o.value());
|
426
|
+
}
|
427
|
+
|
428
|
+
RObject(std::initializer_list<size_t> shape) {
|
429
|
+
construct_shape(this->dtype(), shape);
|
430
|
+
}
|
431
|
+
|
432
|
+
const VALUE* read_ptr() {
|
433
|
+
return reinterpret_cast<const VALUE*>(NArray::read_ptr());
|
434
|
+
}
|
435
|
+
|
436
|
+
VALUE* write_ptr() {
|
437
|
+
return reinterpret_cast<VALUE*>(NArray::write_ptr());
|
438
|
+
}
|
439
|
+
|
440
|
+
private:
|
441
|
+
VALUE dtype() {
|
442
|
+
return numo_cRObject;
|
443
|
+
}
|
444
|
+
};
|
445
|
+
}
|
446
|
+
|
447
|
+
namespace Rice::detail {
|
448
|
+
template<>
|
449
|
+
struct Type<numo::NArray>
|
450
|
+
{
|
451
|
+
static bool verify()
|
452
|
+
{
|
453
|
+
return true;
|
454
|
+
}
|
455
|
+
};
|
456
|
+
|
457
|
+
template<>
|
458
|
+
class From_Ruby<numo::NArray>
|
459
|
+
{
|
460
|
+
public:
|
461
|
+
numo::NArray convert(VALUE x)
|
462
|
+
{
|
463
|
+
return numo::NArray(x);
|
464
|
+
}
|
465
|
+
};
|
466
|
+
|
467
|
+
template<>
|
468
|
+
class To_Ruby<numo::NArray>
|
469
|
+
{
|
470
|
+
public:
|
471
|
+
VALUE convert(const numo::NArray& x) {
|
472
|
+
return x.value();
|
473
|
+
}
|
474
|
+
};
|
475
|
+
|
476
|
+
template<>
|
477
|
+
struct Type<numo::SFloat>
|
478
|
+
{
|
479
|
+
static bool verify()
|
480
|
+
{
|
481
|
+
return true;
|
482
|
+
}
|
483
|
+
};
|
484
|
+
|
485
|
+
template<>
|
486
|
+
class From_Ruby<numo::SFloat>
|
487
|
+
{
|
488
|
+
public:
|
489
|
+
numo::SFloat convert(VALUE x)
|
490
|
+
{
|
491
|
+
return numo::SFloat(x);
|
492
|
+
}
|
493
|
+
};
|
494
|
+
|
495
|
+
template<>
|
496
|
+
class To_Ruby<numo::SFloat>
|
497
|
+
{
|
498
|
+
public:
|
499
|
+
VALUE convert(const numo::SFloat& x) {
|
500
|
+
return x.value();
|
501
|
+
}
|
502
|
+
};
|
503
|
+
|
504
|
+
template<>
|
505
|
+
struct Type<numo::DFloat>
|
506
|
+
{
|
507
|
+
static bool verify()
|
508
|
+
{
|
509
|
+
return true;
|
510
|
+
}
|
511
|
+
};
|
512
|
+
|
513
|
+
template<>
|
514
|
+
class From_Ruby<numo::DFloat>
|
515
|
+
{
|
516
|
+
public:
|
517
|
+
numo::DFloat convert(VALUE x)
|
518
|
+
{
|
519
|
+
return numo::DFloat(x);
|
520
|
+
}
|
521
|
+
};
|
522
|
+
|
523
|
+
template<>
|
524
|
+
class To_Ruby<numo::DFloat>
|
525
|
+
{
|
526
|
+
public:
|
527
|
+
VALUE convert(const numo::DFloat& x) {
|
528
|
+
return x.value();
|
529
|
+
}
|
530
|
+
};
|
531
|
+
|
532
|
+
template<>
|
533
|
+
struct Type<numo::Int8>
|
534
|
+
{
|
535
|
+
static bool verify()
|
536
|
+
{
|
537
|
+
return true;
|
538
|
+
}
|
539
|
+
};
|
540
|
+
|
541
|
+
template<>
|
542
|
+
class From_Ruby<numo::Int8>
|
543
|
+
{
|
544
|
+
public:
|
545
|
+
numo::Int8 convert(VALUE x)
|
546
|
+
{
|
547
|
+
return numo::Int8(x);
|
548
|
+
}
|
549
|
+
};
|
550
|
+
|
551
|
+
template<>
|
552
|
+
class To_Ruby<numo::Int8>
|
553
|
+
{
|
554
|
+
public:
|
555
|
+
VALUE convert(const numo::Int8& x) {
|
556
|
+
return x.value();
|
557
|
+
}
|
558
|
+
};
|
559
|
+
|
560
|
+
template<>
|
561
|
+
struct Type<numo::Int16>
|
562
|
+
{
|
563
|
+
static bool verify()
|
564
|
+
{
|
565
|
+
return true;
|
566
|
+
}
|
567
|
+
};
|
568
|
+
|
569
|
+
template<>
|
570
|
+
class From_Ruby<numo::Int16>
|
571
|
+
{
|
572
|
+
public:
|
573
|
+
numo::Int16 convert(VALUE x)
|
574
|
+
{
|
575
|
+
return numo::Int16(x);
|
576
|
+
}
|
577
|
+
};
|
578
|
+
|
579
|
+
template<>
|
580
|
+
class To_Ruby<numo::Int16>
|
581
|
+
{
|
582
|
+
public:
|
583
|
+
VALUE convert(const numo::Int16& x) {
|
584
|
+
return x.value();
|
585
|
+
}
|
586
|
+
};
|
587
|
+
|
588
|
+
template<>
|
589
|
+
struct Type<numo::Int32>
|
590
|
+
{
|
591
|
+
static bool verify()
|
592
|
+
{
|
593
|
+
return true;
|
594
|
+
}
|
595
|
+
};
|
596
|
+
|
597
|
+
template<>
|
598
|
+
class From_Ruby<numo::Int32>
|
599
|
+
{
|
600
|
+
public:
|
601
|
+
numo::Int32 convert(VALUE x)
|
602
|
+
{
|
603
|
+
return numo::Int32(x);
|
604
|
+
}
|
605
|
+
};
|
606
|
+
|
607
|
+
template<>
|
608
|
+
class To_Ruby<numo::Int32>
|
609
|
+
{
|
610
|
+
public:
|
611
|
+
VALUE convert(const numo::Int32& x) {
|
612
|
+
return x.value();
|
613
|
+
}
|
614
|
+
};
|
615
|
+
|
616
|
+
template<>
|
617
|
+
struct Type<numo::Int64>
|
618
|
+
{
|
619
|
+
static bool verify()
|
620
|
+
{
|
621
|
+
return true;
|
622
|
+
}
|
623
|
+
};
|
624
|
+
|
625
|
+
template<>
|
626
|
+
class From_Ruby<numo::Int64>
|
627
|
+
{
|
628
|
+
public:
|
629
|
+
numo::Int64 convert(VALUE x)
|
630
|
+
{
|
631
|
+
return numo::Int64(x);
|
632
|
+
}
|
633
|
+
};
|
634
|
+
|
635
|
+
template<>
|
636
|
+
class To_Ruby<numo::Int64>
|
637
|
+
{
|
638
|
+
public:
|
639
|
+
VALUE convert(const numo::Int64& x) {
|
640
|
+
return x.value();
|
641
|
+
}
|
642
|
+
};
|
643
|
+
|
644
|
+
template<>
|
645
|
+
struct Type<numo::UInt8>
|
646
|
+
{
|
647
|
+
static bool verify()
|
648
|
+
{
|
649
|
+
return true;
|
650
|
+
}
|
651
|
+
};
|
652
|
+
|
653
|
+
template<>
|
654
|
+
class From_Ruby<numo::UInt8>
|
655
|
+
{
|
656
|
+
public:
|
657
|
+
numo::UInt8 convert(VALUE x)
|
658
|
+
{
|
659
|
+
return numo::UInt8(x);
|
660
|
+
}
|
661
|
+
};
|
662
|
+
|
663
|
+
template<>
|
664
|
+
class To_Ruby<numo::UInt8>
|
665
|
+
{
|
666
|
+
public:
|
667
|
+
VALUE convert(const numo::UInt8& x) {
|
668
|
+
return x.value();
|
669
|
+
}
|
670
|
+
};
|
671
|
+
|
672
|
+
template<>
|
673
|
+
struct Type<numo::UInt16>
|
674
|
+
{
|
675
|
+
static bool verify()
|
676
|
+
{
|
677
|
+
return true;
|
678
|
+
}
|
679
|
+
};
|
680
|
+
|
681
|
+
template<>
|
682
|
+
class From_Ruby<numo::UInt16>
|
683
|
+
{
|
684
|
+
public:
|
685
|
+
numo::UInt16 convert(VALUE x)
|
686
|
+
{
|
687
|
+
return numo::UInt16(x);
|
688
|
+
}
|
689
|
+
};
|
690
|
+
|
691
|
+
template<>
|
692
|
+
class To_Ruby<numo::UInt16>
|
693
|
+
{
|
694
|
+
public:
|
695
|
+
VALUE convert(const numo::UInt16& x) {
|
696
|
+
return x.value();
|
697
|
+
}
|
698
|
+
};
|
699
|
+
|
700
|
+
template<>
|
701
|
+
struct Type<numo::UInt32>
|
702
|
+
{
|
703
|
+
static bool verify()
|
704
|
+
{
|
705
|
+
return true;
|
706
|
+
}
|
707
|
+
};
|
708
|
+
|
709
|
+
template<>
|
710
|
+
class From_Ruby<numo::UInt32>
|
711
|
+
{
|
712
|
+
public:
|
713
|
+
numo::UInt32 convert(VALUE x)
|
714
|
+
{
|
715
|
+
return numo::UInt32(x);
|
716
|
+
}
|
717
|
+
};
|
718
|
+
|
719
|
+
template<>
|
720
|
+
class To_Ruby<numo::UInt32>
|
721
|
+
{
|
722
|
+
public:
|
723
|
+
VALUE convert(const numo::UInt32& x) {
|
724
|
+
return x.value();
|
725
|
+
}
|
726
|
+
};
|
727
|
+
|
728
|
+
template<>
|
729
|
+
struct Type<numo::UInt64>
|
730
|
+
{
|
731
|
+
static bool verify()
|
732
|
+
{
|
733
|
+
return true;
|
734
|
+
}
|
735
|
+
};
|
736
|
+
|
737
|
+
template<>
|
738
|
+
class From_Ruby<numo::UInt64>
|
739
|
+
{
|
740
|
+
public:
|
741
|
+
numo::UInt64 convert(VALUE x)
|
742
|
+
{
|
743
|
+
return numo::UInt64(x);
|
744
|
+
}
|
745
|
+
};
|
746
|
+
|
747
|
+
template<>
|
748
|
+
class To_Ruby<numo::UInt64>
|
749
|
+
{
|
750
|
+
public:
|
751
|
+
VALUE convert(const numo::UInt64& x) {
|
752
|
+
return x.value();
|
753
|
+
}
|
754
|
+
};
|
755
|
+
|
756
|
+
template<>
|
757
|
+
struct Type<numo::SComplex>
|
758
|
+
{
|
759
|
+
static bool verify()
|
760
|
+
{
|
761
|
+
return true;
|
762
|
+
}
|
763
|
+
};
|
764
|
+
|
765
|
+
template<>
|
766
|
+
class From_Ruby<numo::SComplex>
|
767
|
+
{
|
768
|
+
public:
|
769
|
+
numo::SComplex convert(VALUE x)
|
770
|
+
{
|
771
|
+
return numo::SComplex(x);
|
772
|
+
}
|
773
|
+
};
|
774
|
+
|
775
|
+
template<>
|
776
|
+
class To_Ruby<numo::SComplex>
|
777
|
+
{
|
778
|
+
public:
|
779
|
+
VALUE convert(const numo::SComplex& x) {
|
780
|
+
return x.value();
|
781
|
+
}
|
782
|
+
};
|
783
|
+
|
784
|
+
template<>
|
785
|
+
struct Type<numo::DComplex>
|
786
|
+
{
|
787
|
+
static bool verify()
|
788
|
+
{
|
789
|
+
return true;
|
790
|
+
}
|
791
|
+
};
|
792
|
+
|
793
|
+
template<>
|
794
|
+
class From_Ruby<numo::DComplex>
|
795
|
+
{
|
796
|
+
public:
|
797
|
+
numo::DComplex convert(VALUE x)
|
798
|
+
{
|
799
|
+
return numo::DComplex(x);
|
800
|
+
}
|
801
|
+
};
|
802
|
+
|
803
|
+
template<>
|
804
|
+
class To_Ruby<numo::DComplex>
|
805
|
+
{
|
806
|
+
public:
|
807
|
+
VALUE convert(const numo::DComplex& x) {
|
808
|
+
return x.value();
|
809
|
+
}
|
810
|
+
};
|
811
|
+
|
812
|
+
template<>
|
813
|
+
struct Type<numo::Bit>
|
814
|
+
{
|
815
|
+
static bool verify()
|
816
|
+
{
|
817
|
+
return true;
|
818
|
+
}
|
819
|
+
};
|
820
|
+
|
821
|
+
template<>
|
822
|
+
class From_Ruby<numo::Bit>
|
823
|
+
{
|
824
|
+
public:
|
825
|
+
numo::Bit convert(VALUE x)
|
826
|
+
{
|
827
|
+
return numo::Bit(x);
|
828
|
+
}
|
829
|
+
};
|
830
|
+
|
831
|
+
template<>
|
832
|
+
class To_Ruby<numo::Bit>
|
833
|
+
{
|
834
|
+
public:
|
835
|
+
VALUE convert(const numo::Bit& x) {
|
836
|
+
return x.value();
|
837
|
+
}
|
838
|
+
};
|
839
|
+
|
840
|
+
template<>
|
841
|
+
struct Type<numo::RObject>
|
842
|
+
{
|
843
|
+
static bool verify()
|
844
|
+
{
|
845
|
+
return true;
|
846
|
+
}
|
847
|
+
};
|
848
|
+
|
849
|
+
template<>
|
850
|
+
class From_Ruby<numo::RObject>
|
851
|
+
{
|
852
|
+
public:
|
853
|
+
numo::RObject convert(VALUE x)
|
854
|
+
{
|
855
|
+
return numo::RObject(x);
|
856
|
+
}
|
857
|
+
};
|
858
|
+
|
859
|
+
template<>
|
860
|
+
class To_Ruby<numo::RObject>
|
861
|
+
{
|
862
|
+
public:
|
863
|
+
VALUE convert(const numo::RObject& x) {
|
864
|
+
return x.value();
|
865
|
+
}
|
866
|
+
};
|
867
|
+
}
|