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,751 @@
|
|
|
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/invlists/RaBitQSSDInvertedLists.h>
|
|
9
|
+
|
|
10
|
+
#include <fcntl.h>
|
|
11
|
+
#include <sys/types.h>
|
|
12
|
+
#include <unistd.h>
|
|
13
|
+
|
|
14
|
+
#include <algorithm>
|
|
15
|
+
#include <cerrno>
|
|
16
|
+
#include <chrono>
|
|
17
|
+
#include <cinttypes>
|
|
18
|
+
#include <cstring>
|
|
19
|
+
#include <memory>
|
|
20
|
+
#include <mutex>
|
|
21
|
+
#include <random>
|
|
22
|
+
#include <typeinfo>
|
|
23
|
+
#include <vector>
|
|
24
|
+
|
|
25
|
+
#include <faiss/impl/FaissAssert.h>
|
|
26
|
+
#include <faiss/impl/RaBitQUtils.h>
|
|
27
|
+
#include <faiss/impl/fast_scan/fast_scan.h>
|
|
28
|
+
#include <faiss/impl/io.h>
|
|
29
|
+
#include <faiss/impl/io_macros.h>
|
|
30
|
+
#include <faiss/index_io.h>
|
|
31
|
+
#include <faiss/invlists/DiskIOBackend.h>
|
|
32
|
+
|
|
33
|
+
namespace faiss {
|
|
34
|
+
|
|
35
|
+
namespace {
|
|
36
|
+
|
|
37
|
+
constexpr size_t kSectorSize = RaBitQSSDInvertedLists::kSectorSize;
|
|
38
|
+
constexpr char kFileMagic[8] = {'F', 'A', 'I', 'S', 'S', 'D', 'R', 'Q'};
|
|
39
|
+
constexpr uint32_t kFileVersion = 1;
|
|
40
|
+
|
|
41
|
+
/// stored at the start of the file
|
|
42
|
+
struct FileHeader {
|
|
43
|
+
char magic[8];
|
|
44
|
+
uint32_t version;
|
|
45
|
+
uint32_t reserved;
|
|
46
|
+
uint64_t d;
|
|
47
|
+
uint64_t mem_dim;
|
|
48
|
+
uint64_t ex_bits;
|
|
49
|
+
uint64_t record_size;
|
|
50
|
+
uint64_t page_size;
|
|
51
|
+
uint64_t file_id;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
static_assert(sizeof(FileHeader) <= kSectorSize);
|
|
55
|
+
|
|
56
|
+
/// I/O buffers are aligned for O_DIRECT
|
|
57
|
+
constexpr int kBufferAlignment = 4096;
|
|
58
|
+
static_assert(kBufferAlignment == kSectorSize);
|
|
59
|
+
using IOBuffer = AlignedTableTightAlloc<uint8_t, kBufferAlignment>;
|
|
60
|
+
|
|
61
|
+
/// largest single read or write issued when processing a whole list
|
|
62
|
+
constexpr size_t kMaxIOBytes = size_t(1) << 23;
|
|
63
|
+
|
|
64
|
+
/// Open a file with O_DIRECT, so that reads bypass the page cache.
|
|
65
|
+
int open_direct(const std::string& fname, int flags) {
|
|
66
|
+
return open(fname.c_str(), flags | O_DIRECT, 0644);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void pread_all(
|
|
70
|
+
int fd,
|
|
71
|
+
uint8_t* buf,
|
|
72
|
+
size_t len,
|
|
73
|
+
size_t offset,
|
|
74
|
+
const std::string& fname) {
|
|
75
|
+
while (len > 0) {
|
|
76
|
+
ssize_t ret = pread(fd, buf, len, static_cast<off_t>(offset));
|
|
77
|
+
if (ret < 0 && errno == EINTR) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
81
|
+
ret >= 0,
|
|
82
|
+
"read of %s at offset %zu failed: %s",
|
|
83
|
+
fname.c_str(),
|
|
84
|
+
offset,
|
|
85
|
+
strerror(errno));
|
|
86
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
87
|
+
ret > 0,
|
|
88
|
+
"read of %s at offset %zu: unexpected end of file",
|
|
89
|
+
fname.c_str(),
|
|
90
|
+
offset);
|
|
91
|
+
buf += ret;
|
|
92
|
+
len -= ret;
|
|
93
|
+
offset += ret;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void pwrite_all(
|
|
98
|
+
int fd,
|
|
99
|
+
const uint8_t* buf,
|
|
100
|
+
size_t len,
|
|
101
|
+
size_t offset,
|
|
102
|
+
const std::string& fname) {
|
|
103
|
+
while (len > 0) {
|
|
104
|
+
ssize_t ret = pwrite(fd, buf, len, static_cast<off_t>(offset));
|
|
105
|
+
if (ret < 0 && errno == EINTR) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
109
|
+
ret > 0,
|
|
110
|
+
"write of %s at offset %zu failed: %s",
|
|
111
|
+
fname.c_str(),
|
|
112
|
+
offset,
|
|
113
|
+
strerror(errno));
|
|
114
|
+
buf += ret;
|
|
115
|
+
len -= ret;
|
|
116
|
+
offset += ret;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
uint64_t random_file_id() {
|
|
121
|
+
std::random_device rd;
|
|
122
|
+
uint64_t id = (uint64_t(rd()) << 32) ^ rd();
|
|
123
|
+
return id ^
|
|
124
|
+
uint64_t(
|
|
125
|
+
std::chrono::steady_clock::now()
|
|
126
|
+
.time_since_epoch()
|
|
127
|
+
.count());
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// pages from page_no to the end of its extent
|
|
131
|
+
size_t pages_left_in_extent(
|
|
132
|
+
const RaBitQSSDInvertedLists& il,
|
|
133
|
+
size_t list_no,
|
|
134
|
+
size_t page_no) {
|
|
135
|
+
for (const RaBitQSSDInvertedLists::Extent& e : il.extents[list_no]) {
|
|
136
|
+
if (page_no < e.n_pages) {
|
|
137
|
+
return e.n_pages - page_no;
|
|
138
|
+
}
|
|
139
|
+
page_no -= e.n_pages;
|
|
140
|
+
}
|
|
141
|
+
FAISS_THROW_FMT(
|
|
142
|
+
"RaBitQSSDInvertedLists: page beyond the end of list %zu", list_no);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// read or write n_pages consecutive pages of a list, which must lie in one
|
|
146
|
+
/// extent, with a blocking call
|
|
147
|
+
void read_pages(
|
|
148
|
+
const RaBitQSSDInvertedLists& il,
|
|
149
|
+
size_t list_no,
|
|
150
|
+
size_t page_no,
|
|
151
|
+
size_t n_pages,
|
|
152
|
+
uint8_t* buf) {
|
|
153
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
154
|
+
il.fd >= 0, "RaBitQSSDInvertedLists: the file is not open");
|
|
155
|
+
FAISS_ASSERT(n_pages <= pages_left_in_extent(il, list_no, page_no));
|
|
156
|
+
pread_all(
|
|
157
|
+
il.fd,
|
|
158
|
+
buf,
|
|
159
|
+
n_pages * il.page_size,
|
|
160
|
+
il.page_offset(list_no, page_no),
|
|
161
|
+
il.filename);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
void write_pages(
|
|
165
|
+
RaBitQSSDInvertedLists& il,
|
|
166
|
+
size_t list_no,
|
|
167
|
+
size_t page_no,
|
|
168
|
+
size_t n_pages,
|
|
169
|
+
const uint8_t* buf) {
|
|
170
|
+
FAISS_ASSERT(n_pages <= pages_left_in_extent(il, list_no, page_no));
|
|
171
|
+
pwrite_all(
|
|
172
|
+
il.fd,
|
|
173
|
+
buf,
|
|
174
|
+
n_pages * il.page_size,
|
|
175
|
+
il.page_offset(list_no, page_no),
|
|
176
|
+
il.filename);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
void write_header(RaBitQSSDInvertedLists& il) {
|
|
180
|
+
FileHeader h;
|
|
181
|
+
memcpy(h.magic, kFileMagic, sizeof(kFileMagic));
|
|
182
|
+
h.version = kFileVersion;
|
|
183
|
+
h.reserved = 0;
|
|
184
|
+
h.d = il.d;
|
|
185
|
+
h.mem_dim = il.mem_dim;
|
|
186
|
+
h.ex_bits = il.ex_bits;
|
|
187
|
+
h.record_size = il.record_size;
|
|
188
|
+
h.page_size = il.page_size;
|
|
189
|
+
h.file_id = il.file_id;
|
|
190
|
+
IOBuffer buf(kSectorSize);
|
|
191
|
+
memset(buf.get(), 0, kSectorSize);
|
|
192
|
+
memcpy(buf.get(), &h, sizeof(h));
|
|
193
|
+
pwrite_all(il.fd, buf.get(), kSectorSize, 0, il.filename);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
void check_header(const RaBitQSSDInvertedLists& il) {
|
|
197
|
+
IOBuffer buf(kSectorSize);
|
|
198
|
+
pread_all(il.fd, buf.get(), kSectorSize, 0, il.filename);
|
|
199
|
+
FileHeader h;
|
|
200
|
+
memcpy(&h, buf.get(), sizeof(h));
|
|
201
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
202
|
+
memcmp(h.magic, kFileMagic, sizeof(kFileMagic)) == 0,
|
|
203
|
+
"%s is not a RaBitQSSDInvertedLists file",
|
|
204
|
+
il.filename.c_str());
|
|
205
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
206
|
+
h.version == kFileVersion,
|
|
207
|
+
"%s: unsupported file version %u",
|
|
208
|
+
il.filename.c_str(),
|
|
209
|
+
h.version);
|
|
210
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
211
|
+
h.d == il.d && h.mem_dim == il.mem_dim && h.ex_bits == il.ex_bits &&
|
|
212
|
+
h.record_size == il.record_size &&
|
|
213
|
+
h.page_size == il.page_size,
|
|
214
|
+
"%s does not match the inverted lists: file has d=%" PRIu64
|
|
215
|
+
" mem_dim=%" PRIu64 " ex_bits=%" PRIu64
|
|
216
|
+
", lists have d=%zu mem_dim=%zu ex_bits=%zu",
|
|
217
|
+
il.filename.c_str(),
|
|
218
|
+
h.d,
|
|
219
|
+
h.mem_dim,
|
|
220
|
+
h.ex_bits,
|
|
221
|
+
il.d,
|
|
222
|
+
il.mem_dim,
|
|
223
|
+
il.ex_bits);
|
|
224
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
225
|
+
h.file_id == il.file_id,
|
|
226
|
+
"%s was written for another index (file id %016" PRIx64
|
|
227
|
+
", expected %016" PRIx64 ")",
|
|
228
|
+
il.filename.c_str(),
|
|
229
|
+
h.file_id,
|
|
230
|
+
il.file_id);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
} // namespace
|
|
234
|
+
|
|
235
|
+
struct RaBitQSSDInvertedLists::IOState {
|
|
236
|
+
/// contexts of acquire_io() that are not in use
|
|
237
|
+
std::vector<std::unique_ptr<DiskIOBackend>> io_pool;
|
|
238
|
+
std::mutex io_pool_mutex; ///< protects io_pool
|
|
239
|
+
std::mutex alloc_mutex; ///< protects total_pages
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/*********************************************************
|
|
243
|
+
* RaBitQSSDInvertedLists
|
|
244
|
+
*********************************************************/
|
|
245
|
+
|
|
246
|
+
RaBitQSSDInvertedLists::RaBitQSSDInvertedLists(
|
|
247
|
+
size_t nlist_in,
|
|
248
|
+
size_t d_in,
|
|
249
|
+
size_t mem_dim_in,
|
|
250
|
+
size_t ex_bits_in,
|
|
251
|
+
const char* filename_in)
|
|
252
|
+
: InvertedLists(nlist_in, INVALID_CODE_SIZE),
|
|
253
|
+
d(d_in),
|
|
254
|
+
mem_dim(mem_dim_in),
|
|
255
|
+
ex_bits(ex_bits_in) {
|
|
256
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
257
|
+
filename_in && filename_in[0],
|
|
258
|
+
"RaBitQSSDInvertedLists needs a file name");
|
|
259
|
+
filename = filename_in;
|
|
260
|
+
set_derived_values();
|
|
261
|
+
sizes.resize(nlist, 0);
|
|
262
|
+
mem_data.resize(nlist);
|
|
263
|
+
extents.resize(nlist);
|
|
264
|
+
file_id = random_file_id();
|
|
265
|
+
|
|
266
|
+
fd = open_direct(filename, O_RDWR | O_CREAT | O_TRUNC);
|
|
267
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
268
|
+
fd >= 0,
|
|
269
|
+
"could not create %s with O_DIRECT: %s",
|
|
270
|
+
filename.c_str(),
|
|
271
|
+
strerror(errno));
|
|
272
|
+
try {
|
|
273
|
+
write_header(*this);
|
|
274
|
+
} catch (...) {
|
|
275
|
+
close(fd);
|
|
276
|
+
fd = -1;
|
|
277
|
+
throw;
|
|
278
|
+
}
|
|
279
|
+
io_state = new IOState;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
RaBitQSSDInvertedLists::RaBitQSSDInvertedLists()
|
|
283
|
+
: InvertedLists(0, INVALID_CODE_SIZE), io_state(new IOState) {}
|
|
284
|
+
|
|
285
|
+
RaBitQSSDInvertedLists::~RaBitQSSDInvertedLists() {
|
|
286
|
+
// destroying a libaio context waits for an RCU grace period, tens of
|
|
287
|
+
// milliseconds: destroy them in parallel. The contexts of a parent
|
|
288
|
+
// process, before a fork(), are only dropped, without a parallel region:
|
|
289
|
+
// OpenMP can hang in a forked child.
|
|
290
|
+
std::vector<std::unique_ptr<DiskIOBackend>>& io_pool = io_state->io_pool;
|
|
291
|
+
const auto own_end = std::partition(
|
|
292
|
+
io_pool.begin(),
|
|
293
|
+
io_pool.end(),
|
|
294
|
+
[](const std::unique_ptr<DiskIOBackend>& io) {
|
|
295
|
+
return io->owned_by_this_process();
|
|
296
|
+
});
|
|
297
|
+
const int n_io = int(own_end - io_pool.begin());
|
|
298
|
+
if (n_io > 1) {
|
|
299
|
+
#pragma omp parallel for num_threads(n_io)
|
|
300
|
+
for (int i = 0; i < n_io; i++) {
|
|
301
|
+
io_pool[i].reset();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
delete io_state;
|
|
305
|
+
if (fd >= 0) {
|
|
306
|
+
close(fd);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
void RaBitQSSDInvertedLists::set_derived_values() {
|
|
311
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
312
|
+
d > 0 && d % 64 == 0 && d < (size_t(1) << 31),
|
|
313
|
+
"d=%zu must be a positive multiple of 64",
|
|
314
|
+
d);
|
|
315
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
316
|
+
mem_dim >= 64 && mem_dim <= d && mem_dim % 64 == 0,
|
|
317
|
+
"mem_dim=%zu must be a multiple of 64 in [64, d=%zu]",
|
|
318
|
+
mem_dim,
|
|
319
|
+
d);
|
|
320
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
321
|
+
ex_bits >= 1 && ex_bits <= 8,
|
|
322
|
+
"ex_bits=%zu must be in [1, 8]",
|
|
323
|
+
ex_bits);
|
|
324
|
+
batch_bytes =
|
|
325
|
+
kBatchSize * mem_dim / 8 + kBatchSize * kMemFactors * sizeof(float);
|
|
326
|
+
record_size = (d - mem_dim) / 8 + d * ex_bits / 8 +
|
|
327
|
+
sizeof(rabitq_utils::ExtraBitsFactors) + sizeof(idx_t);
|
|
328
|
+
page_size = (record_size + kSectorSize - 1) / kSectorSize * kSectorSize;
|
|
329
|
+
records_per_page = page_size / record_size;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
size_t RaBitQSSDInvertedLists::list_batches(size_t list_no) const {
|
|
333
|
+
return (sizes[list_no] + kBatchSize - 1) / kBatchSize;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
size_t RaBitQSSDInvertedLists::list_pages(size_t list_no) const {
|
|
337
|
+
return (sizes[list_no] + records_per_page - 1) / records_per_page;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
std::unique_ptr<DiskIOBackend> RaBitQSSDInvertedLists::acquire_io(
|
|
341
|
+
int max_events) const {
|
|
342
|
+
std::unique_ptr<DiskIOBackend> too_small;
|
|
343
|
+
{
|
|
344
|
+
std::lock_guard<std::mutex> lock(io_state->io_pool_mutex);
|
|
345
|
+
std::vector<std::unique_ptr<DiskIOBackend>>& io_pool =
|
|
346
|
+
io_state->io_pool;
|
|
347
|
+
for (size_t i = io_pool.size(); i > 0; i--) {
|
|
348
|
+
DiskIOBackend& io = *io_pool[i - 1];
|
|
349
|
+
if (!io.owned_by_this_process()) {
|
|
350
|
+
// kept by a parent process before a fork()
|
|
351
|
+
io_pool.erase(io_pool.begin() + (i - 1));
|
|
352
|
+
} else if (io.max_events >= max_events) {
|
|
353
|
+
std::unique_ptr<DiskIOBackend> taken =
|
|
354
|
+
std::move(io_pool[i - 1]);
|
|
355
|
+
io_pool.erase(io_pool.begin() + (i - 1));
|
|
356
|
+
return taken;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// the contexts left are too small: replace one, so that the pool
|
|
360
|
+
// does not grow each time qd_per_thread increases
|
|
361
|
+
if (!io_pool.empty()) {
|
|
362
|
+
too_small = std::move(io_pool.back());
|
|
363
|
+
io_pool.pop_back();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// destroyed out of the lock (io_destroy takes milliseconds), and before
|
|
367
|
+
// the new context takes its share of fs.aio-max-nr
|
|
368
|
+
too_small.reset();
|
|
369
|
+
return std::make_unique<DiskIOBackend>(max_events);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
void RaBitQSSDInvertedLists::release_io(
|
|
373
|
+
std::unique_ptr<DiskIOBackend> io) const noexcept {
|
|
374
|
+
try {
|
|
375
|
+
std::lock_guard<std::mutex> lock(io_state->io_pool_mutex);
|
|
376
|
+
io_state->io_pool.push_back(std::move(io));
|
|
377
|
+
} catch (...) {
|
|
378
|
+
// the context is destroyed instead of kept
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
size_t RaBitQSSDInvertedLists::list_size(size_t list_no) const {
|
|
383
|
+
return sizes[list_no];
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const uint8_t* RaBitQSSDInvertedLists::get_codes(size_t) const {
|
|
387
|
+
FAISS_THROW_MSG(
|
|
388
|
+
"RaBitQSSDInvertedLists: the codes are split between memory "
|
|
389
|
+
"and disk and cannot be accessed as flat codes");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const idx_t* RaBitQSSDInvertedLists::get_ids(size_t list_no) const {
|
|
393
|
+
const size_t n = sizes[list_no];
|
|
394
|
+
if (n == 0) {
|
|
395
|
+
return nullptr;
|
|
396
|
+
}
|
|
397
|
+
std::unique_ptr<idx_t[]> ids(new idx_t[n]);
|
|
398
|
+
const size_t n_pages = list_pages(list_no);
|
|
399
|
+
const size_t max_pages = std::max<size_t>(1, kMaxIOBytes / page_size);
|
|
400
|
+
const size_t id_offset = record_size - sizeof(idx_t);
|
|
401
|
+
IOBuffer buf(std::min(n_pages, max_pages) * page_size);
|
|
402
|
+
|
|
403
|
+
for (size_t page = 0; page < n_pages;) {
|
|
404
|
+
size_t n_read = std::min(
|
|
405
|
+
{n_pages - page,
|
|
406
|
+
max_pages,
|
|
407
|
+
pages_left_in_extent(*this, list_no, page)});
|
|
408
|
+
read_pages(*this, list_no, page, n_read, buf.get());
|
|
409
|
+
for (size_t i = page * records_per_page;
|
|
410
|
+
i < std::min(n, (page + n_read) * records_per_page);
|
|
411
|
+
i++) {
|
|
412
|
+
const size_t pos = i - page * records_per_page;
|
|
413
|
+
const uint8_t* rec = buf.get() +
|
|
414
|
+
pos / records_per_page * page_size +
|
|
415
|
+
pos % records_per_page * record_size;
|
|
416
|
+
memcpy(&ids[i], rec + id_offset, sizeof(idx_t));
|
|
417
|
+
}
|
|
418
|
+
page += n_read;
|
|
419
|
+
}
|
|
420
|
+
return ids.release();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
void RaBitQSSDInvertedLists::release_ids(size_t, const idx_t* ids) const {
|
|
424
|
+
delete[] ids;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
idx_t RaBitQSSDInvertedLists::get_single_id(size_t list_no, size_t offset)
|
|
428
|
+
const {
|
|
429
|
+
FAISS_THROW_IF_NOT(offset < sizes[list_no]);
|
|
430
|
+
IOBuffer buf(page_size);
|
|
431
|
+
read_pages(*this, list_no, offset / records_per_page, 1, buf.get());
|
|
432
|
+
idx_t id;
|
|
433
|
+
memcpy(&id,
|
|
434
|
+
buf.get() + offset % records_per_page * record_size + record_size -
|
|
435
|
+
sizeof(idx_t),
|
|
436
|
+
sizeof(idx_t));
|
|
437
|
+
return id;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
size_t RaBitQSSDInvertedLists::add_entries(
|
|
441
|
+
size_t,
|
|
442
|
+
size_t,
|
|
443
|
+
const idx_t*,
|
|
444
|
+
const uint8_t*) {
|
|
445
|
+
FAISS_THROW_MSG(
|
|
446
|
+
"RaBitQSSDInvertedLists: add vectors with "
|
|
447
|
+
"IndexIVFRaBitQSSD::add");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
void RaBitQSSDInvertedLists::update_entries(
|
|
451
|
+
size_t,
|
|
452
|
+
size_t,
|
|
453
|
+
size_t,
|
|
454
|
+
const idx_t*,
|
|
455
|
+
const uint8_t*) {
|
|
456
|
+
FAISS_THROW_MSG("RaBitQSSDInvertedLists: update_entries not supported");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
void RaBitQSSDInvertedLists::resize(size_t list_no, size_t new_size) {
|
|
460
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
461
|
+
new_size == sizes[list_no],
|
|
462
|
+
"RaBitQSSDInvertedLists: lists cannot be resized");
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
void RaBitQSSDInvertedLists::reset() {
|
|
466
|
+
FAISS_THROW_IF_MSG(read_only, "RaBitQSSDInvertedLists: opened read-only");
|
|
467
|
+
std::fill(sizes.begin(), sizes.end(), 0);
|
|
468
|
+
for (auto& table : mem_data) {
|
|
469
|
+
table.resize(0);
|
|
470
|
+
}
|
|
471
|
+
for (auto& list_extents : extents) {
|
|
472
|
+
list_extents.clear();
|
|
473
|
+
}
|
|
474
|
+
total_pages = 0;
|
|
475
|
+
if (fd >= 0) {
|
|
476
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
477
|
+
ftruncate(fd, kSectorSize) == 0,
|
|
478
|
+
"could not truncate %s: %s",
|
|
479
|
+
filename.c_str(),
|
|
480
|
+
strerror(errno));
|
|
481
|
+
file_id = random_file_id();
|
|
482
|
+
write_header(*this);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
void RaBitQSSDInvertedLists::reserve(size_t list_no, size_t n_more) {
|
|
487
|
+
FAISS_THROW_IF_MSG(read_only, "RaBitQSSDInvertedLists: opened read-only");
|
|
488
|
+
const size_t needed =
|
|
489
|
+
(sizes[list_no] + n_more + records_per_page - 1) / records_per_page;
|
|
490
|
+
std::vector<Extent>& list_extents = extents[list_no];
|
|
491
|
+
size_t capacity = 0;
|
|
492
|
+
for (const Extent& e : list_extents) {
|
|
493
|
+
capacity += e.n_pages;
|
|
494
|
+
}
|
|
495
|
+
if (needed <= capacity) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
// grow by at least half the capacity, so that a list made by many
|
|
499
|
+
// appends has few extents while at most a third of its pages are unused
|
|
500
|
+
const size_t n_pages = std::max(needed - capacity, capacity / 2);
|
|
501
|
+
size_t first_page;
|
|
502
|
+
{
|
|
503
|
+
std::lock_guard<std::mutex> lock(io_state->alloc_mutex);
|
|
504
|
+
first_page = total_pages;
|
|
505
|
+
total_pages += n_pages;
|
|
506
|
+
}
|
|
507
|
+
if (!list_extents.empty() &&
|
|
508
|
+
list_extents.back().first_page + list_extents.back().n_pages ==
|
|
509
|
+
first_page) {
|
|
510
|
+
list_extents.back().n_pages += n_pages;
|
|
511
|
+
} else {
|
|
512
|
+
list_extents.push_back(Extent{first_page, n_pages});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
void RaBitQSSDInvertedLists::append(
|
|
517
|
+
size_t list_no,
|
|
518
|
+
size_t n,
|
|
519
|
+
const uint8_t* mem_codes_in,
|
|
520
|
+
const float* factors,
|
|
521
|
+
const uint8_t* records) {
|
|
522
|
+
if (n == 0) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
526
|
+
fd >= 0, "RaBitQSSDInvertedLists: the file is not open");
|
|
527
|
+
reserve(list_no, n);
|
|
528
|
+
const size_t n0 = sizes[list_no];
|
|
529
|
+
const size_t n1 = n0 + n;
|
|
530
|
+
|
|
531
|
+
// write the records to the pages [page0, page1)
|
|
532
|
+
const size_t page0 = n0 / records_per_page;
|
|
533
|
+
const size_t page1 = (n1 + records_per_page - 1) / records_per_page;
|
|
534
|
+
const size_t max_pages = std::max<size_t>(1, kMaxIOBytes / page_size);
|
|
535
|
+
IOBuffer buf(std::min(page1 - page0, max_pages) * page_size);
|
|
536
|
+
size_t i = 0; // next record to write
|
|
537
|
+
for (size_t page = page0; page < page1;) {
|
|
538
|
+
const size_t n_write = std::min(
|
|
539
|
+
{page1 - page,
|
|
540
|
+
max_pages,
|
|
541
|
+
pages_left_in_extent(*this, list_no, page)});
|
|
542
|
+
memset(buf.get(), 0, n_write * page_size);
|
|
543
|
+
// the first page may already hold records of this list
|
|
544
|
+
size_t slot = n0 + i - page * records_per_page;
|
|
545
|
+
if (slot > 0) {
|
|
546
|
+
read_pages(*this, list_no, page, 1, buf.get());
|
|
547
|
+
}
|
|
548
|
+
for (size_t j = 0; j < n_write; j++) {
|
|
549
|
+
uint8_t* page_buf = buf.get() + j * page_size;
|
|
550
|
+
for (; slot < records_per_page && i < n; slot++, i++) {
|
|
551
|
+
memcpy(page_buf + slot * record_size,
|
|
552
|
+
records + i * record_size,
|
|
553
|
+
record_size);
|
|
554
|
+
}
|
|
555
|
+
slot = 0;
|
|
556
|
+
}
|
|
557
|
+
write_pages(*this, list_no, page, n_write, buf.get());
|
|
558
|
+
page += n_write;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// in-memory batches: the sign bits in the FastScan layout, then the
|
|
562
|
+
// factors. pq4_pack_codes_range only ORs the new codes in, so the bytes
|
|
563
|
+
// after the used ones are kept at 0.
|
|
564
|
+
const size_t n_batches = (n1 + kBatchSize - 1) / kBatchSize;
|
|
565
|
+
AlignedTableTightAlloc<uint8_t>& table = mem_data[list_no];
|
|
566
|
+
if (table.size() < n_batches * batch_bytes) {
|
|
567
|
+
const size_t old_size = table.size();
|
|
568
|
+
table.resize(std::max(n_batches * batch_bytes, 2 * old_size));
|
|
569
|
+
memset(table.get() + old_size, 0, table.size() - old_size);
|
|
570
|
+
}
|
|
571
|
+
const size_t nsq = mem_dim / 4;
|
|
572
|
+
pq4_pack_codes_range(
|
|
573
|
+
mem_codes_in,
|
|
574
|
+
nsq,
|
|
575
|
+
n0,
|
|
576
|
+
n1,
|
|
577
|
+
kBatchSize,
|
|
578
|
+
nsq,
|
|
579
|
+
table.get(),
|
|
580
|
+
mem_dim / 8,
|
|
581
|
+
batch_bytes);
|
|
582
|
+
const size_t code_bytes = kBatchSize * mem_dim / 8;
|
|
583
|
+
for (size_t j = 0; j < n; j++) {
|
|
584
|
+
const size_t pos = n0 + j;
|
|
585
|
+
uint8_t* batch = table.get() + pos / kBatchSize * batch_bytes;
|
|
586
|
+
for (size_t k = 0; k < kMemFactors; k++) {
|
|
587
|
+
memcpy(batch + code_bytes +
|
|
588
|
+
(k * kBatchSize + pos % kBatchSize) * sizeof(float),
|
|
589
|
+
factors + j * kMemFactors + k,
|
|
590
|
+
sizeof(float));
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
sizes[list_no] = n1;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
size_t RaBitQSSDInvertedLists::page_offset(size_t list_no, size_t page_no)
|
|
598
|
+
const {
|
|
599
|
+
for (const Extent& e : extents[list_no]) {
|
|
600
|
+
if (page_no < e.n_pages) {
|
|
601
|
+
return kSectorSize + (e.first_page + page_no) * page_size;
|
|
602
|
+
}
|
|
603
|
+
page_no -= e.n_pages;
|
|
604
|
+
}
|
|
605
|
+
FAISS_THROW_FMT(
|
|
606
|
+
"RaBitQSSDInvertedLists: page beyond the end of list %zu", list_no);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
void RaBitQSSDInvertedLists::open_file() {
|
|
610
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
611
|
+
fd < 0, "RaBitQSSDInvertedLists: the file is already open");
|
|
612
|
+
fd = open_direct(filename, read_only ? O_RDONLY : O_RDWR);
|
|
613
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
614
|
+
fd >= 0,
|
|
615
|
+
"could not open %s with O_DIRECT: %s",
|
|
616
|
+
filename.c_str(),
|
|
617
|
+
strerror(errno));
|
|
618
|
+
check_header(*this);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/*********************************************************
|
|
622
|
+
* I/O hook
|
|
623
|
+
*********************************************************/
|
|
624
|
+
|
|
625
|
+
RaBitQSSDInvertedListsIOHook::RaBitQSSDInvertedListsIOHook()
|
|
626
|
+
: InvertedListsIOHook("ildr", typeid(RaBitQSSDInvertedLists).name()) {}
|
|
627
|
+
|
|
628
|
+
void RaBitQSSDInvertedListsIOHook::write(
|
|
629
|
+
const InvertedLists* ils_in,
|
|
630
|
+
IOWriter* f) const {
|
|
631
|
+
const auto* ils = dynamic_cast<const RaBitQSSDInvertedLists*>(ils_in);
|
|
632
|
+
FAISS_THROW_IF_NOT(ils);
|
|
633
|
+
uint32_t h = fourcc("ildr");
|
|
634
|
+
WRITE1(h);
|
|
635
|
+
WRITE1(ils->nlist);
|
|
636
|
+
WRITE1(ils->d);
|
|
637
|
+
WRITE1(ils->mem_dim);
|
|
638
|
+
WRITE1(ils->ex_bits);
|
|
639
|
+
WRITE1(ils->file_id);
|
|
640
|
+
WRITE1(ils->total_pages);
|
|
641
|
+
{
|
|
642
|
+
std::vector<char> x(ils->filename.begin(), ils->filename.end());
|
|
643
|
+
WRITEVECTOR(x);
|
|
644
|
+
}
|
|
645
|
+
WRITEVECTOR(ils->sizes);
|
|
646
|
+
{
|
|
647
|
+
std::vector<size_t> n_extents, flat_extents;
|
|
648
|
+
for (const auto& list_extents : ils->extents) {
|
|
649
|
+
n_extents.push_back(list_extents.size());
|
|
650
|
+
for (const auto& e : list_extents) {
|
|
651
|
+
flat_extents.push_back(e.first_page);
|
|
652
|
+
flat_extents.push_back(e.n_pages);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
WRITEVECTOR(n_extents);
|
|
656
|
+
WRITEVECTOR(flat_extents);
|
|
657
|
+
}
|
|
658
|
+
for (size_t l = 0; l < ils->nlist; l++) {
|
|
659
|
+
if (ils->sizes[l] > 0) {
|
|
660
|
+
WRITEANDCHECK(
|
|
661
|
+
ils->mem_data[l].get(),
|
|
662
|
+
ils->list_batches(l) * ils->batch_bytes);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
InvertedLists* RaBitQSSDInvertedListsIOHook::read(IOReader* f, int io_flags)
|
|
668
|
+
const {
|
|
669
|
+
FAISS_THROW_IF_MSG(
|
|
670
|
+
io_flags & IO_FLAG_SKIP_IVF_DATA,
|
|
671
|
+
"RaBitQSSDInvertedLists: IO_FLAG_SKIP_IVF_DATA and IO_FLAG_MMAP "
|
|
672
|
+
"are not supported");
|
|
673
|
+
auto ils = std::make_unique<RaBitQSSDInvertedLists>();
|
|
674
|
+
ils->read_only = io_flags & IO_FLAG_READ_ONLY;
|
|
675
|
+
READ1(ils->nlist);
|
|
676
|
+
READ1(ils->d);
|
|
677
|
+
READ1(ils->mem_dim);
|
|
678
|
+
READ1(ils->ex_bits);
|
|
679
|
+
ils->set_derived_values();
|
|
680
|
+
READ1(ils->file_id);
|
|
681
|
+
READ1(ils->total_pages);
|
|
682
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
683
|
+
ils->total_pages <= (SIZE_MAX - kSectorSize) / ils->page_size,
|
|
684
|
+
"RaBitQSSDInvertedLists: total_pages too large");
|
|
685
|
+
{
|
|
686
|
+
std::vector<char> x;
|
|
687
|
+
READVECTOR(x);
|
|
688
|
+
ils->filename.assign(x.begin(), x.end());
|
|
689
|
+
}
|
|
690
|
+
if (io_flags & IO_FLAG_ONDISK_SAME_DIR) {
|
|
691
|
+
ils->filename = ondisk_same_dir_filename(f, ils->filename);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const size_t nlist = ils->nlist;
|
|
695
|
+
const size_t byte_limit = get_deserialization_vector_byte_limit();
|
|
696
|
+
READVECTOR(ils->sizes);
|
|
697
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
698
|
+
ils->sizes.size() == nlist,
|
|
699
|
+
"RaBitQSSDInvertedLists: %zu list sizes for nlist=%zu",
|
|
700
|
+
ils->sizes.size(),
|
|
701
|
+
nlist);
|
|
702
|
+
for (size_t size : ils->sizes) {
|
|
703
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
704
|
+
size < byte_limit, "RaBitQSSDInvertedLists: invalid size");
|
|
705
|
+
}
|
|
706
|
+
{
|
|
707
|
+
std::vector<size_t> n_extents, flat_extents;
|
|
708
|
+
READVECTOR(n_extents);
|
|
709
|
+
READVECTOR(flat_extents);
|
|
710
|
+
FAISS_THROW_IF_NOT(n_extents.size() == nlist);
|
|
711
|
+
ils->extents.resize(nlist);
|
|
712
|
+
size_t k = 0;
|
|
713
|
+
for (size_t l = 0; l < nlist; l++) {
|
|
714
|
+
size_t capacity = 0;
|
|
715
|
+
for (size_t j = 0; j < n_extents[l]; j++) {
|
|
716
|
+
FAISS_THROW_IF_NOT(k + 2 <= flat_extents.size());
|
|
717
|
+
RaBitQSSDInvertedLists::Extent e{
|
|
718
|
+
flat_extents[k], flat_extents[k + 1]};
|
|
719
|
+
k += 2;
|
|
720
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
721
|
+
e.first_page <= ils->total_pages &&
|
|
722
|
+
e.n_pages <= ils->total_pages - e.first_page,
|
|
723
|
+
"RaBitQSSDInvertedLists: extent out of the file");
|
|
724
|
+
capacity += e.n_pages;
|
|
725
|
+
ils->extents[l].push_back(e);
|
|
726
|
+
}
|
|
727
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
728
|
+
ils->list_pages(l) <= capacity,
|
|
729
|
+
"RaBitQSSDInvertedLists: list larger than its extents");
|
|
730
|
+
}
|
|
731
|
+
FAISS_THROW_IF_NOT(k == flat_extents.size());
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
ils->mem_data.resize(nlist);
|
|
735
|
+
for (size_t l = 0; l < nlist; l++) {
|
|
736
|
+
const size_t nbytes = mul_no_overflow(
|
|
737
|
+
ils->list_batches(l),
|
|
738
|
+
ils->batch_bytes,
|
|
739
|
+
"RaBitQSSDInvertedLists batches");
|
|
740
|
+
FAISS_THROW_IF_NOT(nbytes < byte_limit);
|
|
741
|
+
ils->mem_data[l].resize(nbytes);
|
|
742
|
+
if (nbytes > 0) {
|
|
743
|
+
READANDCHECK(ils->mem_data[l].get(), nbytes);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
ils->open_file();
|
|
748
|
+
return ils.release();
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
} // namespace faiss
|