faiss 0.6.0 → 0.6.1
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 +4 -0
- data/ext/faiss/extconf.rb +2 -1
- data/ext/faiss/{index_rb.cpp → index.cpp} +1 -1
- data/ext/faiss/index_binary.cpp +1 -1
- data/ext/faiss/kmeans.cpp +1 -1
- data/ext/faiss/pca_matrix.cpp +1 -1
- data/ext/faiss/product_quantizer.cpp +1 -1
- data/ext/faiss/{utils_rb.cpp → utils.cpp} +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +93 -80
- data/vendor/faiss/faiss/Clustering.cpp +39 -240
- data/vendor/faiss/faiss/Clustering.h +6 -0
- data/vendor/faiss/faiss/IVFlib.cpp +41 -21
- data/vendor/faiss/faiss/Index.cpp +6 -5
- data/vendor/faiss/faiss/Index.h +5 -5
- data/vendor/faiss/faiss/Index2Layer.cpp +37 -53
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +49 -37
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +36 -34
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +4 -1
- data/vendor/faiss/faiss/IndexBinary.cpp +5 -3
- data/vendor/faiss/faiss/IndexBinary.h +4 -4
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -1
- data/vendor/faiss/faiss/IndexBinaryFlat.h +1 -1
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +4 -4
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +84 -92
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +9 -3
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +45 -236
- data/vendor/faiss/faiss/IndexBinaryHash.h +6 -6
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +87 -415
- data/vendor/faiss/faiss/IndexFastScan.cpp +72 -109
- data/vendor/faiss/faiss/IndexFastScan.h +25 -23
- data/vendor/faiss/faiss/IndexFlat.cpp +27 -20
- data/vendor/faiss/faiss/IndexFlat.h +21 -18
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +42 -19
- data/vendor/faiss/faiss/IndexHNSW.cpp +283 -145
- data/vendor/faiss/faiss/IndexHNSW.h +16 -2
- data/vendor/faiss/faiss/IndexIDMap.cpp +25 -21
- data/vendor/faiss/faiss/IndexIDMap.h +9 -7
- data/vendor/faiss/faiss/IndexIVF.cpp +465 -362
- data/vendor/faiss/faiss/IndexIVF.h +33 -12
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +77 -74
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +96 -93
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +4 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +357 -238
- data/vendor/faiss/faiss/IndexIVFFastScan.h +42 -41
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +36 -68
- data/vendor/faiss/faiss/IndexIVFFlat.h +32 -0
- data/vendor/faiss/faiss/IndexIVFFlatPanorama.cpp +53 -30
- data/vendor/faiss/faiss/IndexIVFFlatPanorama.h +3 -1
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +18 -15
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +71 -843
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +151 -121
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +3 -0
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +21 -17
- data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +26 -39
- data/vendor/faiss/faiss/IndexIVFRaBitQ.h +2 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +475 -476
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +248 -93
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +41 -127
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +1 -1
- data/vendor/faiss/faiss/IndexLSH.cpp +36 -19
- data/vendor/faiss/faiss/IndexLattice.cpp +13 -13
- data/vendor/faiss/faiss/IndexNNDescent.cpp +36 -21
- data/vendor/faiss/faiss/IndexNNDescent.h +2 -2
- data/vendor/faiss/faiss/IndexNSG.cpp +39 -23
- data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +31 -11
- data/vendor/faiss/faiss/IndexPQ.cpp +128 -221
- data/vendor/faiss/faiss/IndexPQ.h +3 -2
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +20 -14
- data/vendor/faiss/faiss/IndexPQFastScan.h +3 -0
- data/vendor/faiss/faiss/IndexPreTransform.cpp +25 -18
- data/vendor/faiss/faiss/IndexPreTransform.h +1 -1
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +11 -36
- data/vendor/faiss/faiss/IndexRaBitQ.h +2 -1
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +41 -277
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +183 -27
- data/vendor/faiss/faiss/IndexRefine.cpp +30 -25
- data/vendor/faiss/faiss/IndexRefine.h +4 -4
- data/vendor/faiss/faiss/IndexReplicas.cpp +6 -6
- data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +15 -14
- data/vendor/faiss/faiss/IndexRowwiseMinMax.h +1 -1
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +82 -14
- data/vendor/faiss/faiss/IndexShards.cpp +10 -9
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +21 -15
- data/vendor/faiss/faiss/MatrixStats.cpp +5 -4
- data/vendor/faiss/faiss/MetaIndexes.cpp +19 -17
- data/vendor/faiss/faiss/MetaIndexes.h +1 -1
- data/vendor/faiss/faiss/MetricType.h +14 -7
- data/vendor/faiss/faiss/SuperKMeans.cpp +656 -0
- data/vendor/faiss/faiss/SuperKMeans.h +97 -0
- data/vendor/faiss/faiss/VectorTransform.cpp +237 -149
- data/vendor/faiss/faiss/VectorTransform.h +16 -16
- data/vendor/faiss/faiss/build.cpp +23 -0
- data/vendor/faiss/faiss/build.h +15 -0
- data/vendor/faiss/faiss/clone_index.cpp +48 -47
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +47 -47
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +11 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +38 -38
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +11 -0
- data/vendor/faiss/faiss/factory_tools.cpp +5 -0
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +6 -5
- data/vendor/faiss/faiss/gpu/GpuResources.h +1 -1
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +9 -9
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +4 -3
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +46 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +56 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +78 -1
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +72 -0
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +23 -0
- data/vendor/faiss/faiss/gpu/utils/CuvsFilterConvert.h +1 -1
- data/vendor/faiss/faiss/gpu/utils/CuvsUtils.h +21 -10
- data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +22 -0
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +35 -0
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +40 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +51 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +65 -0
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +66 -0
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +79 -0
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +35 -0
- data/vendor/faiss/faiss/impl/AdSampling.cpp +103 -0
- data/vendor/faiss/faiss/impl/AdSampling.h +35 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +29 -25
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +1 -0
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +10 -9
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +3 -0
- data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +244 -0
- data/vendor/faiss/faiss/impl/ClusteringHelpers.h +94 -0
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +16 -16
- data/vendor/faiss/faiss/impl/CodePacker.cpp +3 -3
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +1 -1
- data/vendor/faiss/faiss/impl/DistanceComputer.h +8 -8
- data/vendor/faiss/faiss/impl/FaissAssert.h +6 -3
- data/vendor/faiss/faiss/impl/FaissException.h +50 -3
- data/vendor/faiss/faiss/impl/HNSW.cpp +92 -317
- data/vendor/faiss/faiss/impl/HNSW.h +13 -34
- data/vendor/faiss/faiss/impl/IDSelector.cpp +15 -11
- data/vendor/faiss/faiss/impl/IDSelector.h +8 -8
- data/vendor/faiss/faiss/impl/InvertedListScannerStats.h +26 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +82 -77
- data/vendor/faiss/faiss/impl/NNDescent.cpp +62 -25
- data/vendor/faiss/faiss/impl/NNDescent.h +6 -2
- data/vendor/faiss/faiss/impl/NSG.cpp +38 -21
- data/vendor/faiss/faiss/impl/NSG.h +4 -4
- data/vendor/faiss/faiss/impl/Panorama.cpp +23 -6
- data/vendor/faiss/faiss/impl/Panorama.h +258 -87
- data/vendor/faiss/faiss/impl/PdxLayout.cpp +93 -0
- data/vendor/faiss/faiss/impl/PdxLayout.h +41 -0
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +46 -32
- data/vendor/faiss/faiss/impl/PolysemousTraining.h +3 -3
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +35 -35
- data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +21 -16
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +30 -23
- data/vendor/faiss/faiss/impl/Quantizer.h +2 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +55 -49
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +65 -0
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +296 -283
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +26 -23
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +1 -1
- data/vendor/faiss/faiss/impl/ResultHandler.h +99 -75
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +52 -4
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +27 -1
- data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +14 -11
- data/vendor/faiss/faiss/impl/VisitedTable.h +7 -0
- data/vendor/faiss/faiss/impl/approx_topk/approx_topk.h +276 -0
- data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +68 -0
- data/vendor/faiss/faiss/{utils → impl}/approx_topk/generic.h +15 -8
- data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +68 -0
- data/vendor/faiss/faiss/impl/approx_topk/rq_beam_search_tab-inl.h +169 -0
- data/vendor/faiss/faiss/impl/approx_topk/rq_beam_search_tab.h +117 -0
- data/vendor/faiss/faiss/impl/approx_topk/simdlib256-inl.h +146 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHNSW_impl.h +73 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +270 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +460 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexIVFSpectralHash_impl.h +159 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexPQ_impl.h +92 -0
- data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +26 -0
- data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +26 -0
- data/vendor/faiss/faiss/impl/binary_hamming/dispatch.h +143 -0
- data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +26 -0
- data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +26 -0
- data/vendor/faiss/faiss/impl/expanded_scanners.h +8 -3
- data/vendor/faiss/faiss/impl/{FastScanDistancePostProcessing.h → fast_scan/FastScanDistancePostProcessing.h} +13 -6
- data/vendor/faiss/faiss/impl/{LookupTableScaler.h → fast_scan/LookupTableScaler.h} +16 -5
- data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +237 -0
- data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +185 -0
- data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +229 -0
- data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +268 -0
- data/vendor/faiss/faiss/impl/{pq4_fast_scan.cpp → fast_scan/fast_scan.cpp} +169 -2
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +341 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +36 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +40 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +120 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +104 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +213 -0
- data/vendor/faiss/faiss/impl/{pq4_fast_scan_search_qbs.cpp → fast_scan/kernels_simd512.h} +26 -356
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_dispatching.h +90 -0
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +108 -0
- data/vendor/faiss/faiss/impl/{simd_result_handlers.h → fast_scan/simd_result_handlers.h} +282 -134
- data/vendor/faiss/faiss/impl/hnsw/LockVector.cpp +54 -0
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +64 -0
- data/vendor/faiss/faiss/impl/hnsw/MinimaxHeap.cpp +91 -0
- data/vendor/faiss/faiss/impl/hnsw/MinimaxHeap.h +64 -0
- data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +104 -0
- data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +111 -0
- data/vendor/faiss/faiss/impl/index_read.cpp +1132 -45
- data/vendor/faiss/faiss/impl/index_read_utils.h +1 -1
- data/vendor/faiss/faiss/impl/index_write.cpp +95 -13
- data/vendor/faiss/faiss/impl/io.cpp +6 -6
- data/vendor/faiss/faiss/impl/io_macros.h +33 -16
- data/vendor/faiss/faiss/impl/kmeans1d.cpp +10 -10
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +37 -23
- data/vendor/faiss/faiss/impl/lattice_Zn.h +6 -6
- data/vendor/faiss/faiss/impl/mapped_io.cpp +6 -6
- data/vendor/faiss/faiss/impl/platform_macros.h +11 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +549 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +245 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.h +105 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +106 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +21 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +21 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +21 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/{pq_code_distance-avx2.cpp → pq_code_distance-avx2.h} +9 -13
- data/vendor/faiss/faiss/impl/pq_code_distance/{pq_code_distance-avx512.cpp → pq_code_distance-avx512.h} +9 -57
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +29 -111
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +96 -0
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +238 -5
- data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +5 -7
- data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +68 -0
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +311 -477
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/codecs.h +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +3 -2
- data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +102 -11
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +27 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/similarities.h +3 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +148 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +167 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +59 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +163 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +311 -0
- data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +192 -8
- data/vendor/faiss/faiss/impl/scalar_quantizer/training.h +12 -0
- data/vendor/faiss/faiss/impl/simd_dispatch.h +100 -66
- data/vendor/faiss/faiss/impl/simdlib/simdlib.h +57 -0
- data/vendor/faiss/faiss/{utils → impl/simdlib}/simdlib_avx2.h +264 -172
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +414 -0
- data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +44 -0
- data/vendor/faiss/faiss/{utils → impl/simdlib}/simdlib_emulated.h +231 -166
- data/vendor/faiss/faiss/{utils → impl/simdlib}/simdlib_neon.h +270 -218
- data/vendor/faiss/faiss/{utils → impl/simdlib}/simdlib_ppc64.h +201 -160
- data/vendor/faiss/faiss/impl/svs_io.cpp +12 -3
- data/vendor/faiss/faiss/impl/svs_io.h +8 -2
- data/vendor/faiss/faiss/index_factory.cpp +86 -18
- data/vendor/faiss/faiss/index_io.h +24 -0
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +66 -16
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +24 -14
- data/vendor/faiss/faiss/invlists/DirectMap.h +4 -3
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +157 -73
- data/vendor/faiss/faiss/invlists/InvertedLists.h +86 -23
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +4 -4
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +13 -13
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +350 -0
- data/vendor/faiss/faiss/svs/IndexSVSIVF.h +128 -0
- data/vendor/faiss/faiss/svs/IndexSVSIVFLVQ.cpp +40 -0
- data/vendor/faiss/faiss/svs/IndexSVSIVFLVQ.h +43 -0
- data/vendor/faiss/faiss/svs/IndexSVSIVFLeanVec.cpp +225 -0
- data/vendor/faiss/faiss/svs/IndexSVSIVFLeanVec.h +71 -0
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +25 -1
- data/vendor/faiss/faiss/svs/IndexSVSVamana.h +18 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +12 -3
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +7 -2
- data/vendor/faiss/faiss/utils/Heap.cpp +10 -10
- data/vendor/faiss/faiss/utils/NeuralNet.cpp +47 -36
- data/vendor/faiss/faiss/utils/NeuralNet.h +1 -1
- data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +10 -4
- data/vendor/faiss/faiss/utils/distances.cpp +390 -560
- data/vendor/faiss/faiss/utils/distances.h +20 -1
- data/vendor/faiss/faiss/utils/distances_dispatch.h +117 -37
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +8 -7
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +33 -14
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +12 -1
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +16 -293
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +57 -0
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +290 -0
- data/vendor/faiss/faiss/utils/distances_simd.cpp +5 -177
- data/vendor/faiss/faiss/utils/extra_distances.cpp +9 -8
- data/vendor/faiss/faiss/utils/extra_distances.h +32 -6
- data/vendor/faiss/faiss/utils/hamming-inl.h +13 -11
- data/vendor/faiss/faiss/utils/hamming.cpp +66 -517
- data/vendor/faiss/faiss/utils/hamming.h +92 -2
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +287 -10
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +15 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +15 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx2.h +142 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +234 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-generic.h +368 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +322 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-rvv.h +39 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer.h +146 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +481 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +15 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +15 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +66 -987
- data/vendor/faiss/faiss/utils/partitioning.h +31 -0
- data/vendor/faiss/faiss/utils/popcount.h +29 -0
- data/vendor/faiss/faiss/utils/pq_code_distance.h +2 -2
- data/vendor/faiss/faiss/utils/prefetch.h +2 -2
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +30 -30
- data/vendor/faiss/faiss/utils/quantize_lut.h +1 -1
- data/vendor/faiss/faiss/utils/rabitq_simd.h +57 -536
- data/vendor/faiss/faiss/utils/random.cpp +6 -6
- data/vendor/faiss/faiss/utils/simd_impl/IVFFlatScanner-inl.h +51 -0
- data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +5 -1
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +213 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +163 -10
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +250 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +7 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +189 -0
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +195 -0
- data/vendor/faiss/faiss/utils/simd_impl/distances_sse-inl.h +2 -1
- data/vendor/faiss/faiss/utils/{distances_fused/simdlib_based.h → simd_impl/exhaustive_L2sqr_blas_cmax.h} +5 -10
- data/vendor/faiss/faiss/utils/simd_impl/hamming_impl.h +481 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +14 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +14 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +1085 -0
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +355 -0
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +477 -0
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +55 -0
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +55 -0
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +32 -0
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +43 -0
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +57 -0
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +45 -0
- data/vendor/faiss/faiss/utils/simd_levels.cpp +17 -5
- data/vendor/faiss/faiss/utils/simd_levels.h +93 -1
- data/vendor/faiss/faiss/utils/sorting.cpp +48 -36
- data/vendor/faiss/faiss/utils/utils.cpp +5 -5
- data/vendor/faiss/faiss/utils/utils.h +3 -3
- metadata +119 -34
- data/vendor/faiss/faiss/impl/RaBitQStats.cpp +0 -29
- data/vendor/faiss/faiss/impl/RaBitQStats.h +0 -56
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +0 -224
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +0 -230
- data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +0 -84
- data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +0 -196
- data/vendor/faiss/faiss/utils/approx_topk/mode.h +0 -34
- data/vendor/faiss/faiss/utils/distances_fused/avx512.h +0 -36
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +0 -235
- data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +0 -462
- data/vendor/faiss/faiss/utils/hamming_distance/avx512-inl.h +0 -490
- data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +0 -449
- data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +0 -87
- data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +0 -524
- data/vendor/faiss/faiss/utils/simdlib.h +0 -42
- data/vendor/faiss/faiss/utils/simdlib_avx512.h +0 -365
- /data/ext/faiss/{utils_rb.h → utils.h} +0 -0
|
@@ -85,6 +85,19 @@ ScalarQuantizer::SQuantizer* sq_select_quantizer<THE_LEVEL_TO_DISPATCH>(
|
|
|
85
85
|
return new Quantizer8bitDirect<SL>(d, trained);
|
|
86
86
|
case ScalarQuantizer::QT_8bit_direct_signed:
|
|
87
87
|
return new Quantizer8bitDirectSigned<SL>(d, trained);
|
|
88
|
+
case ScalarQuantizer::QT_0bit:
|
|
89
|
+
FAISS_THROW_MSG(
|
|
90
|
+
"QT_0bit does not support standalone quantization, use IndexIVFScalarQuantizer");
|
|
91
|
+
case ScalarQuantizer::QT_1bit_tqmse:
|
|
92
|
+
return new QuantizerTurboQuantMSE<1, SL>(d, trained);
|
|
93
|
+
case ScalarQuantizer::QT_2bit_tqmse:
|
|
94
|
+
return new QuantizerTurboQuantMSE<2, SL>(d, trained);
|
|
95
|
+
case ScalarQuantizer::QT_3bit_tqmse:
|
|
96
|
+
return new QuantizerTurboQuantMSE<3, SL>(d, trained);
|
|
97
|
+
case ScalarQuantizer::QT_4bit_tqmse:
|
|
98
|
+
return new QuantizerTurboQuantMSE<4, SL>(d, trained);
|
|
99
|
+
case ScalarQuantizer::QT_8bit_tqmse:
|
|
100
|
+
return new QuantizerTurboQuantMSE<8, SL>(d, trained);
|
|
88
101
|
default:
|
|
89
102
|
FAISS_THROW_MSG("unknown qtype");
|
|
90
103
|
}
|
|
@@ -175,6 +188,24 @@ SQDistanceComputer* select_distance_computer_body(
|
|
|
175
188
|
case ScalarQuantizer::QT_8bit_direct_signed:
|
|
176
189
|
return new DCTemplate<Quantizer8bitDirectSigned<SL2>, Sim, SL2>(
|
|
177
190
|
d, trained);
|
|
191
|
+
case ScalarQuantizer::QT_0bit:
|
|
192
|
+
FAISS_THROW_MSG(
|
|
193
|
+
"QT_0bit does not support standalone distance computation, use IndexIVFScalarQuantizer");
|
|
194
|
+
case ScalarQuantizer::QT_1bit_tqmse:
|
|
195
|
+
return new DCTemplate<QuantizerTurboQuantMSE<1, SL2>, Sim, SL2>(
|
|
196
|
+
d, trained);
|
|
197
|
+
case ScalarQuantizer::QT_2bit_tqmse:
|
|
198
|
+
return new DCTemplate<QuantizerTurboQuantMSE<2, SL2>, Sim, SL2>(
|
|
199
|
+
d, trained);
|
|
200
|
+
case ScalarQuantizer::QT_3bit_tqmse:
|
|
201
|
+
return new DCTemplate<QuantizerTurboQuantMSE<3, SL2>, Sim, SL2>(
|
|
202
|
+
d, trained);
|
|
203
|
+
case ScalarQuantizer::QT_4bit_tqmse:
|
|
204
|
+
return new DCTemplate<QuantizerTurboQuantMSE<4, SL2>, Sim, SL2>(
|
|
205
|
+
d, trained);
|
|
206
|
+
case ScalarQuantizer::QT_8bit_tqmse:
|
|
207
|
+
return new DCTemplate<QuantizerTurboQuantMSE<8, SL2>, Sim, SL2>(
|
|
208
|
+
d, trained);
|
|
178
209
|
default:
|
|
179
210
|
FAISS_THROW_MSG("unknown qtype");
|
|
180
211
|
}
|
|
@@ -309,6 +340,34 @@ InvertedListScanner* sq_select_InvertedListScanner<THE_LEVEL_TO_DISPATCH>(
|
|
|
309
340
|
Quantizer8bitDirectSigned<SL2>,
|
|
310
341
|
Similarity,
|
|
311
342
|
SL2>>();
|
|
343
|
+
case ScalarQuantizer::QT_0bit:
|
|
344
|
+
return new IVFCoarseDistanceScanner(
|
|
345
|
+
Similarity::metric_type != METRIC_L2, store_pairs, sel);
|
|
346
|
+
case ScalarQuantizer::QT_1bit_tqmse:
|
|
347
|
+
return scan.template operator()<DCTemplate<
|
|
348
|
+
QuantizerTurboQuantMSE<1, SL2>,
|
|
349
|
+
Similarity,
|
|
350
|
+
SL2>>();
|
|
351
|
+
case ScalarQuantizer::QT_2bit_tqmse:
|
|
352
|
+
return scan.template operator()<DCTemplate<
|
|
353
|
+
QuantizerTurboQuantMSE<2, SL2>,
|
|
354
|
+
Similarity,
|
|
355
|
+
SL2>>();
|
|
356
|
+
case ScalarQuantizer::QT_3bit_tqmse:
|
|
357
|
+
return scan.template operator()<DCTemplate<
|
|
358
|
+
QuantizerTurboQuantMSE<3, SL2>,
|
|
359
|
+
Similarity,
|
|
360
|
+
SL2>>();
|
|
361
|
+
case ScalarQuantizer::QT_4bit_tqmse:
|
|
362
|
+
return scan.template operator()<DCTemplate<
|
|
363
|
+
QuantizerTurboQuantMSE<4, SL2>,
|
|
364
|
+
Similarity,
|
|
365
|
+
SL2>>();
|
|
366
|
+
case ScalarQuantizer::QT_8bit_tqmse:
|
|
367
|
+
return scan.template operator()<DCTemplate<
|
|
368
|
+
QuantizerTurboQuantMSE<8, SL2>,
|
|
369
|
+
Similarity,
|
|
370
|
+
SL2>>();
|
|
312
371
|
default:
|
|
313
372
|
FAISS_THROW_MSG("unknown qtype");
|
|
314
373
|
}
|
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
|
|
8
8
|
#ifdef COMPILE_SIMD_ARM_NEON
|
|
9
9
|
|
|
10
|
+
#include <faiss/impl/simdlib/simdlib_neon.h>
|
|
11
|
+
|
|
12
|
+
#include <cstring>
|
|
13
|
+
|
|
10
14
|
#include <faiss/impl/scalar_quantizer/codecs.h>
|
|
11
15
|
#include <faiss/impl/scalar_quantizer/distance_computers.h>
|
|
12
16
|
#include <faiss/impl/scalar_quantizer/quantizers.h>
|
|
@@ -17,6 +21,81 @@ namespace faiss {
|
|
|
17
21
|
|
|
18
22
|
namespace scalar_quantizer {
|
|
19
23
|
|
|
24
|
+
using simd8float32 = faiss::simd8float32_tpl<SIMDLevel::ARM_NEON>;
|
|
25
|
+
|
|
26
|
+
namespace {
|
|
27
|
+
|
|
28
|
+
FAISS_ALWAYS_INLINE uint16_t load_u16(const uint8_t* ptr) {
|
|
29
|
+
uint16_t value;
|
|
30
|
+
std::memcpy(&value, ptr, sizeof(value));
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
FAISS_ALWAYS_INLINE uint32_t load_u32(const uint8_t* ptr) {
|
|
35
|
+
uint32_t value;
|
|
36
|
+
std::memcpy(&value, ptr, sizeof(value));
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
FAISS_ALWAYS_INLINE uint32_t load_u24(const uint8_t* ptr) {
|
|
41
|
+
return static_cast<uint32_t>(ptr[0]) |
|
|
42
|
+
(static_cast<uint32_t>(ptr[1]) << 8) |
|
|
43
|
+
(static_cast<uint32_t>(ptr[2]) << 16);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
FAISS_ALWAYS_INLINE void unpack_8x1bit_to_u8(
|
|
47
|
+
const uint8_t* code,
|
|
48
|
+
int i,
|
|
49
|
+
uint8_t out[8]) {
|
|
50
|
+
const uint8_t packed = code[static_cast<size_t>(i) >> 3];
|
|
51
|
+
for (size_t j = 0; j < 8; ++j) {
|
|
52
|
+
out[j] = (packed >> j) & 0x1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
FAISS_ALWAYS_INLINE void unpack_8x2bit_to_u8(
|
|
57
|
+
const uint8_t* code,
|
|
58
|
+
int i,
|
|
59
|
+
uint8_t out[8]) {
|
|
60
|
+
const uint16_t packed = load_u16(code + (static_cast<size_t>(i) >> 2));
|
|
61
|
+
for (size_t j = 0; j < 8; ++j) {
|
|
62
|
+
out[j] = (packed >> (2 * j)) & 0x3;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
FAISS_ALWAYS_INLINE void unpack_8x3bit_to_u8(
|
|
67
|
+
const uint8_t* code,
|
|
68
|
+
int i,
|
|
69
|
+
uint8_t out[8]) {
|
|
70
|
+
const uint32_t packed =
|
|
71
|
+
load_u24(code + ((static_cast<size_t>(i) >> 3) * 3));
|
|
72
|
+
for (size_t j = 0; j < 8; ++j) {
|
|
73
|
+
out[j] = (packed >> (3 * j)) & 0x7;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
FAISS_ALWAYS_INLINE void unpack_8x4bit_to_u8(
|
|
78
|
+
const uint8_t* code,
|
|
79
|
+
int i,
|
|
80
|
+
uint8_t out[8]) {
|
|
81
|
+
const uint32_t packed = load_u32(code + (static_cast<size_t>(i) >> 1));
|
|
82
|
+
for (size_t j = 0; j < 8; ++j) {
|
|
83
|
+
out[j] = (packed >> (4 * j)) & 0xf;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
FAISS_ALWAYS_INLINE simd8float32
|
|
88
|
+
gather_8_components(const float* codebook, const uint8_t indices[8]) {
|
|
89
|
+
float result[8];
|
|
90
|
+
for (size_t j = 0; j < 8; ++j) {
|
|
91
|
+
result[j] = codebook[indices[j]];
|
|
92
|
+
}
|
|
93
|
+
return simd8float32(
|
|
94
|
+
float32x4x2_t{vld1q_f32(result), vld1q_f32(result + 4)});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
} // namespace
|
|
98
|
+
|
|
20
99
|
/**********************************************************
|
|
21
100
|
* Codecs
|
|
22
101
|
**********************************************************/
|
|
@@ -136,6 +215,54 @@ struct QuantizerTemplate<
|
|
|
136
215
|
}
|
|
137
216
|
};
|
|
138
217
|
|
|
218
|
+
/**********************************************************
|
|
219
|
+
* TurboQuant MSE quantizer
|
|
220
|
+
**********************************************************/
|
|
221
|
+
|
|
222
|
+
#define DEFINE_TQMSE_NEON_SPECIALIZATION(NBITS, UNPACK_FN) \
|
|
223
|
+
template <> \
|
|
224
|
+
struct QuantizerTurboQuantMSE<NBITS, SIMDLevel::ARM_NEON> \
|
|
225
|
+
: QuantizerTurboQuantMSE<NBITS, SIMDLevel::NONE> { \
|
|
226
|
+
using Base = QuantizerTurboQuantMSE<NBITS, SIMDLevel::NONE>; \
|
|
227
|
+
\
|
|
228
|
+
QuantizerTurboQuantMSE(size_t d, const std::vector<float>& trained) \
|
|
229
|
+
: Base(d, trained) { \
|
|
230
|
+
assert(d % 8 == 0); \
|
|
231
|
+
} \
|
|
232
|
+
\
|
|
233
|
+
FAISS_ALWAYS_INLINE simd8float32 \
|
|
234
|
+
reconstruct_8_components(const uint8_t* code, int i) const { \
|
|
235
|
+
uint8_t indices[8]; \
|
|
236
|
+
UNPACK_FN(code, i, indices); \
|
|
237
|
+
return gather_8_components(this->centroids, indices); \
|
|
238
|
+
} \
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
DEFINE_TQMSE_NEON_SPECIALIZATION(1, unpack_8x1bit_to_u8);
|
|
242
|
+
DEFINE_TQMSE_NEON_SPECIALIZATION(2, unpack_8x2bit_to_u8);
|
|
243
|
+
DEFINE_TQMSE_NEON_SPECIALIZATION(3, unpack_8x3bit_to_u8);
|
|
244
|
+
DEFINE_TQMSE_NEON_SPECIALIZATION(4, unpack_8x4bit_to_u8);
|
|
245
|
+
|
|
246
|
+
#undef DEFINE_TQMSE_NEON_SPECIALIZATION
|
|
247
|
+
|
|
248
|
+
template <>
|
|
249
|
+
struct QuantizerTurboQuantMSE<8, SIMDLevel::ARM_NEON>
|
|
250
|
+
: QuantizerTurboQuantMSE<8, SIMDLevel::NONE> {
|
|
251
|
+
using Base = QuantizerTurboQuantMSE<8, SIMDLevel::NONE>;
|
|
252
|
+
|
|
253
|
+
QuantizerTurboQuantMSE(size_t d, const std::vector<float>& trained)
|
|
254
|
+
: Base(d, trained) {
|
|
255
|
+
assert(d % 8 == 0);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
FAISS_ALWAYS_INLINE simd8float32
|
|
259
|
+
reconstruct_8_components(const uint8_t* code, int i) const {
|
|
260
|
+
uint8_t indices[8];
|
|
261
|
+
std::memcpy(indices, code + static_cast<size_t>(i), sizeof(indices));
|
|
262
|
+
return gather_8_components(this->centroids, indices);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
139
266
|
/**********************************************************
|
|
140
267
|
* FP16 Quantizer
|
|
141
268
|
**********************************************************/
|
|
@@ -354,6 +481,42 @@ struct DCTemplate<Quantizer, Similarity, SIMDLevel::ARM_NEON>
|
|
|
354
481
|
float query_to_code(const uint8_t* code) const final {
|
|
355
482
|
return compute_distance(q, code);
|
|
356
483
|
}
|
|
484
|
+
|
|
485
|
+
void query_to_codes_batch_4(
|
|
486
|
+
const uint8_t* code_0,
|
|
487
|
+
const uint8_t* code_1,
|
|
488
|
+
const uint8_t* code_2,
|
|
489
|
+
const uint8_t* code_3,
|
|
490
|
+
float& dis0,
|
|
491
|
+
float& dis1,
|
|
492
|
+
float& dis2,
|
|
493
|
+
float& dis3) const final {
|
|
494
|
+
Similarity sim0(q);
|
|
495
|
+
Similarity sim1(q);
|
|
496
|
+
Similarity sim2(q);
|
|
497
|
+
Similarity sim3(q);
|
|
498
|
+
|
|
499
|
+
sim0.begin_8();
|
|
500
|
+
sim1.begin_8();
|
|
501
|
+
sim2.begin_8();
|
|
502
|
+
sim3.begin_8();
|
|
503
|
+
|
|
504
|
+
for (size_t i = 0; i < quant.d; i += 8) {
|
|
505
|
+
simd8float32 xi0 = quant.reconstruct_8_components(code_0, i);
|
|
506
|
+
simd8float32 xi1 = quant.reconstruct_8_components(code_1, i);
|
|
507
|
+
simd8float32 xi2 = quant.reconstruct_8_components(code_2, i);
|
|
508
|
+
simd8float32 xi3 = quant.reconstruct_8_components(code_3, i);
|
|
509
|
+
sim0.add_8_components(xi0);
|
|
510
|
+
sim1.add_8_components(xi1);
|
|
511
|
+
sim2.add_8_components(xi2);
|
|
512
|
+
sim3.add_8_components(xi3);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
dis0 = sim0.result_8();
|
|
516
|
+
dis1 = sim1.result_8();
|
|
517
|
+
dis2 = sim2.result_8();
|
|
518
|
+
dis3 = sim3.result_8();
|
|
519
|
+
}
|
|
357
520
|
};
|
|
358
521
|
|
|
359
522
|
template <class Similarity>
|
|
@@ -0,0 +1,311 @@
|
|
|
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
|
+
#ifdef COMPILE_SIMD_RISCV_RVV
|
|
9
|
+
|
|
10
|
+
#include <faiss/impl/scalar_quantizer/codecs.h>
|
|
11
|
+
#include <faiss/impl/scalar_quantizer/distance_computers.h>
|
|
12
|
+
#include <faiss/impl/scalar_quantizer/quantizers.h>
|
|
13
|
+
#include <faiss/impl/scalar_quantizer/scanners.h>
|
|
14
|
+
#include <faiss/impl/scalar_quantizer/similarities.h>
|
|
15
|
+
|
|
16
|
+
#include <riscv_vector.h>
|
|
17
|
+
#include <cmath>
|
|
18
|
+
|
|
19
|
+
namespace faiss {
|
|
20
|
+
|
|
21
|
+
namespace scalar_quantizer {
|
|
22
|
+
|
|
23
|
+
/*************************************************************************
|
|
24
|
+
* Marker specializations.
|
|
25
|
+
*
|
|
26
|
+
* Unlike x86/NEON sq-*.cpp files that expose a fixed 8-wide / 16-wide codec
|
|
27
|
+
* interface (reconstruct_8_components / reconstruct_16_components), RVV is
|
|
28
|
+
* variable-width: the native vector length is implementation-defined and
|
|
29
|
+
* queried at runtime via __riscv_vsetvl. Forcing RVV into a fixed-width
|
|
30
|
+
* codec would leave performance on the table on wider hardware.
|
|
31
|
+
*
|
|
32
|
+
* So the strategy here is: Codec / Quantizer / Similarity classes for
|
|
33
|
+
* RISCV_RVV act as opaque TAG TYPES — they only need to be complete types
|
|
34
|
+
* so that baseline's sq-dispatch.h can form template arguments like
|
|
35
|
+
* `DCTemplate<QuantizerTemplate<Codec4bit<RISCV_RVV>, UNIFORM, RISCV_RVV>,
|
|
36
|
+
* SimilarityL2<RISCV_RVV>, RISCV_RVV>`.
|
|
37
|
+
*
|
|
38
|
+
* The real SIMD work lives in full DCTemplate specializations below.
|
|
39
|
+
* Unspecialized combinations fall through to scalar via the fallback
|
|
40
|
+
* `DCTemplate<Q, Sim, RISCV_RVV> : DCTemplate<Q, Sim, NONE>`.
|
|
41
|
+
************************************************************************/
|
|
42
|
+
|
|
43
|
+
template <>
|
|
44
|
+
struct Codec8bit<SIMDLevel::RISCV_RVV> : Codec8bit<SIMDLevel::NONE> {};
|
|
45
|
+
|
|
46
|
+
template <>
|
|
47
|
+
struct Codec4bit<SIMDLevel::RISCV_RVV> : Codec4bit<SIMDLevel::NONE> {};
|
|
48
|
+
|
|
49
|
+
template <>
|
|
50
|
+
struct Codec6bit<SIMDLevel::RISCV_RVV> : Codec6bit<SIMDLevel::NONE> {};
|
|
51
|
+
|
|
52
|
+
template <class Codec>
|
|
53
|
+
struct QuantizerTemplate<
|
|
54
|
+
Codec,
|
|
55
|
+
QuantizerTemplateScaling::UNIFORM,
|
|
56
|
+
SIMDLevel::RISCV_RVV>
|
|
57
|
+
: QuantizerTemplate<
|
|
58
|
+
Codec,
|
|
59
|
+
QuantizerTemplateScaling::UNIFORM,
|
|
60
|
+
SIMDLevel::NONE> {
|
|
61
|
+
QuantizerTemplate(size_t d, const std::vector<float>& trained)
|
|
62
|
+
: QuantizerTemplate<
|
|
63
|
+
Codec,
|
|
64
|
+
QuantizerTemplateScaling::UNIFORM,
|
|
65
|
+
SIMDLevel::NONE>(d, trained) {}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
template <class Codec>
|
|
69
|
+
struct QuantizerTemplate<
|
|
70
|
+
Codec,
|
|
71
|
+
QuantizerTemplateScaling::NON_UNIFORM,
|
|
72
|
+
SIMDLevel::RISCV_RVV>
|
|
73
|
+
: QuantizerTemplate<
|
|
74
|
+
Codec,
|
|
75
|
+
QuantizerTemplateScaling::NON_UNIFORM,
|
|
76
|
+
SIMDLevel::NONE> {
|
|
77
|
+
QuantizerTemplate(size_t d, const std::vector<float>& trained)
|
|
78
|
+
: QuantizerTemplate<
|
|
79
|
+
Codec,
|
|
80
|
+
QuantizerTemplateScaling::NON_UNIFORM,
|
|
81
|
+
SIMDLevel::NONE>(d, trained) {}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
template <>
|
|
85
|
+
struct QuantizerFP16<SIMDLevel::RISCV_RVV> : QuantizerFP16<SIMDLevel::NONE> {
|
|
86
|
+
QuantizerFP16(size_t d, const std::vector<float>& trained)
|
|
87
|
+
: QuantizerFP16<SIMDLevel::NONE>(d, trained) {}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
template <>
|
|
91
|
+
struct QuantizerBF16<SIMDLevel::RISCV_RVV> : QuantizerBF16<SIMDLevel::NONE> {
|
|
92
|
+
QuantizerBF16(size_t d, const std::vector<float>& trained)
|
|
93
|
+
: QuantizerBF16<SIMDLevel::NONE>(d, trained) {}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
template <>
|
|
97
|
+
struct Quantizer8bitDirect<SIMDLevel::RISCV_RVV>
|
|
98
|
+
: Quantizer8bitDirect<SIMDLevel::NONE> {
|
|
99
|
+
Quantizer8bitDirect(size_t d, const std::vector<float>& trained)
|
|
100
|
+
: Quantizer8bitDirect<SIMDLevel::NONE>(d, trained) {}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
template <>
|
|
104
|
+
struct Quantizer8bitDirectSigned<SIMDLevel::RISCV_RVV>
|
|
105
|
+
: Quantizer8bitDirectSigned<SIMDLevel::NONE> {
|
|
106
|
+
Quantizer8bitDirectSigned(size_t d, const std::vector<float>& trained)
|
|
107
|
+
: Quantizer8bitDirectSigned<SIMDLevel::NONE>(d, trained) {}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
template <>
|
|
111
|
+
struct SimilarityL2<SIMDLevel::RISCV_RVV> : SimilarityL2<SIMDLevel::NONE> {
|
|
112
|
+
using SimilarityL2<SIMDLevel::NONE>::SimilarityL2;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
template <>
|
|
116
|
+
struct SimilarityIP<SIMDLevel::RISCV_RVV> : SimilarityIP<SIMDLevel::NONE> {
|
|
117
|
+
using SimilarityIP<SIMDLevel::NONE>::SimilarityIP;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/*************************************************************************
|
|
121
|
+
* Fallback DCTemplate / DistanceComputerByte for RISCV_RVV.
|
|
122
|
+
*
|
|
123
|
+
* Inheriting from the NONE specialization means every (Quantizer, Similarity)
|
|
124
|
+
* combination that does NOT have a hand-tuned RVV full specialization below
|
|
125
|
+
* falls through to scalar code. Callers and the dispatcher don't know or care.
|
|
126
|
+
************************************************************************/
|
|
127
|
+
|
|
128
|
+
template <class Quantizer, class Similarity>
|
|
129
|
+
struct DCTemplate<Quantizer, Similarity, SIMDLevel::RISCV_RVV>
|
|
130
|
+
: DCTemplate<Quantizer, Similarity, SIMDLevel::NONE> {
|
|
131
|
+
using Base = DCTemplate<Quantizer, Similarity, SIMDLevel::NONE>;
|
|
132
|
+
using Base::Base;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
template <class Similarity>
|
|
136
|
+
struct DistanceComputerByte<Similarity, SIMDLevel::RISCV_RVV>
|
|
137
|
+
: DistanceComputerByte<Similarity, SIMDLevel::NONE> {
|
|
138
|
+
using Base = DistanceComputerByte<Similarity, SIMDLevel::NONE>;
|
|
139
|
+
using Base::Base;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/*************************************************************************
|
|
143
|
+
* Fast path — QT_4bit_uniform + L2
|
|
144
|
+
*
|
|
145
|
+
* 4-bit UNIFORM scaling: every component reconstructs as an affine function
|
|
146
|
+
* of the 4-bit code,
|
|
147
|
+
* recon(c) = vmin + vdiff * (c + 0.5) / 15 = final_scale * c + bias
|
|
148
|
+
* where final_scale = vdiff / 15. L2 distance between two reconstructions
|
|
149
|
+
* therefore reduces to final_scale^2 * (q_c - c_c)^2 over integer codes,
|
|
150
|
+
* so we can stay in the int domain and pay one float multiply at the end.
|
|
151
|
+
*
|
|
152
|
+
* The RVV path pre-nibbles the query into q_lo / q_hi (even / odd lanes)
|
|
153
|
+
* once at set_query time and then processes native-VL-sized chunks of code
|
|
154
|
+
* without ever decoding to float.
|
|
155
|
+
************************************************************************/
|
|
156
|
+
|
|
157
|
+
template <>
|
|
158
|
+
struct DCTemplate<
|
|
159
|
+
QuantizerTemplate<
|
|
160
|
+
Codec4bit<SIMDLevel::RISCV_RVV>,
|
|
161
|
+
QuantizerTemplateScaling::UNIFORM,
|
|
162
|
+
SIMDLevel::RISCV_RVV>,
|
|
163
|
+
SimilarityL2<SIMDLevel::RISCV_RVV>,
|
|
164
|
+
SIMDLevel::RISCV_RVV> : SQDistanceComputer {
|
|
165
|
+
using Sim = SimilarityL2<SIMDLevel::RISCV_RVV>;
|
|
166
|
+
|
|
167
|
+
size_t d;
|
|
168
|
+
float vmin;
|
|
169
|
+
float vdiff;
|
|
170
|
+
float final_scale_sq;
|
|
171
|
+
std::vector<uint8_t> q_lo;
|
|
172
|
+
std::vector<uint8_t> q_hi;
|
|
173
|
+
|
|
174
|
+
DCTemplate(size_t d_in, const std::vector<float>& trained)
|
|
175
|
+
: d(d_in),
|
|
176
|
+
vmin(trained[0]),
|
|
177
|
+
vdiff(trained[1]),
|
|
178
|
+
q_lo((d_in + 1) / 2, 0),
|
|
179
|
+
q_hi((d_in + 1) / 2, 0) {
|
|
180
|
+
const float final_scale = vdiff / 15.0f;
|
|
181
|
+
final_scale_sq = final_scale * final_scale;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
void set_query(const float* x) final {
|
|
185
|
+
this->q = x;
|
|
186
|
+
const float inv_scale = (vdiff == 0.0f) ? 0.0f : 15.0f / vdiff;
|
|
187
|
+
for (size_t i = 0; i < d; i++) {
|
|
188
|
+
float val = (x[i] - vmin) * inv_scale;
|
|
189
|
+
int code = static_cast<int>(val);
|
|
190
|
+
if (code < 0) {
|
|
191
|
+
code = 0;
|
|
192
|
+
}
|
|
193
|
+
if (code > 15) {
|
|
194
|
+
code = 15;
|
|
195
|
+
}
|
|
196
|
+
if (i % 2 == 0) {
|
|
197
|
+
q_lo[i / 2] = static_cast<uint8_t>(code);
|
|
198
|
+
} else {
|
|
199
|
+
q_hi[i / 2] = static_cast<uint8_t>(code);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// Squared integer-domain L2 between pre-nibbled q and packed 4-bit code.
|
|
205
|
+
/// Uses RVV's native VL; no fixed width assumptions. Returns the raw
|
|
206
|
+
/// integer sum — caller multiplies by final_scale_sq.
|
|
207
|
+
int64_t accumulate_int_l2(const uint8_t* code) const {
|
|
208
|
+
int64_t acc = 0;
|
|
209
|
+
size_t i = 0;
|
|
210
|
+
while (i < d) {
|
|
211
|
+
// Process up to vl codes per iteration. Each code byte packs two
|
|
212
|
+
// 4-bit codes, so we load (vl + 1) / 2 bytes; keep vl even to
|
|
213
|
+
// keep the nibble split aligned with the i % 2 split we used at
|
|
214
|
+
// set_query time.
|
|
215
|
+
size_t remaining = d - i;
|
|
216
|
+
size_t vl = __riscv_vsetvl_e8m1(remaining);
|
|
217
|
+
if (vl & 1) {
|
|
218
|
+
vl -= 1; // keep even; tail handled on next iter or scalar
|
|
219
|
+
}
|
|
220
|
+
if (vl == 0) {
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
const size_t byte_vl = vl / 2;
|
|
224
|
+
|
|
225
|
+
vuint8m1_t packed = __riscv_vle8_v_u8m1(code + i / 2, byte_vl);
|
|
226
|
+
vuint8m1_t ql = __riscv_vle8_v_u8m1(q_lo.data() + i / 2, byte_vl);
|
|
227
|
+
vuint8m1_t qh = __riscv_vle8_v_u8m1(q_hi.data() + i / 2, byte_vl);
|
|
228
|
+
|
|
229
|
+
vuint8m1_t lo_nib = __riscv_vand_vx_u8m1(packed, 0x0F, byte_vl);
|
|
230
|
+
vuint8m1_t hi_nib = __riscv_vsrl_vx_u8m1(packed, 4, byte_vl);
|
|
231
|
+
|
|
232
|
+
// |ql - lo| and |qh - hi| fit in u8 (values are in [0, 15]).
|
|
233
|
+
vuint8m1_t d_lo = __riscv_vsub_vv_u8m1(
|
|
234
|
+
__riscv_vmaxu_vv_u8m1(ql, lo_nib, byte_vl),
|
|
235
|
+
__riscv_vminu_vv_u8m1(ql, lo_nib, byte_vl),
|
|
236
|
+
byte_vl);
|
|
237
|
+
vuint8m1_t d_hi = __riscv_vsub_vv_u8m1(
|
|
238
|
+
__riscv_vmaxu_vv_u8m1(qh, hi_nib, byte_vl),
|
|
239
|
+
__riscv_vminu_vv_u8m1(qh, hi_nib, byte_vl),
|
|
240
|
+
byte_vl);
|
|
241
|
+
|
|
242
|
+
// Square via widening multiply (each byte squared fits in u16,
|
|
243
|
+
// since max byte value is 15 -> 225).
|
|
244
|
+
vuint16m2_t sq_lo = __riscv_vwmulu_vv_u16m2(d_lo, d_lo, byte_vl);
|
|
245
|
+
vuint16m2_t sq_hi = __riscv_vwmulu_vv_u16m2(d_hi, d_hi, byte_vl);
|
|
246
|
+
vuint16m2_t sq_sum = __riscv_vadd_vv_u16m2(sq_lo, sq_hi, byte_vl);
|
|
247
|
+
|
|
248
|
+
// Reduce to a scalar u32 (safe: byte_vl * 450 fits in u32 for
|
|
249
|
+
// any realistic d).
|
|
250
|
+
vuint32m1_t zero = __riscv_vmv_v_x_u32m1(0, 1);
|
|
251
|
+
vuint32m1_t red =
|
|
252
|
+
__riscv_vwredsumu_vs_u16m2_u32m1(sq_sum, zero, byte_vl);
|
|
253
|
+
acc += __riscv_vmv_x_s_u32m1_u32(red);
|
|
254
|
+
|
|
255
|
+
i += vl;
|
|
256
|
+
}
|
|
257
|
+
// Scalar tail: cover any leftover odd lane (at most one).
|
|
258
|
+
for (; i < d; i++) {
|
|
259
|
+
uint8_t c_code =
|
|
260
|
+
(i % 2 == 0) ? (code[i / 2] & 0x0F) : (code[i / 2] >> 4);
|
|
261
|
+
uint8_t q_code = (i % 2 == 0) ? q_lo[i / 2] : q_hi[i / 2];
|
|
262
|
+
int diff = int(q_code) - int(c_code);
|
|
263
|
+
acc += diff * diff;
|
|
264
|
+
}
|
|
265
|
+
return acc;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
float query_to_code(const uint8_t* code) const final {
|
|
269
|
+
return static_cast<float>(accumulate_int_l2(code)) * final_scale_sq;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
float symmetric_dis(idx_t i, idx_t j) override {
|
|
273
|
+
// Not on the critical path for most workloads; reconstruct both
|
|
274
|
+
// codes into nibbles scalar-style and compute squared distance.
|
|
275
|
+
const uint8_t* c1 = codes + i * code_size;
|
|
276
|
+
const uint8_t* c2 = codes + j * code_size;
|
|
277
|
+
int64_t acc = 0;
|
|
278
|
+
for (size_t k = 0; k < d; k++) {
|
|
279
|
+
uint8_t a = (k % 2 == 0) ? (c1[k / 2] & 0x0F) : (c1[k / 2] >> 4);
|
|
280
|
+
uint8_t b = (k % 2 == 0) ? (c2[k / 2] & 0x0F) : (c2[k / 2] >> 4);
|
|
281
|
+
int diff = int(a) - int(b);
|
|
282
|
+
acc += diff * diff;
|
|
283
|
+
}
|
|
284
|
+
return static_cast<float>(acc) * final_scale_sq;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
void query_to_codes_batch_4(
|
|
288
|
+
const uint8_t* code_0,
|
|
289
|
+
const uint8_t* code_1,
|
|
290
|
+
const uint8_t* code_2,
|
|
291
|
+
const uint8_t* code_3,
|
|
292
|
+
float& dis0,
|
|
293
|
+
float& dis1,
|
|
294
|
+
float& dis2,
|
|
295
|
+
float& dis3) const final {
|
|
296
|
+
// Simple 4x unroll of the single-code path; good enough as a first
|
|
297
|
+
// cut — gives ILP across the four independent accumulate loops.
|
|
298
|
+
dis0 = static_cast<float>(accumulate_int_l2(code_0)) * final_scale_sq;
|
|
299
|
+
dis1 = static_cast<float>(accumulate_int_l2(code_1)) * final_scale_sq;
|
|
300
|
+
dis2 = static_cast<float>(accumulate_int_l2(code_2)) * final_scale_sq;
|
|
301
|
+
dis3 = static_cast<float>(accumulate_int_l2(code_3)) * final_scale_sq;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
} // namespace scalar_quantizer
|
|
306
|
+
} // namespace faiss
|
|
307
|
+
|
|
308
|
+
#define THE_LEVEL_TO_DISPATCH SIMDLevel::RISCV_RVV
|
|
309
|
+
#include <faiss/impl/scalar_quantizer/sq-dispatch.h>
|
|
310
|
+
|
|
311
|
+
#endif // COMPILE_SIMD_RISCV_RVV
|