faiss 0.6.3 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/ext/faiss/extconf.rb +1 -1
- 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 +37 -0
- data/vendor/faiss/faiss/IVFlib.cpp +99 -0
- data/vendor/faiss/faiss/IVFlib.h +15 -0
- 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/IndexBinaryFromFloat.cpp +23 -2
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
- data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
- data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
- data/vendor/faiss/faiss/IndexFastScan.h +6 -3
- data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
- data/vendor/faiss/faiss/IndexFlat.h +33 -2
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
- data/vendor/faiss/faiss/IndexHNSW.h +69 -7
- data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
- data/vendor/faiss/faiss/IndexIDMap.h +7 -0
- data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
- data/vendor/faiss/faiss/IndexIVF.h +53 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
- data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
- data/vendor/faiss/faiss/IndexIVFFlat.h +16 -0
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
- data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
- data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
- data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
- data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
- 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 +13 -3
- data/vendor/faiss/faiss/IndexShards.h +5 -0
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
- data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
- data/vendor/faiss/faiss/SuperKMeans.h +12 -6
- data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
- data/vendor/faiss/faiss/VectorTransform.h +3 -0
- data/vendor/faiss/faiss/clone_index.cpp +27 -0
- data/vendor/faiss/faiss/gpu/Dataset.h +112 -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 +4 -5
- data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
- data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
- 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/utils/DeviceUtils.h +11 -0
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
- 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 +287 -134
- 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 +465 -125
- data/vendor/faiss/faiss/impl/HNSW.h +118 -7
- data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
- data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
- data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
- data/vendor/faiss/faiss/impl/Panorama.h +14 -4
- data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
- data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
- data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
- data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
- data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
- 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 +89 -13
- 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 -1
- 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 +18 -129
- data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
- 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 +22 -1
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
- 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 +302 -29
- data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
- 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 +45 -0
- data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
- 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 +23 -4
- 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/avx2.cpp +0 -4
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
- 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 +6 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
- 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 +47 -22
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
- data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
- data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
- data/vendor/faiss/faiss/index_factory.cpp +84 -1
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
- data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
- data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
- data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
- 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 +246 -28
- data/vendor/faiss/faiss/utils/distances.h +34 -0
- data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
- 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/fp16-fp16c.h +4 -0
- data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
- data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
- data/vendor/faiss/faiss/utils/hamming.h +10 -1
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
- 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 +20 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -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/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 +51 -16
- data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
- data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -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/rabitq_avx2.cpp +54 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
- data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
- 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_dispatch.h +2 -7
- data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
- 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 +30 -0
- data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
- data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
- 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 +80 -0
- data/vendor/faiss/faiss/utils/utils.h +24 -0
- metadata +38 -5
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
- data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
|
@@ -0,0 +1,129 @@
|
|
|
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 <vector>
|
|
11
|
+
|
|
12
|
+
#include <faiss/IndexFastScan.h>
|
|
13
|
+
#include <faiss/IndexFlatCodes.h>
|
|
14
|
+
#include <faiss/IndexScalarQuantizer.h>
|
|
15
|
+
#include <faiss/impl/ScalarQuantizer.h>
|
|
16
|
+
|
|
17
|
+
namespace faiss {
|
|
18
|
+
|
|
19
|
+
/** Fast scan version of IndexScalarQuantizer.
|
|
20
|
+
*
|
|
21
|
+
* Supported quantizer types:
|
|
22
|
+
* - QT_4bit, QT_4bit_uniform: native 4-bit codes mapped onto the
|
|
23
|
+
* PQ4 FastScan SIMD (vpshufb / equivalent).
|
|
24
|
+
*
|
|
25
|
+
* - QT_6bit, QT_8bit, QT_8bit_uniform, QT_8bit_direct,
|
|
26
|
+
* QT_8bit_direct_signed: re-quantised to 4-bit for the scan, reranked
|
|
27
|
+
* against the originals in orig_codes.
|
|
28
|
+
*
|
|
29
|
+
* - All other types (QT_fp16, QT_bf16): use IndexScalarQuantizer directly.
|
|
30
|
+
*/
|
|
31
|
+
struct IndexSQFastScan : IndexFastScan {
|
|
32
|
+
ScalarQuantizer sq;
|
|
33
|
+
|
|
34
|
+
/// Overselection ratio for reranking.
|
|
35
|
+
float rerank_factor = 2;
|
|
36
|
+
|
|
37
|
+
/// Low nibbles of the 8-bit codes, two dimensions per byte, indexed by
|
|
38
|
+
/// id. The high nibbles live in the packed scan codes. Empty for the
|
|
39
|
+
/// native 4-bit types, which need no rerank.
|
|
40
|
+
std::vector<uint8_t> lo_codes;
|
|
41
|
+
|
|
42
|
+
/** Constructor.
|
|
43
|
+
*
|
|
44
|
+
* @param d dimensionality of input vectors
|
|
45
|
+
* @param qtype any native 4-bit or reranked type (see above)
|
|
46
|
+
* @param metric distance metric (METRIC_L2 or METRIC_INNER_PRODUCT)
|
|
47
|
+
* @param bbs block size for SIMD processing (multiple of 32)
|
|
48
|
+
*/
|
|
49
|
+
IndexSQFastScan(
|
|
50
|
+
int d,
|
|
51
|
+
ScalarQuantizer::QuantizerType qtype,
|
|
52
|
+
MetricType metric = METRIC_L2,
|
|
53
|
+
int bbs = 32);
|
|
54
|
+
|
|
55
|
+
IndexSQFastScan();
|
|
56
|
+
|
|
57
|
+
/// Build from an existing IndexScalarQuantizer
|
|
58
|
+
explicit IndexSQFastScan(const IndexScalarQuantizer& orig, int bbs = 32);
|
|
59
|
+
|
|
60
|
+
void train(idx_t n, const float* x) override;
|
|
61
|
+
|
|
62
|
+
void add(idx_t n, const float* x) override;
|
|
63
|
+
|
|
64
|
+
void reset() override;
|
|
65
|
+
|
|
66
|
+
void search(
|
|
67
|
+
idx_t n,
|
|
68
|
+
const float* x,
|
|
69
|
+
idx_t k,
|
|
70
|
+
float* distances,
|
|
71
|
+
idx_t* labels,
|
|
72
|
+
const SearchParameters* params = nullptr) const override;
|
|
73
|
+
|
|
74
|
+
void compute_codes(uint8_t* codes, idx_t n, const float* x) const override;
|
|
75
|
+
|
|
76
|
+
void check_compatible_for_merge(const Index& otherIndex) const override;
|
|
77
|
+
|
|
78
|
+
void compute_float_LUT(
|
|
79
|
+
float* lut,
|
|
80
|
+
idx_t n,
|
|
81
|
+
const float* x,
|
|
82
|
+
const FastScanDistancePostProcessing& context) const override;
|
|
83
|
+
|
|
84
|
+
void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override;
|
|
85
|
+
|
|
86
|
+
void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override;
|
|
87
|
+
|
|
88
|
+
void reconstruct(idx_t key, float* recons) const override;
|
|
89
|
+
|
|
90
|
+
void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override;
|
|
91
|
+
|
|
92
|
+
void add_sa_codes(idx_t n, const uint8_t* codes, const idx_t* xids)
|
|
93
|
+
override;
|
|
94
|
+
|
|
95
|
+
void permute_entries(const idx_t* perm);
|
|
96
|
+
|
|
97
|
+
FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const;
|
|
98
|
+
|
|
99
|
+
DistanceComputer* get_distance_computer() const override;
|
|
100
|
+
|
|
101
|
+
void range_search(
|
|
102
|
+
idx_t n,
|
|
103
|
+
const float* x,
|
|
104
|
+
float radius,
|
|
105
|
+
RangeSearchResult* result,
|
|
106
|
+
const SearchParameters* params = nullptr) const override;
|
|
107
|
+
|
|
108
|
+
size_t remove_ids(const IDSelector& sel) override;
|
|
109
|
+
|
|
110
|
+
void merge_from(Index& otherIndex, idx_t add_id = 0) override;
|
|
111
|
+
|
|
112
|
+
void search1(
|
|
113
|
+
const float* x,
|
|
114
|
+
ResultHandler& handler,
|
|
115
|
+
SearchParameters* params = nullptr) const override;
|
|
116
|
+
|
|
117
|
+
size_t sa_code_size() const override;
|
|
118
|
+
|
|
119
|
+
void fill_sa_code(
|
|
120
|
+
const CodePacker& packer,
|
|
121
|
+
idx_t id,
|
|
122
|
+
uint8_t* scratch,
|
|
123
|
+
int* values,
|
|
124
|
+
uint8_t* code_out) const;
|
|
125
|
+
|
|
126
|
+
size_t fast_scan_code_size() const override;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
} // namespace faiss
|
|
@@ -193,6 +193,16 @@ void IndexShardsTemplate<IndexT>::add_with_ids(
|
|
|
193
193
|
syncWithSubIndexes();
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
+
template <typename IndexT>
|
|
197
|
+
size_t IndexShardsTemplate<IndexT>::remove_ids(const IDSelector& sel) {
|
|
198
|
+
FAISS_THROW_IF_MSG(
|
|
199
|
+
successive_ids, "remove_ids needs successive_ids = false");
|
|
200
|
+
const idx_t before = this->ntotal;
|
|
201
|
+
this->runOnIndex([&sel](int, IndexT* index) { index->remove_ids(sel); });
|
|
202
|
+
syncWithSubIndexes();
|
|
203
|
+
return before - this->ntotal;
|
|
204
|
+
}
|
|
205
|
+
|
|
196
206
|
template <typename IndexT>
|
|
197
207
|
void IndexShardsTemplate<IndexT>::search(
|
|
198
208
|
idx_t n,
|
|
@@ -243,8 +253,8 @@ void IndexShardsTemplate<IndexT>::search(
|
|
|
243
253
|
|
|
244
254
|
this->runOnIndex(fn);
|
|
245
255
|
|
|
246
|
-
if (this->metric_type
|
|
247
|
-
merge_knn_results<idx_t,
|
|
256
|
+
if (is_similarity_metric(this->metric_type)) {
|
|
257
|
+
merge_knn_results<idx_t, CMax<distance_t, int>>(
|
|
248
258
|
n,
|
|
249
259
|
k,
|
|
250
260
|
nshard,
|
|
@@ -253,7 +263,7 @@ void IndexShardsTemplate<IndexT>::search(
|
|
|
253
263
|
distances,
|
|
254
264
|
labels);
|
|
255
265
|
} else {
|
|
256
|
-
merge_knn_results<idx_t,
|
|
266
|
+
merge_knn_results<idx_t, CMin<distance_t, int>>(
|
|
257
267
|
n,
|
|
258
268
|
k,
|
|
259
269
|
nshard,
|
|
@@ -91,6 +91,11 @@ struct IndexShardsTemplate : public ThreadedIndex<IndexT> {
|
|
|
91
91
|
|
|
92
92
|
void train(idx_t n, const component_t* x) override;
|
|
93
93
|
|
|
94
|
+
/// Removes the ids from every shard, and returns the sum of the counts.
|
|
95
|
+
/// Needs successive_ids = false: with successive ids, the ids of a shard
|
|
96
|
+
/// depend on the sizes of the shards before it.
|
|
97
|
+
size_t remove_ids(const IDSelector& sel) override;
|
|
98
|
+
|
|
94
99
|
bool successive_ids;
|
|
95
100
|
|
|
96
101
|
/// Synchronize the top-level index (IndexShards) with data in the
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
#include <cstdio>
|
|
14
14
|
#include <functional>
|
|
15
15
|
|
|
16
|
+
#include <faiss/MetricType.h>
|
|
16
17
|
#include <faiss/impl/FaissAssert.h>
|
|
17
18
|
#include <faiss/utils/Heap.h>
|
|
18
19
|
#include <faiss/utils/WorkerThread.h>
|
|
@@ -227,8 +228,8 @@ void IndexShardsIVF::search(
|
|
|
227
228
|
|
|
228
229
|
this->runOnIndex(fn);
|
|
229
230
|
|
|
230
|
-
if (
|
|
231
|
-
merge_knn_results<idx_t,
|
|
231
|
+
if (is_similarity_metric(metric_type)) {
|
|
232
|
+
merge_knn_results<idx_t, CMax<distance_t, int>>(
|
|
232
233
|
n,
|
|
233
234
|
k,
|
|
234
235
|
nshard,
|
|
@@ -237,7 +238,7 @@ void IndexShardsIVF::search(
|
|
|
237
238
|
distances,
|
|
238
239
|
labels);
|
|
239
240
|
} else {
|
|
240
|
-
merge_knn_results<idx_t,
|
|
241
|
+
merge_knn_results<idx_t, CMin<distance_t, int>>(
|
|
241
242
|
n,
|
|
242
243
|
k,
|
|
243
244
|
nshard,
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
#include <faiss/impl/ClusteringHelpers.h>
|
|
23
23
|
#include <faiss/impl/FaissAssert.h>
|
|
24
24
|
#include <faiss/impl/PdxLayout.h>
|
|
25
|
+
#include <faiss/impl/platform_macros.h>
|
|
25
26
|
#include <faiss/impl/simd_dispatch.h>
|
|
26
27
|
#include <faiss/utils/distances.h>
|
|
27
28
|
#include <faiss/utils/random.h>
|
|
@@ -59,7 +60,7 @@ namespace {
|
|
|
59
60
|
struct TrainState {
|
|
60
61
|
/// Orthogonal rotation. Train in rotated space (X_tilde = X * R);
|
|
61
62
|
/// un-rotate centroids before return.
|
|
62
|
-
faiss::
|
|
63
|
+
std::unique_ptr<faiss::VectorTransform> R;
|
|
63
64
|
|
|
64
65
|
std::vector<float> X_tilde; // (n, d) row-major
|
|
65
66
|
int n = 0;
|
|
@@ -77,7 +78,17 @@ struct TrainState {
|
|
|
77
78
|
int low_pruning_streak = 0;
|
|
78
79
|
bool low_pruning_warning_printed = false;
|
|
79
80
|
|
|
80
|
-
explicit TrainState(int d
|
|
81
|
+
explicit TrainState(int d, bool spherical)
|
|
82
|
+
: R([d, spherical]() -> std::unique_ptr<faiss::VectorTransform> {
|
|
83
|
+
// Spherical (inner-product) clustering only: a power-of-two
|
|
84
|
+
// dimension can use the fast Hadamard rotation instead of
|
|
85
|
+
// the generic random rotation. L2 training keeps the
|
|
86
|
+
// original RandomRotationMatrix path unchanged.
|
|
87
|
+
if (spherical && d > 0 && (d & (d - 1)) == 0) {
|
|
88
|
+
return std::make_unique<faiss::HadamardRotation>(d);
|
|
89
|
+
}
|
|
90
|
+
return std::make_unique<faiss::RandomRotationMatrix>(d, d);
|
|
91
|
+
}()) {}
|
|
81
92
|
};
|
|
82
93
|
|
|
83
94
|
/// PDX block layout for the trailing pruning sweep: block b covers original
|
|
@@ -256,9 +267,9 @@ float adapt_d_prime(
|
|
|
256
267
|
}
|
|
257
268
|
|
|
258
269
|
/// Pre-loop setup: subsample, rotate, Forgy init, build ADSampling table,
|
|
259
|
-
/// size the label scratch.
|
|
260
|
-
///
|
|
261
|
-
|
|
270
|
+
/// and size the label scratch. The subsampled input only needs to remain alive
|
|
271
|
+
/// through the rotation and is released before the iteration loop starts.
|
|
272
|
+
void setup_train_state(
|
|
262
273
|
TrainState& state,
|
|
263
274
|
std::vector<int64_t>& labels64,
|
|
264
275
|
std::vector<float>& hassign,
|
|
@@ -266,10 +277,13 @@ std::unique_ptr<uint8_t[]> setup_train_state(
|
|
|
266
277
|
int d,
|
|
267
278
|
int k,
|
|
268
279
|
idx_t n,
|
|
269
|
-
const
|
|
270
|
-
|
|
280
|
+
const void* x,
|
|
281
|
+
NumericType numeric_type) {
|
|
282
|
+
const bool fp16 = numeric_type == NumericType::Float16;
|
|
283
|
+
const size_t line_size =
|
|
284
|
+
(fp16 ? sizeof(uint16_t) : sizeof(float)) * static_cast<size_t>(d);
|
|
271
285
|
idx_t nx = n;
|
|
272
|
-
const uint8_t* x_bytes =
|
|
286
|
+
const uint8_t* x_bytes = static_cast<const uint8_t*>(x);
|
|
273
287
|
std::unique_ptr<uint8_t[]> sampled_x_owner;
|
|
274
288
|
if (static_cast<size_t>(nx) >
|
|
275
289
|
static_cast<size_t>(k) * cp.max_points_per_centroid) {
|
|
@@ -288,17 +302,44 @@ std::unique_ptr<uint8_t[]> setup_train_state(
|
|
|
288
302
|
sampled_x_owner.reset(x_new);
|
|
289
303
|
x_bytes = x_new;
|
|
290
304
|
}
|
|
291
|
-
const float* x_sampled = reinterpret_cast<const float*>(x_bytes);
|
|
292
305
|
|
|
293
306
|
FAISS_THROW_IF_NOT_MSG(
|
|
294
307
|
nx <= static_cast<idx_t>(std::numeric_limits<int>::max()),
|
|
295
308
|
"SuperKMeans: training set size exceeds INT_MAX after sampling");
|
|
296
309
|
state.n = static_cast<int>(nx);
|
|
297
310
|
|
|
298
|
-
state.R.
|
|
311
|
+
if (auto* R = dynamic_cast<HadamardRotation*>(state.R.get())) {
|
|
312
|
+
R->init(cp.seed);
|
|
313
|
+
} else {
|
|
314
|
+
auto* dense_rotation =
|
|
315
|
+
dynamic_cast<RandomRotationMatrix*>(state.R.get());
|
|
316
|
+
FAISS_ASSERT(dense_rotation != nullptr);
|
|
317
|
+
dense_rotation->init(cp.seed);
|
|
318
|
+
}
|
|
299
319
|
|
|
300
320
|
state.X_tilde.resize(static_cast<size_t>(state.n) * d);
|
|
301
|
-
|
|
321
|
+
if (!fp16) {
|
|
322
|
+
state.R->apply_noalloc(
|
|
323
|
+
state.n,
|
|
324
|
+
reinterpret_cast<const float*>(x_bytes),
|
|
325
|
+
state.X_tilde.data());
|
|
326
|
+
} else {
|
|
327
|
+
const size_t block_size =
|
|
328
|
+
std::min(static_cast<size_t>(state.n), cp.decode_block_size);
|
|
329
|
+
std::vector<float> decoded(block_size * d);
|
|
330
|
+
const uint16_t* x16 = reinterpret_cast<const uint16_t*>(x_bytes);
|
|
331
|
+
for (size_t i0 = 0; i0 < static_cast<size_t>(state.n);
|
|
332
|
+
i0 += block_size) {
|
|
333
|
+
const size_t i1 =
|
|
334
|
+
std::min(static_cast<size_t>(state.n), i0 + block_size);
|
|
335
|
+
fp16_to_fp32((i1 - i0) * d, x16 + i0 * d, decoded.data());
|
|
336
|
+
state.R->apply_noalloc(
|
|
337
|
+
static_cast<idx_t>(i1 - i0),
|
|
338
|
+
decoded.data(),
|
|
339
|
+
state.X_tilde.data() + i0 * d);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
sampled_x_owner.reset();
|
|
302
343
|
|
|
303
344
|
// Forgy init: pick k random rows from the rotated pool as initial
|
|
304
345
|
// centroids. These remain in rotated space; un-rotation happens
|
|
@@ -313,6 +354,9 @@ std::unique_ptr<uint8_t[]> setup_train_state(
|
|
|
313
354
|
state.X_tilde.data() + static_cast<size_t>(perm[j]) * d,
|
|
314
355
|
sizeof(float) * d);
|
|
315
356
|
}
|
|
357
|
+
if (cp.spherical) {
|
|
358
|
+
fvec_renorm_L2(d, k, state.Y_tilde.data());
|
|
359
|
+
}
|
|
316
360
|
}
|
|
317
361
|
|
|
318
362
|
state.d_prime =
|
|
@@ -331,15 +375,13 @@ std::unique_ptr<uint8_t[]> setup_train_state(
|
|
|
331
375
|
hassign.assign(k, 0.0f);
|
|
332
376
|
|
|
333
377
|
labels64.resize(state.n);
|
|
334
|
-
|
|
335
|
-
return sampled_x_owner;
|
|
336
378
|
}
|
|
337
379
|
|
|
338
380
|
/// Un-rotate centroids into output buffer. R orthogonal, so
|
|
339
381
|
/// reverse_transform applies R^T = R^-1.
|
|
340
382
|
void untransform_centroids(
|
|
341
383
|
std::vector<float>& centroids,
|
|
342
|
-
const
|
|
384
|
+
const VectorTransform& R,
|
|
343
385
|
int d,
|
|
344
386
|
int k,
|
|
345
387
|
const float* Y_tilde) {
|
|
@@ -391,15 +433,36 @@ SuperKMeans::SuperKMeans(int d, int k, const SuperKMeansParameters& cp_in)
|
|
|
391
433
|
}
|
|
392
434
|
|
|
393
435
|
void SuperKMeans::train(idx_t n, const float* x) {
|
|
436
|
+
train_ex(n, x, NumericType::Float32);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
void SuperKMeans::train_ex(idx_t n, const void* x, NumericType numeric_type) {
|
|
440
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
441
|
+
numeric_type == NumericType::Float32 ||
|
|
442
|
+
numeric_type == NumericType::Float16,
|
|
443
|
+
"SuperKMeans::train_ex: unsupported numeric type");
|
|
394
444
|
FAISS_THROW_IF_NOT_MSG(n > 0, "SuperKMeans: n must be positive");
|
|
395
445
|
FAISS_THROW_IF_MSG(x == nullptr, "SuperKMeans: x must not be null");
|
|
396
446
|
FAISS_THROW_IF_NOT_MSG(
|
|
397
447
|
n >= static_cast<idx_t>(k), "SuperKMeans: n must be >= k");
|
|
448
|
+
const bool fp16 = numeric_type == NumericType::Float16;
|
|
449
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
450
|
+
!fp16 || cp.decode_block_size > 0,
|
|
451
|
+
"SuperKMeans: decode_block_size must be positive");
|
|
398
452
|
if (cp.check_input_data_for_NaNs) {
|
|
399
|
-
|
|
453
|
+
const size_t num_values = static_cast<size_t>(n) * d;
|
|
454
|
+
if (fp16) {
|
|
400
455
|
FAISS_THROW_IF_NOT_MSG(
|
|
401
|
-
|
|
456
|
+
detail::fp16_all_finite(
|
|
457
|
+
num_values, static_cast<const uint16_t*>(x)),
|
|
402
458
|
"SuperKMeans: input contains NaN's or Inf's");
|
|
459
|
+
} else {
|
|
460
|
+
const float* x32 = static_cast<const float*>(x);
|
|
461
|
+
for (size_t i = 0; i < num_values; i++) {
|
|
462
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
463
|
+
std::isfinite(x32[i]),
|
|
464
|
+
"SuperKMeans: input contains NaN's or Inf's");
|
|
465
|
+
}
|
|
403
466
|
}
|
|
404
467
|
}
|
|
405
468
|
if (cp.verbose && n < static_cast<idx_t>(k) * cp.min_points_per_centroid) {
|
|
@@ -411,12 +474,11 @@ void SuperKMeans::train(idx_t n, const float* x) {
|
|
|
411
474
|
static_cast<idx_t>(k) * cp.min_points_per_centroid);
|
|
412
475
|
}
|
|
413
476
|
|
|
414
|
-
TrainState state(d);
|
|
477
|
+
TrainState state(d, cp.spherical);
|
|
415
478
|
std::vector<int64_t> labels64;
|
|
416
479
|
SuperKMeansAssignScratch assign_scratch;
|
|
417
480
|
std::vector<float> hassign;
|
|
418
|
-
|
|
419
|
-
setup_train_state(state, labels64, hassign, cp, d, k, n, x);
|
|
481
|
+
setup_train_state(state, labels64, hassign, cp, d, k, n, x, numeric_type);
|
|
420
482
|
|
|
421
483
|
iteration_stats.clear();
|
|
422
484
|
iteration_stats.reserve(cp.niter);
|
|
@@ -446,6 +508,9 @@ void SuperKMeans::train(idx_t n, const float* x) {
|
|
|
446
508
|
|
|
447
509
|
const int nsplit =
|
|
448
510
|
update_centroids_and_split(d, k, state, labels64, hassign);
|
|
511
|
+
if (cp.spherical) {
|
|
512
|
+
fvec_renorm_L2(d, k, state.Y_tilde.data());
|
|
513
|
+
}
|
|
449
514
|
const float pruning_rate = (iter == 0)
|
|
450
515
|
? 0.0f
|
|
451
516
|
: adapt_d_prime(d, cp, state, total_pairs, pruned_at_gemm);
|
|
@@ -492,7 +557,7 @@ void SuperKMeans::train(idx_t n, const float* x) {
|
|
|
492
557
|
(getmillisecs() - t_train_start) / 1000.0);
|
|
493
558
|
}
|
|
494
559
|
|
|
495
|
-
untransform_centroids(centroids, state.R, d, k, state.Y_tilde.data());
|
|
560
|
+
untransform_centroids(centroids, *state.R, d, k, state.Y_tilde.data());
|
|
496
561
|
}
|
|
497
562
|
|
|
498
563
|
void super_kmeans_assign_iteration(
|
|
@@ -598,11 +663,11 @@ void super_kmeans_assign_iteration(
|
|
|
598
663
|
}
|
|
599
664
|
|
|
600
665
|
// One SIMD dispatch per (xi, yj) tile.
|
|
601
|
-
|
|
666
|
+
with_simd_level_with_sve([&]<SIMDLevel SL>() {
|
|
602
667
|
[[maybe_unused]] const int omp_chunk_local = cp.omp_chunk;
|
|
603
668
|
int64_t tile_total = 0;
|
|
604
669
|
int64_t tile_pruned = 0;
|
|
605
|
-
#pragma omp parallel for schedule(
|
|
670
|
+
#pragma omp parallel for schedule(FAISS_OMP_DYNAMIC, omp_chunk_local) \
|
|
606
671
|
reduction(+ : tile_total) reduction(+ : tile_pruned)
|
|
607
672
|
for (int i = 0; i < bx; ++i) {
|
|
608
673
|
const float xnp_i = x_norms_partial[xi + i];
|
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
// "A Super Fast K-means for Indexing Vector Embeddings."
|
|
14
14
|
// arXiv preprint arXiv:2603.20009.
|
|
15
15
|
//
|
|
16
|
-
// Use when: L2 metric,
|
|
17
|
-
//
|
|
16
|
+
// Use when: L2 metric, or spherical inner-product clustering with
|
|
17
|
+
// cp.spherical=true, k >= 1024, d >= 128, dense float embeddings.
|
|
18
|
+
// Do not use for: small k,
|
|
18
19
|
// binary data (use IndexBinaryIVF), or near-unit-sphere embeddings with
|
|
19
20
|
// k < 4096 (chi-squared assumption breaks down).
|
|
20
21
|
//
|
|
@@ -88,11 +89,16 @@ struct SuperKMeans {
|
|
|
88
89
|
|
|
89
90
|
SuperKMeans(int d, int k, const SuperKMeansParameters& cp = {});
|
|
90
91
|
|
|
91
|
-
/// Train on `n` row-major vectors of dimension `d`.
|
|
92
|
-
/// ClusteringParameters fields (niter, seed, verbose,
|
|
93
|
-
/// max_points_per_centroid, use_faster_subsampling,
|
|
94
|
-
/// check_input_data_for_NaNs).
|
|
92
|
+
/// Train on `n` row-major fp32 vectors of dimension `d`.
|
|
95
93
|
void train(idx_t n, const float* x);
|
|
94
|
+
|
|
95
|
+
/** Train on vectors of the given numeric type.
|
|
96
|
+
*
|
|
97
|
+
* Float32 is the same as train(). Float16 takes packed IEEE binary16
|
|
98
|
+
* vectors and widens bounded blocks while rotating them into the fp32
|
|
99
|
+
* training workspace. Centroids and all iteration state remain fp32.
|
|
100
|
+
*/
|
|
101
|
+
void train_ex(idx_t n, const void* x, NumericType numeric_type);
|
|
96
102
|
};
|
|
97
103
|
|
|
98
104
|
/// Reusable scratch for super_kmeans_assign_iteration; pass one instance across
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
|
|
18
18
|
#include <faiss/IndexPQ.h>
|
|
19
19
|
#include <faiss/impl/FaissAssert.h>
|
|
20
|
+
#include <faiss/impl/platform_macros.h>
|
|
20
21
|
#include <faiss/utils/distances.h>
|
|
21
22
|
#include <faiss/utils/random.h>
|
|
22
23
|
#include <faiss/utils/utils.h>
|
|
@@ -481,7 +482,7 @@ void HadamardRotation::apply_noalloc(idx_t n, const float* x, float* xt) const {
|
|
|
481
482
|
// Three rounds scale by p^(3/2). Normalize once at the end.
|
|
482
483
|
float total_scale = 1.0f / (p * std::sqrt(static_cast<float>(p)));
|
|
483
484
|
|
|
484
|
-
#pragma omp parallel for schedule(
|
|
485
|
+
#pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
|
|
485
486
|
for (idx_t i = 0; i < n; i++) {
|
|
486
487
|
const float* xi = x + i * d;
|
|
487
488
|
float* xo = xt + i * p;
|
|
@@ -513,6 +514,44 @@ void HadamardRotation::apply_noalloc(idx_t n, const float* x, float* xt) const {
|
|
|
513
514
|
}
|
|
514
515
|
}
|
|
515
516
|
|
|
517
|
+
void HadamardRotation::reverse_transform(idx_t n, const float* xt, float* x)
|
|
518
|
+
const {
|
|
519
|
+
FAISS_THROW_IF_NOT_MSG(is_trained, "Transformation not trained yet");
|
|
520
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
521
|
+
d_in == d_out,
|
|
522
|
+
"HadamardRotation inverse requires equal input/output dimensions");
|
|
523
|
+
|
|
524
|
+
const size_t p = d_out;
|
|
525
|
+
// Reverse of apply_noalloc: three unnormalized FWHT rounds scale norms
|
|
526
|
+
// by (sqrt(p))^3 = p*sqrt(p); the forward pass cancels this with
|
|
527
|
+
// total_scale = 1/(p*sqrt(p)), so the inverse applies the same factor.
|
|
528
|
+
const float inverse_scale = 1.0f / (p * std::sqrt(static_cast<float>(p)));
|
|
529
|
+
|
|
530
|
+
#pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
|
|
531
|
+
for (idx_t i = 0; i < n; i++) {
|
|
532
|
+
const float* xi = xt + i * p;
|
|
533
|
+
float* xo = x + i * p;
|
|
534
|
+
|
|
535
|
+
// The inverse reverses the three sign-flip/Hadamard factors.
|
|
536
|
+
std::memcpy(xo, xi, p * sizeof(float));
|
|
537
|
+
fwht_inplace(xo, p);
|
|
538
|
+
|
|
539
|
+
for (size_t j = 0; j < p; j++) {
|
|
540
|
+
xo[j] *= signs3[j];
|
|
541
|
+
}
|
|
542
|
+
fwht_inplace(xo, p);
|
|
543
|
+
|
|
544
|
+
for (size_t j = 0; j < p; j++) {
|
|
545
|
+
xo[j] *= signs2[j];
|
|
546
|
+
}
|
|
547
|
+
fwht_inplace(xo, p);
|
|
548
|
+
|
|
549
|
+
for (size_t j = 0; j < p; j++) {
|
|
550
|
+
xo[j] *= signs1[j] * inverse_scale;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
516
555
|
void HadamardRotation::check_identical(const VectorTransform& other) const {
|
|
517
556
|
auto* hr = dynamic_cast<const HadamardRotation*>(&other);
|
|
518
557
|
FAISS_THROW_IF_NOT_MSG(hr, "failed to cast to HadamardRotation");
|
|
@@ -144,6 +144,9 @@ struct HadamardRotation : VectorTransform {
|
|
|
144
144
|
|
|
145
145
|
void apply_noalloc(idx_t n, const float* x, float* xt) const override;
|
|
146
146
|
|
|
147
|
+
/// Apply the inverse transform when d_in == d_out.
|
|
148
|
+
void reverse_transform(idx_t n, const float* xt, float* x) const override;
|
|
149
|
+
|
|
147
150
|
void check_identical(const VectorTransform& other) const override;
|
|
148
151
|
|
|
149
152
|
HadamardRotation() {}
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
#include <faiss/clone_index.h>
|
|
11
11
|
|
|
12
|
+
#include <memory>
|
|
13
|
+
|
|
12
14
|
#include <faiss/impl/FaissAssert.h>
|
|
13
15
|
|
|
14
16
|
#include <faiss/Index2Layer.h>
|
|
@@ -37,7 +39,9 @@
|
|
|
37
39
|
#include <faiss/IndexNSG.h>
|
|
38
40
|
#include <faiss/IndexPQ.h>
|
|
39
41
|
#include <faiss/IndexPQFastScan.h>
|
|
42
|
+
#include <faiss/IndexPiPNN.h>
|
|
40
43
|
#include <faiss/IndexPreTransform.h>
|
|
44
|
+
#include <faiss/IndexRaBitQ.h>
|
|
41
45
|
#include <faiss/IndexRefine.h>
|
|
42
46
|
#include <faiss/IndexRowwiseMinMax.h>
|
|
43
47
|
#include <faiss/IndexScalarQuantizer.h>
|
|
@@ -139,6 +143,7 @@ IndexIDMap* clone_IndexIDMap(const IndexIDMap* im) {
|
|
|
139
143
|
IndexHNSW* clone_IndexHNSW(const IndexHNSW* ihnsw) {
|
|
140
144
|
TRYCLONE(IndexHNSW2Level, ihnsw)
|
|
141
145
|
TRYCLONE(IndexHNSWFlatPanorama, ihnsw)
|
|
146
|
+
TRYCLONE(IndexHNSWRaBitQ, ihnsw)
|
|
142
147
|
TRYCLONE(IndexHNSWFlat, ihnsw)
|
|
143
148
|
TRYCLONE(IndexHNSWPQ, ihnsw)
|
|
144
149
|
TRYCLONE(IndexHNSWSQ, ihnsw)
|
|
@@ -168,6 +173,17 @@ IndexNSG* clone_IndexNSG(const IndexNSG* insg) {
|
|
|
168
173
|
}
|
|
169
174
|
}
|
|
170
175
|
|
|
176
|
+
static IndexPiPNN* clone_IndexPiPNN(const IndexPiPNN* ipipnn) {
|
|
177
|
+
TRYCLONE(IndexPiPNNFlat, ipipnn)
|
|
178
|
+
TRYCLONE(IndexPiPNNSQ, ipipnn) {
|
|
179
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
180
|
+
typeid(*ipipnn) == typeid(IndexPiPNN),
|
|
181
|
+
"clone not supported for this type of IndexPiPNN: %s",
|
|
182
|
+
typeid(*ipipnn).name());
|
|
183
|
+
return new IndexPiPNN(*ipipnn);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
171
187
|
IndexRowwiseMinMaxBase* clone_IndexRowwiseMinMax(
|
|
172
188
|
const IndexRowwiseMinMaxBase* irmmb) {
|
|
173
189
|
TRYCLONE(IndexRowwiseMinMaxFP16, irmmb)
|
|
@@ -294,6 +310,7 @@ Index* Cloner::clone_Index(const Index* index) {
|
|
|
294
310
|
TRYCLONE(IndexEDEN, index)
|
|
295
311
|
|
|
296
312
|
TRYCLONE(IndexScalarQuantizer, index)
|
|
313
|
+
TRYCLONE(IndexRaBitQ, index)
|
|
297
314
|
TRYCLONE(MultiIndexQuantizer, index)
|
|
298
315
|
|
|
299
316
|
if (const IndexIVF* ivf = dynamic_cast<const IndexIVF*>(index)) {
|
|
@@ -353,6 +370,16 @@ Index* Cloner::clone_Index(const Index* index) {
|
|
|
353
370
|
res->own_fields = true;
|
|
354
371
|
res->storage = clone_Index(insg->storage);
|
|
355
372
|
return res;
|
|
373
|
+
} else if (
|
|
374
|
+
const IndexPiPNN* ipipnn = dynamic_cast<const IndexPiPNN*>(index)) {
|
|
375
|
+
// make sure we don't get a GPU index here
|
|
376
|
+
std::unique_ptr<Index> storage(
|
|
377
|
+
ipipnn->storage ? Cloner::clone_Index(ipipnn->storage)
|
|
378
|
+
: nullptr);
|
|
379
|
+
IndexPiPNN* res = clone_IndexPiPNN(ipipnn);
|
|
380
|
+
res->storage = storage.release();
|
|
381
|
+
res->own_fields = true;
|
|
382
|
+
return res;
|
|
356
383
|
} else if (
|
|
357
384
|
const IndexNNDescent* innd =
|
|
358
385
|
dynamic_cast<const IndexNNDescent*>(index)) {
|