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
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <faiss/impl/optimal_kmeans/OptimalKMeans.h>
|
|
11
|
+
|
|
12
|
+
#include <cstddef>
|
|
13
|
+
#include <cstdint>
|
|
14
|
+
#include <memory>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
/** Compressed intermediate tables of the optimal k-means
|
|
19
|
+
* (contrib/optimal_kmeans/07_compressed.md).
|
|
20
|
+
*
|
|
21
|
+
* A table of 2^n entries is cut into blocks of 2^L entries. In each block, a
|
|
22
|
+
* fraction of the entries is stored exactly (cost and split); every other
|
|
23
|
+
* entry is replaced by a lower bound
|
|
24
|
+
*
|
|
25
|
+
* off(z) + gmin[group(z)] + code[z] * step[group(z)]
|
|
26
|
+
*
|
|
27
|
+
* rounded down to float32, where off(z) is an offset model (one value per
|
|
28
|
+
* popcount of z, plus a sum of point offsets o[j] over the bits j of z),
|
|
29
|
+
* gmin and step are per group of entries and code has q bits. An entry of a
|
|
30
|
+
* combination is exact when its winning split uses two exact entries; the
|
|
31
|
+
* others are recomputed when needed (OptimalKMeansExactEngine).
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
namespace faiss {
|
|
35
|
+
|
|
36
|
+
struct IOReader;
|
|
37
|
+
struct IOWriter;
|
|
38
|
+
|
|
39
|
+
/** Fixed layout of the blocks of 2^L entries: groups and code size.
|
|
40
|
+
*
|
|
41
|
+
* Group of an entry z: z is split into its sub-block t = z >> sub_bits and
|
|
42
|
+
* w = z & (2^sub_bits - 1). Within a sub-block, the entries of equal popcount
|
|
43
|
+
* are taken in increasing order and cut into groups of g entries. */
|
|
44
|
+
struct OptimalKMeansCodec {
|
|
45
|
+
int L = 0; ///< log2 of the block size
|
|
46
|
+
int g = 64; ///< entries per group
|
|
47
|
+
int q = 4; ///< bits per code: 0 (no code), 1, 2, 4 or 8
|
|
48
|
+
int sub_bits = 16; ///< log2 of the sub-blocks (min(sub_bits, L) is used)
|
|
49
|
+
|
|
50
|
+
// derived
|
|
51
|
+
int sb = 0; ///< min(sub_bits, L)
|
|
52
|
+
int L1 = 0; ///< low half of the offset model (L / 2)
|
|
53
|
+
size_t groups_per_sub = 0; ///< groups of a sub-block
|
|
54
|
+
size_t ngroups = 0; ///< groups of a block
|
|
55
|
+
std::vector<uint32_t> class_off; ///< first group of each popcount of w
|
|
56
|
+
std::vector<uint32_t> binom; ///< binomial coefficients C(i, j), 33 x 33
|
|
57
|
+
|
|
58
|
+
OptimalKMeansCodec() {}
|
|
59
|
+
OptimalKMeansCodec(int L, int g = 64, int q = 4, int sub_bits = 16);
|
|
60
|
+
|
|
61
|
+
/// group of entry z
|
|
62
|
+
uint32_t group(uint32_t z) const;
|
|
63
|
+
/// bytes of the codes of a block
|
|
64
|
+
size_t code_bytes() const;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** One compressed block. The exact entries are listed by offset in an
|
|
68
|
+
* Elias-Fano code (low parts of ef_lbits bits packed in ef_lo, high parts in
|
|
69
|
+
* unary in the bit array ef_hi). */
|
|
70
|
+
struct OptimalKMeansCompressedBlock {
|
|
71
|
+
std::vector<double> C; ///< offset of each popcount (L + 1)
|
|
72
|
+
std::vector<float> gmin; ///< lower end of each group
|
|
73
|
+
std::vector<float> step; ///< code step of each group (q > 0)
|
|
74
|
+
std::vector<uint8_t> codes; ///< q bits per entry
|
|
75
|
+
// exact entries
|
|
76
|
+
uint64_t k = 0; ///< number of exact entries
|
|
77
|
+
int ef_lbits = 0; ///< low bits of the Elias-Fano code
|
|
78
|
+
std::vector<uint32_t> ef_lo; ///< packed low parts (+ 1 padding word)
|
|
79
|
+
std::vector<uint64_t> ef_hi; ///< unary high parts (+ 1 padding word)
|
|
80
|
+
std::vector<uint32_t> ef_samples; ///< start of every 64th bucket in ef_hi
|
|
81
|
+
std::vector<float> e_cost; ///< costs of the exact entries
|
|
82
|
+
std::vector<uint64_t> e_split; ///< splits of the exact entries
|
|
83
|
+
/// all the costs of the block are >= +0: the bounds are clamped to >= 0
|
|
84
|
+
bool clamp = false;
|
|
85
|
+
uint64_t ncert = 0; ///< certified entries (the exact ones are among them)
|
|
86
|
+
|
|
87
|
+
/// index of entry z among the exact entries, or -1
|
|
88
|
+
int64_t find(uint32_t z) const;
|
|
89
|
+
/// offsets of the exact entries (decoded Elias-Fano list)
|
|
90
|
+
std::vector<uint32_t> exact_offsets() const;
|
|
91
|
+
/// memory used by the block
|
|
92
|
+
size_t nbytes() const;
|
|
93
|
+
|
|
94
|
+
/// serialization (without e_split)
|
|
95
|
+
void write(IOWriter* f) const;
|
|
96
|
+
void read(IOReader* f);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
struct OptimalKMeansDiskCacheState;
|
|
100
|
+
|
|
101
|
+
/** Memory budget of the blocks of levels stored on disk (09_disk_cache.md),
|
|
102
|
+
* shared by the levels of a run: the blocks loaded from their files (or
|
|
103
|
+
* just encoded) stay in memory until the budget is exceeded, then the
|
|
104
|
+
* oldest ones are released. */
|
|
105
|
+
struct OptimalKMeansDiskCache {
|
|
106
|
+
explicit OptimalKMeansDiskCache(size_t max_bytes = size_t(64) << 30);
|
|
107
|
+
|
|
108
|
+
size_t max_bytes;
|
|
109
|
+
|
|
110
|
+
size_t bytes() const; ///< blocks in memory
|
|
111
|
+
size_t peak_bytes() const; ///< maximum of bytes()
|
|
112
|
+
uint64_t loads() const; ///< blocks read from their files
|
|
113
|
+
|
|
114
|
+
std::shared_ptr<OptimalKMeansDiskCacheState> state;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
struct OptimalKMeansLevelStorage;
|
|
118
|
+
|
|
119
|
+
/** A compressed table: the combination a + b -> c, 2^n entries in blocks
|
|
120
|
+
* of 2^codec.L. The blocks are filled by optimal_kmeans_encode_block
|
|
121
|
+
* (concurrent calls for different blocks are allowed). */
|
|
122
|
+
struct OptimalKMeansCompressedLevel {
|
|
123
|
+
int c = 0, a = 0, b = 0;
|
|
124
|
+
int n = 0, L = 0;
|
|
125
|
+
OptimalKMeansCodec codec;
|
|
126
|
+
std::vector<double> o; ///< point offsets of the offset model (L)
|
|
127
|
+
/// the 2^(n - L) blocks, if the level is in RAM
|
|
128
|
+
std::vector<OptimalKMeansCompressedBlock> blocks;
|
|
129
|
+
|
|
130
|
+
OptimalKMeansCompressedLevel() {}
|
|
131
|
+
OptimalKMeansCompressedLevel(
|
|
132
|
+
int c,
|
|
133
|
+
int a,
|
|
134
|
+
int b,
|
|
135
|
+
int n,
|
|
136
|
+
const OptimalKMeansCodec& codec);
|
|
137
|
+
|
|
138
|
+
/// point offsets of the level, before the first block is encoded
|
|
139
|
+
void set_offsets(const double* o);
|
|
140
|
+
|
|
141
|
+
/** Store the blocks in files of the directory dir (it must exist) instead
|
|
142
|
+
* of RAM: block s in dir/b<s>.blk, the splits of its exact entries in
|
|
143
|
+
* dir/b<s>.spl. The blocks are loaded on demand, within the budget of
|
|
144
|
+
* the cache. Before the first block is stored. */
|
|
145
|
+
void set_disk_cache(const char* dir, OptimalKMeansDiskCache* cache);
|
|
146
|
+
std::string dir; ///< empty: the level is in RAM
|
|
147
|
+
|
|
148
|
+
/// store block s (called by optimal_kmeans_encode_block; blk is moved)
|
|
149
|
+
void store_block(uint64_t s, OptimalKMeansCompressedBlock& blk);
|
|
150
|
+
|
|
151
|
+
#ifndef SWIG
|
|
152
|
+
/** Block s (read from its file if needed). The block stays valid while
|
|
153
|
+
* the pointer is held, even if the cache releases it. */
|
|
154
|
+
std::shared_ptr<const OptimalKMeansCompressedBlock> get_block(
|
|
155
|
+
uint64_t s) const;
|
|
156
|
+
#endif
|
|
157
|
+
/** Release block s from memory (disk mode, no-op in RAM mode): the
|
|
158
|
+
* combinations do not read it any more. It is read again from its file
|
|
159
|
+
* if needed (final step). */
|
|
160
|
+
void drop_block(uint64_t s);
|
|
161
|
+
|
|
162
|
+
/// split of the exact entry i of block s
|
|
163
|
+
uint64_t exact_split(uint64_t s, int64_t i) const;
|
|
164
|
+
|
|
165
|
+
/** Block s with the splits of its exact entries, serialized (to send it
|
|
166
|
+
* to another machine, 10_distributed.md). */
|
|
167
|
+
void serialize_block(uint64_t s, std::vector<uint8_t>& data) const;
|
|
168
|
+
/// store block s from the nbytes of data of serialize_block
|
|
169
|
+
void store_serialized_block(uint64_t s, size_t nbytes, const uint8_t* data);
|
|
170
|
+
/// sum of the point offsets of the bits of z (low half + high half)
|
|
171
|
+
double low_sum(uint32_t z) const;
|
|
172
|
+
|
|
173
|
+
/// stored value of entry Y (the cost if exact, else a lower bound)
|
|
174
|
+
float lower(uint64_t Y, bool* exact = nullptr) const;
|
|
175
|
+
bool is_exact(uint64_t Y) const;
|
|
176
|
+
|
|
177
|
+
size_t nbytes() const;
|
|
178
|
+
uint64_t nexact() const;
|
|
179
|
+
uint64_t ncert() const;
|
|
180
|
+
/// every block is clamped (all costs >= +0)
|
|
181
|
+
bool nonneg() const;
|
|
182
|
+
|
|
183
|
+
/// tables of low_sum (low and high halves of z)
|
|
184
|
+
std::vector<double> tlo, thi;
|
|
185
|
+
/// per block, set by store_block: exact entries, certified entries,
|
|
186
|
+
/// bytes, clamp flag
|
|
187
|
+
std::vector<uint64_t> block_k, block_ncert, block_nbytes;
|
|
188
|
+
std::vector<uint8_t> block_clamp;
|
|
189
|
+
std::shared_ptr<OptimalKMeansLevelStorage> storage; ///< disk mode
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** Lower bound of an entry from its offset, group bounds and code. */
|
|
193
|
+
float optimal_kmeans_lower_bound(
|
|
194
|
+
double off,
|
|
195
|
+
float gmin,
|
|
196
|
+
float step,
|
|
197
|
+
int code,
|
|
198
|
+
int q,
|
|
199
|
+
bool clamp);
|
|
200
|
+
|
|
201
|
+
/** Compress the block s of level `level` from its combination result
|
|
202
|
+
* (cost, split: 2^L entries).
|
|
203
|
+
*
|
|
204
|
+
* @param in_a, in_b input levels of the combination, for the
|
|
205
|
+
* certification; nullptr: T1 (every entry exact)
|
|
206
|
+
* @param f fraction of the entries stored exactly (chosen among
|
|
207
|
+
* the certified entries)
|
|
208
|
+
* @param final_other optional, for the last stored level: the other term of
|
|
209
|
+
* the final candidates, final_other[z] = B[U ^ S] for
|
|
210
|
+
* the entry S = (s << L) | z (U: the full set, B: the
|
|
211
|
+
* other table of the final combination). The exact
|
|
212
|
+
* entries are then those with the smallest final
|
|
213
|
+
* candidate value cost[z] + final_other[z], instead of
|
|
214
|
+
* the smallest residuals per popcount.
|
|
215
|
+
*/
|
|
216
|
+
void optimal_kmeans_encode_block(
|
|
217
|
+
OptimalKMeansCompressedLevel* level,
|
|
218
|
+
uint64_t s,
|
|
219
|
+
const float* cost,
|
|
220
|
+
const uint64_t* split,
|
|
221
|
+
const OptimalKMeansCompressedLevel* in_a,
|
|
222
|
+
const OptimalKMeansCompressedLevel* in_b,
|
|
223
|
+
double f,
|
|
224
|
+
const float* final_other = nullptr);
|
|
225
|
+
|
|
226
|
+
/** Decoded values of block s (2^L floats) and optionally the mask of the
|
|
227
|
+
* exact entries. */
|
|
228
|
+
void optimal_kmeans_decode_block(
|
|
229
|
+
const OptimalKMeansCompressedLevel& level,
|
|
230
|
+
uint64_t s,
|
|
231
|
+
float* out,
|
|
232
|
+
uint8_t* exact = nullptr);
|
|
233
|
+
|
|
234
|
+
/** Parameters of the exact recomputation. */
|
|
235
|
+
struct OptimalKMeansExactEngineParams {
|
|
236
|
+
/// candidates of a row kept in memory at a time (larger rows take
|
|
237
|
+
/// several passes)
|
|
238
|
+
size_t max_candidates = size_t(1) << 22;
|
|
239
|
+
/// memory of the decoded blocks (rows over large sets decode whole
|
|
240
|
+
/// blocks)
|
|
241
|
+
size_t cache_bytes = size_t(16) << 30;
|
|
242
|
+
/// threads of the rows over large sets (0: OpenMP default)
|
|
243
|
+
int nthreads = 0;
|
|
244
|
+
|
|
245
|
+
OptimalKMeansExactEngineParams() {}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
struct OptimalKMeansExactEngineState;
|
|
249
|
+
|
|
250
|
+
/** Exact values of the entries of compressed levels: rows over the level
|
|
251
|
+
* below, branch and bound with the stored lower bounds, recursive down to
|
|
252
|
+
* T1. The values computed are memoized. */
|
|
253
|
+
struct OptimalKMeansExactEngine {
|
|
254
|
+
/// t1: the single-cluster costs; levels in blocks of 2^L entries
|
|
255
|
+
OptimalKMeansExactEngine(
|
|
256
|
+
const OptimalKMeansCost1* t1,
|
|
257
|
+
int n,
|
|
258
|
+
int L,
|
|
259
|
+
const OptimalKMeansExactEngineParams& params =
|
|
260
|
+
OptimalKMeansExactEngineParams());
|
|
261
|
+
~OptimalKMeansExactEngine();
|
|
262
|
+
|
|
263
|
+
/// make a level available (not owned; it must be complete and stay
|
|
264
|
+
/// valid while the engine is used)
|
|
265
|
+
void add_level(const OptimalKMeansCompressedLevel* level);
|
|
266
|
+
|
|
267
|
+
/** Row min over L subset of Y of (A[L] + B[Y ^ L], L), exact, with A
|
|
268
|
+
* the table of level a and B of level b (level 1 is T1). */
|
|
269
|
+
void row(uint64_t Y, int a, int b, float* cost, uint64_t* split);
|
|
270
|
+
|
|
271
|
+
/// exact cost and split of entry Y of level c
|
|
272
|
+
void value(int c, uint64_t Y, float* cost, uint64_t* split);
|
|
273
|
+
|
|
274
|
+
/// rows recomputed in level c so far
|
|
275
|
+
uint64_t rows(int c) const;
|
|
276
|
+
|
|
277
|
+
OptimalKMeansExactEngineParams params;
|
|
278
|
+
std::shared_ptr<OptimalKMeansExactEngineState> state;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
} // namespace faiss
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <faiss/impl/pipnn/HashPrune.h>
|
|
9
|
+
|
|
10
|
+
#include <cstring>
|
|
11
|
+
|
|
12
|
+
#include <faiss/impl/FaissAssert.h>
|
|
13
|
+
#include <faiss/impl/platform_macros.h>
|
|
14
|
+
#include <faiss/utils/bf16.h>
|
|
15
|
+
#include <faiss/utils/random.h>
|
|
16
|
+
|
|
17
|
+
namespace faiss {
|
|
18
|
+
namespace pipnn {
|
|
19
|
+
|
|
20
|
+
uint16_t distance_key(float v) {
|
|
21
|
+
// Normalized on the bit pattern, so -ffast-math cannot remove the checks.
|
|
22
|
+
uint32_t u;
|
|
23
|
+
std::memcpy(&u, &v, sizeof(u));
|
|
24
|
+
if ((u & 0x7fffffffu) == 0) {
|
|
25
|
+
u = 0; // -0 -> +0
|
|
26
|
+
} else if ((u & 0x7f800000u) == 0x7f800000u && (u & 0x007fffffu) != 0) {
|
|
27
|
+
u = 0x7f800000u; // NaN -> +inf
|
|
28
|
+
}
|
|
29
|
+
std::memcpy(&v, &u, sizeof(v));
|
|
30
|
+
// Scalar `encode_bf16` only: the SIMD path rounds differently, so keys
|
|
31
|
+
// would depend on the CPU.
|
|
32
|
+
const uint16_t b = encode_bf16(v);
|
|
33
|
+
return (b & 0x8000) ? static_cast<uint16_t>(~b)
|
|
34
|
+
: static_cast<uint16_t>(b | 0x8000);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
bool ReservoirRef::offer(int32_t id, uint16_t hash, uint16_t key) {
|
|
38
|
+
const int n = *count;
|
|
39
|
+
// A full reservoir rejects anything not below its maximum (rules 1 and 3).
|
|
40
|
+
if (n == capacity && *farthest != kNoFarthest) {
|
|
41
|
+
const Slot& f = slots[*farthest];
|
|
42
|
+
if (!slot_less(key, id, f.key, f.id)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
int lo = 0;
|
|
48
|
+
int hi = n;
|
|
49
|
+
while (lo < hi) {
|
|
50
|
+
const int mid = (lo + hi) / 2;
|
|
51
|
+
if (slots[mid].hash < hash) {
|
|
52
|
+
lo = mid + 1;
|
|
53
|
+
} else {
|
|
54
|
+
hi = mid;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Rule 1: bucket hit. The cached farthest stays valid only when a
|
|
59
|
+
// non-farthest slot is lowered.
|
|
60
|
+
if (lo < n && slots[lo].hash == hash) {
|
|
61
|
+
Slot& s = slots[lo];
|
|
62
|
+
if (!slot_less(key, id, s.key, s.id)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
s.id = id;
|
|
66
|
+
s.key = key;
|
|
67
|
+
if (*farthest == lo) {
|
|
68
|
+
*farthest = kNoFarthest;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Rule 2: miss with room.
|
|
74
|
+
if (n < capacity) {
|
|
75
|
+
std::memmove(
|
|
76
|
+
slots + lo + 1,
|
|
77
|
+
slots + lo,
|
|
78
|
+
sizeof(Slot) * static_cast<size_t>(n - lo));
|
|
79
|
+
slots[lo] = Slot{id, hash, key};
|
|
80
|
+
*count = static_cast<uint8_t>(n + 1);
|
|
81
|
+
*farthest = kNoFarthest;
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Rule 3: miss, full. Compare against the slot_less-maximum.
|
|
86
|
+
int z = *farthest;
|
|
87
|
+
if (z == kNoFarthest) {
|
|
88
|
+
z = 0;
|
|
89
|
+
for (int i = 1; i < n; i++) {
|
|
90
|
+
if (slot_less(
|
|
91
|
+
slots[z].key, slots[z].id, slots[i].key, slots[i].id)) {
|
|
92
|
+
z = i;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
*farthest = static_cast<uint8_t>(z);
|
|
96
|
+
}
|
|
97
|
+
const Slot& zs = slots[z];
|
|
98
|
+
if (!slot_less(key, id, zs.key, zs.id)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (z < lo) {
|
|
102
|
+
std::memmove(
|
|
103
|
+
slots + z,
|
|
104
|
+
slots + z + 1,
|
|
105
|
+
sizeof(Slot) * static_cast<size_t>(lo - 1 - z));
|
|
106
|
+
slots[lo - 1] = Slot{id, hash, key};
|
|
107
|
+
} else {
|
|
108
|
+
std::memmove(
|
|
109
|
+
slots + lo + 1,
|
|
110
|
+
slots + lo,
|
|
111
|
+
sizeof(Slot) * static_cast<size_t>(z - lo));
|
|
112
|
+
slots[lo] = Slot{id, hash, key};
|
|
113
|
+
}
|
|
114
|
+
*farthest = kNoFarthest;
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
Hyperplanes::Hyperplanes(int d_in, int m_in, int64_t seed) : d(d_in), m(m_in) {
|
|
119
|
+
FAISS_THROW_IF_NOT_FMT(d >= 1, "Hyperplanes: d must be >= 1, got %d", d);
|
|
120
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
121
|
+
m >= 1 && m <= 16, "Hyperplanes: m must be in [1, 16], got %d", m);
|
|
122
|
+
std::vector<float> h(static_cast<size_t>(m) * d);
|
|
123
|
+
float_randn(h.data(), h.size(), seed);
|
|
124
|
+
ht.assign(static_cast<size_t>(d) * 16, 0.0f);
|
|
125
|
+
for (int i = 0; i < m; i++) {
|
|
126
|
+
for (int j = 0; j < d; j++) {
|
|
127
|
+
ht[static_cast<size_t>(j) * 16 + i] =
|
|
128
|
+
h[static_cast<size_t>(i) * d + j];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
FAISS_NOINLINE void Hyperplanes::sketch(const float* x, float* out16) const {
|
|
134
|
+
// Local accumulators, j ascending: the result depends only on the values
|
|
135
|
+
// of x, never on its buffer, alignment or caller.
|
|
136
|
+
float acc[16] = {};
|
|
137
|
+
const float* h = ht.data();
|
|
138
|
+
for (int j = 0; j < d; j++) {
|
|
139
|
+
const float xj = x[j];
|
|
140
|
+
const float* hj = h + static_cast<size_t>(j) * 16;
|
|
141
|
+
for (int i = 0; i < 16; i++) {
|
|
142
|
+
acc[i] += xj * hj[i];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
std::memcpy(out16, acc, sizeof(acc));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
uint16_t residual_hash(const float* sketch_p, const float* sketch_c, int m) {
|
|
149
|
+
uint16_t h = 0;
|
|
150
|
+
for (int i = 0; i < m; i++) {
|
|
151
|
+
if (sketch_c[i] - sketch_p[i] >= 0) {
|
|
152
|
+
h = static_cast<uint16_t>(h | (1u << i));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return h;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
} // namespace pipnn
|
|
159
|
+
} // namespace faiss
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
namespace faiss {
|
|
14
|
+
namespace pipnn {
|
|
15
|
+
|
|
16
|
+
/// One reservoir slot: candidate id, residual-LSH bucket, order-preserving key.
|
|
17
|
+
struct Slot {
|
|
18
|
+
int32_t id;
|
|
19
|
+
uint16_t hash;
|
|
20
|
+
uint16_t key;
|
|
21
|
+
};
|
|
22
|
+
static_assert(sizeof(Slot) == 8, "PiPNN reservoir slots must be 8 bytes");
|
|
23
|
+
|
|
24
|
+
/// "No cached farthest slot"; never a slot index since capacity <= 255.
|
|
25
|
+
constexpr uint8_t kNoFarthest = 0xFF;
|
|
26
|
+
|
|
27
|
+
/// Monotone 16-bit key of a distance, which may be negative, from its scalar
|
|
28
|
+
/// bf16 encoding. NaN maps to +inf and -0 to +0.
|
|
29
|
+
uint16_t distance_key(float v);
|
|
30
|
+
|
|
31
|
+
/// Strict (key, id) order. Deciding key ties by id makes the reservoir
|
|
32
|
+
/// independent of the offer order.
|
|
33
|
+
inline bool slot_less(uint16_t ka, int32_t ia, uint16_t kb, int32_t ib) {
|
|
34
|
+
return ka < kb || (ka == kb && ia < ib);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// One point's reservoir in caller-owned storage; not thread-safe. Slots are
|
|
38
|
+
/// sorted by distinct hash, and *farthest caches the slot_less-maximum.
|
|
39
|
+
struct ReservoirRef {
|
|
40
|
+
Slot* slots;
|
|
41
|
+
uint8_t* count; // 0 when fresh
|
|
42
|
+
uint8_t* farthest; // kNoFarthest when fresh or unknown
|
|
43
|
+
int capacity; // 1..255
|
|
44
|
+
|
|
45
|
+
/// Keeps the smaller offer on a bucket hit, else inserts or evicts the
|
|
46
|
+
/// maximum. An id must always come with the same hash. True iff changed.
|
|
47
|
+
bool offer(int32_t id, uint16_t hash, uint16_t key);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/// m <= 16 Gaussian hyperplanes from float_randn(seed), stored transposed
|
|
51
|
+
/// and zero-padded: ht[j * 16 + i].
|
|
52
|
+
struct Hyperplanes {
|
|
53
|
+
int d = 0;
|
|
54
|
+
int m = 0;
|
|
55
|
+
std::vector<float> ht; // d * 16
|
|
56
|
+
|
|
57
|
+
Hyperplanes() = default;
|
|
58
|
+
Hyperplanes(int d, int m, int64_t seed);
|
|
59
|
+
|
|
60
|
+
/// out16[i] = sum over ascending j of x[j] * ht[j * 16 + i]. Not inlined,
|
|
61
|
+
/// so all call sites produce bitwise-identical sketches.
|
|
62
|
+
void sketch(const float* x, float* out16) const;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/// Bit i (i < m <= 16) is set iff sketch_c[i] - sketch_p[i] >= 0.
|
|
66
|
+
uint16_t residual_hash(const float* sketch_p, const float* sketch_c, int m);
|
|
67
|
+
|
|
68
|
+
} // namespace pipnn
|
|
69
|
+
} // namespace faiss
|