faiss 0.6.3 → 0.6.5
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 +8 -0
- data/ext/faiss/extconf.rb +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +175 -0
- data/vendor/faiss/faiss/Clustering.cpp +150 -60
- data/vendor/faiss/faiss/Clustering.h +37 -0
- data/vendor/faiss/faiss/IVFlib.cpp +99 -0
- data/vendor/faiss/faiss/IVFlib.h +15 -0
- data/vendor/faiss/faiss/Index.cpp +44 -0
- data/vendor/faiss/faiss/Index.h +18 -13
- data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
- data/vendor/faiss/faiss/IndexBinary.h +8 -0
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +23 -2
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
- data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
- data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
- data/vendor/faiss/faiss/IndexFastScan.h +6 -3
- data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
- data/vendor/faiss/faiss/IndexFlat.h +33 -2
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
- data/vendor/faiss/faiss/IndexHNSW.h +69 -7
- data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
- data/vendor/faiss/faiss/IndexIDMap.h +7 -0
- data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
- data/vendor/faiss/faiss/IndexIVF.h +53 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
- data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
- data/vendor/faiss/faiss/IndexIVFFlat.h +16 -0
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
- data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
- data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
- data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
- data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
- data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
- data/vendor/faiss/faiss/IndexReplicas.h +9 -0
- data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
- data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
- data/vendor/faiss/faiss/IndexShards.cpp +13 -3
- data/vendor/faiss/faiss/IndexShards.h +5 -0
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
- data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
- data/vendor/faiss/faiss/SuperKMeans.h +12 -6
- data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
- data/vendor/faiss/faiss/VectorTransform.h +3 -0
- data/vendor/faiss/faiss/clone_index.cpp +27 -0
- data/vendor/faiss/faiss/gpu/Dataset.h +112 -0
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
- data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +4 -5
- data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
- data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
- data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
- data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +287 -134
- data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
- data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
- data/vendor/faiss/faiss/impl/HNSW.cpp +465 -125
- data/vendor/faiss/faiss/impl/HNSW.h +118 -7
- data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
- data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
- data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
- data/vendor/faiss/faiss/impl/Panorama.h +14 -4
- data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
- data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
- data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
- data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
- data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
- data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +89 -13
- data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
- data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
- data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -1
- data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
- data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
- data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
- data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +18 -129
- data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
- data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +22 -1
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
- data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/index_read.cpp +302 -29
- data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
- data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
- data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
- data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
- data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
- data/vendor/faiss/faiss/impl/platform_macros.h +45 -0
- data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
- data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +23 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
- data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +6 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
- data/vendor/faiss/faiss/impl/simd_dispatch.h +47 -22
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
- data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
- data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
- data/vendor/faiss/faiss/index_factory.cpp +84 -1
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
- data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
- data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
- data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
- data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
- data/vendor/faiss/faiss/utils/bf16.h +36 -0
- data/vendor/faiss/faiss/utils/distances.cpp +246 -28
- data/vendor/faiss/faiss/utils/distances.h +34 -0
- data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
- data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
- data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
- data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
- data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
- data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
- data/vendor/faiss/faiss/utils/hamming.h +10 -1
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +20 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -1
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
- data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
- data/vendor/faiss/faiss/utils/prefetch.h +1 -26
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
- data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
- data/vendor/faiss/faiss/utils/rabitq_simd.h +51 -16
- data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -0
- data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
- data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
- data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
- data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
- data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
- data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
- data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +54 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
- data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +30 -0
- data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
- data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
- data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
- data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
- data/vendor/faiss/faiss/utils/utils.cpp +80 -0
- data/vendor/faiss/faiss/utils/utils.h +24 -0
- metadata +38 -5
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
#include <faiss/IVFlib.h>
|
|
9
9
|
#include <omp.h>
|
|
10
10
|
|
|
11
|
+
#include <algorithm>
|
|
11
12
|
#include <memory>
|
|
12
13
|
|
|
13
14
|
#include <faiss/IndexAdditiveQuantizer.h>
|
|
@@ -20,6 +21,7 @@
|
|
|
20
21
|
#include <faiss/impl/FaissAssert.h>
|
|
21
22
|
#include <faiss/impl/simd_dispatch.h>
|
|
22
23
|
#include <faiss/index_io.h>
|
|
24
|
+
#include <faiss/utils/Heap.h>
|
|
23
25
|
#include <faiss/utils/distances.h>
|
|
24
26
|
#include <faiss/utils/hamming.h>
|
|
25
27
|
#include <faiss/utils/utils.h>
|
|
@@ -407,6 +409,103 @@ void search_with_parameters(
|
|
|
407
409
|
}
|
|
408
410
|
}
|
|
409
411
|
|
|
412
|
+
namespace {
|
|
413
|
+
|
|
414
|
+
template <class C>
|
|
415
|
+
void scan_one_query_per_code(
|
|
416
|
+
const IndexIVF& index_ivf,
|
|
417
|
+
InvertedListScanner& scanner,
|
|
418
|
+
const idx_t* keys,
|
|
419
|
+
const float* coarse_dis,
|
|
420
|
+
size_t nprobe,
|
|
421
|
+
idx_t k,
|
|
422
|
+
float* distances,
|
|
423
|
+
idx_t* labels) {
|
|
424
|
+
heap_heapify<C>(k, distances, labels);
|
|
425
|
+
const InvertedLists* invlists = index_ivf.invlists;
|
|
426
|
+
for (size_t j = 0; j < nprobe; j++) {
|
|
427
|
+
const idx_t key = keys[j];
|
|
428
|
+
if (key < 0) {
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
scanner.set_list(key, coarse_dis[j]);
|
|
432
|
+
const size_t list_size = invlists->list_size(key);
|
|
433
|
+
InvertedLists::ScopedCodes codes(invlists, key);
|
|
434
|
+
InvertedLists::ScopedIds ids(invlists, key);
|
|
435
|
+
const uint8_t* code = codes.get();
|
|
436
|
+
for (size_t i = 0; i < list_size; i++, code += index_ivf.code_size) {
|
|
437
|
+
const float dis = scanner.distance_to_code(code);
|
|
438
|
+
if (C::cmp(distances[0], dis)) {
|
|
439
|
+
heap_replace_top<C>(k, distances, labels, dis, ids[i]);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
heap_reorder<C>(k, distances, labels);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
} // namespace
|
|
447
|
+
|
|
448
|
+
void search_with_distance_to_code(
|
|
449
|
+
const Index* index,
|
|
450
|
+
idx_t n,
|
|
451
|
+
const float* x,
|
|
452
|
+
idx_t k,
|
|
453
|
+
float* distances,
|
|
454
|
+
idx_t* labels,
|
|
455
|
+
size_t nprobe) {
|
|
456
|
+
std::unique_ptr<const float[]> del;
|
|
457
|
+
if (auto ip = dynamic_cast<const IndexPreTransform*>(index)) {
|
|
458
|
+
const float* xt = ip->apply_chain(n, x);
|
|
459
|
+
if (xt != x) {
|
|
460
|
+
del.reset(xt);
|
|
461
|
+
}
|
|
462
|
+
x = xt;
|
|
463
|
+
index = ip->index;
|
|
464
|
+
}
|
|
465
|
+
const IndexIVF* index_ivf = dynamic_cast<const IndexIVF*>(index);
|
|
466
|
+
FAISS_THROW_IF_NOT(index_ivf);
|
|
467
|
+
nprobe = std::min(nprobe, index_ivf->nlist);
|
|
468
|
+
|
|
469
|
+
std::vector<idx_t> Iq(nprobe * n);
|
|
470
|
+
std::vector<float> Dq(nprobe * n);
|
|
471
|
+
index_ivf->quantizer->search(n, x, nprobe, Dq.data(), Iq.data());
|
|
472
|
+
|
|
473
|
+
#pragma omp parallel
|
|
474
|
+
{
|
|
475
|
+
std::unique_ptr<InvertedListScanner> scanner(
|
|
476
|
+
index_ivf->get_InvertedListScanner());
|
|
477
|
+
#pragma omp for
|
|
478
|
+
for (idx_t q = 0; q < n; q++) {
|
|
479
|
+
scanner->set_query(x + q * index_ivf->d);
|
|
480
|
+
const idx_t* keys = Iq.data() + q * nprobe;
|
|
481
|
+
const float* coarse_dis = Dq.data() + q * nprobe;
|
|
482
|
+
float* D = distances + q * k;
|
|
483
|
+
idx_t* I = labels + q * k;
|
|
484
|
+
if (scanner->keep_max) {
|
|
485
|
+
scan_one_query_per_code<CMin<float, idx_t>>(
|
|
486
|
+
*index_ivf,
|
|
487
|
+
*scanner,
|
|
488
|
+
keys,
|
|
489
|
+
coarse_dis,
|
|
490
|
+
nprobe,
|
|
491
|
+
k,
|
|
492
|
+
D,
|
|
493
|
+
I);
|
|
494
|
+
} else {
|
|
495
|
+
scan_one_query_per_code<CMax<float, idx_t>>(
|
|
496
|
+
*index_ivf,
|
|
497
|
+
*scanner,
|
|
498
|
+
keys,
|
|
499
|
+
coarse_dis,
|
|
500
|
+
nprobe,
|
|
501
|
+
k,
|
|
502
|
+
D,
|
|
503
|
+
I);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
410
509
|
void range_search_with_parameters(
|
|
411
510
|
const Index* index,
|
|
412
511
|
idx_t n,
|
data/vendor/faiss/faiss/IVFlib.h
CHANGED
|
@@ -136,6 +136,21 @@ void search_with_parameters(
|
|
|
136
136
|
size_t* nb_dis = nullptr,
|
|
137
137
|
double* ms_per_stage = nullptr);
|
|
138
138
|
|
|
139
|
+
/** search an IndexIVF, possibly embedded in an IndexPreTransform, scoring
|
|
140
|
+
* every code with InvertedListScanner::distance_to_code, one call per code,
|
|
141
|
+
* as callers that run their own scan loop do (search() uses scan_codes,
|
|
142
|
+
* which batches codes). Meant for benchmarking that path. Each query is
|
|
143
|
+
* scanned by one thread; queries run in parallel.
|
|
144
|
+
*/
|
|
145
|
+
void search_with_distance_to_code(
|
|
146
|
+
const Index* index,
|
|
147
|
+
idx_t n,
|
|
148
|
+
const float* x,
|
|
149
|
+
idx_t k,
|
|
150
|
+
float* distances,
|
|
151
|
+
idx_t* labels,
|
|
152
|
+
size_t nprobe);
|
|
153
|
+
|
|
139
154
|
/** same as search_with_parameters but for range search */
|
|
140
155
|
void range_search_with_parameters(
|
|
141
156
|
const Index* index,
|
|
@@ -13,8 +13,11 @@
|
|
|
13
13
|
#include <faiss/impl/DistanceComputer.h>
|
|
14
14
|
#include <faiss/impl/FaissAssert.h>
|
|
15
15
|
#include <faiss/utils/distances.h>
|
|
16
|
+
#include <faiss/utils/utils.h>
|
|
16
17
|
|
|
18
|
+
#include <algorithm>
|
|
17
19
|
#include <cstring>
|
|
20
|
+
#include <vector>
|
|
18
21
|
|
|
19
22
|
namespace faiss {
|
|
20
23
|
|
|
@@ -24,6 +27,14 @@ void Index::train(idx_t /*n*/, const float* /*x*/) {
|
|
|
24
27
|
// does nothing by default
|
|
25
28
|
}
|
|
26
29
|
|
|
30
|
+
void Index::train(const gpu::Dataset&) {
|
|
31
|
+
FAISS_THROW_MSG("Dataset training not implemented for this type of index");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void Index::add(const gpu::Dataset&) {
|
|
35
|
+
FAISS_THROW_MSG("Dataset addition not implemented for this type of index");
|
|
36
|
+
}
|
|
37
|
+
|
|
27
38
|
void Index::train_with_queries(
|
|
28
39
|
idx_t /*n*/,
|
|
29
40
|
const float* /*x*/,
|
|
@@ -123,6 +134,39 @@ void Index::search_subset(
|
|
|
123
134
|
FAISS_THROW_MSG("search_subset not implemented for this type of index");
|
|
124
135
|
}
|
|
125
136
|
|
|
137
|
+
void Index::search_ex(
|
|
138
|
+
idx_t n,
|
|
139
|
+
const void* x,
|
|
140
|
+
NumericType numeric_type,
|
|
141
|
+
idx_t k,
|
|
142
|
+
float* distances,
|
|
143
|
+
idx_t* labels,
|
|
144
|
+
const SearchParameters* params) const {
|
|
145
|
+
if (numeric_type == NumericType::Float32) {
|
|
146
|
+
search(n, static_cast<const float*>(x), k, distances, labels, params);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
150
|
+
numeric_type == NumericType::Float16,
|
|
151
|
+
"Index::search: unsupported numeric type");
|
|
152
|
+
FAISS_THROW_IF_NOT(d > 0);
|
|
153
|
+
// widen blocks of at most 64 MiB of fp32 queries
|
|
154
|
+
const idx_t bs = std::max<idx_t>(
|
|
155
|
+
1, idx_t(64) * 1024 * 1024 / (sizeof(float) * idx_t(d)));
|
|
156
|
+
const uint16_t* x16 = static_cast<const uint16_t*>(x);
|
|
157
|
+
std::vector<float> buffer(size_t(std::min(n, bs)) * d);
|
|
158
|
+
for (idx_t i0 = 0; i0 < n; i0 += bs) {
|
|
159
|
+
const idx_t i1 = std::min(n, i0 + bs);
|
|
160
|
+
fp16_to_fp32(size_t(i1 - i0) * d, x16 + i0 * d, buffer.data());
|
|
161
|
+
search(i1 - i0,
|
|
162
|
+
buffer.data(),
|
|
163
|
+
k,
|
|
164
|
+
distances + i0 * k,
|
|
165
|
+
labels + i0 * k,
|
|
166
|
+
params);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
126
170
|
void Index::search1(const float*, ResultHandler&, SearchParameters*) const {
|
|
127
171
|
FAISS_THROW_MSG("search1 not implemented for this type of index");
|
|
128
172
|
}
|
data/vendor/faiss/faiss/Index.h
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
#include <cstdio>
|
|
17
17
|
|
|
18
18
|
#define FAISS_VERSION_MAJOR 1
|
|
19
|
-
#define FAISS_VERSION_MINOR
|
|
19
|
+
#define FAISS_VERSION_MINOR 16
|
|
20
20
|
#define FAISS_VERSION_PATCH 0
|
|
21
21
|
|
|
22
22
|
// Macro to combine the version components into a single string
|
|
@@ -49,6 +49,10 @@
|
|
|
49
49
|
|
|
50
50
|
namespace faiss {
|
|
51
51
|
|
|
52
|
+
namespace gpu {
|
|
53
|
+
class Dataset;
|
|
54
|
+
}
|
|
55
|
+
|
|
52
56
|
/// Forward declarations see impl/AuxIndexStructures.h, impl/IDSelector.h
|
|
53
57
|
/// and impl/DistanceComputer.h
|
|
54
58
|
struct IDSelector;
|
|
@@ -131,6 +135,9 @@ struct Index {
|
|
|
131
135
|
*/
|
|
132
136
|
virtual void train(idx_t n, const float* x);
|
|
133
137
|
|
|
138
|
+
/// Train from caller-controlled storage. Unsupported by default.
|
|
139
|
+
virtual void train(const gpu::Dataset& dataset);
|
|
140
|
+
|
|
134
141
|
/** Perform training on a representative set of vectors and a representative
|
|
135
142
|
* set of queries
|
|
136
143
|
*
|
|
@@ -163,6 +170,9 @@ struct Index {
|
|
|
163
170
|
*/
|
|
164
171
|
virtual void add(idx_t n, const float* x) = 0;
|
|
165
172
|
|
|
173
|
+
/// Add caller-controlled storage. Unsupported by default.
|
|
174
|
+
virtual void add(const gpu::Dataset& dataset);
|
|
175
|
+
|
|
166
176
|
virtual void add_ex(idx_t n, const void* x, NumericType numeric_type) {
|
|
167
177
|
if (numeric_type == NumericType::Float32) {
|
|
168
178
|
add(n, static_cast<const float*>(x));
|
|
@@ -212,6 +222,12 @@ struct Index {
|
|
|
212
222
|
idx_t* labels,
|
|
213
223
|
const SearchParameters* params = nullptr) const = 0;
|
|
214
224
|
|
|
225
|
+
/** search() for queries of the given numeric type
|
|
226
|
+
*
|
|
227
|
+
* Float32 calls search(). By default, Float16 queries (packed IEEE
|
|
228
|
+
* binary16, size n * d) are widened to fp32 in bounded blocks and passed
|
|
229
|
+
* to search(); indexes with a native fp16 search override this.
|
|
230
|
+
*/
|
|
215
231
|
virtual void search_ex(
|
|
216
232
|
idx_t n,
|
|
217
233
|
const void* x,
|
|
@@ -219,18 +235,7 @@ struct Index {
|
|
|
219
235
|
idx_t k,
|
|
220
236
|
float* distances,
|
|
221
237
|
idx_t* labels,
|
|
222
|
-
const SearchParameters* params = nullptr) const
|
|
223
|
-
if (numeric_type == NumericType::Float32) {
|
|
224
|
-
search(n,
|
|
225
|
-
static_cast<const float*>(x),
|
|
226
|
-
k,
|
|
227
|
-
distances,
|
|
228
|
-
labels,
|
|
229
|
-
params);
|
|
230
|
-
} else {
|
|
231
|
-
FAISS_THROW_MSG("Index::search: unsupported numeric type");
|
|
232
|
-
}
|
|
233
|
-
}
|
|
238
|
+
const SearchParameters* params = nullptr) const;
|
|
234
239
|
|
|
235
240
|
/** search one vector with a custom result handler */
|
|
236
241
|
virtual void search1(
|
|
@@ -29,6 +29,14 @@ void IndexBinary::train(idx_t, const uint8_t*) {
|
|
|
29
29
|
// Does nothing by default.
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
void IndexBinary::train(const gpu::Dataset&) {
|
|
33
|
+
FAISS_THROW_MSG("Dataset training not implemented for this type of index");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
void IndexBinary::add(const gpu::Dataset&) {
|
|
37
|
+
FAISS_THROW_MSG("Dataset addition not implemented for this type of index");
|
|
38
|
+
}
|
|
39
|
+
|
|
32
40
|
void IndexBinary::range_search(
|
|
33
41
|
idx_t,
|
|
34
42
|
const uint8_t*,
|
|
@@ -52,6 +52,10 @@ struct IndexBinary {
|
|
|
52
52
|
* @param x training vectors, size n * d / 8
|
|
53
53
|
*/
|
|
54
54
|
virtual void train(idx_t n, const uint8_t* x);
|
|
55
|
+
|
|
56
|
+
/// Train from caller-controlled storage. Unsupported by default.
|
|
57
|
+
virtual void train(const gpu::Dataset& dataset);
|
|
58
|
+
|
|
55
59
|
virtual void train_ex(idx_t n, const void* x, NumericType numeric_type) {
|
|
56
60
|
if (numeric_type == NumericType::UInt8) {
|
|
57
61
|
train(n, static_cast<const uint8_t*>(x));
|
|
@@ -66,6 +70,10 @@ struct IndexBinary {
|
|
|
66
70
|
* @param x input matrix, size n * d / 8
|
|
67
71
|
*/
|
|
68
72
|
virtual void add(idx_t n, const uint8_t* x) = 0;
|
|
73
|
+
|
|
74
|
+
/// Add caller-controlled storage. Unsupported by default.
|
|
75
|
+
virtual void add(const gpu::Dataset& dataset);
|
|
76
|
+
|
|
69
77
|
virtual void add_ex(idx_t n, const void* x, NumericType numeric_type) {
|
|
70
78
|
if (numeric_type == NumericType::UInt8) {
|
|
71
79
|
add(n, static_cast<const uint8_t*>(x));
|
|
@@ -9,9 +9,11 @@
|
|
|
9
9
|
|
|
10
10
|
#include <faiss/IndexBinaryFromFloat.h>
|
|
11
11
|
|
|
12
|
+
#include <faiss/MetricType.h>
|
|
12
13
|
#include <faiss/impl/FaissAssert.h>
|
|
13
14
|
#include <faiss/utils/utils.h>
|
|
14
15
|
#include <algorithm>
|
|
16
|
+
#include <cmath>
|
|
15
17
|
#include <memory>
|
|
16
18
|
|
|
17
19
|
namespace faiss {
|
|
@@ -67,8 +69,27 @@ void IndexBinaryFromFloat::search(
|
|
|
67
69
|
binary_to_real(bn * d, x + b * code_size, xf.get());
|
|
68
70
|
|
|
69
71
|
index->search(bn, xf.get(), k, df.get(), labels + b * k);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
// binary_to_real maps bits to {-1, +1}, so the float distance relates
|
|
73
|
+
// to the Hamming distance differently for each metric. In all cases the
|
|
74
|
+
// mapping is monotonic, so the result order needs no adjustment.
|
|
75
|
+
switch (index->metric_type) {
|
|
76
|
+
case METRIC_INNER_PRODUCT:
|
|
77
|
+
for (int i = 0; i < bn * k; ++i) {
|
|
78
|
+
distances[b * k + i] =
|
|
79
|
+
int32_t(std::round((d - df[i]) / 2.0));
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case METRIC_L1:
|
|
83
|
+
for (int i = 0; i < bn * k; ++i) {
|
|
84
|
+
distances[b * k + i] = int32_t(std::round(df[i] / 2.0));
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
case METRIC_L2:
|
|
88
|
+
default:
|
|
89
|
+
for (int i = 0; i < bn * k; ++i) {
|
|
90
|
+
distances[b * k + i] = int32_t(std::round(df[i] / 4.0));
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
72
93
|
}
|
|
73
94
|
}
|
|
74
95
|
}
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
#include <faiss/IndexBinaryHNSW.h>
|
|
9
9
|
|
|
10
|
-
#include <omp.h>
|
|
11
10
|
#include <cassert>
|
|
12
11
|
#include <cmath>
|
|
13
12
|
#include <cstdio>
|
|
@@ -25,6 +24,7 @@
|
|
|
25
24
|
#include <faiss/impl/VisitedTable.h>
|
|
26
25
|
#include <faiss/utils/Heap.h>
|
|
27
26
|
#include <faiss/utils/random.h>
|
|
27
|
+
#include <faiss/utils/utils.h>
|
|
28
28
|
|
|
29
29
|
#include <faiss/impl/simd_dispatch.h>
|
|
30
30
|
|
|
@@ -39,140 +39,6 @@
|
|
|
39
39
|
|
|
40
40
|
namespace faiss {
|
|
41
41
|
|
|
42
|
-
/**************************************************************
|
|
43
|
-
* add / search blocks of descriptors
|
|
44
|
-
**************************************************************/
|
|
45
|
-
|
|
46
|
-
namespace {
|
|
47
|
-
|
|
48
|
-
void hnsw_add_vertices(
|
|
49
|
-
IndexBinaryHNSW& index_hnsw,
|
|
50
|
-
size_t n0,
|
|
51
|
-
size_t n,
|
|
52
|
-
const uint8_t* x,
|
|
53
|
-
bool verbose,
|
|
54
|
-
bool preset_levels = false) {
|
|
55
|
-
HNSW& hnsw = index_hnsw.hnsw;
|
|
56
|
-
size_t ntotal = n0 + n;
|
|
57
|
-
double t0 = getmillisecs();
|
|
58
|
-
if (verbose) {
|
|
59
|
-
printf("hnsw_add_vertices: adding %zd elements on top of %zd "
|
|
60
|
-
"(preset_levels=%d)\n",
|
|
61
|
-
n,
|
|
62
|
-
n0,
|
|
63
|
-
int(preset_levels));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
int max_level = hnsw.prepare_level_tab(n, preset_levels);
|
|
67
|
-
|
|
68
|
-
if (verbose) {
|
|
69
|
-
printf(" max_level = %d\n", max_level);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
auto& locks = index_hnsw.locks;
|
|
73
|
-
locks.prepare(ntotal);
|
|
74
|
-
|
|
75
|
-
// add vectors from highest to lowest level
|
|
76
|
-
std::vector<int> hist;
|
|
77
|
-
std::vector<int> order(n);
|
|
78
|
-
|
|
79
|
-
{ // make buckets with vectors of the same level
|
|
80
|
-
|
|
81
|
-
// build histogram
|
|
82
|
-
for (size_t i = 0; i < n; i++) {
|
|
83
|
-
HNSW::storage_idx_t pt_id =
|
|
84
|
-
static_cast<HNSW::storage_idx_t>(i + n0);
|
|
85
|
-
int pt_level = hnsw.levels[pt_id] - 1;
|
|
86
|
-
while (pt_level >= static_cast<int>(hist.size())) {
|
|
87
|
-
hist.push_back(0);
|
|
88
|
-
}
|
|
89
|
-
hist[pt_level]++;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// accumulate
|
|
93
|
-
std::vector<int> offsets(hist.size() + 1, 0);
|
|
94
|
-
for (size_t i = 0; i < hist.size() - 1; i++) {
|
|
95
|
-
offsets[i + 1] = offsets[i] + hist[i];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// bucket sort
|
|
99
|
-
for (size_t i = 0; i < n; i++) {
|
|
100
|
-
HNSW::storage_idx_t pt_id =
|
|
101
|
-
static_cast<HNSW::storage_idx_t>(i + n0);
|
|
102
|
-
int pt_level = hnsw.levels[pt_id] - 1;
|
|
103
|
-
order[offsets[pt_level]++] = pt_id;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
{ // perform add
|
|
108
|
-
RandomGenerator rng2(789);
|
|
109
|
-
|
|
110
|
-
size_t i1 = static_cast<int>(n);
|
|
111
|
-
|
|
112
|
-
for (int pt_level = static_cast<int>(hist.size()) - 1;
|
|
113
|
-
pt_level >= int(!index_hnsw.init_level0);
|
|
114
|
-
pt_level--) {
|
|
115
|
-
size_t i0 = i1 - hist[pt_level];
|
|
116
|
-
|
|
117
|
-
if (verbose) {
|
|
118
|
-
printf("Adding %zu elements at level %d\n", i1 - i0, pt_level);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// random permutation to get rid of dataset order bias
|
|
122
|
-
for (size_t j = i0; j < i1; j++) {
|
|
123
|
-
std::swap(
|
|
124
|
-
order[j],
|
|
125
|
-
order[j + rng2.rand_int(static_cast<int>(i1 - j))]);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
#pragma omp parallel
|
|
129
|
-
{
|
|
130
|
-
std::unique_ptr<VisitedTable> vt = VisitedTable::create(ntotal);
|
|
131
|
-
|
|
132
|
-
std::unique_ptr<DistanceComputer> dis(
|
|
133
|
-
index_hnsw.get_distance_computer());
|
|
134
|
-
bool do_display = verbose && omp_get_thread_num() == 0;
|
|
135
|
-
size_t prev_display = 0;
|
|
136
|
-
|
|
137
|
-
#pragma omp for schedule(dynamic)
|
|
138
|
-
for (int64_t i = i0; i < i1; i++) {
|
|
139
|
-
HNSW::storage_idx_t pt_id = order[i];
|
|
140
|
-
dis->set_query(
|
|
141
|
-
(float*)(x + (pt_id - n0) * index_hnsw.code_size));
|
|
142
|
-
|
|
143
|
-
hnsw.add_with_locks(
|
|
144
|
-
*dis,
|
|
145
|
-
pt_level,
|
|
146
|
-
pt_id,
|
|
147
|
-
locks,
|
|
148
|
-
*vt,
|
|
149
|
-
index_hnsw.keep_max_size_level0 && (pt_level == 0));
|
|
150
|
-
|
|
151
|
-
if (do_display && i - i0 > prev_display + 10000) {
|
|
152
|
-
prev_display = i - i0;
|
|
153
|
-
printf(" %zu / %zu\r", i - i0, i1 - i0);
|
|
154
|
-
fflush(stdout);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
i1 = i0;
|
|
159
|
-
}
|
|
160
|
-
if (index_hnsw.init_level0) {
|
|
161
|
-
FAISS_ASSERT(i1 == 0);
|
|
162
|
-
} else {
|
|
163
|
-
FAISS_ASSERT((i1 - hist[0]) == 0);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (verbose) {
|
|
167
|
-
printf("Done in %.3f ms\n", getmillisecs() - t0);
|
|
168
|
-
}
|
|
169
|
-
if (!index_hnsw.retain_locks) {
|
|
170
|
-
locks.clear();
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
} // anonymous namespace
|
|
175
|
-
|
|
176
42
|
/**************************************************************
|
|
177
43
|
* IndexBinaryHNSW implementation
|
|
178
44
|
**************************************************************/
|
|
@@ -256,7 +122,9 @@ void IndexBinaryHNSW::search(
|
|
|
256
122
|
}
|
|
257
123
|
}
|
|
258
124
|
|
|
259
|
-
|
|
125
|
+
if (get_search_stats_enabled()) {
|
|
126
|
+
hnsw_stats.combine_atomic({n1, n2, ndis, nhops});
|
|
127
|
+
}
|
|
260
128
|
|
|
261
129
|
#pragma omp parallel for
|
|
262
130
|
for (idx_t i = 0; i < n * k; ++i) {
|
|
@@ -270,18 +138,25 @@ void IndexBinaryHNSW::add(idx_t n, const uint8_t* x) {
|
|
|
270
138
|
storage->add(n, x);
|
|
271
139
|
ntotal = storage->ntotal;
|
|
272
140
|
|
|
273
|
-
|
|
274
|
-
|
|
141
|
+
bool preset_levels = hnsw.levels.size() == static_cast<size_t>(ntotal);
|
|
142
|
+
|
|
143
|
+
hnsw_add_vertices_deterministic(
|
|
144
|
+
hnsw,
|
|
275
145
|
n0,
|
|
276
146
|
n,
|
|
277
|
-
|
|
147
|
+
d,
|
|
148
|
+
init_level0,
|
|
149
|
+
keep_max_size_level0,
|
|
150
|
+
preset_levels,
|
|
278
151
|
verbose,
|
|
279
|
-
|
|
152
|
+
[this] { return get_distance_computer(); },
|
|
153
|
+
[this, x, n0](DistanceComputer& dc, HNSW::storage_idx_t pt_id) {
|
|
154
|
+
dc.set_query((const float*)(x + (pt_id - n0) * code_size));
|
|
155
|
+
});
|
|
280
156
|
}
|
|
281
157
|
|
|
282
158
|
void IndexBinaryHNSW::reset() {
|
|
283
159
|
hnsw.reset();
|
|
284
|
-
locks.clear();
|
|
285
160
|
storage->reset();
|
|
286
161
|
ntotal = 0;
|
|
287
162
|
}
|
|
@@ -301,6 +176,81 @@ DistanceComputer* IndexBinaryHNSW::get_distance_computer() const {
|
|
|
301
176
|
});
|
|
302
177
|
}
|
|
303
178
|
|
|
179
|
+
namespace {
|
|
180
|
+
|
|
181
|
+
// The sample of an IndexBinaryHNSW. Hamming distances are integers, so a
|
|
182
|
+
// nearer node has a strictly smaller distance.
|
|
183
|
+
struct BinaryLinkSample : HNSWLinkSampleSearch {
|
|
184
|
+
const IndexBinaryHNSW& index;
|
|
185
|
+
const uint8_t* x;
|
|
186
|
+
|
|
187
|
+
BinaryLinkSample(const IndexBinaryHNSW& index, idx_t n, const uint8_t* x)
|
|
188
|
+
: index(index), x(x) {
|
|
189
|
+
this->n = n;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
std::vector<uint8_t> gather(size_t m, const idx_t* ids) const {
|
|
193
|
+
const size_t cs = index.code_size;
|
|
194
|
+
std::vector<uint8_t> rows(m * cs);
|
|
195
|
+
for (size_t j = 0; j < m; j++) {
|
|
196
|
+
memcpy(rows.data() + j * cs, x + ids[j] * cs, cs);
|
|
197
|
+
}
|
|
198
|
+
return rows;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
void search(
|
|
202
|
+
const SearchParametersHNSW* params,
|
|
203
|
+
size_t m,
|
|
204
|
+
const idx_t* ids,
|
|
205
|
+
idx_t* labels) const override {
|
|
206
|
+
std::vector<int32_t> distances(m);
|
|
207
|
+
if (!ids) {
|
|
208
|
+
index.search(m, x, 1, distances.data(), labels, params);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
index.search(
|
|
212
|
+
m, gather(m, ids).data(), 1, distances.data(), labels, params);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void search_exact(
|
|
216
|
+
size_t m,
|
|
217
|
+
const idx_t* ids,
|
|
218
|
+
const idx_t* found,
|
|
219
|
+
idx_t* exact,
|
|
220
|
+
uint8_t* nearer) const override {
|
|
221
|
+
const size_t cs = index.code_size;
|
|
222
|
+
const std::vector<uint8_t> rows = gather(m, ids);
|
|
223
|
+
std::vector<int32_t> distances(m);
|
|
224
|
+
index.storage->search(m, rows.data(), 1, distances.data(), exact);
|
|
225
|
+
#pragma omp parallel
|
|
226
|
+
{
|
|
227
|
+
std::unique_ptr<DistanceComputer> dis(
|
|
228
|
+
index.get_distance_computer());
|
|
229
|
+
#pragma omp for schedule(static)
|
|
230
|
+
for (int64_t j = 0; j < static_cast<int64_t>(m); j++) {
|
|
231
|
+
if (exact[j] < 0 || exact[j] == found[j]) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
dis->set_query(
|
|
235
|
+
reinterpret_cast<const float*>(rows.data() + j * cs));
|
|
236
|
+
nearer[j] = (*dis)(exact[j]) < (*dis)(found[j]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
} // namespace
|
|
243
|
+
|
|
244
|
+
HNSWLevel0LinkStats IndexBinaryHNSW::add_level_0_links_from_sample(
|
|
245
|
+
idx_t n,
|
|
246
|
+
const uint8_t* x,
|
|
247
|
+
const HNSWLevel0LinkParameters& params) {
|
|
248
|
+
const BinaryLinkSample sample(*this, n, x);
|
|
249
|
+
std::unique_ptr<DistanceComputer> dis(get_distance_computer());
|
|
250
|
+
return hnsw_add_level_0_links_from_sample(
|
|
251
|
+
hnsw, ntotal, *dis, false, sample, params);
|
|
252
|
+
}
|
|
253
|
+
|
|
304
254
|
/**************************************************************
|
|
305
255
|
* IndexBinaryHNSWCagra implementation
|
|
306
256
|
**************************************************************/
|
|
@@ -397,10 +347,10 @@ void IndexBinaryHNSWCagra::search(
|
|
|
397
347
|
params);
|
|
398
348
|
|
|
399
349
|
res.end();
|
|
350
|
+
vt->advance();
|
|
400
351
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
hnsw_stats.combine(search_stats);
|
|
352
|
+
if (get_search_stats_enabled()) {
|
|
353
|
+
hnsw_stats.combine_atomic(search_stats);
|
|
404
354
|
}
|
|
405
355
|
}
|
|
406
356
|
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
|
|
12
12
|
#include <faiss/IndexBinaryFlat.h>
|
|
13
13
|
#include <faiss/impl/HNSW.h>
|
|
14
|
-
#include <faiss/impl/hnsw/LockVector.h>
|
|
15
14
|
#include <faiss/utils/utils.h>
|
|
16
15
|
|
|
17
16
|
namespace faiss {
|
|
@@ -41,10 +40,13 @@ struct IndexBinaryHNSW : IndexBinary {
|
|
|
41
40
|
// used when GpuIndexBinaryCagra::copyFrom(IndexBinaryHNSW*) is called.
|
|
42
41
|
bool keep_max_size_level0 = false;
|
|
43
42
|
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
43
|
+
// When this index is the coarse quantizer of an IndexBinaryIVF,
|
|
44
|
+
// IndexBinaryIVF::train() calls add_level_0_links_from_sample() after it
|
|
45
|
+
// adds the centroids. The sample is the first
|
|
46
|
+
// min(n, level_0_link_samples_per_node * ntotal) training vectors. 0
|
|
47
|
+
// disables the call. Training does not call it when the quantizer is
|
|
48
|
+
// already trained. The index format does not store this value.
|
|
49
|
+
int level_0_link_samples_per_node = 3;
|
|
48
50
|
|
|
49
51
|
explicit IndexBinaryHNSW();
|
|
50
52
|
explicit IndexBinaryHNSW(int d, int M = 32);
|
|
@@ -71,6 +73,17 @@ struct IndexBinaryHNSW : IndexBinary {
|
|
|
71
73
|
void reconstruct(idx_t key, uint8_t* recons) const override;
|
|
72
74
|
|
|
73
75
|
void reset() override;
|
|
76
|
+
|
|
77
|
+
/** Adds level-0 links so that the search no longer stops at "sinks":
|
|
78
|
+
* nodes where it stops for many vectors whose nearest node is elsewhere.
|
|
79
|
+
* See IndexHNSW::add_level_0_links_from_sample(). x holds n binary
|
|
80
|
+
* vectors. Call this function after the last add().
|
|
81
|
+
*/
|
|
82
|
+
HNSWLevel0LinkStats add_level_0_links_from_sample(
|
|
83
|
+
idx_t n,
|
|
84
|
+
const uint8_t* x,
|
|
85
|
+
const HNSWLevel0LinkParameters& params =
|
|
86
|
+
HNSWLevel0LinkParameters());
|
|
74
87
|
};
|
|
75
88
|
|
|
76
89
|
struct IndexBinaryHNSWCagra : IndexBinaryHNSW {
|