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,89 @@
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
+ /* 8-bit product-quantizer code distance for RISC-V Vector.
11
+ *
12
+ * Free-standing (no exceptions, heap or threads), so a freestanding RISC-V
13
+ * target can compile it. pq_code_distance/rvv.cpp calls it; no Buck target
14
+ * here builds that RISC-V CPU path.
15
+ *
16
+ * PQDecoder8 fixes nbits = 8, so ksub = 256 and the scalar loop is
17
+ *
18
+ * for (m = 0; m < M; m++) result += sim_table[m * 256 + code[m]];
19
+ *
20
+ * That is a gather, not a contiguous read. RVV issues one vluxei32 per strip:
21
+ * build the element offset m * 256 + code[m] across the lanes, scale it to
22
+ * bytes, then index sim_table.
23
+ *
24
+ * The offset stays inside uint32 while M * 256 * 4 bytes does. At M = 1024
25
+ * that is 1 MiB, so no PQ shape in Faiss overflows it.
26
+ *
27
+ * The accumulator is LMUL 4, as in utils/simd_impl/distances_rvv_kernel.h.
28
+ * The lanes add the M terms in a different order than the scalar loop, so
29
+ * the last bits can differ.
30
+ */
31
+
32
+ #include <cstddef>
33
+ #include <cstdint>
34
+
35
+ #include <riscv_vector.h>
36
+
37
+ namespace faiss {
38
+ namespace pq_rvv_kernel {
39
+
40
+ /// Element offsets into sim_table for subquantizers [m, m + vl).
41
+ inline vuint32m4_t row_offsets(size_t m, size_t vl) {
42
+ // (m + lane) * 256
43
+ vuint32m4_t row = __riscv_vadd_vx_u32m4(__riscv_vid_v_u32m4(vl), m, vl);
44
+ return __riscv_vsll_vx_u32m4(row, 8, vl);
45
+ }
46
+
47
+ /// Byte offsets for one code, given the precomputed row offsets.
48
+ inline vuint32m4_t byte_offsets(
49
+ vuint32m4_t row_off,
50
+ const uint8_t* code,
51
+ size_t m,
52
+ size_t vl) {
53
+ vuint8m1_t cb = __riscv_vle8_v_u8m1(code + m, vl);
54
+ vuint32m4_t idx = __riscv_vzext_vf4_u32m4(cb, vl);
55
+ // (row + code) * sizeof(float)
56
+ return __riscv_vsll_vx_u32m4(
57
+ __riscv_vadd_vv_u32m4(row_off, idx, vl), 2, vl);
58
+ }
59
+
60
+ inline float reduce(vfloat32m4_t acc, size_t vlmax) {
61
+ vfloat32m1_t zero = __riscv_vfmv_v_f_f32m1(0.0f, 1);
62
+ vfloat32m1_t sum = __riscv_vfredusum_vs_f32m4_f32m1(acc, zero, vlmax);
63
+ return __riscv_vfmv_f_s_f32m1_f32(sum);
64
+ }
65
+
66
+ /// Distance between a query and one 8-bit PQ code.
67
+ /// @param sim_table M * 256 floats, laid out (M, ksub)
68
+ inline float distance_8bit(
69
+ size_t M,
70
+ const float* sim_table,
71
+ const uint8_t* code) {
72
+ const size_t vlmax = __riscv_vsetvlmax_e32m4();
73
+ vfloat32m4_t acc = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
74
+
75
+ size_t m = 0;
76
+ while (m < M) {
77
+ const size_t vl = __riscv_vsetvl_e32m4(M - m);
78
+ vuint32m4_t off = byte_offsets(row_offsets(m, vl), code, m, vl);
79
+ vfloat32m4_t v = __riscv_vluxei32_v_f32m4(sim_table, off, vl);
80
+ // Tail-undisturbed: lanes past vl keep earlier partial sums, and the
81
+ // final reduction covers vlmax.
82
+ acc = __riscv_vfadd_vv_f32m4_tu(acc, acc, v, vl);
83
+ m += vl;
84
+ }
85
+ return reduce(acc, vlmax);
86
+ }
87
+
88
+ } // namespace pq_rvv_kernel
89
+ } // namespace faiss
@@ -19,7 +19,7 @@ namespace pq_code_distance {
19
19
 
20
20
  namespace {
21
21
 
22
- template <class C>
22
+ template <class C, class PQCodeDist>
23
23
  void pq_scan_8bit_at_level(
24
24
  size_t M,
25
25
  const float* dis_table,
@@ -33,8 +33,9 @@ void pq_scan_8bit_at_level(
33
33
  size_t j = 0;
34
34
  for (; j + 3 < ncodes; j += 4) {
35
35
  float d0, d1, d2, d3;
36
- pq_code_distance_8bit_four_impl<THE_SIMD_LEVEL>( // NOLINT(facebook-modularize-issue-check)
36
+ PQCodeDist::distance_four_codes(
37
37
  M,
38
+ 8,
38
39
  dis_table,
39
40
  codes + j * code_size,
40
41
  codes + (j + 1) * code_size,
@@ -60,8 +61,8 @@ void pq_scan_8bit_at_level(
60
61
  }
61
62
 
62
63
  for (; j < ncodes; j++) {
63
- float dis = pq_code_distance_8bit_single_impl<THE_SIMD_LEVEL>( // NOLINT(facebook-modularize-issue-check)
64
- M, dis_table, codes + j * code_size);
64
+ float dis = PQCodeDist::distance_single_code(
65
+ M, 8, dis_table, codes + j * code_size);
65
66
  if (C::cmp(heap_dis[0], dis)) {
66
67
  heap_replace_top<C>(k, heap_dis, heap_ids, dis, j);
67
68
  }
@@ -92,13 +93,23 @@ void pq_scan_8bit_impl<THE_SIMD_LEVEL>(
92
93
  float* heap_dis,
93
94
  int64_t* heap_ids,
94
95
  bool max_heap) {
95
- if (max_heap) {
96
- pq_scan_8bit_at_level<CMax<float, int64_t>>(
97
- M, dis_table, codes, ncodes, k, heap_dis, heap_ids);
98
- } else {
99
- pq_scan_8bit_at_level<CMin<float, int64_t>>(
100
- M, dis_table, codes, ncodes, k, heap_dis, heap_ids);
96
+ auto scan = [&]<class PQCodeDist>() {
97
+ if (max_heap) {
98
+ pq_scan_8bit_at_level<CMax<float, int64_t>, PQCodeDist>(
99
+ M, dis_table, codes, ncodes, k, heap_dis, heap_ids);
100
+ } else {
101
+ pq_scan_8bit_at_level<CMin<float, int64_t>, PQCodeDist>(
102
+ M, dis_table, codes, ncodes, k, heap_dis, heap_ids);
103
+ }
104
+ };
105
+ if constexpr (has_scalar_8bit_kernel<THE_SIMD_LEVEL>()) {
106
+ if (use_scalar_8bit_kernel<THE_SIMD_LEVEL>(M)) {
107
+ scan.template
108
+ operator()<PQCodeDistanceScalar8Dist<THE_SIMD_LEVEL>>();
109
+ return;
110
+ }
101
111
  }
112
+ scan.template operator()<PQCodeDistance<PQDecoder8, THE_SIMD_LEVEL>>();
102
113
  }
103
114
 
104
115
  } // namespace pq_code_distance
@@ -5,14 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_RISCV_RVV
8
+ #include <riscv_vector.h>
9
9
 
10
10
  #include <faiss/impl/pq_code_distance/pq_code_distance-inl.h>
11
+ #include <faiss/impl/pq_code_distance/pq_rvv_kernel.h>
11
12
 
12
13
  namespace faiss {
13
14
  namespace pq_code_distance {
14
15
 
15
- // RISCV_RVV: no RVV-optimized PQ code distance exists yet. Use scalar.
16
+ // The kernel lives in pq_rvv_kernel.h. See that header for the gather layout.
16
17
 
17
18
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
18
19
  template <>
@@ -20,8 +21,7 @@ float pq_code_distance_8bit_single_impl<SIMDLevel::RISCV_RVV>(
20
21
  size_t M,
21
22
  const float* sim_table,
22
23
  const uint8_t* code) {
23
- return PQCodeDistanceScalar<PQDecoder8>::distance_single_code(
24
- M, 8, sim_table, code);
24
+ return pq_rvv_kernel::distance_8bit(M, sim_table, code);
25
25
  }
26
26
 
27
27
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
@@ -37,18 +37,47 @@ void pq_code_distance_8bit_four_impl<SIMDLevel::RISCV_RVV>(
37
37
  float& result1,
38
38
  float& result2,
39
39
  float& result3) {
40
- PQCodeDistanceScalar<PQDecoder8>::distance_four_codes(
41
- M,
42
- 8,
43
- sim_table,
44
- code0,
45
- code1,
46
- code2,
47
- code3,
48
- result0,
49
- result1,
50
- result2,
51
- result3);
40
+ const size_t vlmax = __riscv_vsetvlmax_e32m4();
41
+ vfloat32m4_t acc0 = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
42
+ vfloat32m4_t acc1 = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
43
+ vfloat32m4_t acc2 = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
44
+ vfloat32m4_t acc3 = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
45
+
46
+ size_t m = 0;
47
+ while (m < M) {
48
+ const size_t vl = __riscv_vsetvl_e32m4(M - m);
49
+ // The row term is the same for all four codes, so compute it once.
50
+ vuint32m4_t row_off = pq_rvv_kernel::row_offsets(m, vl);
51
+
52
+ vfloat32m4_t v0 = __riscv_vluxei32_v_f32m4(
53
+ sim_table,
54
+ pq_rvv_kernel::byte_offsets(row_off, code0, m, vl),
55
+ vl);
56
+ vfloat32m4_t v1 = __riscv_vluxei32_v_f32m4(
57
+ sim_table,
58
+ pq_rvv_kernel::byte_offsets(row_off, code1, m, vl),
59
+ vl);
60
+ vfloat32m4_t v2 = __riscv_vluxei32_v_f32m4(
61
+ sim_table,
62
+ pq_rvv_kernel::byte_offsets(row_off, code2, m, vl),
63
+ vl);
64
+ vfloat32m4_t v3 = __riscv_vluxei32_v_f32m4(
65
+ sim_table,
66
+ pq_rvv_kernel::byte_offsets(row_off, code3, m, vl),
67
+ vl);
68
+
69
+ acc0 = __riscv_vfadd_vv_f32m4_tu(acc0, acc0, v0, vl);
70
+ acc1 = __riscv_vfadd_vv_f32m4_tu(acc1, acc1, v1, vl);
71
+ acc2 = __riscv_vfadd_vv_f32m4_tu(acc2, acc2, v2, vl);
72
+ acc3 = __riscv_vfadd_vv_f32m4_tu(acc3, acc3, v3, vl);
73
+
74
+ m += vl;
75
+ }
76
+
77
+ result0 = pq_rvv_kernel::reduce(acc0, vlmax);
78
+ result1 = pq_rvv_kernel::reduce(acc1, vlmax);
79
+ result2 = pq_rvv_kernel::reduce(acc2, vlmax);
80
+ result3 = pq_rvv_kernel::reduce(acc3, vlmax);
52
81
  }
53
82
 
54
83
  } // namespace pq_code_distance
@@ -66,5 +95,3 @@ void pq_code_distance_8bit_four_impl<SIMDLevel::RISCV_RVV>(
66
95
  #include <faiss/impl/pq_code_distance/IVFPQScanner_impl.h>
67
96
 
68
97
  #undef THE_SIMD_LEVEL
69
-
70
- #endif // COMPILE_SIMD_RISCV_RVV
@@ -17,6 +17,7 @@
17
17
 
18
18
  #include <faiss/impl/approx_topk/approx_topk.h>
19
19
  #include <faiss/impl/approx_topk/rq_beam_search_tab.h>
20
+ #include <faiss/impl/platform_macros.h>
20
21
 
21
22
  extern "C" {
22
23
 
@@ -367,7 +368,7 @@ void beam_search_encode_step_tab(
367
368
 
368
369
  // Resolve SIMD level once, not per iteration of the n-parallel loop.
369
370
  with_simd_level_256bit([&]<SIMDLevel SL>() {
370
- #pragma omp parallel for if (n > 100) schedule(dynamic)
371
+ #pragma omp parallel for if (n > 100) schedule(FAISS_OMP_DYNAMIC)
371
372
  for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
372
373
  std::vector<float> cent_distances(beam_size * K);
373
374
  std::vector<float> cd_common(K);
@@ -8,8 +8,6 @@
8
8
  // AVX2 specialisation of Top1 add_results (8-wide branchless argmin/argmax).
9
9
  // Reservoir stays on the NONE path — VPCOMPRESSPS requires AVX512F.
10
10
 
11
- #ifdef COMPILE_SIMD_AVX2
12
-
13
11
  #include <faiss/impl/ResultHandler.h>
14
12
 
15
13
  #include <immintrin.h>
@@ -129,5 +127,3 @@ void top1_add_results_tpl<CMin<float, int64_t>, true, SIMDLevel::AVX2>(
129
127
  }
130
128
 
131
129
  } // namespace faiss
132
-
133
- #endif // COMPILE_SIMD_AVX2
@@ -11,8 +11,6 @@
11
11
  // Reservoir: VPCOMPRESSPS / VPCOMPRESSD bulk-insert of passing elements,
12
12
  // eliminating the per-element threshold branch entirely.
13
13
 
14
- #ifdef COMPILE_SIMD_AVX512
15
-
16
14
  #include <faiss/impl/ResultHandler.h>
17
15
  #include <faiss/utils/popcount.h>
18
16
 
@@ -277,5 +275,3 @@ void reservoir_add_results_tpl<CMin<float, int64_t>, true, SIMDLevel::AVX512>(
277
275
  }
278
276
 
279
277
  } // namespace faiss
280
-
281
- #endif // COMPILE_SIMD_AVX512
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_AVX2
9
-
10
8
  #include <faiss/impl/EDENQuantizer.h>
11
9
  #include <faiss/impl/simdlib/simdlib_avx2.h>
12
10
 
@@ -68,5 +66,3 @@ void compute_code_dot_lut_batch_8_avx2(
68
66
  } // namespace eden_distance
69
67
 
70
68
  } // namespace faiss
71
-
72
- #endif
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_AVX512
9
-
10
8
  #include <faiss/impl/EDENQuantizer.h>
11
9
  #include <faiss/impl/simdlib/simdlib_avx2.h>
12
10
  #include <faiss/impl/simdlib/simdlib_avx512.h>
@@ -224,5 +222,3 @@ void compute_code_dot_lut_batch_16_avx512(
224
222
  } // namespace eden_distance
225
223
 
226
224
  } // namespace faiss
227
-
228
- #endif
@@ -319,8 +319,13 @@ struct EDENDistanceComputerBase : EDENFlatCodesDistanceComputer {
319
319
  const EDENCodeFactors* factors,
320
320
  float code_dot_query) const {
321
321
  if (metric_type == MetricType::METRIC_L2) {
322
- return query_base + factors->l2_norm_term -
322
+ // The unbiased scale reports an unbiased estimator of the squared
323
+ // L2 distance rather than a true squared distance, so the raw
324
+ // value can dip below zero for strongly-aligned residuals. Clamp
325
+ // to keep the METRIC_L2 contract (distances are never negative).
326
+ const float dist = query_base + factors->l2_norm_term -
323
327
  2.0f * factors->scale * code_dot_query;
328
+ return dist < 0.0f ? 0.0f : dist;
324
329
  }
325
330
  return query_base + factors->scale * code_dot_query;
326
331
  }
@@ -22,7 +22,11 @@ using SQDistanceComputer = ScalarQuantizer::SQDistanceComputer;
22
22
  * code-to-vector or code-to-code comparisons
23
23
  *******************************************************************/
24
24
 
25
- template <class Quantizer, class Similarity, SIMDLevel SL>
25
+ template <
26
+ class Quantizer,
27
+ class Similarity,
28
+ SIMDLevel SL,
29
+ bool four_acc = false>
26
30
  struct DCTemplate : SQDistanceComputer {};
27
31
 
28
32
  template <class Quantizer, class Similarity>
@@ -78,8 +82,8 @@ template <class Similarity, SIMDLevel SL>
78
82
  struct DistanceComputerByte : SQDistanceComputer {};
79
83
 
80
84
  // Byte-domain distance computer for QT_8bit_direct_signed (storage is
81
- // value+128). Only specialized for AVX512_SPR; other levels fall back to
82
- // the float-domain DCTemplate path via the dispatch logic.
85
+ // value+128). A level added to the sq-dispatch.h chain without a
86
+ // specialization here instantiates this empty template.
83
87
  template <class Similarity, SIMDLevel SL>
84
88
  struct DistanceComputerByteSigned : SQDistanceComputer {};
85
89
 
@@ -149,11 +149,11 @@ struct QuantizerTemplate<
149
149
  /*******************************************************************
150
150
  * Lloyd-Max scalar quantizer
151
151
  *******************************************************************/
152
+ // Each QuantizerXBase<SL> holds the scalar code of a quantizer. It takes the
153
+ // SIMD level of the file that uses it, so each per-level file compiles its own
154
+ // copy under its own name.
152
155
  template <int NBits, SIMDLevel SL>
153
- struct QuantizerLloydMax;
154
-
155
- template <int NBits>
156
- struct QuantizerLloydMax<NBits, SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
156
+ struct QuantizerLloydMaxBase : ScalarQuantizer::SQuantizer {
157
157
  static_assert(NBits >= 1 && NBits <= 8);
158
158
 
159
159
  static constexpr size_t kCentroidsCount = size_t(1) << NBits;
@@ -164,7 +164,7 @@ struct QuantizerLloydMax<NBits, SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
164
164
  const float* centroids;
165
165
  const float* boundaries;
166
166
 
167
- QuantizerLloydMax(size_t d_in, const std::vector<float>& trained)
167
+ QuantizerLloydMaxBase(size_t d_in, const std::vector<float>& trained)
168
168
  : d(d_in), centroids(nullptr), boundaries(nullptr) {
169
169
  FAISS_THROW_IF_NOT(trained.size() == 2 * kCentroidsCount - 1);
170
170
  centroids = trained.data();
@@ -220,8 +220,8 @@ struct QuantizerLloydMax<NBits, SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
220
220
  };
221
221
 
222
222
  template <int NBits, SIMDLevel SL>
223
- struct QuantizerLloydMax : QuantizerLloydMax<NBits, SIMDLevel::NONE> {
224
- using QuantizerLloydMax<NBits, SIMDLevel::NONE>::QuantizerLloydMax;
223
+ struct QuantizerLloydMax : QuantizerLloydMaxBase<NBits, SL> {
224
+ using QuantizerLloydMaxBase<NBits, SL>::QuantizerLloydMaxBase;
225
225
  };
226
226
 
227
227
  template <int NBits, SIMDLevel SL>
@@ -232,13 +232,10 @@ using QuantizerTurboQuantMSE = QuantizerLloydMax<NBits, SL>;
232
232
  *******************************************************************/
233
233
 
234
234
  template <SIMDLevel SL>
235
- struct QuantizerFP16;
236
-
237
- template <>
238
- struct QuantizerFP16<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
235
+ struct QuantizerFP16Base : ScalarQuantizer::SQuantizer {
239
236
  const size_t d;
240
237
 
241
- QuantizerFP16(size_t d_in, const std::vector<float>& /* unused */)
238
+ QuantizerFP16Base(size_t d_in, const std::vector<float>& /* unused */)
242
239
  : d(d_in) {}
243
240
 
244
241
  void encode_vector(const float* x, uint8_t* code) const final {
@@ -247,7 +244,7 @@ struct QuantizerFP16<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
247
244
  }
248
245
  }
249
246
 
250
- void decode_vector(const uint8_t* code, float* x) const final {
247
+ void decode_vector(const uint8_t* code, float* x) const override {
251
248
  for (size_t i = 0; i < d; i++) {
252
249
  x[i] = decode_fp16(((uint16_t*)code)[i]);
253
250
  }
@@ -261,8 +258,8 @@ struct QuantizerFP16<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
261
258
  };
262
259
 
263
260
  template <SIMDLevel SL>
264
- struct QuantizerFP16 : QuantizerFP16<SIMDLevel::NONE> {
265
- using QuantizerFP16<SIMDLevel::NONE>::QuantizerFP16;
261
+ struct QuantizerFP16 : QuantizerFP16Base<SL> {
262
+ using QuantizerFP16Base<SL>::QuantizerFP16Base;
266
263
  };
267
264
 
268
265
  /*******************************************************************
@@ -270,13 +267,10 @@ struct QuantizerFP16 : QuantizerFP16<SIMDLevel::NONE> {
270
267
  *******************************************************************/
271
268
 
272
269
  template <SIMDLevel SL>
273
- struct QuantizerBF16;
274
-
275
- template <>
276
- struct QuantizerBF16<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
270
+ struct QuantizerBF16Base : ScalarQuantizer::SQuantizer {
277
271
  const size_t d;
278
272
 
279
- QuantizerBF16(size_t d_in, const std::vector<float>& /* unused */)
273
+ QuantizerBF16Base(size_t d_in, const std::vector<float>& /* unused */)
280
274
  : d(d_in) {}
281
275
 
282
276
  void encode_vector(const float* x, uint8_t* code) const override {
@@ -295,8 +289,8 @@ struct QuantizerBF16<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
295
289
  };
296
290
 
297
291
  template <SIMDLevel SL>
298
- struct QuantizerBF16 : QuantizerBF16<SIMDLevel::NONE> {
299
- using QuantizerBF16<SIMDLevel::NONE>::QuantizerBF16;
292
+ struct QuantizerBF16 : QuantizerBF16Base<SL> {
293
+ using QuantizerBF16Base<SL>::QuantizerBF16Base;
300
294
  };
301
295
 
302
296
  template <>
@@ -309,22 +303,19 @@ struct QuantizerBF16<SIMDLevel::AVX512_SPR>;
309
303
  *******************************************************************/
310
304
 
311
305
  template <SIMDLevel SL>
312
- struct Quantizer8bitDirect;
313
-
314
- template <>
315
- struct Quantizer8bitDirect<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
306
+ struct Quantizer8bitDirectBase : ScalarQuantizer::SQuantizer {
316
307
  const size_t d;
317
308
 
318
- Quantizer8bitDirect(size_t d_in, const std::vector<float>& /* unused */)
309
+ Quantizer8bitDirectBase(size_t d_in, const std::vector<float>& /* unused */)
319
310
  : d(d_in) {}
320
311
 
321
- void encode_vector(const float* x, uint8_t* code) const final {
312
+ void encode_vector(const float* x, uint8_t* code) const override {
322
313
  for (size_t i = 0; i < d; i++) {
323
314
  code[i] = (uint8_t)x[i];
324
315
  }
325
316
  }
326
317
 
327
- void decode_vector(const uint8_t* code, float* x) const final {
318
+ void decode_vector(const uint8_t* code, float* x) const override {
328
319
  for (size_t i = 0; i < d; i++) {
329
320
  x[i] = code[i];
330
321
  }
@@ -338,8 +329,8 @@ struct Quantizer8bitDirect<SIMDLevel::NONE> : ScalarQuantizer::SQuantizer {
338
329
  };
339
330
 
340
331
  template <SIMDLevel SL>
341
- struct Quantizer8bitDirect : Quantizer8bitDirect<SIMDLevel::NONE> {
342
- using Quantizer8bitDirect<SIMDLevel::NONE>::Quantizer8bitDirect;
332
+ struct Quantizer8bitDirect : Quantizer8bitDirectBase<SL> {
333
+ using Quantizer8bitDirectBase<SL>::Quantizer8bitDirectBase;
343
334
  };
344
335
 
345
336
  /*******************************************************************
@@ -347,14 +338,10 @@ struct Quantizer8bitDirect : Quantizer8bitDirect<SIMDLevel::NONE> {
347
338
  *******************************************************************/
348
339
 
349
340
  template <SIMDLevel SL>
350
- struct Quantizer8bitDirectSigned;
351
-
352
- template <>
353
- struct Quantizer8bitDirectSigned<SIMDLevel::NONE>
354
- : ScalarQuantizer::SQuantizer {
341
+ struct Quantizer8bitDirectSignedBase : ScalarQuantizer::SQuantizer {
355
342
  const size_t d;
356
343
 
357
- Quantizer8bitDirectSigned(
344
+ Quantizer8bitDirectSignedBase(
358
345
  size_t d_in,
359
346
  const std::vector<float>& /* unused */)
360
347
  : d(d_in) {}
@@ -365,7 +352,7 @@ struct Quantizer8bitDirectSigned<SIMDLevel::NONE>
365
352
  }
366
353
  }
367
354
 
368
- void decode_vector(const uint8_t* code, float* x) const final {
355
+ void decode_vector(const uint8_t* code, float* x) const override {
369
356
  for (size_t i = 0; i < d; i++) {
370
357
  x[i] = code[i] - 128;
371
358
  }
@@ -379,8 +366,8 @@ struct Quantizer8bitDirectSigned<SIMDLevel::NONE>
379
366
  };
380
367
 
381
368
  template <SIMDLevel SL>
382
- struct Quantizer8bitDirectSigned : Quantizer8bitDirectSigned<SIMDLevel::NONE> {
383
- using Quantizer8bitDirectSigned<SIMDLevel::NONE>::Quantizer8bitDirectSigned;
369
+ struct Quantizer8bitDirectSigned : Quantizer8bitDirectSignedBase<SL> {
370
+ using Quantizer8bitDirectSignedBase<SL>::Quantizer8bitDirectSignedBase;
384
371
  };
385
372
 
386
373
  /*******************************************************************
@@ -403,11 +390,7 @@ struct SQTurboQFactors {
403
390
  FAISS_PACK_STRUCTS_END
404
391
 
405
392
  template <int NBits, SIMDLevel SL>
406
- struct QuantizerTurboQuantFull;
407
-
408
- template <int NBits>
409
- struct QuantizerTurboQuantFull<NBits, SIMDLevel::NONE>
410
- : ScalarQuantizer::SQuantizer {
393
+ struct QuantizerTurboQuantFull : ScalarQuantizer::SQuantizer {
411
394
  static_assert(NBits >= 2 && NBits <= 5);
412
395
 
413
396
  static constexpr int kMSEBits = NBits - 1;
@@ -658,13 +641,6 @@ struct QuantizerTurboQuantFull<NBits, SIMDLevel::NONE>
658
641
  }
659
642
  };
660
643
 
661
- template <int NBits, SIMDLevel SL>
662
- struct QuantizerTurboQuantFull
663
- : QuantizerTurboQuantFull<NBits, SIMDLevel::NONE> {
664
- using QuantizerTurboQuantFull<NBits, SIMDLevel::NONE>::
665
- QuantizerTurboQuantFull;
666
- };
667
-
668
644
  /*******************************************************************
669
645
  * Selection function
670
646
  *******************************************************************/
@@ -35,6 +35,19 @@ namespace scalar_quantizer {
35
35
  using QuantizerType = ScalarQuantizer::QuantizerType;
36
36
  using SQDistanceComputer = ScalarQuantizer::SQDistanceComputer;
37
37
 
38
+ // True when query_to_codes_batch_4 matches four query_to_code calls. The SIMD
39
+ // DCTemplate batch differs from the single path only for the uniform
40
+ // quantizers, whose single path predecodes the query, so those stay on the
41
+ // scalar scan and batched results never differ from scalar.
42
+ template <class DC>
43
+ constexpr bool sq_batch_4_is_exact() {
44
+ if constexpr (requires { DC::has_decode_raw(); }) {
45
+ return !DC::has_decode_raw();
46
+ } else {
47
+ return true;
48
+ }
49
+ }
50
+
38
51
  /*******************************************************************
39
52
  * IVFSQScannerIP / IVFSQScannerL2 — moved from anonymous namespace
40
53
  * in ScalarQuantizer.cpp
@@ -74,13 +87,35 @@ struct IVFSQScannerIP : InvertedListScanner {
74
87
  return accu0 + dc.query_to_code(code);
75
88
  }
76
89
 
90
+ void distance_to_codes_batch_4(
91
+ const uint8_t* code_0,
92
+ const uint8_t* code_1,
93
+ const uint8_t* code_2,
94
+ const uint8_t* code_3,
95
+ float& dis0,
96
+ float& dis1,
97
+ float& dis2,
98
+ float& dis3) const {
99
+ dc.query_to_codes_batch_4(
100
+ code_0, code_1, code_2, code_3, dis0, dis1, dis2, dis3);
101
+ dis0 += accu0;
102
+ dis1 += accu0;
103
+ dis2 += accu0;
104
+ dis3 += accu0;
105
+ }
106
+
77
107
  size_t scan_codes(
78
108
  size_t list_size,
79
109
  const uint8_t* codes,
80
110
  const idx_t* ids,
81
111
  ResultHandler& handler) const override {
82
- return run_scan_codes_fix_C<CMin<float, idx_t>>(
83
- *this, list_size, codes, ids, handler);
112
+ if constexpr (sq_batch_4_is_exact<DCClass>()) {
113
+ return run_scan_codes4_fix_C<CMin<float, idx_t>>(
114
+ *this, list_size, codes, ids, handler);
115
+ } else {
116
+ return run_scan_codes_fix_C<CMin<float, idx_t>>(
117
+ *this, list_size, codes, ids, handler);
118
+ }
84
119
  }
85
120
  };
86
121
 
@@ -133,13 +168,31 @@ struct IVFSQScannerL2 : InvertedListScanner {
133
168
  return dc.query_to_code(code);
134
169
  }
135
170
 
171
+ void distance_to_codes_batch_4(
172
+ const uint8_t* code_0,
173
+ const uint8_t* code_1,
174
+ const uint8_t* code_2,
175
+ const uint8_t* code_3,
176
+ float& dis0,
177
+ float& dis1,
178
+ float& dis2,
179
+ float& dis3) const {
180
+ dc.query_to_codes_batch_4(
181
+ code_0, code_1, code_2, code_3, dis0, dis1, dis2, dis3);
182
+ }
183
+
136
184
  size_t scan_codes(
137
185
  size_t list_size,
138
186
  const uint8_t* codes,
139
187
  const idx_t* ids,
140
188
  ResultHandler& handler) const override {
141
- return run_scan_codes_fix_C<CMax<float, idx_t>>(
142
- *this, list_size, codes, ids, handler);
189
+ if constexpr (sq_batch_4_is_exact<DCClass>()) {
190
+ return run_scan_codes4_fix_C<CMax<float, idx_t>>(
191
+ *this, list_size, codes, ids, handler);
192
+ } else {
193
+ return run_scan_codes_fix_C<CMax<float, idx_t>>(
194
+ *this, list_size, codes, ids, handler);
195
+ }
143
196
  }
144
197
  };
145
198
 
@@ -175,14 +228,9 @@ struct IVFCoarseDistanceScanner : InvertedListScanner {
175
228
 
176
229
  void set_query(const float* /*query_vector*/) override {}
177
230
 
178
- void set_list(idx_t list_no_in, float coarse_dis_in) override {
179
- this->list_no = list_no_in;
180
- this->coarse_dis = coarse_dis_in;
181
- }
231
+ void set_list(idx_t list_no_in, float coarse_dis_in) override;
182
232
 
183
- float distance_to_code(const uint8_t* /*code*/) const override {
184
- return coarse_dis;
185
- }
233
+ float distance_to_code(const uint8_t* /*code*/) const override;
186
234
  };
187
235
 
188
236
  } // namespace scalar_quantizer