faiss 0.6.4 → 0.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/ext/faiss/extconf.rb +2 -1
- data/ext/faiss/index.cpp +42 -0
- 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 +29 -0
- data/vendor/faiss/faiss/IVFlib.cpp +147 -51
- data/vendor/faiss/faiss/IVFlib.h +21 -2
- 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/IndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +85 -4
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +19 -0
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -11
- data/vendor/faiss/faiss/IndexFastScan.cpp +34 -18
- data/vendor/faiss/faiss/IndexFastScan.h +6 -3
- data/vendor/faiss/faiss/IndexFlat.cpp +57 -1
- data/vendor/faiss/faiss/IndexFlat.h +31 -0
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +461 -13
- data/vendor/faiss/faiss/IndexHNSW.h +40 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +45 -0
- data/vendor/faiss/faiss/IndexIDMap.h +15 -3
- data/vendor/faiss/faiss/IndexIVF.cpp +349 -68
- data/vendor/faiss/faiss/IndexIVF.h +90 -2
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +51 -29
- data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
- data/vendor/faiss/faiss/IndexIVFFlat.h +22 -6
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +2 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +78 -58
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +7 -13
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +860 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
- data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -3
- data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
- data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
- data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +7 -4
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +12 -6
- 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 +10 -0
- data/vendor/faiss/faiss/IndexShards.h +5 -0
- data/vendor/faiss/faiss/SuperKMeans.cpp +59 -17
- data/vendor/faiss/faiss/SuperKMeans.h +9 -4
- data/vendor/faiss/faiss/VectorTransform.cpp +159 -11
- data/vendor/faiss/faiss/VectorTransform.h +24 -1
- data/vendor/faiss/faiss/clone_index.cpp +24 -0
- data/vendor/faiss/faiss/factory_tools.cpp +19 -1
- data/vendor/faiss/faiss/gpu/Dataset.h +164 -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 +3 -0
- data/vendor/faiss/faiss/gpu/GpuIndex.h +4 -13
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +32 -13
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +51 -22
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
- data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
- data/vendor/faiss/faiss/gpu/test/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/test/TestUtils.cpp +6 -5
- 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/GpuIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +12 -1
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +93 -1
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +5 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +39 -1
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +107 -1
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +11 -1
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +3 -1
- 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 +267 -136
- data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
- data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
- data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
- data/vendor/faiss/faiss/impl/HNSW.cpp +48 -12
- data/vendor/faiss/faiss/impl/HNSW.h +74 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
- data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -1
- data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
- data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
- data/vendor/faiss/faiss/impl/Panorama.h +3 -2
- data/vendor/faiss/faiss/impl/PiPNN.cpp +824 -0
- data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +54 -11
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +30 -1
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +91 -66
- data/vendor/faiss/faiss/impl/RaBitQuantizer.h +15 -0
- data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -0
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +1 -6
- 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 +1 -3
- 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 -0
- 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 +17 -129
- data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +73 -6
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +10 -85
- 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 +106 -1
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -6
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +105 -78
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
- 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 +318 -9
- data/vendor/faiss/faiss/impl/index_write.cpp +124 -5
- data/vendor/faiss/faiss/impl/io.cpp +1 -0
- data/vendor/faiss/faiss/impl/io_macros.h +2 -2
- data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
- data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
- data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
- data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +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 +91 -27
- data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +84 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
- 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 +19 -0
- 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/ResultHandler.cpp +68 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -6
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -5
- 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 +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +5 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +85 -37
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +262 -110
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +503 -122
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +10 -155
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +7 -701
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +66 -57
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +133 -127
- 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 +7 -0
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
- data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
- data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
- data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
- data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
- data/vendor/faiss/faiss/index_factory.cpp +106 -6
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
- data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
- data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +15 -10
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
- data/vendor/faiss/faiss/svs/IndexSVSVamana.h +3 -2
- data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
- data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
- data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
- 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 +466 -43
- data/vendor/faiss/faiss/utils/distances.h +179 -2
- data/vendor/faiss/faiss/utils/distances_dispatch.h +6 -4
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +0 -14
- 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/extra_distances.h +2 -1
- 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-inl.h +3 -0
- 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 +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +33 -5
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +15 -30
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +2 -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/partitioning.cpp +2 -2
- 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 +171 -22
- data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +81 -9
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +108 -9
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +111 -12
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +353 -71
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +106 -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/partitioning_simdlib256.h +16 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +185 -51
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +194 -66
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512_vpopcnt.cpp +51 -14
- 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_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 +0 -4
- data/vendor/faiss/faiss/utils/simd_levels.cpp +22 -6
- data/vendor/faiss/faiss/utils/simd_levels.h +74 -1
- data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
- 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 +84 -0
- data/vendor/faiss/faiss/utils/utils.h +24 -0
- metadata +52 -1
|
@@ -17,16 +17,20 @@
|
|
|
17
17
|
#include <cinttypes>
|
|
18
18
|
#include <cstdio>
|
|
19
19
|
#include <limits>
|
|
20
|
+
#include <optional>
|
|
20
21
|
|
|
22
|
+
#include <faiss/utils/random.h>
|
|
21
23
|
#include <faiss/utils/utils.h>
|
|
22
24
|
|
|
23
25
|
#include <faiss/IndexFlat.h>
|
|
26
|
+
#include <faiss/IndexHNSW.h>
|
|
24
27
|
#include <faiss/impl/AuxIndexStructures.h>
|
|
25
28
|
#include <faiss/impl/CodePacker.h>
|
|
26
29
|
#include <faiss/impl/FaissAssert.h>
|
|
27
30
|
#include <faiss/impl/IDSelector.h>
|
|
28
31
|
#include <faiss/impl/ResultHandler.h>
|
|
29
32
|
#include <faiss/impl/expanded_scanners.h>
|
|
33
|
+
#include <faiss/impl/platform_macros.h>
|
|
30
34
|
|
|
31
35
|
namespace faiss {
|
|
32
36
|
|
|
@@ -53,28 +57,89 @@ Level1Quantizer::~Level1Quantizer() {
|
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
59
|
|
|
56
|
-
|
|
60
|
+
namespace {
|
|
61
|
+
|
|
62
|
+
void add_hnsw_quantizer_links(
|
|
63
|
+
IndexHNSW& hnsw,
|
|
57
64
|
size_t n,
|
|
58
|
-
const
|
|
65
|
+
const uint8_t* x,
|
|
66
|
+
NumericType numeric_type,
|
|
67
|
+
const Index* codec,
|
|
68
|
+
bool verbose) {
|
|
69
|
+
const size_t samples = std::min(
|
|
70
|
+
n,
|
|
71
|
+
size_t(hnsw.level_0_link_samples_per_node) * size_t(hnsw.ntotal));
|
|
72
|
+
const HNSWLevel0LinkStats stats = codec
|
|
73
|
+
? hnsw.add_level_0_links_from_sample_encoded(samples, x, codec)
|
|
74
|
+
: hnsw.add_level_0_links_from_sample_ex(samples, x, numeric_type);
|
|
75
|
+
if (verbose) {
|
|
76
|
+
printf("HNSW quantizer level-0 links from %zd samples: %zd sinks found, "
|
|
77
|
+
"%zd remaining, %zd links added, %zd replaced\n",
|
|
78
|
+
samples,
|
|
79
|
+
stats.n_sinks_found,
|
|
80
|
+
stats.n_sinks_remaining,
|
|
81
|
+
stats.n_links_added,
|
|
82
|
+
stats.n_links_replaced);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Trains `q1` on rows encoded by `codec` or, if codec is null, stored as
|
|
87
|
+
* `numeric_type`. */
|
|
88
|
+
void train_q1_impl(
|
|
89
|
+
Level1Quantizer& q1,
|
|
90
|
+
size_t n,
|
|
91
|
+
const uint8_t* x,
|
|
92
|
+
NumericType numeric_type,
|
|
93
|
+
const Index* codec,
|
|
59
94
|
bool verbose,
|
|
60
95
|
MetricType metric_type) {
|
|
96
|
+
Index* quantizer = q1.quantizer;
|
|
97
|
+
const size_t nlist = q1.nlist;
|
|
98
|
+
const ClusteringParameters& cp = q1.cp;
|
|
99
|
+
Index* clustering_index = q1.clustering_index;
|
|
100
|
+
|
|
101
|
+
auto add_quantizer_links = [&]() {
|
|
102
|
+
auto* hnsw = dynamic_cast<IndexHNSW*>(quantizer);
|
|
103
|
+
if (hnsw && hnsw->level_0_link_samples_per_node > 0) {
|
|
104
|
+
add_hnsw_quantizer_links(*hnsw, n, x, numeric_type, codec, verbose);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// k-means on the training rows, with `assigner` as assignment index
|
|
109
|
+
auto train_clustering = [&](Clustering& clus, Index& assigner) {
|
|
110
|
+
if (codec) {
|
|
111
|
+
clus.train_encoded(n, x, codec, assigner);
|
|
112
|
+
} else {
|
|
113
|
+
clus.train_ex(n, x, numeric_type, assigner);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
61
117
|
FAISS_THROW_IF_NOT_MSG(quantizer, "IVF quantizer must not be null");
|
|
62
118
|
size_t d = quantizer->d;
|
|
119
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
120
|
+
!codec || static_cast<size_t>(codec->d) == d,
|
|
121
|
+
"Codec dimension %d not the same as quantizer dimension %d",
|
|
122
|
+
codec ? codec->d : 0,
|
|
123
|
+
int(d));
|
|
63
124
|
if (quantizer->is_trained &&
|
|
64
125
|
(static_cast<size_t>(quantizer->ntotal) == nlist)) {
|
|
65
126
|
if (verbose) {
|
|
66
127
|
printf("IVF quantizer does not need training.\n");
|
|
67
128
|
}
|
|
68
|
-
} else if (quantizer_trains_alone == 1) {
|
|
129
|
+
} else if (q1.quantizer_trains_alone == 1) {
|
|
130
|
+
FAISS_THROW_IF_MSG(
|
|
131
|
+
codec,
|
|
132
|
+
"encoded training is not supported when the IVF quantizer "
|
|
133
|
+
"trains alone");
|
|
69
134
|
if (verbose) {
|
|
70
135
|
printf("IVF quantizer trains alone...\n");
|
|
71
136
|
}
|
|
72
137
|
quantizer->verbose = verbose;
|
|
73
|
-
quantizer->
|
|
138
|
+
quantizer->train_ex(n, x, numeric_type);
|
|
74
139
|
FAISS_THROW_IF_NOT_MSG(
|
|
75
140
|
static_cast<size_t>(quantizer->ntotal) == nlist,
|
|
76
141
|
"nlist not consistent with quantizer size");
|
|
77
|
-
} else if (quantizer_trains_alone == 0) {
|
|
142
|
+
} else if (q1.quantizer_trains_alone == 0) {
|
|
78
143
|
if (verbose) {
|
|
79
144
|
printf("Training level-1 quantizer on %zd vectors in %zdD\n", n, d);
|
|
80
145
|
}
|
|
@@ -83,6 +148,16 @@ void Level1Quantizer::train_q1(
|
|
|
83
148
|
cp.use_super_kmeans && clustering_index,
|
|
84
149
|
"cp.use_super_kmeans is incompatible with a user-provided "
|
|
85
150
|
"clustering_index: SuperKMeans assigns with its own index");
|
|
151
|
+
FAISS_THROW_IF_MSG(
|
|
152
|
+
cp.use_super_kmeans && codec,
|
|
153
|
+
"SuperKMeans does not support encoded training data");
|
|
154
|
+
FAISS_THROW_IF_MSG(
|
|
155
|
+
cp.use_super_kmeans &&
|
|
156
|
+
!(metric_type == METRIC_L2 ||
|
|
157
|
+
(metric_type == METRIC_INNER_PRODUCT &&
|
|
158
|
+
cp.spherical)),
|
|
159
|
+
"SuperKMeans requires L2 or spherical inner-product "
|
|
160
|
+
"clustering");
|
|
86
161
|
|
|
87
162
|
quantizer->reset();
|
|
88
163
|
if (cp.use_super_kmeans) {
|
|
@@ -90,19 +165,20 @@ void Level1Quantizer::train_q1(
|
|
|
90
165
|
static_cast<ClusteringParameters&>(super_cp) = cp;
|
|
91
166
|
SuperKMeans clus(
|
|
92
167
|
static_cast<int>(d), static_cast<int>(nlist), super_cp);
|
|
93
|
-
clus.
|
|
168
|
+
clus.train_ex(n, x, numeric_type);
|
|
94
169
|
quantizer->add(nlist, clus.centroids.data());
|
|
95
170
|
} else {
|
|
96
171
|
Clustering clus(static_cast<int>(d), static_cast<int>(nlist), cp);
|
|
97
172
|
if (clustering_index) {
|
|
98
|
-
clus
|
|
173
|
+
train_clustering(clus, *clustering_index);
|
|
99
174
|
quantizer->add(nlist, clus.centroids.data());
|
|
100
175
|
} else {
|
|
101
|
-
clus
|
|
176
|
+
train_clustering(clus, *quantizer);
|
|
102
177
|
}
|
|
103
178
|
}
|
|
104
179
|
quantizer->is_trained = true;
|
|
105
|
-
|
|
180
|
+
add_quantizer_links();
|
|
181
|
+
} else if (q1.quantizer_trains_alone == 2) {
|
|
106
182
|
if (verbose) {
|
|
107
183
|
printf("Training L2 quantizer on %zd vectors in %zdD%s\n",
|
|
108
184
|
n,
|
|
@@ -118,9 +194,9 @@ void Level1Quantizer::train_q1(
|
|
|
118
194
|
Clustering clus(static_cast<int>(d), static_cast<int>(nlist), cp);
|
|
119
195
|
if (!clustering_index) {
|
|
120
196
|
IndexFlatL2 assigner(d);
|
|
121
|
-
clus
|
|
197
|
+
train_clustering(clus, assigner);
|
|
122
198
|
} else {
|
|
123
|
-
clus
|
|
199
|
+
train_clustering(clus, *clustering_index);
|
|
124
200
|
}
|
|
125
201
|
if (verbose) {
|
|
126
202
|
printf("Adding centroids to quantizer\n");
|
|
@@ -132,9 +208,46 @@ void Level1Quantizer::train_q1(
|
|
|
132
208
|
quantizer->train(nlist, clus.centroids.data());
|
|
133
209
|
}
|
|
134
210
|
quantizer->add(nlist, clus.centroids.data());
|
|
211
|
+
add_quantizer_links();
|
|
135
212
|
}
|
|
136
213
|
}
|
|
137
214
|
|
|
215
|
+
} // namespace
|
|
216
|
+
|
|
217
|
+
void Level1Quantizer::train_q1(
|
|
218
|
+
size_t n,
|
|
219
|
+
const float* x,
|
|
220
|
+
bool verbose,
|
|
221
|
+
MetricType metric_type) {
|
|
222
|
+
train_q1_ex(n, x, NumericType::Float32, verbose, metric_type);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
void Level1Quantizer::train_q1_ex(
|
|
226
|
+
size_t n,
|
|
227
|
+
const void* x,
|
|
228
|
+
NumericType numeric_type,
|
|
229
|
+
bool verbose,
|
|
230
|
+
MetricType metric_type) {
|
|
231
|
+
train_q1_impl(
|
|
232
|
+
*this,
|
|
233
|
+
n,
|
|
234
|
+
static_cast<const uint8_t*>(x),
|
|
235
|
+
numeric_type,
|
|
236
|
+
nullptr,
|
|
237
|
+
verbose,
|
|
238
|
+
metric_type);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
void Level1Quantizer::train_q1_encoded(
|
|
242
|
+
size_t n,
|
|
243
|
+
const uint8_t* x,
|
|
244
|
+
const Index* codec,
|
|
245
|
+
bool verbose,
|
|
246
|
+
MetricType metric_type) {
|
|
247
|
+
train_q1_impl(
|
|
248
|
+
*this, n, x, NumericType::Float32, codec, verbose, metric_type);
|
|
249
|
+
}
|
|
250
|
+
|
|
138
251
|
size_t Level1Quantizer::coarse_code_size() const {
|
|
139
252
|
size_t nl = nlist - 1;
|
|
140
253
|
size_t nbyte = 0;
|
|
@@ -384,7 +497,6 @@ void IndexIVF::search(
|
|
|
384
497
|
#pragma omp parallel for if (nt > 1)
|
|
385
498
|
for (idx_t slice = 0; slice < nt; slice++) {
|
|
386
499
|
try {
|
|
387
|
-
IndexIVFStats local_stats;
|
|
388
500
|
idx_t i0 = n * slice / nt;
|
|
389
501
|
idx_t i1 = n * (slice + 1) / nt;
|
|
390
502
|
if (i1 > i0) {
|
|
@@ -403,13 +515,17 @@ void IndexIVF::search(
|
|
|
403
515
|
omp_rethrow_if_exception(ex);
|
|
404
516
|
|
|
405
517
|
// collect stats
|
|
518
|
+
IndexIVFStats search_stats;
|
|
406
519
|
for (idx_t slice = 0; slice < nt; slice++) {
|
|
407
|
-
|
|
520
|
+
search_stats.add(stats[slice]);
|
|
408
521
|
}
|
|
522
|
+
indexIVF_stats.add(search_stats);
|
|
409
523
|
} else {
|
|
410
524
|
// handle parallelization at level below (or don't run in parallel at
|
|
411
525
|
// all)
|
|
412
|
-
|
|
526
|
+
IndexIVFStats search_stats;
|
|
527
|
+
sub_search_func(n, x, distances, labels, &search_stats);
|
|
528
|
+
indexIVF_stats.add(search_stats);
|
|
413
529
|
}
|
|
414
530
|
}
|
|
415
531
|
|
|
@@ -437,19 +553,21 @@ void IndexIVF::search_preassigned(
|
|
|
437
553
|
const bool ensure_topk_full = params ? params->ensure_topk_full : false;
|
|
438
554
|
|
|
439
555
|
IDSelector* sel = params ? params->sel : nullptr;
|
|
556
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
557
|
+
!(sel && store_pairs),
|
|
558
|
+
"selector and store_pairs cannot be combined");
|
|
559
|
+
|
|
440
560
|
const IDSelectorRange* selr = dynamic_cast<const IDSelectorRange*>(sel);
|
|
441
561
|
if (selr) {
|
|
442
|
-
|
|
562
|
+
// The sorted-range shortcut bounds a section of an array-backed list,
|
|
563
|
+
// so an iterable list has to keep the selector and filter per entry.
|
|
564
|
+
if (selr->assume_sorted && !invlists->use_iterator) {
|
|
443
565
|
sel = nullptr; // use special IDSelectorRange processing
|
|
444
566
|
} else {
|
|
445
567
|
selr = nullptr; // use generic processing
|
|
446
568
|
}
|
|
447
569
|
}
|
|
448
570
|
|
|
449
|
-
FAISS_THROW_IF_NOT_MSG(
|
|
450
|
-
!(sel && store_pairs),
|
|
451
|
-
"selector and store_pairs cannot be combined");
|
|
452
|
-
|
|
453
571
|
FAISS_THROW_IF_NOT_MSG(
|
|
454
572
|
!invlists->use_iterator ||
|
|
455
573
|
(cur_max_codes == 0 && store_pairs == false),
|
|
@@ -502,6 +620,10 @@ void IndexIVF::search_preassigned(
|
|
|
502
620
|
try {
|
|
503
621
|
std::unique_ptr<InvertedListScanner> scanner(
|
|
504
622
|
get_InvertedListScanner(store_pairs, sel, params));
|
|
623
|
+
// max_codes counts codes passing the selector, so test it first.
|
|
624
|
+
scanner->sel_after_distance = params &&
|
|
625
|
+
params->sel_after_distance &&
|
|
626
|
+
cur_max_codes == unlimited_list_size;
|
|
505
627
|
|
|
506
628
|
/*****************************************************
|
|
507
629
|
* Depending on parallel_mode, there are two possible ways
|
|
@@ -568,10 +690,9 @@ void IndexIVF::search_preassigned(
|
|
|
568
690
|
return (size_t)0;
|
|
569
691
|
}
|
|
570
692
|
|
|
571
|
-
scanner->set_list(key, coarse_dis_i);
|
|
572
|
-
|
|
573
693
|
nlistv++;
|
|
574
694
|
if (invlists->use_iterator) {
|
|
695
|
+
scanner->set_list(key, coarse_dis_i);
|
|
575
696
|
size_t list_size = 0;
|
|
576
697
|
std::unique_ptr<InvertedListsIterator> it(
|
|
577
698
|
invlists->get_iterator(key, inverted_list_context));
|
|
@@ -586,31 +707,30 @@ void IndexIVF::search_preassigned(
|
|
|
586
707
|
list_size = static_cast<size_t>(list_size_max);
|
|
587
708
|
}
|
|
588
709
|
|
|
589
|
-
InvertedLists::
|
|
590
|
-
const uint8_t* codes = scodes.get();
|
|
591
|
-
|
|
592
|
-
std::unique_ptr<InvertedLists::ScopedIds> sids;
|
|
710
|
+
std::optional<InvertedLists::ScopedIds> sids;
|
|
593
711
|
const idx_t* ids = nullptr;
|
|
594
|
-
|
|
595
712
|
if (!store_pairs) {
|
|
596
|
-
sids
|
|
597
|
-
invlists, key);
|
|
713
|
+
sids.emplace(invlists, key);
|
|
598
714
|
ids = sids->get();
|
|
599
715
|
}
|
|
600
716
|
|
|
717
|
+
size_t jmin = 0;
|
|
601
718
|
if (selr) { // IDSelectorRange
|
|
602
719
|
// restrict search to a section of the inverted list
|
|
603
|
-
size_t
|
|
720
|
+
size_t jmax;
|
|
604
721
|
selr->find_sorted_ids_bounds(
|
|
605
722
|
list_size, ids, &jmin, &jmax);
|
|
606
723
|
list_size = jmax - jmin;
|
|
607
724
|
if (list_size == 0) {
|
|
608
725
|
return (size_t)0;
|
|
609
726
|
}
|
|
610
|
-
codes += jmin * code_size;
|
|
611
727
|
ids += jmin;
|
|
612
728
|
}
|
|
613
729
|
|
|
730
|
+
scanner->set_list(key, coarse_dis_i);
|
|
731
|
+
InvertedLists::ScopedCodes scodes(invlists, key);
|
|
732
|
+
const uint8_t* codes = scodes.get() + jmin * code_size;
|
|
733
|
+
|
|
614
734
|
size_t old_scan_cnt = 0;
|
|
615
735
|
size_t old_heap_updates = 0;
|
|
616
736
|
if (is_similarity_metric(metric_type)) {
|
|
@@ -692,7 +812,7 @@ void IndexIVF::search_preassigned(
|
|
|
692
812
|
scanner->set_query(x + i * d);
|
|
693
813
|
init_result(local_dis.data(), local_idx.data());
|
|
694
814
|
|
|
695
|
-
#pragma omp for schedule(
|
|
815
|
+
#pragma omp for schedule(FAISS_OMP_DYNAMIC)
|
|
696
816
|
for (idx_t ik = 0; ik < cur_nprobe; ik++) {
|
|
697
817
|
try {
|
|
698
818
|
ndis += scan_one_list(
|
|
@@ -734,7 +854,7 @@ void IndexIVF::search_preassigned(
|
|
|
734
854
|
init_result(distances + i * k, labels + i * k);
|
|
735
855
|
}
|
|
736
856
|
|
|
737
|
-
#pragma omp for schedule(
|
|
857
|
+
#pragma omp for schedule(FAISS_OMP_DYNAMIC)
|
|
738
858
|
for (int64_t ij = 0; ij < n * cur_nprobe; ij++) {
|
|
739
859
|
try {
|
|
740
860
|
size_t i = ij / cur_nprobe;
|
|
@@ -773,13 +893,7 @@ void IndexIVF::search_preassigned(
|
|
|
773
893
|
|
|
774
894
|
omp_rethrow_if_exception(ex);
|
|
775
895
|
|
|
776
|
-
|
|
777
|
-
ivf_stats = &indexIVF_stats;
|
|
778
|
-
}
|
|
779
|
-
ivf_stats->nq += n;
|
|
780
|
-
ivf_stats->nlist += nlistv;
|
|
781
|
-
ivf_stats->ndis += ndis;
|
|
782
|
-
ivf_stats->nheap_updates += nheap;
|
|
896
|
+
add_search_stats(n, nlistv, ndis, nheap, ivf_stats);
|
|
783
897
|
}
|
|
784
898
|
|
|
785
899
|
void IndexIVF::range_search(
|
|
@@ -805,23 +919,15 @@ void IndexIVF::range_search(
|
|
|
805
919
|
double t0 = getmillisecs();
|
|
806
920
|
quantizer->search(
|
|
807
921
|
nx, x, cur_nprobe, coarse_dis.get(), keys.get(), quantizer_params);
|
|
808
|
-
|
|
922
|
+
double quantization_time = getmillisecs() - t0;
|
|
809
923
|
|
|
810
924
|
t0 = getmillisecs();
|
|
811
925
|
invlists->prefetch_lists(keys.get(), static_cast<int>(nx * cur_nprobe));
|
|
812
926
|
|
|
813
927
|
range_search_preassigned(
|
|
814
|
-
nx,
|
|
815
|
-
x,
|
|
816
|
-
radius,
|
|
817
|
-
keys.get(),
|
|
818
|
-
coarse_dis.get(),
|
|
819
|
-
result,
|
|
820
|
-
false,
|
|
821
|
-
params,
|
|
822
|
-
&indexIVF_stats);
|
|
928
|
+
nx, x, radius, keys.get(), coarse_dis.get(), result, false, params);
|
|
823
929
|
|
|
824
|
-
|
|
930
|
+
add_search_times(quantization_time, getmillisecs() - t0);
|
|
825
931
|
}
|
|
826
932
|
|
|
827
933
|
void IndexIVF::range_search_preassigned(
|
|
@@ -957,7 +1063,7 @@ void IndexIVF::range_search_preassigned(
|
|
|
957
1063
|
|
|
958
1064
|
RangeQueryResult& qres = pres.new_result(i);
|
|
959
1065
|
|
|
960
|
-
#pragma omp for schedule(
|
|
1066
|
+
#pragma omp for schedule(FAISS_OMP_DYNAMIC)
|
|
961
1067
|
for (int64_t ik = 0; ik < cur_nprobe; ik++) {
|
|
962
1068
|
try {
|
|
963
1069
|
scan_list_func(i, ik, qres);
|
|
@@ -969,7 +1075,7 @@ void IndexIVF::range_search_preassigned(
|
|
|
969
1075
|
} else if (parallel_mode == 2) {
|
|
970
1076
|
RangeQueryResult* qres = nullptr;
|
|
971
1077
|
|
|
972
|
-
#pragma omp for schedule(
|
|
1078
|
+
#pragma omp for schedule(FAISS_OMP_DYNAMIC)
|
|
973
1079
|
for (idx_t iik = 0; iik < nx * (idx_t)cur_nprobe; iik++) {
|
|
974
1080
|
try {
|
|
975
1081
|
idx_t i = iik / (idx_t)cur_nprobe;
|
|
@@ -1002,12 +1108,7 @@ void IndexIVF::range_search_preassigned(
|
|
|
1002
1108
|
|
|
1003
1109
|
omp_rethrow_if_exception(ex);
|
|
1004
1110
|
|
|
1005
|
-
|
|
1006
|
-
stats = &indexIVF_stats;
|
|
1007
|
-
}
|
|
1008
|
-
stats->nq += nx;
|
|
1009
|
-
stats->nlist += nlistv;
|
|
1010
|
-
stats->ndis += ndis;
|
|
1111
|
+
add_search_stats(nx, nlistv, ndis, 0, stats);
|
|
1011
1112
|
}
|
|
1012
1113
|
|
|
1013
1114
|
void IndexIVF::search1(
|
|
@@ -1030,7 +1131,7 @@ void IndexIVF::search1(
|
|
|
1030
1131
|
double t0 = getmillisecs();
|
|
1031
1132
|
quantizer->search(
|
|
1032
1133
|
nx, x, cur_nprobe, coarse_dis.get(), keys.get(), quantizer_params);
|
|
1033
|
-
|
|
1134
|
+
double quantization_time = getmillisecs() - t0;
|
|
1034
1135
|
|
|
1035
1136
|
t0 = getmillisecs();
|
|
1036
1137
|
invlists->prefetch_lists(keys.get(), static_cast<int>(nx * cur_nprobe));
|
|
@@ -1058,7 +1159,7 @@ void IndexIVF::search1(
|
|
|
1058
1159
|
scanner->scan_codes(list_size, scodes.get(), ids.get(), handler);
|
|
1059
1160
|
}
|
|
1060
1161
|
|
|
1061
|
-
|
|
1162
|
+
add_search_times(quantization_time, getmillisecs() - t0);
|
|
1062
1163
|
}
|
|
1063
1164
|
|
|
1064
1165
|
InvertedListScanner* IndexIVF::get_InvertedListScanner(
|
|
@@ -1160,7 +1261,13 @@ void IndexIVF::search_and_reconstruct(
|
|
|
1160
1261
|
std::unique_ptr<idx_t[]> idx(new idx_t[n * cur_nprobe]);
|
|
1161
1262
|
std::unique_ptr<float[]> coarse_dis(new float[n * cur_nprobe]);
|
|
1162
1263
|
|
|
1163
|
-
quantizer->search(
|
|
1264
|
+
quantizer->search(
|
|
1265
|
+
n,
|
|
1266
|
+
x,
|
|
1267
|
+
cur_nprobe,
|
|
1268
|
+
coarse_dis.get(),
|
|
1269
|
+
idx.get(),
|
|
1270
|
+
params ? params->quantizer_params : nullptr);
|
|
1164
1271
|
|
|
1165
1272
|
invlists->prefetch_lists(idx.get(), static_cast<int>(n * cur_nprobe));
|
|
1166
1273
|
|
|
@@ -1209,6 +1316,9 @@ void IndexIVF::search_and_return_codes(
|
|
|
1209
1316
|
params = dynamic_cast<const IVFSearchParameters*>(params_in);
|
|
1210
1317
|
FAISS_THROW_IF_NOT_MSG(params, "IndexIVF params have incorrect type");
|
|
1211
1318
|
}
|
|
1319
|
+
FAISS_THROW_IF_MSG(
|
|
1320
|
+
invlists->code_size == InvertedLists::INVALID_CODE_SIZE,
|
|
1321
|
+
"search_and_return_codes not supported for these inverted lists");
|
|
1212
1322
|
const size_t cur_nprobe =
|
|
1213
1323
|
std::min(nlist, params ? params->nprobe : this->nprobe);
|
|
1214
1324
|
FAISS_THROW_IF_NOT(cur_nprobe > 0);
|
|
@@ -1216,7 +1326,13 @@ void IndexIVF::search_and_return_codes(
|
|
|
1216
1326
|
std::unique_ptr<idx_t[]> idx(new idx_t[n * cur_nprobe]);
|
|
1217
1327
|
std::unique_ptr<float[]> coarse_dis(new float[n * cur_nprobe]);
|
|
1218
1328
|
|
|
1219
|
-
quantizer->search(
|
|
1329
|
+
quantizer->search(
|
|
1330
|
+
n,
|
|
1331
|
+
x,
|
|
1332
|
+
cur_nprobe,
|
|
1333
|
+
coarse_dis.get(),
|
|
1334
|
+
idx.get(),
|
|
1335
|
+
params ? params->quantizer_params : nullptr);
|
|
1220
1336
|
|
|
1221
1337
|
invlists->prefetch_lists(idx.get(), static_cast<int>(n * cur_nprobe));
|
|
1222
1338
|
|
|
@@ -1308,6 +1424,125 @@ void IndexIVF::update_vectors(int n, const idx_t* new_ids, const float* x) {
|
|
|
1308
1424
|
invlists, n, new_ids, assign.data(), flat_codes.data());
|
|
1309
1425
|
}
|
|
1310
1426
|
|
|
1427
|
+
namespace {
|
|
1428
|
+
|
|
1429
|
+
/** Trains the encoder of `ivf` on training rows that are not fp32.
|
|
1430
|
+
*
|
|
1431
|
+
* Only the train_encoder_num_vectors() rows sampled for the encoder are
|
|
1432
|
+
* widened to fp32. `decode_rows(n, rows, out)` widens n consecutive rows of
|
|
1433
|
+
* `row_size` bytes.
|
|
1434
|
+
*/
|
|
1435
|
+
template <class DecodeRows>
|
|
1436
|
+
void train_encoder_on_encoded_rows(
|
|
1437
|
+
IndexIVF& ivf,
|
|
1438
|
+
idx_t n,
|
|
1439
|
+
const uint8_t* x,
|
|
1440
|
+
size_t row_size,
|
|
1441
|
+
const DecodeRows& decode_rows) {
|
|
1442
|
+
const size_t d = ivf.d;
|
|
1443
|
+
idx_t max_nt = ivf.train_encoder_num_vectors();
|
|
1444
|
+
if (max_nt <= 0 || n <= max_nt) {
|
|
1445
|
+
max_nt = n;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
std::vector<float> decoded(static_cast<size_t>(max_nt) * d);
|
|
1449
|
+
if (max_nt == n) {
|
|
1450
|
+
decode_rows(n, x, decoded.data());
|
|
1451
|
+
} else {
|
|
1452
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
1453
|
+
n <= static_cast<idx_t>(std::numeric_limits<int>::max()),
|
|
1454
|
+
"Dataset too large (%" PRId64 ") for standard subsampling",
|
|
1455
|
+
n);
|
|
1456
|
+
if (ivf.verbose) {
|
|
1457
|
+
printf(" Input training set too big (max size is %" PRId64
|
|
1458
|
+
"), sampling %" PRId64 " / %" PRId64 " vectors\n",
|
|
1459
|
+
max_nt,
|
|
1460
|
+
max_nt,
|
|
1461
|
+
n);
|
|
1462
|
+
}
|
|
1463
|
+
std::vector<int> subset(static_cast<size_t>(n));
|
|
1464
|
+
rand_perm(subset.data(), n, 1234);
|
|
1465
|
+
for (idx_t i = 0; i < max_nt; ++i) {
|
|
1466
|
+
decode_rows(
|
|
1467
|
+
1,
|
|
1468
|
+
x + static_cast<size_t>(subset[i]) * row_size,
|
|
1469
|
+
decoded.data() + static_cast<size_t>(i) * d);
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
n = max_nt;
|
|
1473
|
+
|
|
1474
|
+
if (ivf.by_residual) {
|
|
1475
|
+
FAISS_THROW_IF_NOT_MSG(ivf.quantizer, "IVF quantizer must not be null");
|
|
1476
|
+
std::vector<idx_t> assign(n);
|
|
1477
|
+
ivf.quantizer->assign(n, decoded.data(), assign.data());
|
|
1478
|
+
|
|
1479
|
+
std::vector<float> residuals(static_cast<size_t>(n) * d);
|
|
1480
|
+
ivf.quantizer->compute_residual_n(
|
|
1481
|
+
n, decoded.data(), residuals.data(), assign.data());
|
|
1482
|
+
|
|
1483
|
+
ivf.train_encoder(n, residuals.data(), assign.data());
|
|
1484
|
+
} else {
|
|
1485
|
+
ivf.train_encoder(n, decoded.data(), nullptr);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
} // namespace
|
|
1490
|
+
|
|
1491
|
+
void IndexIVF::train_ex(idx_t n, const void* x, NumericType numeric_type) {
|
|
1492
|
+
if (numeric_type != NumericType::Float16) {
|
|
1493
|
+
Index::train_ex(n, x, numeric_type);
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
FAISS_THROW_IF_NOT_MSG(x, "training data must not be null");
|
|
1497
|
+
if (verbose) {
|
|
1498
|
+
printf("Training level-1 quantizer on fp16 data\n");
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
train_q1_ex(n, x, numeric_type, verbose, metric_type);
|
|
1502
|
+
|
|
1503
|
+
if (verbose) {
|
|
1504
|
+
printf("Training IVF residual\n");
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
const size_t dim = d;
|
|
1508
|
+
train_encoder_on_encoded_rows(
|
|
1509
|
+
*this,
|
|
1510
|
+
n,
|
|
1511
|
+
static_cast<const uint8_t*>(x),
|
|
1512
|
+
sizeof(uint16_t) * dim,
|
|
1513
|
+
[dim](idx_t ni, const uint8_t* rows, float* out) {
|
|
1514
|
+
fp16_to_fp32(
|
|
1515
|
+
ni * dim, reinterpret_cast<const uint16_t*>(rows), out);
|
|
1516
|
+
});
|
|
1517
|
+
|
|
1518
|
+
is_trained = true;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
void IndexIVF::train_encoded(idx_t n, const uint8_t* x, const Index* codec) {
|
|
1522
|
+
FAISS_THROW_IF_NOT_MSG(codec, "encoded training requires a codec");
|
|
1523
|
+
FAISS_THROW_IF_NOT_MSG(x, "encoded training data must not be null");
|
|
1524
|
+
if (verbose) {
|
|
1525
|
+
printf("Training level-1 quantizer\n");
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
train_q1_encoded(n, x, codec, verbose, metric_type);
|
|
1529
|
+
|
|
1530
|
+
if (verbose) {
|
|
1531
|
+
printf("Training IVF residual\n");
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
train_encoder_on_encoded_rows(
|
|
1535
|
+
*this,
|
|
1536
|
+
n,
|
|
1537
|
+
x,
|
|
1538
|
+
codec->sa_code_size(),
|
|
1539
|
+
[codec](idx_t ni, const uint8_t* rows, float* out) {
|
|
1540
|
+
codec->sa_decode(ni, rows, out);
|
|
1541
|
+
});
|
|
1542
|
+
|
|
1543
|
+
is_trained = true;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1311
1546
|
void IndexIVF::train(idx_t n, const float* x) {
|
|
1312
1547
|
if (verbose) {
|
|
1313
1548
|
printf("Training level-1 quantizer\n");
|
|
@@ -1438,16 +1673,44 @@ void IndexIVFStats::reset() {
|
|
|
1438
1673
|
}
|
|
1439
1674
|
|
|
1440
1675
|
void IndexIVFStats::add(const IndexIVFStats& other) {
|
|
1441
|
-
nq
|
|
1442
|
-
nlist
|
|
1443
|
-
ndis
|
|
1444
|
-
nheap_updates
|
|
1445
|
-
|
|
1446
|
-
|
|
1676
|
+
detail::atomic_fetch_add_relaxed(nq, other.nq);
|
|
1677
|
+
detail::atomic_fetch_add_relaxed(nlist, other.nlist);
|
|
1678
|
+
detail::atomic_fetch_add_relaxed(ndis, other.ndis);
|
|
1679
|
+
detail::atomic_fetch_add_relaxed(nheap_updates, other.nheap_updates);
|
|
1680
|
+
detail::atomic_fetch_add_relaxed(
|
|
1681
|
+
quantization_time, other.quantization_time);
|
|
1682
|
+
detail::atomic_fetch_add_relaxed(search_time, other.search_time);
|
|
1447
1683
|
}
|
|
1448
1684
|
|
|
1449
1685
|
IndexIVFStats indexIVF_stats;
|
|
1450
1686
|
|
|
1687
|
+
void add_search_stats(
|
|
1688
|
+
size_t nq,
|
|
1689
|
+
size_t nlist,
|
|
1690
|
+
size_t ndis,
|
|
1691
|
+
size_t nheap_updates,
|
|
1692
|
+
IndexIVFStats* stats) {
|
|
1693
|
+
if (stats == nullptr) {
|
|
1694
|
+
stats = &indexIVF_stats;
|
|
1695
|
+
}
|
|
1696
|
+
detail::atomic_fetch_add_relaxed(stats->nq, nq);
|
|
1697
|
+
detail::atomic_fetch_add_relaxed(stats->nlist, nlist);
|
|
1698
|
+
detail::atomic_fetch_add_relaxed(stats->ndis, ndis);
|
|
1699
|
+
detail::atomic_fetch_add_relaxed(stats->nheap_updates, nheap_updates);
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
void add_search_times(
|
|
1703
|
+
double quantization_time,
|
|
1704
|
+
double search_time,
|
|
1705
|
+
IndexIVFStats* stats) {
|
|
1706
|
+
if (stats == nullptr) {
|
|
1707
|
+
stats = &indexIVF_stats;
|
|
1708
|
+
}
|
|
1709
|
+
detail::atomic_fetch_add_relaxed(
|
|
1710
|
+
stats->quantization_time, quantization_time);
|
|
1711
|
+
detail::atomic_fetch_add_relaxed(stats->search_time, search_time);
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1451
1714
|
/*************************************************************************
|
|
1452
1715
|
* InvertedListScanner
|
|
1453
1716
|
*************************************************************************/
|
|
@@ -1462,6 +1725,15 @@ size_t InvertedListScanner::scan_codes(
|
|
|
1462
1725
|
return run_scan_codes(*this, list_size, codes, ids, handler);
|
|
1463
1726
|
}
|
|
1464
1727
|
|
|
1728
|
+
void InvertedListScanner::distances_to_codes(
|
|
1729
|
+
size_t n,
|
|
1730
|
+
const uint8_t* codes,
|
|
1731
|
+
float* distances) const {
|
|
1732
|
+
for (size_t j = 0; j < n; j++) {
|
|
1733
|
+
distances[j] = distance_to_code(codes + j * code_size);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1465
1737
|
void InvertedListScanner::set_list(idx_t list_no_in, float /* coarse_dis */) {
|
|
1466
1738
|
this->list_no = list_no_in;
|
|
1467
1739
|
}
|
|
@@ -1498,6 +1770,9 @@ size_t InvertedListScanner::iterate_codes(
|
|
|
1498
1770
|
if (!keep_max) {
|
|
1499
1771
|
for (; it->is_available(); it->next()) {
|
|
1500
1772
|
auto id_and_codes = it->get_id_and_codes();
|
|
1773
|
+
if (sel && !sel->is_member(id_and_codes.first)) {
|
|
1774
|
+
continue;
|
|
1775
|
+
}
|
|
1501
1776
|
float dis = distance_to_code(id_and_codes.second);
|
|
1502
1777
|
if (has_cb) {
|
|
1503
1778
|
it->on_distance_computed(id_and_codes.first, dis);
|
|
@@ -1514,6 +1789,9 @@ size_t InvertedListScanner::iterate_codes(
|
|
|
1514
1789
|
} else {
|
|
1515
1790
|
for (; it->is_available(); it->next()) {
|
|
1516
1791
|
auto id_and_codes = it->get_id_and_codes();
|
|
1792
|
+
if (sel && !sel->is_member(id_and_codes.first)) {
|
|
1793
|
+
continue;
|
|
1794
|
+
}
|
|
1517
1795
|
float dis = distance_to_code(id_and_codes.second);
|
|
1518
1796
|
if (has_cb) {
|
|
1519
1797
|
it->on_distance_computed(id_and_codes.first, dis);
|
|
@@ -1561,6 +1839,9 @@ void InvertedListScanner::iterate_codes_range(
|
|
|
1561
1839
|
list_size = 0;
|
|
1562
1840
|
for (; it->is_available(); it->next()) {
|
|
1563
1841
|
auto id_and_codes = it->get_id_and_codes();
|
|
1842
|
+
if (sel && !sel->is_member(id_and_codes.first)) {
|
|
1843
|
+
continue;
|
|
1844
|
+
}
|
|
1564
1845
|
float dis = distance_to_code(id_and_codes.second);
|
|
1565
1846
|
bool keep = !keep_max
|
|
1566
1847
|
? dis < radius
|