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.
Files changed (255) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/ext/faiss/extconf.rb +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  6. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  7. data/vendor/faiss/faiss/Clustering.h +37 -0
  8. data/vendor/faiss/faiss/IVFlib.cpp +99 -0
  9. data/vendor/faiss/faiss/IVFlib.h +15 -0
  10. data/vendor/faiss/faiss/Index.cpp +44 -0
  11. data/vendor/faiss/faiss/Index.h +18 -13
  12. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  13. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  14. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +23 -2
  15. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
  17. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
  18. data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
  22. data/vendor/faiss/faiss/IndexFlat.h +33 -2
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
  25. data/vendor/faiss/faiss/IndexHNSW.h +69 -7
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
  27. data/vendor/faiss/faiss/IndexIDMap.h +7 -0
  28. data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
  29. data/vendor/faiss/faiss/IndexIVF.h +53 -1
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
  31. data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +16 -0
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
  37. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  38. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
  39. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
  46. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
  47. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  48. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  49. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  50. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  51. data/vendor/faiss/faiss/IndexShards.cpp +13 -3
  52. data/vendor/faiss/faiss/IndexShards.h +5 -0
  53. data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
  54. data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
  55. data/vendor/faiss/faiss/SuperKMeans.h +12 -6
  56. data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
  57. data/vendor/faiss/faiss/VectorTransform.h +3 -0
  58. data/vendor/faiss/faiss/clone_index.cpp +27 -0
  59. data/vendor/faiss/faiss/gpu/Dataset.h +112 -0
  60. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  61. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  62. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +4 -5
  63. data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
  64. data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
  65. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
  67. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  68. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  69. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  70. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  71. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  72. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  73. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  74. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
  75. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
  76. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
  77. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  78. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
  79. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
  80. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
  81. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  82. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
  83. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  84. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  85. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  86. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +287 -134
  87. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  88. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  89. data/vendor/faiss/faiss/impl/HNSW.cpp +465 -125
  90. data/vendor/faiss/faiss/impl/HNSW.h +118 -7
  91. data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
  92. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  93. data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
  94. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  95. data/vendor/faiss/faiss/impl/Panorama.h +14 -4
  96. data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
  97. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  98. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
  99. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
  100. data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
  101. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
  102. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
  103. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
  104. data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
  105. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
  106. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
  107. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  108. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  109. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  110. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +89 -13
  111. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  112. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  113. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  114. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  115. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -1
  116. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  117. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  118. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  119. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +18 -129
  120. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  121. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
  122. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
  123. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
  124. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
  125. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  127. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +22 -1
  128. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
  129. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
  130. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/index_read.cpp +302 -29
  133. data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
  134. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  135. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  136. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  137. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  138. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  139. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  140. data/vendor/faiss/faiss/impl/platform_macros.h +45 -0
  141. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  142. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
  143. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  144. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  145. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  146. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  147. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  148. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  149. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +23 -4
  150. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  151. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  152. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  153. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  154. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  155. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  156. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  157. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +0 -4
  158. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
  159. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  160. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  161. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +6 -1
  162. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
  163. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  164. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
  165. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
  166. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
  167. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
  168. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
  169. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
  170. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
  171. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  172. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  173. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  174. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  175. data/vendor/faiss/faiss/impl/simd_dispatch.h +47 -22
  176. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  177. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  178. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  179. data/vendor/faiss/faiss/index_factory.cpp +84 -1
  180. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
  181. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
  182. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
  183. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
  184. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
  185. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
  186. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
  187. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
  188. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  189. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  190. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  191. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  192. data/vendor/faiss/faiss/utils/distances.cpp +246 -28
  193. data/vendor/faiss/faiss/utils/distances.h +34 -0
  194. data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
  195. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  196. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
  197. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  198. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  199. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  200. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  201. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  202. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  203. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  204. data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
  205. data/vendor/faiss/faiss/utils/hamming.h +10 -1
  206. data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
  207. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  208. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  209. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +20 -0
  210. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
  211. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
  212. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  213. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -1
  214. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  215. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  216. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  217. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  218. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  219. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  220. data/vendor/faiss/faiss/utils/rabitq_simd.h +51 -16
  221. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  222. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
  223. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
  224. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
  225. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
  226. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  227. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -0
  228. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  229. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  230. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  231. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  232. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  233. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  234. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  235. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  236. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  237. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +54 -15
  238. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
  239. data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
  240. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  241. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  242. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
  243. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
  244. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +30 -0
  247. data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
  248. data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
  249. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  250. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  251. data/vendor/faiss/faiss/utils/utils.cpp +80 -0
  252. data/vendor/faiss/faiss/utils/utils.h +24 -0
  253. metadata +38 -5
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
@@ -0,0 +1,122 @@
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 <cstddef>
11
+ #include <cstdint>
12
+ #include <vector>
13
+
14
+ #include <faiss/MetricType.h>
15
+ #include <faiss/impl/maybe_owned_vector.h>
16
+
17
+ namespace faiss {
18
+
19
+ struct DistanceComputer;
20
+ struct IDSelector;
21
+ struct Index;
22
+ struct PiPNN;
23
+ struct VisitedTable;
24
+
25
+ namespace pipnn {
26
+
27
+ // Internal build pieces, exposed for tests; hidden from SWIG.
28
+ #ifndef SWIG
29
+
30
+ /// Points per medoid block. It fixes the summation order of the mean, so
31
+ /// changing it changes the entry point.
32
+ constexpr size_t kMedoidBlock = size_t(1) << 16;
33
+
34
+ /// Throws unless every vector is finite with squared norm <= FLT_MAX / 8, and
35
+ /// returns the L2 medoid: the point closest to the mean, lowest id on ties.
36
+ int32_t medoid_and_validate(const Index& storage, float* norm_cv = nullptr);
37
+
38
+ /// RobustPrune of x (row 0 of the m x m D) over cand[t] (row 1 + t) in
39
+ /// (D, id) order; writes <= R ids to out, -1 padded. order: m - 1 ints.
40
+ void robust_prune(
41
+ const float* D,
42
+ size_t m,
43
+ const int32_t* cand,
44
+ MetricType metric,
45
+ float alpha,
46
+ int R,
47
+ int* order,
48
+ int32_t* out);
49
+
50
+ namespace detail {
51
+
52
+ /// PiPNN::build with a given reservoir chunk size, for tests.
53
+ void build_with_chunk(
54
+ PiPNN& p,
55
+ const Index& storage,
56
+ size_t reservoir_chunk,
57
+ bool verbose);
58
+
59
+ } // namespace detail
60
+
61
+ #endif // SWIG
62
+
63
+ } // namespace pipnn
64
+
65
+ /// PiPNN graph (Rubel et al., KDD '26, arXiv:2602.21247).
66
+ struct PiPNN {
67
+ // build parameters
68
+ int R = 64; ///< max out-degree
69
+ int leaf_k = 2; ///< neighbours each point offers per leaf
70
+ int hash_bits = 12; ///< HashPrune keeps one candidate per hash bucket
71
+ int reservoir_size = 64; ///< candidates kept per point
72
+ int c_max = 1024; ///< largest leaf
73
+ int c_min = 128; ///< smaller partition children are merged
74
+ int leader_cap = 1000; ///< most leaders per split
75
+ float leader_fraction = 0.005f; ///< leaders per split, fraction of its size
76
+ float alpha = 0; ///< 0 = 1.2
77
+ std::vector<int> fanout = {10, 3}; ///< leaders joined per depth, then 1
78
+ int64_t seed = 1234;
79
+ /// IP: rank partition leaders by angle, not inner product
80
+ bool ip_partition_by_angle = false;
81
+ /// point instances per partition wave, which bounds memory
82
+ size_t partition_wave_budget = size_t(1) << 30;
83
+
84
+ int search_L = 64; ///< default beam width of search
85
+
86
+ // result
87
+ MaybeOwnedVector<int32_t> graph; ///< ntotal x R, -1 padded
88
+ /// next higher row with the same code, or -1; empty if all codes differ.
89
+ /// Such copies have empty graph rows; search returns them with the first.
90
+ MaybeOwnedVector<int32_t> next_copy;
91
+ int32_t entry_point = -1; ///< L2 medoid
92
+ idx_t ntotal = 0;
93
+
94
+ PiPNN() = default;
95
+ explicit PiPNN(int R) : R(R) {}
96
+
97
+ float effective_alpha(MetricType metric) const;
98
+
99
+ void check_params() const;
100
+
101
+ #ifndef SWIG
102
+ /// Builds over the vectors and metric of storage. Strong guarantee: the
103
+ /// results are unchanged if it throws.
104
+ void build(const Index& storage, bool verbose = false);
105
+
106
+ /// Beam max(L, k); qdis smaller-is-better; vt: ntotal slots, clean in/out.
107
+ /// Writes the k nearest in sel by (dis, id), padded; sel does not prune.
108
+ void search(
109
+ DistanceComputer& qdis,
110
+ int L,
111
+ idx_t k,
112
+ idx_t* I,
113
+ float* D,
114
+ VisitedTable& vt,
115
+ const IDSelector* sel) const;
116
+ #endif
117
+
118
+ /// Empties the graph; entry_point = -1, ntotal = 0.
119
+ void reset();
120
+ };
121
+
122
+ } // namespace faiss
@@ -9,6 +9,8 @@
9
9
 
10
10
  #include <faiss/impl/ProductQuantizer.h>
11
11
 
12
+ #include <omp.h>
13
+ #include <atomic>
12
14
  #include <cstddef>
13
15
  #include <cstdio>
14
16
  #include <cstring>
@@ -16,10 +18,12 @@
16
18
  #include <type_traits>
17
19
 
18
20
  #include <algorithm>
21
+ #include <atomic>
19
22
 
20
23
  #include <faiss/IndexFlat.h>
21
24
  #include <faiss/VectorTransform.h>
22
25
  #include <faiss/impl/FaissAssert.h>
26
+ #include <faiss/impl/FaissException.h>
23
27
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
24
28
  #include <faiss/impl/pq_code_distance/pq_code_distance-inl.h>
25
29
  #include <faiss/impl/simd_dispatch.h>
@@ -141,8 +145,8 @@ void ProductQuantizer::train(size_t n, const float* x) {
141
145
  }
142
146
  }
143
147
 
144
- std::unique_ptr<float[]> xslice(new float[n * dsub]);
145
- for (size_t m = 0; m < M; m++) {
148
+ const auto train_slice = [&](size_t m) {
149
+ std::unique_ptr<float[]> xslice(new float[n * dsub]);
146
150
  for (size_t j = 0; j < n; j++)
147
151
  memcpy(xslice.get() + j * dsub,
148
152
  x + j * d + m * dsub,
@@ -150,7 +154,6 @@ void ProductQuantizer::train(size_t n, const float* x) {
150
154
 
151
155
  Clustering clus(dsub, ksub, cp);
152
156
 
153
- // we have some initialization for the centroids
154
157
  if (final_train_type != Train_default) {
155
158
  clus.centroids.resize(dsub * ksub);
156
159
  }
@@ -187,6 +190,28 @@ void ProductQuantizer::train(size_t n, const float* x) {
187
190
  IndexFlatL2 index(dsub);
188
191
  clus.train(n, xslice.get(), assign_index ? *assign_index : index);
189
192
  set_params(clus.centroids.data(), m);
193
+ };
194
+
195
+ if (!assign_index && x != centroids.data() && M > 1 &&
196
+ M * 8 >= static_cast<size_t>(omp_get_max_threads())) {
197
+ std::exception_ptr ex;
198
+ std::atomic<bool> failed{false};
199
+ #pragma omp parallel for
200
+ for (int64_t m = 0; m < static_cast<int64_t>(M); m++) {
201
+ if (failed) {
202
+ continue;
203
+ }
204
+ try {
205
+ train_slice(m);
206
+ } catch (...) {
207
+ omp_capture_exception(ex, [&] { failed = true; });
208
+ }
209
+ }
210
+ omp_rethrow_if_exception(ex);
211
+ } else {
212
+ for (size_t m = 0; m < M; m++) {
213
+ train_slice(m);
214
+ }
190
215
  }
191
216
 
192
217
  } else {
@@ -280,7 +305,7 @@ void compute_1_code(const ProductQuantizer& pq, const float* x, uint8_t* code) {
280
305
  } // namespace
281
306
 
282
307
  void ProductQuantizer::compute_code(const float* x, uint8_t* code) const {
283
- with_simd_level([&]<SIMDLevel SL>() {
308
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
284
309
  switch (nbits) {
285
310
  case 8:
286
311
  compute_1_code<PQEncoder8, SL>(*this, x, code);
@@ -294,7 +319,7 @@ void ProductQuantizer::compute_code(const float* x, uint8_t* code) const {
294
319
  compute_1_code<PQEncoderGeneric, SL>(*this, x, code);
295
320
  break;
296
321
  }
297
- }); // with_simd_level
322
+ }); // with_simd_level_with_sve
298
323
  }
299
324
 
300
325
  template <class PQDecoder>
@@ -422,10 +447,21 @@ void ProductQuantizer::compute_codes(const float* x, uint8_t* codes, size_t n)
422
447
 
423
448
  int64_t n_signed = n;
424
449
  if (dsub < 16) { // simple direct computation
450
+ std::exception_ptr ex;
451
+ std::atomic<bool> interrupt{false};
425
452
 
426
453
  #pragma omp parallel for
427
- for (int64_t i = 0; i < n_signed; i++)
428
- compute_code(x + i * d, codes + i * code_size);
454
+ for (int64_t i = 0; i < n_signed; i++) {
455
+ if (interrupt.load(std::memory_order_relaxed)) {
456
+ continue;
457
+ }
458
+ try {
459
+ compute_code(x + i * d, codes + i * code_size);
460
+ } catch (...) {
461
+ omp_capture_exception(ex, [&] { interrupt = true; });
462
+ }
463
+ }
464
+ omp_rethrow_if_exception(ex);
429
465
 
430
466
  } else { // worthwhile to use BLAS
431
467
  std::unique_ptr<float[]> dis_tables(new float[n * ksub * M]);
@@ -442,7 +478,7 @@ void ProductQuantizer::compute_codes(const float* x, uint8_t* codes, size_t n)
442
478
 
443
479
  void ProductQuantizer::compute_distance_table(const float* x, float* dis_table)
444
480
  const {
445
- with_simd_level([&]<SIMDLevel SL>() {
481
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
446
482
  if (transposed_centroids.empty()) {
447
483
  // use regular version
448
484
  for (size_t m = 0; m < M; m++) {
@@ -489,8 +525,12 @@ void ProductQuantizer::compute_distance_tables(
489
525
  const float* x,
490
526
  float* dis_tables) const {
491
527
  int64_t nx_signed = nx;
492
- #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON)
493
- if (dsub == 2 && nbits < 8) { // interesting for a narrow range of settings
528
+ #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON) || \
529
+ defined(COMPILE_SIMD_RISCV_RVV)
530
+ // The dsub2 kernels help for a narrow range of settings. They require
531
+ // ksub = 1 << nbits to be a multiple of 8. Therefore nbits must be 3 or
532
+ // more.
533
+ if (dsub == 2 && nbits >= 3 && nbits < 8) {
494
534
  compute_PQ_dis_tables_dsub2(
495
535
  d, ksub, centroids.data(), nx, x, false, dis_tables);
496
536
  } else
@@ -524,8 +564,9 @@ void ProductQuantizer::compute_inner_prod_tables(
524
564
  const float* x,
525
565
  float* dis_tables) const {
526
566
  int64_t nx_signed = nx;
527
- #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON)
528
- if (dsub == 2 && nbits < 8) {
567
+ #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON) || \
568
+ defined(COMPILE_SIMD_RISCV_RVV)
569
+ if (dsub == 2 && nbits >= 3 && nbits < 8) {
529
570
  compute_PQ_dis_tables_dsub2(
530
571
  d, ksub, centroids.data(), nx, x, true, dis_tables);
531
572
  } else
@@ -824,7 +865,7 @@ void ProductQuantizer::compute_sdc_table() {
824
865
  sdc_table.resize(M * ksub * ksub);
825
866
 
826
867
  if (dsub < 4) {
827
- with_simd_level([&]<SIMDLevel SL>() {
868
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
828
869
  #pragma omp parallel for
829
870
  for (int64_t mk = 0; mk < static_cast<int64_t>(M * ksub); mk++) {
830
871
  // allow omp to schedule in a more fine-grained way
@@ -872,8 +913,9 @@ void ProductQuantizer::search_sdc(
872
913
  float* heap_dis = res->val + i * k;
873
914
  const uint8_t* qcode = qcodes + i * code_size;
874
915
 
875
- if (init_finalize_heap)
916
+ if (init_finalize_heap) {
876
917
  maxheap_heapify(k, heap_dis, heap_ids);
918
+ }
877
919
 
878
920
  // Precompute per-subquantizer row pointers: q_row[m] points to
879
921
  // sdc_table[m*ksub^2 + qcode[m]*ksub], eliminating M
@@ -897,8 +939,9 @@ void ProductQuantizer::search_sdc(
897
939
  bcode += code_size;
898
940
  }
899
941
 
900
- if (init_finalize_heap)
942
+ if (init_finalize_heap) {
901
943
  maxheap_reorder(k, heap_dis, heap_ids);
944
+ }
902
945
  }
903
946
  }
904
947
  }
@@ -329,7 +329,7 @@ float compute_full_multibit_distance(
329
329
  size_t d,
330
330
  size_t ex_bits,
331
331
  MetricType metric_type) {
332
- return with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_A0_SPR>(
332
+ return with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_WITH_VPOPCNT>(
333
333
  [&]<SIMDLevel SL>() {
334
334
  return compute_full_multibit_distance<SL>(
335
335
  sign_bits,
@@ -353,7 +353,7 @@ inline int extract_code_inline(
353
353
  * The multi-bit distance combines the sign bit (1-bit) with additional
354
354
  * magnitude bits (ex_bits) to compute a more accurate distance estimate.
355
355
  * Uses SIMD-optimized bit-plane decomposition (AVX2+BMI2) for ex_bits 1-7,
356
- * with scalar fallback for non-x86 or non-BMI2 platforms.
356
+ * with scalar fallback for non-x86 platforms.
357
357
  *
358
358
  * @param sign_bits unpacked sign bits (1-bit codes in standard format)
359
359
  * @param ex_code packed ex-bit codes
@@ -6,17 +6,20 @@
6
6
  */
7
7
 
8
8
  #include <faiss/impl/RaBitQuantizer.h>
9
+ #include <faiss/utils/prefetch.h>
9
10
 
10
11
  #include <faiss/impl/FaissAssert.h>
11
12
  #include <faiss/impl/IDSelector.h>
12
13
  #include <faiss/impl/RaBitQUtils.h>
13
14
  #include <faiss/impl/RaBitQuantizerMultiBit.h>
14
15
  #include <faiss/impl/ResultHandler.h>
16
+ #include <faiss/impl/platform_macros.h>
15
17
  #include <faiss/impl/simd_dispatch.h>
16
18
  #include <faiss/invlists/DirectMap.h>
17
19
  #include <faiss/utils/distances.h>
18
20
  #include <faiss/utils/rabitq_simd.h>
19
21
 
22
+ #include <algorithm>
20
23
  #include <cmath>
21
24
  #include <cstring>
22
25
  #include <memory>
@@ -24,6 +27,13 @@
24
27
 
25
28
  namespace faiss {
26
29
 
30
+ RaBitQStats rabitq_stats;
31
+
32
+ void RaBitQStats::add_atomic(const RaBitQStats& other) {
33
+ detail::atomic_fetch_add_relaxed(n_1bit, other.n_1bit);
34
+ detail::atomic_fetch_add_relaxed(n_refine, other.n_refine);
35
+ }
36
+
27
37
  // Import shared utilities from RaBitQUtils
28
38
  using rabitq_utils::ExtraBitsFactors;
29
39
  using rabitq_utils::QueryFactorsData;
@@ -224,6 +234,51 @@ void RaBitQuantizer::decode_core(
224
234
  }
225
235
  }
226
236
 
237
+ template <SIMDLevel SL>
238
+ float symmetric_dis_1bit(const RaBitQDistanceComputer& dc, idx_t i, idx_t j) {
239
+ FAISS_THROW_IF_NOT_MSG(
240
+ dc.metric_type == MetricType::METRIC_L2,
241
+ "RaBitQ symmetric distance supports only L2");
242
+ FAISS_ASSERT(i >= 0 && j >= 0);
243
+ FAISS_ASSERT(dc.codes != nullptr);
244
+
245
+ const size_t sign_bytes = (dc.d + 7) / 8;
246
+ const uint8_t* code_i = dc.codes + static_cast<size_t>(i) * dc.code_size;
247
+ const uint8_t* code_j = dc.codes + static_cast<size_t>(j) * dc.code_size;
248
+ const auto* factors_i =
249
+ reinterpret_cast<const SignBitFactors*>(code_i + sign_bytes);
250
+ const auto* factors_j =
251
+ reinterpret_cast<const SignBitFactors*>(code_j + sign_bytes);
252
+
253
+ const uint64_t xor_popcount =
254
+ rabitq::bitwise_xor_dot_product<SL>(code_i, code_j, sign_bytes, 1);
255
+ const float sign_dot =
256
+ static_cast<float>(dc.d) - 2.0f * static_cast<float>(xor_popcount);
257
+
258
+ // The L2-optimal reconstruction of residual r is alpha * sign(r), where
259
+ // alpha = ||r||_1 / d. The stored factors give
260
+ // alpha_i * alpha_j = ||r_i||^2 * ||r_j||^2 /
261
+ // (d * dp_multiplier_i * dp_multiplier_j).
262
+ float cross_term = 0.0f;
263
+ if (factors_i->dp_multiplier != 0.0f && factors_j->dp_multiplier != 0.0f) {
264
+ // Dividing each norm first avoids overflowing the product of two
265
+ // squared norms even when the final distance is representable.
266
+ const float scaled_norm_i =
267
+ factors_i->or_minus_c_l2sqr / factors_i->dp_multiplier;
268
+ const float scaled_norm_j =
269
+ factors_j->or_minus_c_l2sqr / factors_j->dp_multiplier;
270
+ cross_term = (scaled_norm_i * (sign_dot / static_cast<float>(dc.d))) *
271
+ scaled_norm_j;
272
+ }
273
+ const float distance = factors_i->or_minus_c_l2sqr +
274
+ factors_j->or_minus_c_l2sqr - 2.0f * cross_term;
275
+ return std::max(0.0f, distance);
276
+ }
277
+
278
+ float RaBitQDistanceComputer::symmetric_dis(idx_t i, idx_t j) {
279
+ return symmetric_dis_1bit<SIMDLevel::NONE>(*this, i, j);
280
+ }
281
+
227
282
  namespace {
228
283
 
229
284
  // Distance computers templatized on SIMDLevel to avoid per-call dynamic
@@ -240,31 +295,18 @@ struct RaBitQDistanceComputerNotQ final : RaBitQDistanceComputer {
240
295
 
241
296
  RaBitQDistanceComputerNotQ() = default;
242
297
 
298
+ float symmetric_dis(idx_t i, idx_t j) final {
299
+ return symmetric_dis_1bit<SL>(*this, i, j);
300
+ }
301
+
243
302
  // Compute distance using only 1-bit codes (fast)
244
303
  float distance_to_code_1bit_impl(
245
304
  const uint8_t* binary_data,
246
305
  const SignBitFactors* base_fac) const {
247
- // this is the baseline code
248
- //
249
- // compute <q,o> using floats
250
- float dot_qo = 0;
251
- // It was a willful decision (after the discussion) to not to pre-cache
252
- // the sum of all bits, just in order to reduce the overhead per
253
- // vector.
254
- uint64_t sum_q = 0;
306
+ const float dot_qo = fvec_masked_sum<simd512_level_selector<SL>::value>(
307
+ rotated_q.data(), binary_data, d);
255
308
 
256
- for (size_t i = 0; i < d; i++) {
257
- // Extract i-th bit
258
- bool bit = rabitq_utils::extract_bit_standard(binary_data, i);
259
- // accumulate dp
260
- dot_qo += bit ? rotated_q[i] : 0;
261
- // accumulate sum-of-bits
262
- sum_q += bit ? 1 : 0;
263
- }
264
-
265
- // Apply query factors
266
- float final_dot =
267
- query_fac.c1 * dot_qo + query_fac.c2 * sum_q - query_fac.c34;
309
+ float final_dot = query_fac.c1 * dot_qo - query_fac.c34;
268
310
 
269
311
  // pre_dist = ||or - c||^2 + ||qr - c||^2 -
270
312
  // 2 * ||or - c|| * ||qr - c|| * <q,o> - (IP ? ||or||^2 : 0)
@@ -366,7 +408,6 @@ struct RaBitQDistanceComputerNotQ final : RaBitQDistanceComputer {
366
408
  }
367
409
 
368
410
  query_fac.c1 = 2 * inv_d;
369
- query_fac.c2 = 0;
370
411
  query_fac.c34 = sum_q * inv_d;
371
412
 
372
413
  if (metric_type == MetricType::METRIC_INNER_PRODUCT) {
@@ -390,11 +431,17 @@ struct RaBitQDistanceComputerNotQ final : RaBitQDistanceComputer {
390
431
  const size_t ex_bits = nb_bits - 1;
391
432
  FAISS_ASSERT(ex_bits > 0);
392
433
 
434
+ // Honor IDSelectorWithContext on the multibit path too, so a RaBitQ
435
+ // index does not silently lose the context hook once nb_bits >= 2 (the
436
+ // 1-bit path already routes through run_scan_codes1).
437
+ const IDSelectorContextDispatch sel_dispatch(sel, store_pairs);
438
+
393
439
  size_t nup = 0;
394
440
  for (size_t j = 0; j < list_size; j++) {
395
441
  if (sel != nullptr) {
396
442
  idx_t id = store_pairs ? lo_build(list_no, j) : ids[j];
397
- if (!sel->is_member(id)) {
443
+ if (!sel_dispatch.is_member(
444
+ id, IDScanContext{ids, list_size, j})) {
398
445
  codes += code_size;
399
446
  continue;
400
447
  }
@@ -417,7 +464,9 @@ struct RaBitQDistanceComputerNotQ final : RaBitQDistanceComputer {
417
464
  const float dis = distance_to_code_full(codes);
418
465
  idx_t id = store_pairs ? lo_build(list_no, j) : ids[j];
419
466
 
420
- if (handler.add_result(dis, id)) {
467
+ if ((keep_max ? dis > handler.threshold
468
+ : dis < handler.threshold) &&
469
+ handler.add_result(dis, id)) {
421
470
  handler.stats.nheap_updates++;
422
471
  nup++;
423
472
  }
@@ -449,6 +498,10 @@ struct RaBitQDistanceComputerQ final : RaBitQDistanceComputer {
449
498
 
450
499
  RaBitQDistanceComputerQ() = default;
451
500
 
501
+ float symmetric_dis(idx_t i, idx_t j) final {
502
+ return symmetric_dis_1bit<SL>(*this, i, j);
503
+ }
504
+
452
505
  // Compute distance using only 1-bit codes (fast)
453
506
  float distance_to_code_1bit_impl(
454
507
  const uint8_t* binary_data,
@@ -458,8 +511,6 @@ struct RaBitQDistanceComputerQ final : RaBitQDistanceComputer {
458
511
  float final_dot = 0;
459
512
  if (centered) {
460
513
  int64_t int_dot = ((1 << qb) - 1) * d;
461
- // See RaBitDistanceComputerNotQ::distance_to_code() for
462
- // baseline code.
463
514
  int_dot -= 2 *
464
515
  rabitq::bitwise_xor_dot_product<SL>(
465
516
  rearranged_rotated_qq.data(),
@@ -510,6 +561,42 @@ struct RaBitQDistanceComputerQ final : RaBitQDistanceComputer {
510
561
  return distance_to_code_1bit_impl(code, base_fac, size);
511
562
  }
512
563
 
564
+ void distance_to_code_1bit_batch_4(
565
+ const uint8_t* const* codes_in,
566
+ float* distances) final {
567
+ if (qb != 4 || centered) {
568
+ RaBitQDistanceComputer::distance_to_code_1bit_batch_4(
569
+ codes_in, distances);
570
+ return;
571
+ }
572
+ const size_t size = (d + 7) / 8;
573
+ const size_t prefix = size +
574
+ (nb_bits == 1 ? sizeof(SignBitFactors)
575
+ : sizeof(SignBitFactorsWithError));
576
+ for (int i = 0; i < 4; ++i) {
577
+ for (size_t offset = 0; offset < prefix; offset += 64) {
578
+ prefetch_L1(codes_in[i] + offset);
579
+ }
580
+ }
581
+ rabitq::BitwiseAndDotProductResult results[4];
582
+ rabitq::bitwise_q4_batch_4<SL>(
583
+ rearranged_rotated_qq.data(), codes_in, size, results);
584
+ for (int i = 0; i < 4; ++i) {
585
+ const auto* factors =
586
+ reinterpret_cast<const SignBitFactors*>(codes_in[i] + size);
587
+ float final_dot = 0;
588
+ final_dot += query_fac.c1 * results[i].dot_product;
589
+ final_dot += query_fac.c2 * results[i].popcount;
590
+ final_dot -= query_fac.c34;
591
+ const float pre_dist = factors->or_minus_c_l2sqr +
592
+ query_fac.qr_to_c_L2sqr -
593
+ 2 * factors->dp_multiplier * final_dot;
594
+ distances[i] = metric_type == METRIC_L2
595
+ ? std::max(0.0f, pre_dist)
596
+ : -0.5f * (pre_dist - query_fac.qr_norm_L2sqr);
597
+ }
598
+ }
599
+
513
600
  // Compute full distance using 1-bit + ex-bits (accurate)
514
601
  float distance_to_code_full(const uint8_t* code) final {
515
602
  FAISS_ASSERT(code != nullptr);
@@ -598,45 +685,50 @@ struct RaBitQDistanceComputerQ final : RaBitQDistanceComputer {
598
685
  bool keep_max,
599
686
  ResultHandler& handler) final {
600
687
  const size_t code_size_base = (d + 7) / 8;
601
- const size_t ex_bits = nb_bits - 1;
602
- FAISS_ASSERT(ex_bits > 0);
603
688
 
604
- size_t nup = 0;
605
- for (size_t j = 0; j < list_size; j++) {
606
- if (sel != nullptr) {
607
- idx_t id = store_pairs ? lo_build(list_no, j) : ids[j];
608
- if (!sel->is_member(id)) {
609
- codes += code_size;
610
- continue;
689
+ const IDSelectorContextDispatch sel_dispatch(sel, store_pairs);
690
+
691
+ auto scan = [&](auto multibit) {
692
+ size_t nup = 0;
693
+ for (size_t j = 0; j < list_size; j++, codes += code_size) {
694
+ if (sel != nullptr) {
695
+ idx_t id = store_pairs ? lo_build(list_no, j) : ids[j];
696
+ if (!sel_dispatch.is_member(
697
+ id, IDScanContext{ids, list_size, j})) {
698
+ continue;
699
+ }
611
700
  }
612
- }
613
-
614
- const auto* base_fac =
615
- reinterpret_cast<const SignBitFactorsWithError*>(
616
- codes + code_size_base);
617
- const float est_distance =
618
- distance_to_code_1bit_impl(codes, base_fac, code_size_base);
619
701
 
620
- const bool should_refine = rabitq_utils::should_refine_candidate(
621
- est_distance,
622
- base_fac->f_error,
623
- g_error,
624
- handler.threshold,
625
- keep_max);
626
- if (should_refine) {
702
+ const auto* base_fac =
703
+ reinterpret_cast<const SignBitFactorsWithError*>(
704
+ codes + code_size_base);
705
+ float dis = distance_to_code_1bit_impl(
706
+ codes, base_fac, code_size_base);
707
+
708
+ if constexpr (decltype(multibit)::value) {
709
+ if (!rabitq_utils::should_refine_candidate(
710
+ dis,
711
+ base_fac->f_error,
712
+ g_error,
713
+ handler.threshold,
714
+ keep_max)) {
715
+ continue;
716
+ }
717
+ dis = distance_to_code_full(codes);
718
+ }
627
719
  handler.stats.scan_cnt++;
628
- const float dis = distance_to_code_full(codes);
629
720
  idx_t id = store_pairs ? lo_build(list_no, j) : ids[j];
630
721
 
631
- if (handler.add_result(dis, id)) {
722
+ if ((keep_max ? dis > handler.threshold
723
+ : dis < handler.threshold) &&
724
+ handler.add_result(dis, id)) {
632
725
  handler.stats.nheap_updates++;
633
726
  nup++;
634
727
  }
635
728
  }
636
- codes += code_size;
637
- }
638
-
639
- return nup;
729
+ return nup;
730
+ };
731
+ return nb_bits == 1 ? scan(std::false_type{}) : scan(std::true_type{});
640
732
  }
641
733
  };
642
734
 
@@ -653,12 +745,10 @@ FlatCodesDistanceComputer* RaBitQuantizer::get_distance_computer(
653
745
  // call the SIMD-specialized rabitq functions directly (no per-call
654
746
  // with_simd_level overhead).
655
747
  //
656
- // Use A0_SPR (which includes AVX512_SPR) so that on Sapphire Rapids
657
- // and later x86 microarchitectures the VPOPCNTDQ-based RaBitQ
658
- // specialization in rabitq_avx512_spr.cpp is selected. On AVX-512
659
- // CPUs without VPOPCNTDQ, dispatch falls through to the AVX512
660
- // specialization in rabitq_avx512.cpp.
661
- return with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_A0_SPR>(
748
+ // VPOPCNT rather than SPR: Ice Lake and Zen 4 have VPOPCNTDQ without the
749
+ // rest of the SPR feature set. Below it, dispatch falls through to
750
+ // rabitq_avx512.cpp.
751
+ return with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_WITH_VPOPCNT>(
662
752
  [&]<SIMDLevel SL>() -> FlatCodesDistanceComputer* {
663
753
  if (qb == 0) {
664
754
  auto dc =