faiss 0.6.5 → 0.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/faiss/extconf.rb +1 -0
- data/ext/faiss/index.cpp +42 -0
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/IVFlib.cpp +48 -51
- data/vendor/faiss/faiss/IVFlib.h +6 -2
- data/vendor/faiss/faiss/Index.cpp +2 -2
- data/vendor/faiss/faiss/Index.h +3 -3
- data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
- data/vendor/faiss/faiss/IndexBinary.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
- data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
- data/vendor/faiss/faiss/IndexHNSW.h +2 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
- data/vendor/faiss/faiss/IndexIDMap.h +10 -5
- data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
- data/vendor/faiss/faiss/IndexIVF.h +37 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
- data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
- data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
- data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
- data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
- data/vendor/faiss/faiss/VectorTransform.h +24 -1
- data/vendor/faiss/faiss/factory_tools.cpp +19 -1
- data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
- data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
- data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
- data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
- data/vendor/faiss/faiss/impl/HNSW.h +2 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
- data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
- data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
- data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
- data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
- data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
- data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
- data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
- data/vendor/faiss/faiss/impl/io.cpp +1 -0
- data/vendor/faiss/faiss/impl/io_macros.h +2 -2
- data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
- data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
- data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
- data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
- data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
- data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
- data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
- data/vendor/faiss/faiss/index_factory.cpp +30 -6
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
- data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
- data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
- data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
- data/vendor/faiss/faiss/utils/distances.cpp +246 -34
- data/vendor/faiss/faiss/utils/distances.h +145 -2
- data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
- data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
- data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
- data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
- data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
- data/vendor/faiss/faiss/utils/utils.cpp +4 -0
- metadata +20 -1
|
@@ -158,12 +158,12 @@ struct QuantizerTemplate<
|
|
|
158
158
|
Codec,
|
|
159
159
|
scalar_quantizer::QuantizerTemplateScaling::UNIFORM,
|
|
160
160
|
SIMDLevel::NONE> {
|
|
161
|
-
QuantizerTemplate(size_t
|
|
161
|
+
QuantizerTemplate(size_t d_in, const std::vector<float>& trained)
|
|
162
162
|
: QuantizerTemplate<
|
|
163
163
|
Codec,
|
|
164
164
|
scalar_quantizer::QuantizerTemplateScaling::UNIFORM,
|
|
165
|
-
SIMDLevel::NONE>(
|
|
166
|
-
assert(
|
|
165
|
+
SIMDLevel::NONE>(d_in, trained) {
|
|
166
|
+
assert(d_in % 8 == 0);
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -197,12 +197,12 @@ struct QuantizerTemplate<
|
|
|
197
197
|
Codec,
|
|
198
198
|
scalar_quantizer::QuantizerTemplateScaling::NON_UNIFORM,
|
|
199
199
|
SIMDLevel::NONE> {
|
|
200
|
-
QuantizerTemplate(size_t
|
|
200
|
+
QuantizerTemplate(size_t d_in, const std::vector<float>& trained)
|
|
201
201
|
: QuantizerTemplate<
|
|
202
202
|
Codec,
|
|
203
203
|
scalar_quantizer::QuantizerTemplateScaling::NON_UNIFORM,
|
|
204
|
-
SIMDLevel::NONE>(
|
|
205
|
-
assert(
|
|
204
|
+
SIMDLevel::NONE>(d_in, trained) {
|
|
205
|
+
assert(d_in % 8 == 0);
|
|
206
206
|
}
|
|
207
207
|
|
|
208
208
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -233,9 +233,9 @@ struct QuantizerTemplate<
|
|
|
233
233
|
: QuantizerLloydMax<NBITS, SIMDLevel::NONE> { \
|
|
234
234
|
using Base = QuantizerLloydMax<NBITS, SIMDLevel::NONE>; \
|
|
235
235
|
\
|
|
236
|
-
QuantizerLloydMax(size_t
|
|
237
|
-
: Base(
|
|
238
|
-
assert(
|
|
236
|
+
QuantizerLloydMax(size_t d_in, const std::vector<float>& trained) \
|
|
237
|
+
: Base(d_in, trained) { \
|
|
238
|
+
assert(d_in % 8 == 0); \
|
|
239
239
|
} \
|
|
240
240
|
\
|
|
241
241
|
FAISS_ALWAYS_INLINE simd8float32 \
|
|
@@ -267,9 +267,9 @@ struct QuantizerLloydMax<8, SIMDLevel::ARM_NEON>
|
|
|
267
267
|
: QuantizerLloydMax<8, SIMDLevel::NONE> {
|
|
268
268
|
using Base = QuantizerLloydMax<8, SIMDLevel::NONE>;
|
|
269
269
|
|
|
270
|
-
QuantizerLloydMax(size_t
|
|
271
|
-
: Base(
|
|
272
|
-
assert(
|
|
270
|
+
QuantizerLloydMax(size_t d_in, const std::vector<float>& trained)
|
|
271
|
+
: Base(d_in, trained) {
|
|
272
|
+
assert(d_in % 8 == 0);
|
|
273
273
|
}
|
|
274
274
|
|
|
275
275
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -295,9 +295,9 @@ struct QuantizerLloydMax<8, SIMDLevel::ARM_NEON>
|
|
|
295
295
|
|
|
296
296
|
template <>
|
|
297
297
|
struct QuantizerFP16<SIMDLevel::ARM_NEON> : QuantizerFP16<SIMDLevel::NONE> {
|
|
298
|
-
QuantizerFP16(size_t
|
|
299
|
-
: QuantizerFP16<SIMDLevel::NONE>(
|
|
300
|
-
assert(
|
|
298
|
+
QuantizerFP16(size_t d_in, const std::vector<float>& trained)
|
|
299
|
+
: QuantizerFP16<SIMDLevel::NONE>(d_in, trained) {
|
|
300
|
+
assert(d_in % 8 == 0);
|
|
301
301
|
}
|
|
302
302
|
|
|
303
303
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -316,9 +316,9 @@ struct QuantizerFP16<SIMDLevel::ARM_NEON> : QuantizerFP16<SIMDLevel::NONE> {
|
|
|
316
316
|
|
|
317
317
|
template <>
|
|
318
318
|
struct QuantizerBF16<SIMDLevel::ARM_NEON> : QuantizerBF16<SIMDLevel::NONE> {
|
|
319
|
-
QuantizerBF16(size_t
|
|
320
|
-
: QuantizerBF16<SIMDLevel::NONE>(
|
|
321
|
-
assert(
|
|
319
|
+
QuantizerBF16(size_t d_in, const std::vector<float>& trained)
|
|
320
|
+
: QuantizerBF16<SIMDLevel::NONE>(d_in, trained) {
|
|
321
|
+
assert(d_in % 8 == 0);
|
|
322
322
|
}
|
|
323
323
|
|
|
324
324
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -340,9 +340,9 @@ struct QuantizerBF16<SIMDLevel::ARM_NEON> : QuantizerBF16<SIMDLevel::NONE> {
|
|
|
340
340
|
template <>
|
|
341
341
|
struct Quantizer8bitDirect<SIMDLevel::ARM_NEON>
|
|
342
342
|
: Quantizer8bitDirect<SIMDLevel::NONE> {
|
|
343
|
-
Quantizer8bitDirect(size_t
|
|
344
|
-
: Quantizer8bitDirect<SIMDLevel::NONE>(
|
|
345
|
-
assert(
|
|
343
|
+
Quantizer8bitDirect(size_t d_in, const std::vector<float>& trained)
|
|
344
|
+
: Quantizer8bitDirect<SIMDLevel::NONE>(d_in, trained) {
|
|
345
|
+
assert(d_in % 8 == 0);
|
|
346
346
|
}
|
|
347
347
|
|
|
348
348
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -365,9 +365,9 @@ struct Quantizer8bitDirect<SIMDLevel::ARM_NEON>
|
|
|
365
365
|
template <>
|
|
366
366
|
struct Quantizer8bitDirectSigned<SIMDLevel::ARM_NEON>
|
|
367
367
|
: Quantizer8bitDirectSigned<SIMDLevel::NONE> {
|
|
368
|
-
Quantizer8bitDirectSigned(size_t
|
|
369
|
-
: Quantizer8bitDirectSigned<SIMDLevel::NONE>(
|
|
370
|
-
assert(
|
|
368
|
+
Quantizer8bitDirectSigned(size_t d_in, const std::vector<float>& trained)
|
|
369
|
+
: Quantizer8bitDirectSigned<SIMDLevel::NONE>(d_in, trained) {
|
|
370
|
+
assert(d_in % 8 == 0);
|
|
371
371
|
}
|
|
372
372
|
|
|
373
373
|
FAISS_ALWAYS_INLINE simd8float32
|
|
@@ -397,7 +397,7 @@ struct SimilarityL2<SIMDLevel::ARM_NEON> {
|
|
|
397
397
|
|
|
398
398
|
const float *y, *yi;
|
|
399
399
|
|
|
400
|
-
explicit SimilarityL2(const float*
|
|
400
|
+
explicit SimilarityL2(const float* y_in) : y(y_in), yi(nullptr) {}
|
|
401
401
|
|
|
402
402
|
simd8float32 accu8;
|
|
403
403
|
|
|
@@ -449,7 +449,7 @@ struct SimilarityIP<SIMDLevel::ARM_NEON> {
|
|
|
449
449
|
|
|
450
450
|
const float *y, *yi;
|
|
451
451
|
|
|
452
|
-
explicit SimilarityIP(const float*
|
|
452
|
+
explicit SimilarityIP(const float* y_in) : y(y_in), yi(nullptr) {}
|
|
453
453
|
|
|
454
454
|
simd8float32 accu8;
|
|
455
455
|
|
|
@@ -650,7 +650,7 @@ struct DCTemplate<
|
|
|
650
650
|
// single-code ones bit for bit.
|
|
651
651
|
template <size_t N>
|
|
652
652
|
FAISS_ALWAYS_INLINE void l2(
|
|
653
|
-
const uint8_t* const (&
|
|
653
|
+
const uint8_t* const (&code_ptrs)[N],
|
|
654
654
|
float (&dis)[N]) const {
|
|
655
655
|
float32x4_t acc[N][2] = {};
|
|
656
656
|
for (size_t i = 0; i < d; i += 8) {
|
|
@@ -659,7 +659,7 @@ struct DCTemplate<
|
|
|
659
659
|
const float32x4_t e0 = vld1q_f32(e.data() + i);
|
|
660
660
|
const float32x4_t e1 = vld1q_f32(e.data() + i + 4);
|
|
661
661
|
for (size_t n = 0; n < N; n++) {
|
|
662
|
-
const uint16x8_t c16 = vmovl_u8(vld1_u8(
|
|
662
|
+
const uint16x8_t c16 = vmovl_u8(vld1_u8(code_ptrs[n] + i));
|
|
663
663
|
const float32x4_t t0 = vfmsq_f32(
|
|
664
664
|
e0, a0, vcvtq_f32_u32(vmovl_u16(vget_low_u16(c16))));
|
|
665
665
|
const float32x4_t t1 =
|
|
@@ -774,8 +774,9 @@ struct DistanceComputerByte<Similarity, SIMDLevel::ARM_NEON>
|
|
|
774
774
|
int d;
|
|
775
775
|
std::vector<uint8_t> tmp;
|
|
776
776
|
|
|
777
|
-
DistanceComputerByte(int
|
|
778
|
-
|
|
777
|
+
DistanceComputerByte(int d_in, const std::vector<float>&)
|
|
778
|
+
: d(d_in), tmp(d_in) {
|
|
779
|
+
FAISS_THROW_IF_NOT(d_in % 16 == 0);
|
|
779
780
|
}
|
|
780
781
|
|
|
781
782
|
int compute_code_distance(const uint8_t* code1, const uint8_t* code2)
|
|
@@ -816,9 +817,9 @@ struct DistanceComputerByteSigned<Similarity, SIMDLevel::ARM_NEON>
|
|
|
816
817
|
int d;
|
|
817
818
|
std::vector<uint8_t> tmp;
|
|
818
819
|
|
|
819
|
-
DistanceComputerByteSigned(int
|
|
820
|
-
: d(
|
|
821
|
-
FAISS_THROW_IF_NOT(
|
|
820
|
+
DistanceComputerByteSigned(int d_in, const std::vector<float>&)
|
|
821
|
+
: d(d_in), tmp(d_in) {
|
|
822
|
+
FAISS_THROW_IF_NOT(d_in % 16 == 0);
|
|
822
823
|
}
|
|
823
824
|
|
|
824
825
|
int compute_code_distance(const uint8_t* code1, const uint8_t* code2)
|
|
@@ -37,9 +37,9 @@ struct simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
37
37
|
|
|
38
38
|
simd512bit_tpl() {}
|
|
39
39
|
|
|
40
|
-
explicit simd512bit_tpl(__m512i
|
|
40
|
+
explicit simd512bit_tpl(__m512i val) : i(val) {}
|
|
41
41
|
|
|
42
|
-
explicit simd512bit_tpl(__m512
|
|
42
|
+
explicit simd512bit_tpl(__m512 val) : f(val) {}
|
|
43
43
|
|
|
44
44
|
explicit simd512bit_tpl(const void* x)
|
|
45
45
|
: i(_mm512_loadu_si512((__m512i const*)x)) {}
|
|
@@ -79,8 +79,8 @@ struct simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
79
79
|
void bin(char bits[513]) const {
|
|
80
80
|
char bytes[64];
|
|
81
81
|
storeu((void*)bytes);
|
|
82
|
-
for (int
|
|
83
|
-
bits[
|
|
82
|
+
for (int idx = 0; idx < 512; idx++) {
|
|
83
|
+
bits[idx] = '0' + ((bytes[idx / 8] >> (idx % 8)) & 1);
|
|
84
84
|
}
|
|
85
85
|
bits[512] = 0;
|
|
86
86
|
}
|
|
@@ -97,8 +97,8 @@ template <>
|
|
|
97
97
|
struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
98
98
|
simd32uint16_tpl() {}
|
|
99
99
|
|
|
100
|
-
explicit simd32uint16_tpl(__m512i
|
|
101
|
-
: simd512bit_tpl<SIMDLevel::AVX512>(
|
|
100
|
+
explicit simd32uint16_tpl(__m512i val)
|
|
101
|
+
: simd512bit_tpl<SIMDLevel::AVX512>(val) {}
|
|
102
102
|
|
|
103
103
|
explicit simd32uint16_tpl(int x)
|
|
104
104
|
: simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_epi16(x)) {}
|
|
@@ -131,9 +131,9 @@ struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
131
131
|
#pragma GCC diagnostic push
|
|
132
132
|
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
|
133
133
|
#endif
|
|
134
|
-
for (int
|
|
134
|
+
for (int idx = 0; idx < 32; idx++) {
|
|
135
135
|
ptr += snprintf(
|
|
136
|
-
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[
|
|
136
|
+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
|
|
137
137
|
}
|
|
138
138
|
#if defined(__GNUC__) || defined(__clang__)
|
|
139
139
|
#pragma GCC diagnostic pop
|
|
@@ -213,10 +213,10 @@ struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
// for debugging only
|
|
216
|
-
uint16_t operator[](int
|
|
216
|
+
uint16_t operator[](int idx) const {
|
|
217
217
|
ALIGNED(64) uint16_t tab[32];
|
|
218
218
|
store(tab);
|
|
219
|
-
return tab[
|
|
219
|
+
return tab[idx];
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
void accu_min(simd32uint16_tpl incoming) {
|
|
@@ -241,8 +241,8 @@ template <>
|
|
|
241
241
|
struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
242
242
|
simd64uint8_tpl() {}
|
|
243
243
|
|
|
244
|
-
explicit simd64uint8_tpl(__m512i
|
|
245
|
-
: simd512bit_tpl<SIMDLevel::AVX512>(
|
|
244
|
+
explicit simd64uint8_tpl(__m512i val)
|
|
245
|
+
: simd512bit_tpl<SIMDLevel::AVX512>(val) {}
|
|
246
246
|
|
|
247
247
|
explicit simd64uint8_tpl(int x)
|
|
248
248
|
: simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_epi8(x)) {}
|
|
@@ -275,9 +275,9 @@ struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
275
275
|
#pragma GCC diagnostic push
|
|
276
276
|
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
|
277
277
|
#endif
|
|
278
|
-
for (int
|
|
278
|
+
for (int idx = 0; idx < 64; idx++) {
|
|
279
279
|
ptr += snprintf(
|
|
280
|
-
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[
|
|
280
|
+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
|
|
281
281
|
}
|
|
282
282
|
#if defined(__GNUC__) || defined(__clang__)
|
|
283
283
|
#pragma GCC diagnostic pop
|
|
@@ -329,10 +329,10 @@ struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
|
|
|
329
329
|
}
|
|
330
330
|
|
|
331
331
|
// for debugging only
|
|
332
|
-
uint8_t operator[](int
|
|
332
|
+
uint8_t operator[](int idx) const {
|
|
333
333
|
ALIGNED(64) uint8_t tab[64];
|
|
334
334
|
store(tab);
|
|
335
|
-
return tab[
|
|
335
|
+
return tab[idx];
|
|
336
336
|
}
|
|
337
337
|
};
|
|
338
338
|
|
|
@@ -342,8 +342,8 @@ struct simd16float32_tpl<SIMDLevel::AVX512>
|
|
|
342
342
|
: simd512bit_tpl<SIMDLevel::AVX512> {
|
|
343
343
|
simd16float32_tpl() {}
|
|
344
344
|
|
|
345
|
-
explicit simd16float32_tpl(__m512
|
|
346
|
-
: simd512bit_tpl<SIMDLevel::AVX512>(
|
|
345
|
+
explicit simd16float32_tpl(__m512 val)
|
|
346
|
+
: simd512bit_tpl<SIMDLevel::AVX512>(val) {}
|
|
347
347
|
|
|
348
348
|
explicit simd16float32_tpl(float x)
|
|
349
349
|
: simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_ps(x)) {}
|
|
@@ -389,9 +389,9 @@ struct simd16float32_tpl<SIMDLevel::AVX512>
|
|
|
389
389
|
storeu(tab);
|
|
390
390
|
char res[1000];
|
|
391
391
|
char* ptr = res;
|
|
392
|
-
for (int
|
|
392
|
+
for (int idx = 0; idx < 16; idx++) {
|
|
393
393
|
ptr += snprintf(
|
|
394
|
-
ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[
|
|
394
|
+
ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[idx]);
|
|
395
395
|
}
|
|
396
396
|
ptr[-1] = 0;
|
|
397
397
|
return std::string(res);
|
|
@@ -1376,7 +1376,7 @@ inline void cmplt_min_max_fast(
|
|
|
1376
1376
|
namespace {
|
|
1377
1377
|
|
|
1378
1378
|
// get even float32's of a and b, interleaved
|
|
1379
|
-
simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
|
|
1379
|
+
inline simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
|
|
1380
1380
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
|
|
1381
1381
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
|
|
1382
1382
|
return simd8float32_tpl<SIMDLevel::ARM_NEON>{
|
|
@@ -1384,7 +1384,7 @@ simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
|
|
|
1384
1384
|
}
|
|
1385
1385
|
|
|
1386
1386
|
// get odd float32's of a and b, interleaved
|
|
1387
|
-
simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
|
|
1387
|
+
inline simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
|
|
1388
1388
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
|
|
1389
1389
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
|
|
1390
1390
|
return simd8float32_tpl<SIMDLevel::ARM_NEON>{
|
|
@@ -1393,14 +1393,14 @@ simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
|
|
|
1393
1393
|
|
|
1394
1394
|
// 3 cycles
|
|
1395
1395
|
// if the lanes are a = [a0 a1] and b = [b0 b1], return [a0 b0]
|
|
1396
|
-
simd8float32_tpl<SIMDLevel::ARM_NEON> getlow128(
|
|
1396
|
+
inline simd8float32_tpl<SIMDLevel::ARM_NEON> getlow128(
|
|
1397
1397
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
|
|
1398
1398
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
|
|
1399
1399
|
return simd8float32_tpl<SIMDLevel::ARM_NEON>{
|
|
1400
1400
|
float32x4x2_t{a.data.val[0], b.data.val[0]}};
|
|
1401
1401
|
}
|
|
1402
1402
|
|
|
1403
|
-
simd8float32_tpl<SIMDLevel::ARM_NEON> gethigh128(
|
|
1403
|
+
inline simd8float32_tpl<SIMDLevel::ARM_NEON> gethigh128(
|
|
1404
1404
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
|
|
1405
1405
|
const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
|
|
1406
1406
|
return simd8float32_tpl<SIMDLevel::ARM_NEON>{
|
|
@@ -25,7 +25,8 @@ size_t ZeroCopyIOReader::get_data_view(void** ptr, size_t size, size_t nitems) {
|
|
|
25
25
|
actual_size = total_ - rp_;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// Whole items only, so that reading a truncated vector fails.
|
|
29
|
+
size_t actual_nitems = actual_size / size;
|
|
29
30
|
if (actual_nitems == 0) {
|
|
30
31
|
return 0;
|
|
31
32
|
}
|
|
@@ -57,6 +57,10 @@
|
|
|
57
57
|
#include <faiss/IndexBinaryHash.h>
|
|
58
58
|
#include <faiss/IndexBinaryIVF.h>
|
|
59
59
|
|
|
60
|
+
#ifdef FAISS_ENABLE_DISK_IVF
|
|
61
|
+
#include <faiss/IndexIVFRaBitQSSD.h>
|
|
62
|
+
#endif
|
|
63
|
+
|
|
60
64
|
#ifdef FAISS_ENABLE_SVS
|
|
61
65
|
#include <faiss/svs/IndexSVSFlat.h>
|
|
62
66
|
#include <faiss/svs/IndexSVSIVF.h>
|
|
@@ -247,8 +251,11 @@ VectorTransform* parse_VectorTransform(const std::string& description, int d) {
|
|
|
247
251
|
if (match("RR([0-9]+)?")) {
|
|
248
252
|
return new RandomRotationMatrix(d, mres_to_int(sm[1], d));
|
|
249
253
|
}
|
|
250
|
-
|
|
251
|
-
|
|
254
|
+
// "HR" pads d to a power of 2, "HRk" to a multiple of 64 (the FHT-Kac
|
|
255
|
+
// rotation, see HadamardRotation). An optional seed follows: "HRk7".
|
|
256
|
+
if (match("HR(k?)([0-9]+)?")) {
|
|
257
|
+
return new HadamardRotation(
|
|
258
|
+
d, mres_to_int(sm[2], 12345), sm[1].length() == 0);
|
|
252
259
|
}
|
|
253
260
|
if (match("ITQ([0-9]+)?")) {
|
|
254
261
|
return new ITQTransform(d, mres_to_int(sm[1], d), sm[1].length() > 0);
|
|
@@ -387,7 +394,10 @@ IndexIVF* parse_IndexIVF(
|
|
|
387
394
|
auto match = [&sm, &code_string](const std::string pattern) {
|
|
388
395
|
return re_match(code_string, pattern, sm);
|
|
389
396
|
};
|
|
390
|
-
|
|
397
|
+
// we cannot release the quantizer until we know no error happens and
|
|
398
|
+
// destroys the owning index since in this path the index does not own
|
|
399
|
+
// quantizer
|
|
400
|
+
auto get_q = [&quantizer] { return quantizer.get(); };
|
|
391
401
|
int d = quantizer->d;
|
|
392
402
|
|
|
393
403
|
if (match("Flat")) {
|
|
@@ -534,6 +544,18 @@ IndexIVF* parse_IndexIVF(
|
|
|
534
544
|
}
|
|
535
545
|
return index_ivf;
|
|
536
546
|
}
|
|
547
|
+
#ifdef FAISS_ENABLE_DISK_IVF
|
|
548
|
+
// "RaBitQSSD<nb_bits>_<mem_dim>". Call attach_file() before add/search.
|
|
549
|
+
if (match("RaBitQSSD([2-9])_([0-9]+)")) {
|
|
550
|
+
uint8_t nb_bits = mres_to_int(sm[1]);
|
|
551
|
+
size_t mem_dim = mres_to_int(sm[2]);
|
|
552
|
+
auto* index_ivf = new IndexIVFRaBitQSSD(
|
|
553
|
+
quantizer.get(), d, nlist, mem_dim, nb_bits, mt);
|
|
554
|
+
quantizer.release();
|
|
555
|
+
index_ivf->own_invlists = own_il;
|
|
556
|
+
return index_ivf;
|
|
557
|
+
}
|
|
558
|
+
#endif
|
|
537
559
|
// IndexIVFRaBitQ with optional nb_bits (1-9)
|
|
538
560
|
// Accepts: "RaBitQ" (default 1-bit) or "RaBitQ{nb_bits}" (e.g., "RaBitQ4")
|
|
539
561
|
if (match("RaBitQ([1-9])?")) {
|
|
@@ -1258,8 +1280,8 @@ std::unique_ptr<Index> index_factory_sub(
|
|
|
1258
1280
|
|
|
1259
1281
|
// IndexIVF
|
|
1260
1282
|
{
|
|
1261
|
-
size_t nlist;
|
|
1262
|
-
bool use_2layer;
|
|
1283
|
+
size_t nlist = 0;
|
|
1284
|
+
bool use_2layer = false;
|
|
1263
1285
|
size_t comma = description.find(',');
|
|
1264
1286
|
std::string coarse_string = description.substr(0, comma);
|
|
1265
1287
|
// Match coarse quantizer part first
|
|
@@ -1283,7 +1305,8 @@ std::unique_ptr<Index> index_factory_sub(
|
|
|
1283
1305
|
description.c_str());
|
|
1284
1306
|
int M = std::stoi(sm[1].str()), nbit = mres_to_int(sm[2], 8, 1);
|
|
1285
1307
|
Index2Layer* index_2l =
|
|
1286
|
-
new Index2Layer(quantizer.
|
|
1308
|
+
new Index2Layer(quantizer.get(), nlist, M, nbit);
|
|
1309
|
+
(void)quantizer.release(); // now owned by index_2l
|
|
1287
1310
|
index_2l->q1.own_fields = true;
|
|
1288
1311
|
index_2l->q1.quantizer_trains_alone =
|
|
1289
1312
|
get_trains_alone(index_2l->q1.quantizer);
|
|
@@ -1298,6 +1321,7 @@ std::unique_ptr<Index> index_factory_sub(
|
|
|
1298
1321
|
"could not parse code description %s in %s",
|
|
1299
1322
|
code_description.c_str(),
|
|
1300
1323
|
description.c_str());
|
|
1324
|
+
(void)quantizer.release(); // now owned by index_ivf
|
|
1301
1325
|
return std::unique_ptr<Index>(fix_ivf_fields(index_ivf));
|
|
1302
1326
|
}
|
|
1303
1327
|
}
|
|
@@ -93,7 +93,7 @@ void DirectMap::add_single_id(idx_t id, idx_t list_no, size_t offset) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
if (type == Array) {
|
|
96
|
-
assert(id == array.size());
|
|
96
|
+
assert(static_cast<size_t>(id) == array.size());
|
|
97
97
|
if (list_no >= 0) {
|
|
98
98
|
array.push_back(lo_build(list_no, offset));
|
|
99
99
|
} else {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <faiss/invlists/DiskIOBackend.h>
|
|
9
|
+
|
|
10
|
+
#include <libaio.h>
|
|
11
|
+
#include <sys/types.h>
|
|
12
|
+
#include <unistd.h>
|
|
13
|
+
|
|
14
|
+
#include <algorithm>
|
|
15
|
+
#include <cerrno>
|
|
16
|
+
#include <cstring>
|
|
17
|
+
#include <ctime>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
#include <faiss/impl/FaissAssert.h>
|
|
21
|
+
|
|
22
|
+
namespace faiss {
|
|
23
|
+
|
|
24
|
+
DiskIOBackend::DiskIOBackend(int max_events_in) {
|
|
25
|
+
FAISS_THROW_IF_NOT(max_events_in > 0);
|
|
26
|
+
const int ret = io_setup(max_events_in, &ctx);
|
|
27
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
28
|
+
ret == 0,
|
|
29
|
+
"io_setup failed: %s%s",
|
|
30
|
+
strerror(-ret),
|
|
31
|
+
ret == -EAGAIN ? " (too many libaio contexts, see fs.aio-max-nr)"
|
|
32
|
+
: "");
|
|
33
|
+
pid = getpid();
|
|
34
|
+
max_events = max_events_in;
|
|
35
|
+
iocbs.resize(max_events);
|
|
36
|
+
iocb_ptrs.resize(max_events);
|
|
37
|
+
events.resize(max_events);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
DiskIOBackend::~DiskIOBackend() {
|
|
41
|
+
// blocks until the reads in flight are done, so the caller can free their
|
|
42
|
+
// buffers afterwards. The context of a parent process is not ours to
|
|
43
|
+
// destroy.
|
|
44
|
+
if (owned_by_this_process()) {
|
|
45
|
+
io_destroy(ctx);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void DiskIOBackend::stage(
|
|
50
|
+
int fd,
|
|
51
|
+
void* buf,
|
|
52
|
+
size_t len,
|
|
53
|
+
size_t offset,
|
|
54
|
+
void* user_data) {
|
|
55
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
56
|
+
n_staged < max_events, "DiskIOBackend: too many reads staged");
|
|
57
|
+
iocb* cb = &iocbs[n_staged];
|
|
58
|
+
io_prep_pread(cb, fd, buf, len, static_cast<long long>(offset));
|
|
59
|
+
cb->data = user_data;
|
|
60
|
+
iocb_ptrs[n_staged] = cb;
|
|
61
|
+
n_staged++;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void DiskIOBackend::submit() {
|
|
65
|
+
int done = 0;
|
|
66
|
+
while (done < n_staged) {
|
|
67
|
+
const int ret =
|
|
68
|
+
io_submit(ctx, n_staged - done, iocb_ptrs.data() + done);
|
|
69
|
+
if (ret == -EINTR || ret == -EAGAIN) {
|
|
70
|
+
continue; // transient, the queue is sized for all reads in flight
|
|
71
|
+
}
|
|
72
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
73
|
+
ret > 0,
|
|
74
|
+
"io_submit failed: %s",
|
|
75
|
+
ret < 0 ? strerror(-ret) : "no read accepted");
|
|
76
|
+
done += ret;
|
|
77
|
+
n_in_flight += ret;
|
|
78
|
+
}
|
|
79
|
+
n_staged = 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
int DiskIOBackend::reap(void** user_data, long* res, int max, int min_nr) {
|
|
83
|
+
max = std::min(max, max_events);
|
|
84
|
+
min_nr = std::min(min_nr, std::min(max, n_in_flight));
|
|
85
|
+
struct timespec no_wait = {0, 0};
|
|
86
|
+
const int ret = io_getevents(
|
|
87
|
+
ctx, min_nr, max, events.data(), min_nr > 0 ? nullptr : &no_wait);
|
|
88
|
+
if (ret == -EINTR) {
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
FAISS_THROW_IF_NOT_FMT(ret >= 0, "io_getevents failed: %s", strerror(-ret));
|
|
92
|
+
n_in_flight -= ret;
|
|
93
|
+
for (int i = 0; i < ret; i++) {
|
|
94
|
+
user_data[i] = events[i].data;
|
|
95
|
+
// bytes read or a negative errno, in an unsigned field
|
|
96
|
+
res[i] = static_cast<long>(events[i].res);
|
|
97
|
+
}
|
|
98
|
+
return ret;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
bool DiskIOBackend::wait_all() noexcept {
|
|
102
|
+
n_staged = 0;
|
|
103
|
+
while (n_in_flight > 0) {
|
|
104
|
+
const int ret = io_getevents(
|
|
105
|
+
ctx, n_in_flight, max_events, events.data(), nullptr);
|
|
106
|
+
if (ret == -EINTR) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (ret < 0) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
n_in_flight -= ret;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
bool DiskIOBackend::owned_by_this_process() const {
|
|
118
|
+
return pid == getpid();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
} // namespace faiss
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <libaio.h>
|
|
11
|
+
#include <sys/types.h>
|
|
12
|
+
|
|
13
|
+
#include <cstddef>
|
|
14
|
+
#include <vector>
|
|
15
|
+
|
|
16
|
+
namespace faiss {
|
|
17
|
+
|
|
18
|
+
/** Asynchronous reads with Linux libaio, for one thread.
|
|
19
|
+
*
|
|
20
|
+
* Reads are staged with stage(), sent to the kernel together with submit(),
|
|
21
|
+
* and collected with reap(). With a file opened with
|
|
22
|
+
* O_DIRECT, the buffer address, offset and length of every read must be
|
|
23
|
+
* multiples of the device sector size.
|
|
24
|
+
*/
|
|
25
|
+
struct DiskIOBackend {
|
|
26
|
+
io_context_t ctx = nullptr;
|
|
27
|
+
pid_t pid = 0; ///< process that created ctx
|
|
28
|
+
int max_events = 0; ///< at most max_events reads can be in flight
|
|
29
|
+
int n_staged = 0; ///< reads staged for the next submit()
|
|
30
|
+
int n_in_flight = 0; ///< submitted and not reaped
|
|
31
|
+
std::vector<iocb> iocbs;
|
|
32
|
+
std::vector<iocb*> iocb_ptrs;
|
|
33
|
+
std::vector<io_event> events;
|
|
34
|
+
|
|
35
|
+
explicit DiskIOBackend(int max_events);
|
|
36
|
+
~DiskIOBackend();
|
|
37
|
+
|
|
38
|
+
DiskIOBackend(const DiskIOBackend&) = delete;
|
|
39
|
+
DiskIOBackend& operator=(const DiskIOBackend&) = delete;
|
|
40
|
+
|
|
41
|
+
/// add a read of len bytes at offset into buf to the next submit();
|
|
42
|
+
/// user_data identifies it in reap()
|
|
43
|
+
void stage(int fd, void* buf, size_t len, size_t offset, void* user_data);
|
|
44
|
+
|
|
45
|
+
/// send the staged reads to the kernel
|
|
46
|
+
void submit();
|
|
47
|
+
|
|
48
|
+
/** Collect up to max completed reads, waiting until at least min_nr
|
|
49
|
+
* have completed (without waiting if min_nr is 0). min_nr is capped by
|
|
50
|
+
* max and by the reads in flight, and a signal can end the wait early.
|
|
51
|
+
*
|
|
52
|
+
* @param user_data output, the user_data of each completed read
|
|
53
|
+
* @param res output, the bytes read, or a negative errno
|
|
54
|
+
* @return number of completed reads
|
|
55
|
+
*/
|
|
56
|
+
int reap(void** user_data, long* res, int max, int min_nr = 0);
|
|
57
|
+
|
|
58
|
+
/** Drop the staged reads and wait for the submitted ones to complete,
|
|
59
|
+
* ignoring their results, so that their buffers can be freed and the
|
|
60
|
+
* context reused.
|
|
61
|
+
*
|
|
62
|
+
* @return false if waiting failed: reads may still be in flight
|
|
63
|
+
*/
|
|
64
|
+
bool wait_all() noexcept;
|
|
65
|
+
|
|
66
|
+
/// false in a process forked after the context was created: the context
|
|
67
|
+
/// belongs to the parent process and cannot be used
|
|
68
|
+
bool owned_by_this_process() const;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
} // namespace faiss
|
|
@@ -298,23 +298,24 @@ size_t ArrayInvertedLists::add_entries(
|
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
size_t ArrayInvertedLists::list_size(size_t list_no) const {
|
|
301
|
-
|
|
301
|
+
FAISS_THROW_IF_NOT(list_no < nlist);
|
|
302
302
|
return ids[list_no].size();
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
bool ArrayInvertedLists::is_empty(size_t list_no, void* inverted_list_context)
|
|
306
306
|
const {
|
|
307
307
|
FAISS_THROW_IF_NOT(inverted_list_context == nullptr);
|
|
308
|
+
FAISS_THROW_IF_NOT(list_no < nlist);
|
|
308
309
|
return ids[list_no].size() == 0;
|
|
309
310
|
}
|
|
310
311
|
|
|
311
312
|
const uint8_t* ArrayInvertedLists::get_codes(size_t list_no) const {
|
|
312
|
-
|
|
313
|
+
FAISS_THROW_IF_NOT(list_no < nlist);
|
|
313
314
|
return codes[list_no].data();
|
|
314
315
|
}
|
|
315
316
|
|
|
316
317
|
const idx_t* ArrayInvertedLists::get_ids(size_t list_no) const {
|
|
317
|
-
|
|
318
|
+
FAISS_THROW_IF_NOT(list_no < nlist);
|
|
318
319
|
return ids[list_no].data();
|
|
319
320
|
}
|
|
320
321
|
|
|
@@ -931,10 +932,12 @@ void StopWordsInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist_in)
|
|
|
931
932
|
* CappedInvertedLists implementation
|
|
932
933
|
******************************************/
|
|
933
934
|
|
|
934
|
-
CappedInvertedLists::CappedInvertedLists(
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
935
|
+
CappedInvertedLists::CappedInvertedLists(
|
|
936
|
+
InvertedLists* il0_in,
|
|
937
|
+
size_t maxsize_in)
|
|
938
|
+
: InvertedLists(il0_in->nlist, il0_in->code_size),
|
|
939
|
+
il0(il0_in),
|
|
940
|
+
maxsize(maxsize_in) {}
|
|
938
941
|
|
|
939
942
|
size_t CappedInvertedLists::list_size(size_t list_no) const {
|
|
940
943
|
size_t sz = il0->list_size(list_no);
|
|
@@ -974,9 +977,9 @@ const uint8_t* CappedInvertedLists::get_single_code(
|
|
|
974
977
|
return il0->get_single_code(list_no, offset);
|
|
975
978
|
}
|
|
976
979
|
|
|
977
|
-
void CappedInvertedLists::prefetch_lists(const idx_t* list_nos, int
|
|
980
|
+
void CappedInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist_in)
|
|
978
981
|
const {
|
|
979
|
-
il0->prefetch_lists(list_nos,
|
|
982
|
+
il0->prefetch_lists(list_nos, nlist_in);
|
|
980
983
|
}
|
|
981
984
|
|
|
982
985
|
size_t CappedInvertedLists::add_entries(
|