faiss 0.3.1 → 0.3.3
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/LICENSE.txt +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +2 -2
- data/vendor/faiss/faiss/AutoTune.h +3 -3
- data/vendor/faiss/faiss/Clustering.cpp +37 -6
- data/vendor/faiss/faiss/Clustering.h +12 -3
- data/vendor/faiss/faiss/IVFlib.cpp +6 -3
- data/vendor/faiss/faiss/IVFlib.h +2 -2
- data/vendor/faiss/faiss/Index.cpp +6 -2
- data/vendor/faiss/faiss/Index.h +30 -8
- data/vendor/faiss/faiss/Index2Layer.cpp +2 -2
- data/vendor/faiss/faiss/Index2Layer.h +2 -2
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +7 -7
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +2 -2
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +14 -16
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +2 -2
- data/vendor/faiss/faiss/IndexBinary.cpp +13 -2
- data/vendor/faiss/faiss/IndexBinary.h +8 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +2 -3
- data/vendor/faiss/faiss/IndexBinaryFlat.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -2
- data/vendor/faiss/faiss/IndexBinaryFromFloat.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +2 -7
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +3 -3
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -3
- data/vendor/faiss/faiss/IndexBinaryHash.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +3 -3
- data/vendor/faiss/faiss/IndexBinaryIVF.h +2 -2
- data/vendor/faiss/faiss/IndexFastScan.cpp +32 -18
- data/vendor/faiss/faiss/IndexFastScan.h +11 -2
- data/vendor/faiss/faiss/IndexFlat.cpp +13 -10
- data/vendor/faiss/faiss/IndexFlat.h +2 -2
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +170 -7
- data/vendor/faiss/faiss/IndexFlatCodes.h +25 -5
- data/vendor/faiss/faiss/IndexHNSW.cpp +156 -96
- data/vendor/faiss/faiss/IndexHNSW.h +54 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +19 -3
- data/vendor/faiss/faiss/IndexIDMap.h +5 -2
- data/vendor/faiss/faiss/IndexIVF.cpp +5 -6
- data/vendor/faiss/faiss/IndexIVF.h +13 -4
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +21 -7
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +5 -2
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +3 -14
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +2 -4
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +201 -91
- data/vendor/faiss/faiss/IndexIVFFastScan.h +33 -9
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +2 -2
- data/vendor/faiss/faiss/IndexIVFFlat.h +2 -2
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +2 -2
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +2 -2
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +3 -6
- data/vendor/faiss/faiss/IndexIVFPQ.h +2 -2
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +7 -14
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +2 -4
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +2 -2
- data/vendor/faiss/faiss/IndexIVFPQR.h +2 -2
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +2 -3
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +2 -2
- data/vendor/faiss/faiss/IndexLSH.cpp +2 -3
- data/vendor/faiss/faiss/IndexLSH.h +2 -2
- data/vendor/faiss/faiss/IndexLattice.cpp +3 -21
- data/vendor/faiss/faiss/IndexLattice.h +5 -24
- data/vendor/faiss/faiss/IndexNNDescent.cpp +2 -31
- data/vendor/faiss/faiss/IndexNNDescent.h +3 -3
- data/vendor/faiss/faiss/IndexNSG.cpp +2 -5
- data/vendor/faiss/faiss/IndexNSG.h +3 -3
- data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +56 -0
- data/vendor/faiss/faiss/IndexNeuralNetCodec.h +49 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +26 -26
- data/vendor/faiss/faiss/IndexPQ.h +2 -2
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +2 -5
- data/vendor/faiss/faiss/IndexPQFastScan.h +2 -11
- data/vendor/faiss/faiss/IndexPreTransform.cpp +2 -2
- data/vendor/faiss/faiss/IndexPreTransform.h +3 -3
- data/vendor/faiss/faiss/IndexRefine.cpp +46 -9
- data/vendor/faiss/faiss/IndexRefine.h +9 -2
- data/vendor/faiss/faiss/IndexReplicas.cpp +2 -2
- data/vendor/faiss/faiss/IndexReplicas.h +2 -2
- data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +2 -2
- data/vendor/faiss/faiss/IndexRowwiseMinMax.h +2 -2
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +5 -4
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +2 -2
- data/vendor/faiss/faiss/IndexShards.cpp +2 -2
- data/vendor/faiss/faiss/IndexShards.h +2 -2
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +2 -2
- data/vendor/faiss/faiss/IndexShardsIVF.h +2 -2
- data/vendor/faiss/faiss/MatrixStats.cpp +2 -2
- data/vendor/faiss/faiss/MatrixStats.h +2 -2
- data/vendor/faiss/faiss/MetaIndexes.cpp +2 -3
- data/vendor/faiss/faiss/MetaIndexes.h +2 -2
- data/vendor/faiss/faiss/MetricType.h +9 -4
- data/vendor/faiss/faiss/VectorTransform.cpp +2 -2
- data/vendor/faiss/faiss/VectorTransform.h +2 -2
- data/vendor/faiss/faiss/clone_index.cpp +2 -2
- data/vendor/faiss/faiss/clone_index.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/SaDecodeKernels.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/detail/CoarseBitType.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +97 -19
- data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +192 -0
- data/vendor/faiss/faiss/cppcontrib/factory_tools.h +29 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +85 -32
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-neon-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMax-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMaxFP16-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +2 -2
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-neon-inl.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +2 -5
- data/vendor/faiss/faiss/gpu/GpuAutoTune.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +45 -13
- data/vendor/faiss/faiss/gpu/GpuCloner.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +12 -6
- data/vendor/faiss/faiss/gpu/GpuDistance.h +11 -7
- data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +3 -3
- data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuIndex.h +10 -15
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +285 -0
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +8 -2
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +4 -2
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +3 -3
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +2 -2
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +7 -2
- data/vendor/faiss/faiss/gpu/GpuResources.h +11 -4
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +66 -11
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +15 -5
- data/vendor/faiss/faiss/gpu/impl/IndexUtils.h +2 -2
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +28 -23
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +2 -2
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +2 -2
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +2 -2
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +2 -2
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +2 -2
- data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +8 -2
- data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +2 -3
- data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +2 -2
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +10 -7
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +2 -2
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +54 -54
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +144 -77
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +51 -51
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +2 -2
- data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +3 -3
- data/vendor/faiss/faiss/gpu/test/TestGpuResidualQuantizer.cpp +70 -0
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +74 -4
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +2 -2
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +3 -3
- data/vendor/faiss/faiss/gpu/utils/{RaftUtils.h → CuvsUtils.h} +12 -11
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +8 -2
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +2 -2
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +2 -2
- data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +2 -2
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +6 -3
- data/vendor/faiss/faiss/gpu/utils/Timer.h +3 -3
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +79 -11
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +17 -5
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +27 -2
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +11 -3
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePacker.h +2 -2
- data/vendor/faiss/faiss/impl/DistanceComputer.h +48 -2
- data/vendor/faiss/faiss/impl/FaissAssert.h +6 -4
- data/vendor/faiss/faiss/impl/FaissException.cpp +2 -2
- data/vendor/faiss/faiss/impl/FaissException.h +2 -3
- data/vendor/faiss/faiss/impl/HNSW.cpp +378 -205
- data/vendor/faiss/faiss/impl/HNSW.h +55 -24
- data/vendor/faiss/faiss/impl/IDSelector.cpp +2 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +2 -2
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +10 -10
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +2 -2
- data/vendor/faiss/faiss/impl/LookupTableScaler.h +36 -2
- data/vendor/faiss/faiss/impl/NNDescent.cpp +15 -10
- data/vendor/faiss/faiss/impl/NNDescent.h +2 -2
- data/vendor/faiss/faiss/impl/NSG.cpp +26 -49
- data/vendor/faiss/faiss/impl/NSG.h +20 -8
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +2 -2
- data/vendor/faiss/faiss/impl/PolysemousTraining.h +2 -2
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +2 -4
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +2 -2
- data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +2 -2
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +3 -2
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +7 -3
- data/vendor/faiss/faiss/impl/Quantizer.h +2 -2
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +2 -36
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +3 -13
- data/vendor/faiss/faiss/impl/ResultHandler.h +153 -34
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +721 -104
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +5 -2
- data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +2 -2
- data/vendor/faiss/faiss/impl/ThreadedIndex.h +2 -2
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +7 -2
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +248 -0
- data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +2 -2
- data/vendor/faiss/faiss/impl/code_distance/code_distance-sve.h +440 -0
- data/vendor/faiss/faiss/impl/code_distance/code_distance.h +55 -2
- data/vendor/faiss/faiss/impl/index_read.cpp +31 -20
- data/vendor/faiss/faiss/impl/index_read_utils.h +37 -0
- data/vendor/faiss/faiss/impl/index_write.cpp +30 -16
- data/vendor/faiss/faiss/impl/io.cpp +15 -7
- data/vendor/faiss/faiss/impl/io.h +6 -6
- data/vendor/faiss/faiss/impl/io_macros.h +8 -9
- data/vendor/faiss/faiss/impl/kmeans1d.cpp +2 -3
- data/vendor/faiss/faiss/impl/kmeans1d.h +2 -2
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +2 -3
- data/vendor/faiss/faiss/impl/lattice_Zn.h +2 -2
- data/vendor/faiss/faiss/impl/platform_macros.h +34 -2
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +13 -2
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +20 -2
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +3 -3
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +450 -3
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +8 -8
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +3 -3
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +151 -67
- data/vendor/faiss/faiss/index_factory.cpp +51 -34
- data/vendor/faiss/faiss/index_factory.h +2 -2
- data/vendor/faiss/faiss/index_io.h +14 -7
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +30 -10
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +5 -2
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +11 -3
- data/vendor/faiss/faiss/invlists/DirectMap.h +2 -2
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +57 -19
- data/vendor/faiss/faiss/invlists/InvertedLists.h +20 -11
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +2 -2
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +2 -2
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +23 -9
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +4 -3
- data/vendor/faiss/faiss/python/python_callbacks.cpp +5 -5
- data/vendor/faiss/faiss/python/python_callbacks.h +2 -2
- data/vendor/faiss/faiss/utils/AlignedTable.h +5 -3
- data/vendor/faiss/faiss/utils/Heap.cpp +2 -2
- data/vendor/faiss/faiss/utils/Heap.h +107 -2
- data/vendor/faiss/faiss/utils/NeuralNet.cpp +346 -0
- data/vendor/faiss/faiss/utils/NeuralNet.h +147 -0
- data/vendor/faiss/faiss/utils/WorkerThread.cpp +2 -2
- data/vendor/faiss/faiss/utils/WorkerThread.h +2 -2
- data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +2 -2
- data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +2 -2
- data/vendor/faiss/faiss/utils/approx_topk/generic.h +2 -2
- data/vendor/faiss/faiss/utils/approx_topk/mode.h +2 -2
- data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +2 -2
- data/vendor/faiss/faiss/utils/bf16.h +36 -0
- data/vendor/faiss/faiss/utils/distances.cpp +249 -90
- data/vendor/faiss/faiss/utils/distances.h +8 -8
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +2 -2
- data/vendor/faiss/faiss/utils/distances_simd.cpp +1543 -56
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +72 -2
- data/vendor/faiss/faiss/utils/extra_distances.cpp +87 -140
- data/vendor/faiss/faiss/utils/extra_distances.h +5 -4
- data/vendor/faiss/faiss/utils/fp16-arm.h +2 -2
- data/vendor/faiss/faiss/utils/fp16-fp16c.h +2 -2
- data/vendor/faiss/faiss/utils/fp16-inl.h +2 -2
- data/vendor/faiss/faiss/utils/fp16.h +2 -2
- data/vendor/faiss/faiss/utils/hamming-inl.h +2 -2
- data/vendor/faiss/faiss/utils/hamming.cpp +3 -4
- data/vendor/faiss/faiss/utils/hamming.h +2 -2
- data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +2 -2
- data/vendor/faiss/faiss/utils/hamming_distance/avx512-inl.h +490 -0
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +2 -2
- data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +6 -3
- data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +7 -3
- data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +5 -5
- data/vendor/faiss/faiss/utils/ordered_key_value.h +2 -2
- data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
- data/vendor/faiss/faiss/utils/partitioning.h +2 -2
- data/vendor/faiss/faiss/utils/prefetch.h +2 -2
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +2 -2
- data/vendor/faiss/faiss/utils/quantize_lut.h +2 -2
- data/vendor/faiss/faiss/utils/random.cpp +45 -2
- data/vendor/faiss/faiss/utils/random.h +27 -2
- data/vendor/faiss/faiss/utils/simdlib.h +12 -3
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +2 -2
- data/vendor/faiss/faiss/utils/simdlib_avx512.h +296 -0
- data/vendor/faiss/faiss/utils/simdlib_emulated.h +2 -2
- data/vendor/faiss/faiss/utils/simdlib_neon.h +7 -4
- data/vendor/faiss/faiss/utils/simdlib_ppc64.h +1084 -0
- data/vendor/faiss/faiss/utils/sorting.cpp +2 -2
- data/vendor/faiss/faiss/utils/sorting.h +2 -2
- data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +2 -2
- data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +176 -0
- data/vendor/faiss/faiss/utils/utils.cpp +17 -10
- data/vendor/faiss/faiss/utils/utils.h +7 -3
- metadata +22 -11
- data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +0 -102
@@ -1,12 +1,10 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
6
6
|
*/
|
7
7
|
|
8
|
-
// -*- c++ -*-
|
9
|
-
|
10
8
|
#include <faiss/IndexHNSW.h>
|
11
9
|
|
12
10
|
#include <omp.h>
|
@@ -17,11 +15,11 @@
|
|
17
15
|
#include <cstdlib>
|
18
16
|
#include <cstring>
|
19
17
|
|
18
|
+
#include <limits>
|
19
|
+
#include <memory>
|
20
20
|
#include <queue>
|
21
|
-
#include <
|
21
|
+
#include <random>
|
22
22
|
|
23
|
-
#include <sys/stat.h>
|
24
|
-
#include <sys/types.h>
|
25
23
|
#include <cstdint>
|
26
24
|
|
27
25
|
#include <faiss/Index2Layer.h>
|
@@ -30,30 +28,9 @@
|
|
30
28
|
#include <faiss/impl/AuxIndexStructures.h>
|
31
29
|
#include <faiss/impl/FaissAssert.h>
|
32
30
|
#include <faiss/impl/ResultHandler.h>
|
33
|
-
#include <faiss/utils/distances.h>
|
34
31
|
#include <faiss/utils/random.h>
|
35
32
|
#include <faiss/utils/sorting.h>
|
36
33
|
|
37
|
-
extern "C" {
|
38
|
-
|
39
|
-
/* declare BLAS functions, see http://www.netlib.org/clapack/cblas/ */
|
40
|
-
|
41
|
-
int sgemm_(
|
42
|
-
const char* transa,
|
43
|
-
const char* transb,
|
44
|
-
FINTEGER* m,
|
45
|
-
FINTEGER* n,
|
46
|
-
FINTEGER* k,
|
47
|
-
const float* alpha,
|
48
|
-
const float* a,
|
49
|
-
FINTEGER* lda,
|
50
|
-
const float* b,
|
51
|
-
FINTEGER* ldb,
|
52
|
-
float* beta,
|
53
|
-
float* c,
|
54
|
-
FINTEGER* ldc);
|
55
|
-
}
|
56
|
-
|
57
34
|
namespace faiss {
|
58
35
|
|
59
36
|
using MinimaxHeap = HNSW::MinimaxHeap;
|
@@ -68,52 +45,6 @@ HNSWStats hnsw_stats;
|
|
68
45
|
|
69
46
|
namespace {
|
70
47
|
|
71
|
-
/* Wrap the distance computer into one that negates the
|
72
|
-
distances. This makes supporting INNER_PRODUCE search easier */
|
73
|
-
|
74
|
-
struct NegativeDistanceComputer : DistanceComputer {
|
75
|
-
/// owned by this
|
76
|
-
DistanceComputer* basedis;
|
77
|
-
|
78
|
-
explicit NegativeDistanceComputer(DistanceComputer* basedis)
|
79
|
-
: basedis(basedis) {}
|
80
|
-
|
81
|
-
void set_query(const float* x) override {
|
82
|
-
basedis->set_query(x);
|
83
|
-
}
|
84
|
-
|
85
|
-
/// compute distance of vector i to current query
|
86
|
-
float operator()(idx_t i) override {
|
87
|
-
return -(*basedis)(i);
|
88
|
-
}
|
89
|
-
|
90
|
-
void distances_batch_4(
|
91
|
-
const idx_t idx0,
|
92
|
-
const idx_t idx1,
|
93
|
-
const idx_t idx2,
|
94
|
-
const idx_t idx3,
|
95
|
-
float& dis0,
|
96
|
-
float& dis1,
|
97
|
-
float& dis2,
|
98
|
-
float& dis3) override {
|
99
|
-
basedis->distances_batch_4(
|
100
|
-
idx0, idx1, idx2, idx3, dis0, dis1, dis2, dis3);
|
101
|
-
dis0 = -dis0;
|
102
|
-
dis1 = -dis1;
|
103
|
-
dis2 = -dis2;
|
104
|
-
dis3 = -dis3;
|
105
|
-
}
|
106
|
-
|
107
|
-
/// compute distance between two stored vectors
|
108
|
-
float symmetric_dis(idx_t i, idx_t j) override {
|
109
|
-
return -basedis->symmetric_dis(i, j);
|
110
|
-
}
|
111
|
-
|
112
|
-
virtual ~NegativeDistanceComputer() {
|
113
|
-
delete basedis;
|
114
|
-
}
|
115
|
-
};
|
116
|
-
|
117
48
|
DistanceComputer* storage_distance_computer(const Index* storage) {
|
118
49
|
if (is_similarity_metric(storage->metric_type)) {
|
119
50
|
return new NegativeDistanceComputer(storage->get_distance_computer());
|
@@ -192,7 +123,9 @@ void hnsw_add_vertices(
|
|
192
123
|
|
193
124
|
int i1 = n;
|
194
125
|
|
195
|
-
for (int pt_level = hist.size() - 1;
|
126
|
+
for (int pt_level = hist.size() - 1;
|
127
|
+
pt_level >= !index_hnsw.init_level0;
|
128
|
+
pt_level--) {
|
196
129
|
int i0 = i1 - hist[pt_level];
|
197
130
|
|
198
131
|
if (verbose) {
|
@@ -228,7 +161,13 @@ void hnsw_add_vertices(
|
|
228
161
|
continue;
|
229
162
|
}
|
230
163
|
|
231
|
-
hnsw.add_with_locks(
|
164
|
+
hnsw.add_with_locks(
|
165
|
+
*dis,
|
166
|
+
pt_level,
|
167
|
+
pt_id,
|
168
|
+
locks,
|
169
|
+
vt,
|
170
|
+
index_hnsw.keep_max_size_level0 && (pt_level == 0));
|
232
171
|
|
233
172
|
if (prev_display >= 0 && i - i0 > prev_display + 10000) {
|
234
173
|
prev_display = i - i0;
|
@@ -248,7 +187,11 @@ void hnsw_add_vertices(
|
|
248
187
|
}
|
249
188
|
i1 = i0;
|
250
189
|
}
|
251
|
-
|
190
|
+
if (index_hnsw.init_level0) {
|
191
|
+
FAISS_ASSERT(i1 == 0);
|
192
|
+
} else {
|
193
|
+
FAISS_ASSERT((i1 - hist[0]) == 0);
|
194
|
+
}
|
252
195
|
}
|
253
196
|
if (verbose) {
|
254
197
|
printf("Done in %.3f ms\n", getmillisecs() - t0);
|
@@ -297,7 +240,8 @@ void hnsw_search(
|
|
297
240
|
const SearchParameters* params_in) {
|
298
241
|
FAISS_THROW_IF_NOT_MSG(
|
299
242
|
index->storage,
|
300
|
-
"
|
243
|
+
"No storage index, please use IndexHNSWFlat (or variants) "
|
244
|
+
"instead of IndexHNSW directly");
|
301
245
|
const SearchParametersHNSW* params = nullptr;
|
302
246
|
const HNSW& hnsw = index->hnsw;
|
303
247
|
|
@@ -307,7 +251,7 @@ void hnsw_search(
|
|
307
251
|
FAISS_THROW_IF_NOT_MSG(params, "params type invalid");
|
308
252
|
efSearch = params->efSearch;
|
309
253
|
}
|
310
|
-
size_t n1 = 0, n2 = 0,
|
254
|
+
size_t n1 = 0, n2 = 0, ndis = 0, nhops = 0;
|
311
255
|
|
312
256
|
idx_t check_period = InterruptCallback::get_period_hint(
|
313
257
|
hnsw.max_level * index->d * efSearch);
|
@@ -315,7 +259,7 @@ void hnsw_search(
|
|
315
259
|
for (idx_t i0 = 0; i0 < n; i0 += check_period) {
|
316
260
|
idx_t i1 = std::min(i0 + check_period, n);
|
317
261
|
|
318
|
-
#pragma omp parallel
|
262
|
+
#pragma omp parallel if (i1 - i0 > 1)
|
319
263
|
{
|
320
264
|
VisitedTable vt(index->ntotal);
|
321
265
|
typename BlockResultHandler::SingleResultHandler res(bres);
|
@@ -323,7 +267,7 @@ void hnsw_search(
|
|
323
267
|
std::unique_ptr<DistanceComputer> dis(
|
324
268
|
storage_distance_computer(index->storage));
|
325
269
|
|
326
|
-
#pragma omp for reduction(+ : n1, n2,
|
270
|
+
#pragma omp for reduction(+ : n1, n2, ndis, nhops) schedule(guided)
|
327
271
|
for (idx_t i = i0; i < i1; i++) {
|
328
272
|
res.begin(i);
|
329
273
|
dis->set_query(x + i * index->d);
|
@@ -331,16 +275,15 @@ void hnsw_search(
|
|
331
275
|
HNSWStats stats = hnsw.search(*dis, res, vt, params);
|
332
276
|
n1 += stats.n1;
|
333
277
|
n2 += stats.n2;
|
334
|
-
n3 += stats.n3;
|
335
278
|
ndis += stats.ndis;
|
336
|
-
|
279
|
+
nhops += stats.nhops;
|
337
280
|
res.end();
|
338
281
|
}
|
339
282
|
}
|
340
283
|
InterruptCallback::check();
|
341
284
|
}
|
342
285
|
|
343
|
-
hnsw_stats.combine({n1, n2,
|
286
|
+
hnsw_stats.combine({n1, n2, ndis, nhops});
|
344
287
|
}
|
345
288
|
|
346
289
|
} // anonymous namespace
|
@@ -374,7 +317,7 @@ void IndexHNSW::range_search(
|
|
374
317
|
RangeSearchResult* result,
|
375
318
|
const SearchParameters* params) const {
|
376
319
|
using RH = RangeSearchBlockResultHandler<HNSW::C>;
|
377
|
-
RH bres(result, radius);
|
320
|
+
RH bres(result, is_similarity_metric(metric_type) ? -radius : radius);
|
378
321
|
|
379
322
|
hnsw_search(this, n, x, bres, params);
|
380
323
|
|
@@ -408,6 +351,17 @@ void IndexHNSW::reconstruct(idx_t key, float* recons) const {
|
|
408
351
|
storage->reconstruct(key, recons);
|
409
352
|
}
|
410
353
|
|
354
|
+
/**************************************************************
|
355
|
+
* This section of functions were used during the development of HNSW support.
|
356
|
+
* They may be useful in the future but are dormant for now, and thus are not
|
357
|
+
* unit tested at the moment.
|
358
|
+
* shrink_level_0_neighbors
|
359
|
+
* search_level_0
|
360
|
+
* init_level_0_from_knngraph
|
361
|
+
* init_level_0_from_entry_points
|
362
|
+
* reorder_links
|
363
|
+
* link_singletons
|
364
|
+
**************************************************************/
|
411
365
|
void IndexHNSW::shrink_level_0_neighbors(int new_size) {
|
412
366
|
#pragma omp parallel
|
413
367
|
{
|
@@ -453,10 +407,18 @@ void IndexHNSW::search_level_0(
|
|
453
407
|
float* distances,
|
454
408
|
idx_t* labels,
|
455
409
|
int nprobe,
|
456
|
-
int search_type
|
410
|
+
int search_type,
|
411
|
+
const SearchParameters* params_in) const {
|
457
412
|
FAISS_THROW_IF_NOT(k > 0);
|
458
413
|
FAISS_THROW_IF_NOT(nprobe > 0);
|
459
414
|
|
415
|
+
const SearchParametersHNSW* params = nullptr;
|
416
|
+
|
417
|
+
if (params_in) {
|
418
|
+
params = dynamic_cast<const SearchParametersHNSW*>(params_in);
|
419
|
+
FAISS_THROW_IF_NOT_MSG(params, "params type invalid");
|
420
|
+
}
|
421
|
+
|
460
422
|
storage_idx_t ntotal = hnsw.levels.size();
|
461
423
|
|
462
424
|
using RH = HeapBlockResultHandler<HNSW::C>;
|
@@ -483,13 +445,21 @@ void IndexHNSW::search_level_0(
|
|
483
445
|
nearest_d + i * nprobe,
|
484
446
|
search_type,
|
485
447
|
search_stats,
|
486
|
-
vt
|
448
|
+
vt,
|
449
|
+
params);
|
487
450
|
res.end();
|
488
451
|
vt.advance();
|
489
452
|
}
|
490
453
|
#pragma omp critical
|
491
454
|
{ hnsw_stats.combine(search_stats); }
|
492
455
|
}
|
456
|
+
if (is_similarity_metric(this->metric_type)) {
|
457
|
+
// we need to revert the negated distances
|
458
|
+
#pragma omp parallel for
|
459
|
+
for (int64_t i = 0; i < k * n; i++) {
|
460
|
+
distances[i] = -distances[i];
|
461
|
+
}
|
462
|
+
}
|
493
463
|
}
|
494
464
|
|
495
465
|
void IndexHNSW::init_level_0_from_knngraph(
|
@@ -650,6 +620,10 @@ void IndexHNSW::permute_entries(const idx_t* perm) {
|
|
650
620
|
hnsw.permute_entries(perm);
|
651
621
|
}
|
652
622
|
|
623
|
+
DistanceComputer* IndexHNSW::get_distance_computer() const {
|
624
|
+
return storage->get_distance_computer();
|
625
|
+
}
|
626
|
+
|
653
627
|
/**************************************************************
|
654
628
|
* IndexHNSWFlat implementation
|
655
629
|
**************************************************************/
|
@@ -673,8 +647,13 @@ IndexHNSWFlat::IndexHNSWFlat(int d, int M, MetricType metric)
|
|
673
647
|
|
674
648
|
IndexHNSWPQ::IndexHNSWPQ() = default;
|
675
649
|
|
676
|
-
IndexHNSWPQ::IndexHNSWPQ(
|
677
|
-
|
650
|
+
IndexHNSWPQ::IndexHNSWPQ(
|
651
|
+
int d,
|
652
|
+
int pq_m,
|
653
|
+
int M,
|
654
|
+
int pq_nbits,
|
655
|
+
MetricType metric)
|
656
|
+
: IndexHNSW(new IndexPQ(d, pq_m, pq_nbits, metric), M) {
|
678
657
|
own_fields = true;
|
679
658
|
is_trained = false;
|
680
659
|
}
|
@@ -800,7 +779,7 @@ void IndexHNSW2Level::search(
|
|
800
779
|
IndexHNSW::search(n, x, k, distances, labels);
|
801
780
|
|
802
781
|
} else { // "mixed" search
|
803
|
-
size_t n1 = 0, n2 = 0,
|
782
|
+
size_t n1 = 0, n2 = 0, ndis = 0, nhops = 0;
|
804
783
|
|
805
784
|
const IndexIVFPQ* index_ivfpq =
|
806
785
|
dynamic_cast<const IndexIVFPQ*>(storage);
|
@@ -829,10 +808,10 @@ void IndexHNSW2Level::search(
|
|
829
808
|
std::unique_ptr<DistanceComputer> dis(
|
830
809
|
storage_distance_computer(storage));
|
831
810
|
|
832
|
-
int candidates_size =
|
811
|
+
constexpr int candidates_size = 1;
|
833
812
|
MinimaxHeap candidates(candidates_size);
|
834
813
|
|
835
|
-
#pragma omp for reduction(+ : n1, n2,
|
814
|
+
#pragma omp for reduction(+ : n1, n2, ndis, nhops)
|
836
815
|
for (idx_t i = 0; i < n; i++) {
|
837
816
|
idx_t* idxi = labels + i * k;
|
838
817
|
float* simi = distances + i * k;
|
@@ -854,7 +833,7 @@ void IndexHNSW2Level::search(
|
|
854
833
|
|
855
834
|
candidates.clear();
|
856
835
|
|
857
|
-
for (int j = 0; j <
|
836
|
+
for (int j = 0; j < k; j++) {
|
858
837
|
if (idxi[j] < 0)
|
859
838
|
break;
|
860
839
|
candidates.push(idxi[j], simi[j]);
|
@@ -877,9 +856,8 @@ void IndexHNSW2Level::search(
|
|
877
856
|
k);
|
878
857
|
n1 += search_stats.n1;
|
879
858
|
n2 += search_stats.n2;
|
880
|
-
n3 += search_stats.n3;
|
881
859
|
ndis += search_stats.ndis;
|
882
|
-
|
860
|
+
nhops += search_stats.nhops;
|
883
861
|
|
884
862
|
vt.advance();
|
885
863
|
vt.advance();
|
@@ -888,7 +866,7 @@ void IndexHNSW2Level::search(
|
|
888
866
|
}
|
889
867
|
}
|
890
868
|
|
891
|
-
hnsw_stats.combine({n1, n2,
|
869
|
+
hnsw_stats.combine({n1, n2, ndis, nhops});
|
892
870
|
}
|
893
871
|
}
|
894
872
|
|
@@ -914,4 +892,86 @@ void IndexHNSW2Level::flip_to_ivf() {
|
|
914
892
|
delete storage2l;
|
915
893
|
}
|
916
894
|
|
895
|
+
/**************************************************************
|
896
|
+
* IndexHNSWCagra implementation
|
897
|
+
**************************************************************/
|
898
|
+
|
899
|
+
IndexHNSWCagra::IndexHNSWCagra() {
|
900
|
+
is_trained = true;
|
901
|
+
}
|
902
|
+
|
903
|
+
IndexHNSWCagra::IndexHNSWCagra(int d, int M, MetricType metric)
|
904
|
+
: IndexHNSW(
|
905
|
+
(metric == METRIC_L2)
|
906
|
+
? static_cast<IndexFlat*>(new IndexFlatL2(d))
|
907
|
+
: static_cast<IndexFlat*>(new IndexFlatIP(d)),
|
908
|
+
M) {
|
909
|
+
FAISS_THROW_IF_NOT_MSG(
|
910
|
+
((metric == METRIC_L2) || (metric == METRIC_INNER_PRODUCT)),
|
911
|
+
"unsupported metric type for IndexHNSWCagra");
|
912
|
+
own_fields = true;
|
913
|
+
is_trained = true;
|
914
|
+
init_level0 = true;
|
915
|
+
keep_max_size_level0 = true;
|
916
|
+
}
|
917
|
+
|
918
|
+
void IndexHNSWCagra::add(idx_t n, const float* x) {
|
919
|
+
FAISS_THROW_IF_NOT_MSG(
|
920
|
+
!base_level_only,
|
921
|
+
"Cannot add vectors when base_level_only is set to True");
|
922
|
+
|
923
|
+
IndexHNSW::add(n, x);
|
924
|
+
}
|
925
|
+
|
926
|
+
void IndexHNSWCagra::search(
|
927
|
+
idx_t n,
|
928
|
+
const float* x,
|
929
|
+
idx_t k,
|
930
|
+
float* distances,
|
931
|
+
idx_t* labels,
|
932
|
+
const SearchParameters* params) const {
|
933
|
+
if (!base_level_only) {
|
934
|
+
IndexHNSW::search(n, x, k, distances, labels, params);
|
935
|
+
} else {
|
936
|
+
std::vector<storage_idx_t> nearest(n);
|
937
|
+
std::vector<float> nearest_d(n);
|
938
|
+
|
939
|
+
#pragma omp for
|
940
|
+
for (idx_t i = 0; i < n; i++) {
|
941
|
+
std::unique_ptr<DistanceComputer> dis(
|
942
|
+
storage_distance_computer(this->storage));
|
943
|
+
dis->set_query(x + i * d);
|
944
|
+
nearest[i] = -1;
|
945
|
+
nearest_d[i] = std::numeric_limits<float>::max();
|
946
|
+
|
947
|
+
std::random_device rd;
|
948
|
+
std::mt19937 gen(rd());
|
949
|
+
std::uniform_int_distribution<idx_t> distrib(0, this->ntotal - 1);
|
950
|
+
|
951
|
+
for (idx_t j = 0; j < num_base_level_search_entrypoints; j++) {
|
952
|
+
auto idx = distrib(gen);
|
953
|
+
auto distance = (*dis)(idx);
|
954
|
+
if (distance < nearest_d[i]) {
|
955
|
+
nearest[i] = idx;
|
956
|
+
nearest_d[i] = distance;
|
957
|
+
}
|
958
|
+
}
|
959
|
+
FAISS_THROW_IF_NOT_MSG(
|
960
|
+
nearest[i] >= 0, "Could not find a valid entrypoint.");
|
961
|
+
}
|
962
|
+
|
963
|
+
search_level_0(
|
964
|
+
n,
|
965
|
+
x,
|
966
|
+
k,
|
967
|
+
nearest.data(),
|
968
|
+
nearest_d.data(),
|
969
|
+
distances,
|
970
|
+
labels,
|
971
|
+
1, // n_probes
|
972
|
+
1, // search_type
|
973
|
+
params);
|
974
|
+
}
|
975
|
+
}
|
976
|
+
|
917
977
|
} // namespace faiss
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
@@ -27,13 +27,25 @@ struct IndexHNSW;
|
|
27
27
|
struct IndexHNSW : Index {
|
28
28
|
typedef HNSW::storage_idx_t storage_idx_t;
|
29
29
|
|
30
|
-
// the link
|
30
|
+
// the link structure
|
31
31
|
HNSW hnsw;
|
32
32
|
|
33
33
|
// the sequential storage
|
34
34
|
bool own_fields = false;
|
35
35
|
Index* storage = nullptr;
|
36
36
|
|
37
|
+
// When set to false, level 0 in the knn graph is not initialized.
|
38
|
+
// This option is used by GpuIndexCagra::copyTo(IndexHNSWCagra*)
|
39
|
+
// as level 0 knn graph is copied over from the index built by
|
40
|
+
// GpuIndexCagra.
|
41
|
+
bool init_level0 = true;
|
42
|
+
|
43
|
+
// When set to true, all neighbors in level 0 are filled up
|
44
|
+
// to the maximum size allowed (2 * M). This option is used by
|
45
|
+
// IndexHHNSWCagra to create a full base layer graph that is
|
46
|
+
// used when GpuIndexCagra::copyFrom(IndexHNSWCagra*) is invoked.
|
47
|
+
bool keep_max_size_level0 = false;
|
48
|
+
|
37
49
|
explicit IndexHNSW(int d = 0, int M = 32, MetricType metric = METRIC_L2);
|
38
50
|
explicit IndexHNSW(Index* storage, int M = 32);
|
39
51
|
|
@@ -81,7 +93,8 @@ struct IndexHNSW : Index {
|
|
81
93
|
float* distances,
|
82
94
|
idx_t* labels,
|
83
95
|
int nprobe = 1,
|
84
|
-
int search_type = 1
|
96
|
+
int search_type = 1,
|
97
|
+
const SearchParameters* params = nullptr) const;
|
85
98
|
|
86
99
|
/// alternative graph building
|
87
100
|
void init_level_0_from_knngraph(int k, const float* D, const idx_t* I);
|
@@ -98,6 +111,8 @@ struct IndexHNSW : Index {
|
|
98
111
|
void link_singletons();
|
99
112
|
|
100
113
|
void permute_entries(const idx_t* perm);
|
114
|
+
|
115
|
+
DistanceComputer* get_distance_computer() const override;
|
101
116
|
};
|
102
117
|
|
103
118
|
/** Flat index topped with with a HNSW structure to access elements
|
@@ -114,7 +129,12 @@ struct IndexHNSWFlat : IndexHNSW {
|
|
114
129
|
*/
|
115
130
|
struct IndexHNSWPQ : IndexHNSW {
|
116
131
|
IndexHNSWPQ();
|
117
|
-
IndexHNSWPQ(
|
132
|
+
IndexHNSWPQ(
|
133
|
+
int d,
|
134
|
+
int pq_m,
|
135
|
+
int M,
|
136
|
+
int pq_nbits = 8,
|
137
|
+
MetricType metric = METRIC_L2);
|
118
138
|
void train(idx_t n, const float* x) override;
|
119
139
|
};
|
120
140
|
|
@@ -148,4 +168,33 @@ struct IndexHNSW2Level : IndexHNSW {
|
|
148
168
|
const SearchParameters* params = nullptr) const override;
|
149
169
|
};
|
150
170
|
|
171
|
+
struct IndexHNSWCagra : IndexHNSW {
|
172
|
+
IndexHNSWCagra();
|
173
|
+
IndexHNSWCagra(int d, int M, MetricType metric = METRIC_L2);
|
174
|
+
|
175
|
+
/// When set to true, the index is immutable.
|
176
|
+
/// This option is used to copy the knn graph from GpuIndexCagra
|
177
|
+
/// to the base level of IndexHNSWCagra without adding upper levels.
|
178
|
+
/// Doing so enables to search the HNSW index, but removes the
|
179
|
+
/// ability to add vectors.
|
180
|
+
bool base_level_only = false;
|
181
|
+
|
182
|
+
/// When `base_level_only` is set to `True`, the search function
|
183
|
+
/// searches only the base level knn graph of the HNSW index.
|
184
|
+
/// This parameter selects the entry point by randomly selecting
|
185
|
+
/// some points and using the best one.
|
186
|
+
int num_base_level_search_entrypoints = 32;
|
187
|
+
|
188
|
+
void add(idx_t n, const float* x) override;
|
189
|
+
|
190
|
+
/// entry point for search
|
191
|
+
void search(
|
192
|
+
idx_t n,
|
193
|
+
const float* x,
|
194
|
+
idx_t k,
|
195
|
+
float* distances,
|
196
|
+
idx_t* labels,
|
197
|
+
const SearchParameters* params = nullptr) const override;
|
198
|
+
};
|
199
|
+
|
151
200
|
} // namespace faiss
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
@@ -12,7 +12,6 @@
|
|
12
12
|
#include <cinttypes>
|
13
13
|
#include <cstdint>
|
14
14
|
#include <cstdio>
|
15
|
-
#include <limits>
|
16
15
|
|
17
16
|
#include <faiss/impl/AuxIndexStructures.h>
|
18
17
|
#include <faiss/impl/FaissAssert.h>
|
@@ -83,6 +82,23 @@ void IndexIDMapTemplate<IndexT>::add_with_ids(
|
|
83
82
|
this->ntotal = index->ntotal;
|
84
83
|
}
|
85
84
|
|
85
|
+
template <typename IndexT>
|
86
|
+
size_t IndexIDMapTemplate<IndexT>::sa_code_size() const {
|
87
|
+
return index->sa_code_size();
|
88
|
+
}
|
89
|
+
|
90
|
+
template <typename IndexT>
|
91
|
+
void IndexIDMapTemplate<IndexT>::add_sa_codes(
|
92
|
+
idx_t n,
|
93
|
+
const uint8_t* codes,
|
94
|
+
const idx_t* xids) {
|
95
|
+
index->add_sa_codes(n, codes, xids);
|
96
|
+
for (idx_t i = 0; i < n; i++) {
|
97
|
+
id_map.push_back(xids[i]);
|
98
|
+
}
|
99
|
+
this->ntotal = index->ntotal;
|
100
|
+
}
|
101
|
+
|
86
102
|
namespace {
|
87
103
|
|
88
104
|
/// RAII object to reset the IDSelector in the params object
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
@@ -60,6 +60,9 @@ struct IndexIDMapTemplate : IndexT {
|
|
60
60
|
void merge_from(IndexT& otherIndex, idx_t add_id = 0) override;
|
61
61
|
void check_compatible_for_merge(const IndexT& otherIndex) const override;
|
62
62
|
|
63
|
+
size_t sa_code_size() const override;
|
64
|
+
void add_sa_codes(idx_t n, const uint8_t* x, const idx_t* xids) override;
|
65
|
+
|
63
66
|
~IndexIDMapTemplate() override;
|
64
67
|
IndexIDMapTemplate() {
|
65
68
|
own_fields = false;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
@@ -18,7 +18,6 @@
|
|
18
18
|
#include <cinttypes>
|
19
19
|
#include <cstdio>
|
20
20
|
#include <limits>
|
21
|
-
#include <memory>
|
22
21
|
|
23
22
|
#include <faiss/utils/hamming.h>
|
24
23
|
#include <faiss/utils/utils.h>
|
@@ -66,8 +65,8 @@ void Level1Quantizer::train_q1(
|
|
66
65
|
} else if (quantizer_trains_alone == 1) {
|
67
66
|
if (verbose)
|
68
67
|
printf("IVF quantizer trains alone...\n");
|
69
|
-
quantizer->train(n, x);
|
70
68
|
quantizer->verbose = verbose;
|
69
|
+
quantizer->train(n, x);
|
71
70
|
FAISS_THROW_IF_NOT_MSG(
|
72
71
|
quantizer->ntotal == nlist,
|
73
72
|
"nlist not consistent with quantizer size");
|
@@ -444,7 +443,7 @@ void IndexIVF::search_preassigned(
|
|
444
443
|
max_codes = unlimited_list_size;
|
445
444
|
}
|
446
445
|
|
447
|
-
bool do_parallel = omp_get_max_threads() >= 2 &&
|
446
|
+
[[maybe_unused]] bool do_parallel = omp_get_max_threads() >= 2 &&
|
448
447
|
(pmode == 0 ? false
|
449
448
|
: pmode == 3 ? n > 1
|
450
449
|
: pmode == 1 ? nprobe > 1
|
@@ -784,7 +783,7 @@ void IndexIVF::range_search_preassigned(
|
|
784
783
|
|
785
784
|
int pmode = this->parallel_mode & ~PARALLEL_MODE_NO_HEAP_INIT;
|
786
785
|
// don't start parallel section if single query
|
787
|
-
bool do_parallel = omp_get_max_threads() >= 2 &&
|
786
|
+
[[maybe_unused]] bool do_parallel = omp_get_max_threads() >= 2 &&
|
788
787
|
(pmode == 3 ? false
|
789
788
|
: pmode == 0 ? nx > 1
|
790
789
|
: pmode == 1 ? nprobe > 1
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
* Copyright (c)
|
1
|
+
/*
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
3
|
*
|
4
4
|
* This source code is licensed under the MIT license found in the
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
@@ -258,7 +258,8 @@ struct IndexIVF : Index, IndexIVFInterface {
|
|
258
258
|
* @param codes codes to add size n * sa_code_size()
|
259
259
|
* @param xids corresponding ids, size n
|
260
260
|
*/
|
261
|
-
void add_sa_codes(idx_t n, const uint8_t* codes, const idx_t* xids)
|
261
|
+
void add_sa_codes(idx_t n, const uint8_t* codes, const idx_t* xids)
|
262
|
+
override;
|
262
263
|
|
263
264
|
/** Train the encoder for the vectors.
|
264
265
|
*
|
@@ -433,6 +434,14 @@ struct IndexIVF : Index, IndexIVFInterface {
|
|
433
434
|
|
434
435
|
/* The standalone codec interface (except sa_decode that is specific) */
|
435
436
|
size_t sa_code_size() const override;
|
437
|
+
|
438
|
+
/** encode a set of vectors
|
439
|
+
* sa_encode will call encode_vectors with include_listno=true
|
440
|
+
* @param n nb of vectors to encode
|
441
|
+
* @param x the vectors to encode
|
442
|
+
* @param bytes output array for the codes
|
443
|
+
* @return nb of bytes written to codes
|
444
|
+
*/
|
436
445
|
void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override;
|
437
446
|
|
438
447
|
IndexIVF();
|
@@ -471,7 +480,7 @@ struct InvertedListScanner {
|
|
471
480
|
virtual float distance_to_code(const uint8_t* code) const = 0;
|
472
481
|
|
473
482
|
/** scan a set of codes, compute distances to current query and
|
474
|
-
* update heap of results if necessary. Default
|
483
|
+
* update heap of results if necessary. Default implementation
|
475
484
|
* calls distance_to_code.
|
476
485
|
*
|
477
486
|
* @param n number of codes to scan
|