faiss 0.6.4 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/ext/faiss/extconf.rb +2 -1
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  7. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  8. data/vendor/faiss/faiss/Clustering.h +29 -0
  9. data/vendor/faiss/faiss/IVFlib.cpp +147 -51
  10. data/vendor/faiss/faiss/IVFlib.h +21 -2
  11. data/vendor/faiss/faiss/Index.cpp +44 -0
  12. data/vendor/faiss/faiss/Index.h +18 -13
  13. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  14. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  15. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +85 -4
  17. data/vendor/faiss/faiss/IndexBinaryHNSW.h +19 -0
  18. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -11
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +34 -18
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +57 -1
  22. data/vendor/faiss/faiss/IndexFlat.h +31 -0
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +461 -13
  25. data/vendor/faiss/faiss/IndexHNSW.h +40 -5
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +45 -0
  27. data/vendor/faiss/faiss/IndexIDMap.h +15 -3
  28. data/vendor/faiss/faiss/IndexIVF.cpp +349 -68
  29. data/vendor/faiss/faiss/IndexIVF.h +90 -2
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +51 -29
  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 +22 -6
  34. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +2 -1
  35. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  36. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +78 -58
  37. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +7 -13
  38. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  39. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +860 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  46. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  47. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +7 -4
  48. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +12 -6
  49. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  50. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  51. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  52. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  53. data/vendor/faiss/faiss/IndexShards.cpp +10 -0
  54. data/vendor/faiss/faiss/IndexShards.h +5 -0
  55. data/vendor/faiss/faiss/SuperKMeans.cpp +59 -17
  56. data/vendor/faiss/faiss/SuperKMeans.h +9 -4
  57. data/vendor/faiss/faiss/VectorTransform.cpp +159 -11
  58. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  59. data/vendor/faiss/faiss/clone_index.cpp +24 -0
  60. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  61. data/vendor/faiss/faiss/gpu/Dataset.h +164 -0
  62. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  63. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  64. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +3 -0
  65. data/vendor/faiss/faiss/gpu/GpuIndex.h +4 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +32 -13
  67. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +51 -22
  68. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  69. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  70. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  71. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  72. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  73. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  74. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  75. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  76. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  77. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  78. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  79. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +12 -1
  80. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +93 -1
  81. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  82. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +5 -1
  83. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  84. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  85. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  86. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +39 -1
  87. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +107 -1
  88. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +11 -1
  89. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  90. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  91. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  92. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  93. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +3 -1
  94. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  95. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  96. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  97. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +267 -136
  98. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  99. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  100. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  101. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  102. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  103. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  104. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  105. data/vendor/faiss/faiss/impl/HNSW.cpp +48 -12
  106. data/vendor/faiss/faiss/impl/HNSW.h +74 -2
  107. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  108. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  109. data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -1
  110. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  111. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  112. data/vendor/faiss/faiss/impl/Panorama.h +3 -2
  113. data/vendor/faiss/faiss/impl/PiPNN.cpp +824 -0
  114. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  115. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  116. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +54 -11
  117. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  118. data/vendor/faiss/faiss/impl/RaBitQUtils.h +30 -1
  119. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +91 -66
  120. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +15 -0
  121. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  122. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -0
  123. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +1 -6
  124. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  125. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  127. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  128. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  129. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  130. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -0
  133. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  134. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  135. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  136. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +17 -129
  137. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  138. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +73 -6
  139. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  140. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -4
  141. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -4
  142. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +10 -85
  143. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  144. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  145. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +106 -1
  146. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -6
  147. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  148. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +105 -78
  149. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  150. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  151. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  152. data/vendor/faiss/faiss/impl/index_read.cpp +318 -9
  153. data/vendor/faiss/faiss/impl/index_write.cpp +124 -5
  154. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  155. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  156. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  157. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  158. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  159. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  160. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  161. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  162. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  163. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  164. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  165. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  166. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  167. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  168. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  169. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  170. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  171. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  172. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  173. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  174. data/vendor/faiss/faiss/impl/platform_macros.h +91 -27
  175. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  176. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +84 -4
  177. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  178. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  179. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  180. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  181. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  182. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  183. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  184. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +19 -0
  185. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  186. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  187. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  188. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  189. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  190. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  191. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  192. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  193. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -6
  194. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -5
  195. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  196. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  197. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  198. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +5 -1
  199. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  200. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +85 -37
  201. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +262 -110
  202. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +503 -122
  203. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +10 -155
  204. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +7 -701
  205. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +66 -57
  206. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +133 -127
  207. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  208. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  209. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  210. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  211. data/vendor/faiss/faiss/impl/simd_dispatch.h +7 -0
  212. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  213. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  214. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  215. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  216. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  217. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  218. data/vendor/faiss/faiss/index_factory.cpp +106 -6
  219. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  220. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  221. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  222. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +15 -10
  223. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  224. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  225. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  226. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  227. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  228. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  229. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  230. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  231. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  232. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +3 -2
  233. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  234. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  235. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  236. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  237. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  238. data/vendor/faiss/faiss/utils/distances.cpp +466 -43
  239. data/vendor/faiss/faiss/utils/distances.h +179 -2
  240. data/vendor/faiss/faiss/utils/distances_dispatch.h +6 -4
  241. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  242. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +0 -14
  243. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  244. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  247. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  248. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  249. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  250. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  251. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  252. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  253. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +0 -4
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +33 -5
  256. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +15 -30
  257. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  258. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +2 -1
  259. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  260. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  261. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  262. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  263. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  264. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  265. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  266. data/vendor/faiss/faiss/utils/rabitq_simd.h +171 -22
  267. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  268. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +81 -9
  269. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  270. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +108 -9
  271. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +111 -12
  272. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +353 -71
  273. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  274. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  275. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +106 -0
  276. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  277. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  278. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  279. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  280. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  281. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  282. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  283. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  284. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  285. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  286. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +185 -51
  287. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +194 -66
  288. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512_vpopcnt.cpp +51 -14
  289. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  290. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  291. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  292. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  293. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +0 -4
  294. data/vendor/faiss/faiss/utils/simd_levels.cpp +22 -6
  295. data/vendor/faiss/faiss/utils/simd_levels.h +74 -1
  296. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  297. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  298. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  299. data/vendor/faiss/faiss/utils/utils.cpp +84 -0
  300. data/vendor/faiss/faiss/utils/utils.h +24 -0
  301. metadata +52 -1
@@ -37,9 +37,9 @@ struct simd512bit_tpl<SIMDLevel::AVX512> {
37
37
 
38
38
  simd512bit_tpl() {}
39
39
 
40
- explicit simd512bit_tpl(__m512i i) : i(i) {}
40
+ explicit simd512bit_tpl(__m512i val) : i(val) {}
41
41
 
42
- explicit simd512bit_tpl(__m512 f) : f(f) {}
42
+ explicit simd512bit_tpl(__m512 val) : f(val) {}
43
43
 
44
44
  explicit simd512bit_tpl(const void* x)
45
45
  : i(_mm512_loadu_si512((__m512i const*)x)) {}
@@ -79,8 +79,8 @@ struct simd512bit_tpl<SIMDLevel::AVX512> {
79
79
  void bin(char bits[513]) const {
80
80
  char bytes[64];
81
81
  storeu((void*)bytes);
82
- for (int i = 0; i < 512; i++) {
83
- bits[i] = '0' + ((bytes[i / 8] >> (i % 8)) & 1);
82
+ for (int idx = 0; idx < 512; idx++) {
83
+ bits[idx] = '0' + ((bytes[idx / 8] >> (idx % 8)) & 1);
84
84
  }
85
85
  bits[512] = 0;
86
86
  }
@@ -97,8 +97,8 @@ template <>
97
97
  struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
98
98
  simd32uint16_tpl() {}
99
99
 
100
- explicit simd32uint16_tpl(__m512i i)
101
- : simd512bit_tpl<SIMDLevel::AVX512>(i) {}
100
+ explicit simd32uint16_tpl(__m512i val)
101
+ : simd512bit_tpl<SIMDLevel::AVX512>(val) {}
102
102
 
103
103
  explicit simd32uint16_tpl(int x)
104
104
  : simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_epi16(x)) {}
@@ -131,9 +131,9 @@ struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
131
131
  #pragma GCC diagnostic push
132
132
  #pragma GCC diagnostic ignored "-Wformat-nonliteral"
133
133
  #endif
134
- for (int i = 0; i < 32; i++) {
134
+ for (int idx = 0; idx < 32; idx++) {
135
135
  ptr += snprintf(
136
- ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[i]);
136
+ ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
137
137
  }
138
138
  #if defined(__GNUC__) || defined(__clang__)
139
139
  #pragma GCC diagnostic pop
@@ -213,10 +213,10 @@ struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
213
213
  }
214
214
 
215
215
  // for debugging only
216
- uint16_t operator[](int i) const {
216
+ uint16_t operator[](int idx) const {
217
217
  ALIGNED(64) uint16_t tab[32];
218
218
  store(tab);
219
- return tab[i];
219
+ return tab[idx];
220
220
  }
221
221
 
222
222
  void accu_min(simd32uint16_tpl incoming) {
@@ -241,8 +241,8 @@ template <>
241
241
  struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
242
242
  simd64uint8_tpl() {}
243
243
 
244
- explicit simd64uint8_tpl(__m512i i)
245
- : simd512bit_tpl<SIMDLevel::AVX512>(i) {}
244
+ explicit simd64uint8_tpl(__m512i val)
245
+ : simd512bit_tpl<SIMDLevel::AVX512>(val) {}
246
246
 
247
247
  explicit simd64uint8_tpl(int x)
248
248
  : simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_epi8(x)) {}
@@ -275,9 +275,9 @@ struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
275
275
  #pragma GCC diagnostic push
276
276
  #pragma GCC diagnostic ignored "-Wformat-nonliteral"
277
277
  #endif
278
- for (int i = 0; i < 64; i++) {
278
+ for (int idx = 0; idx < 64; idx++) {
279
279
  ptr += snprintf(
280
- ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[i]);
280
+ ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
281
281
  }
282
282
  #if defined(__GNUC__) || defined(__clang__)
283
283
  #pragma GCC diagnostic pop
@@ -329,10 +329,10 @@ struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
329
329
  }
330
330
 
331
331
  // for debugging only
332
- uint8_t operator[](int i) const {
332
+ uint8_t operator[](int idx) const {
333
333
  ALIGNED(64) uint8_t tab[64];
334
334
  store(tab);
335
- return tab[i];
335
+ return tab[idx];
336
336
  }
337
337
  };
338
338
 
@@ -342,8 +342,8 @@ struct simd16float32_tpl<SIMDLevel::AVX512>
342
342
  : simd512bit_tpl<SIMDLevel::AVX512> {
343
343
  simd16float32_tpl() {}
344
344
 
345
- explicit simd16float32_tpl(__m512 f)
346
- : simd512bit_tpl<SIMDLevel::AVX512>(f) {}
345
+ explicit simd16float32_tpl(__m512 val)
346
+ : simd512bit_tpl<SIMDLevel::AVX512>(val) {}
347
347
 
348
348
  explicit simd16float32_tpl(float x)
349
349
  : simd512bit_tpl<SIMDLevel::AVX512>(_mm512_set1_ps(x)) {}
@@ -389,9 +389,9 @@ struct simd16float32_tpl<SIMDLevel::AVX512>
389
389
  storeu(tab);
390
390
  char res[1000];
391
391
  char* ptr = res;
392
- for (int i = 0; i < 16; i++) {
392
+ for (int idx = 0; idx < 16; idx++) {
393
393
  ptr += snprintf(
394
- ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[i]);
394
+ ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[idx]);
395
395
  }
396
396
  ptr[-1] = 0;
397
397
  return std::string(res);
@@ -16,23 +16,22 @@
16
16
  */
17
17
 
18
18
  #include <faiss/impl/simdlib/simdlib.h>
19
+ #include <faiss/utils/simd_levels.h>
19
20
 
20
- // Platform specializations — guarded by COMPILE_SIMD_* AND compiler macros.
21
- // In DD mode: COMPILE_SIMD_* are target-wide, compiler macros are per-file.
22
- // Only per-SIMD TUs (compiled with -mavx2 etc.) see the platform
23
- // specializations. In static mode: only the compiled-in level is available.
21
+ // Platform specializations — guarded by FAISS_SIMD_TU, so that only per-SIMD
22
+ // TUs (compiled with -mavx2 etc.) see them. In static mode: only the
23
+ // compiled-in level is available.
24
24
 
25
- #if (defined(COMPILE_SIMD_AVX512) || defined(COMPILE_SIMD_AVX512_SPR)) && \
26
- defined(__AVX512F__)
25
+ #if FAISS_SIMD_TU(AVX512) || FAISS_SIMD_TU(AVX512_SPR)
27
26
 
28
27
  // AVX512 includes AVX2 (simdlib_avx512.h includes simdlib_avx2.h)
29
28
  #include <faiss/impl/simdlib/simdlib_avx512.h>
30
29
 
31
- #elif defined(COMPILE_SIMD_AVX2) && defined(__AVX2__)
30
+ #elif FAISS_SIMD_TU(AVX2)
32
31
 
33
32
  #include <faiss/impl/simdlib/simdlib_avx2.h>
34
33
 
35
- #elif defined(COMPILE_SIMD_ARM_NEON) && defined(__aarch64__)
34
+ #elif FAISS_SIMD_TU(ARM_NEON)
36
35
 
37
36
  #include <faiss/impl/simdlib/simdlib_neon.h>
38
37
 
@@ -1376,7 +1376,7 @@ inline void cmplt_min_max_fast(
1376
1376
  namespace {
1377
1377
 
1378
1378
  // get even float32's of a and b, interleaved
1379
- simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
1379
+ inline simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
1380
1380
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
1381
1381
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
1382
1382
  return simd8float32_tpl<SIMDLevel::ARM_NEON>{
@@ -1384,7 +1384,7 @@ simd8float32_tpl<SIMDLevel::ARM_NEON> geteven(
1384
1384
  }
1385
1385
 
1386
1386
  // get odd float32's of a and b, interleaved
1387
- simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
1387
+ inline simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
1388
1388
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
1389
1389
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
1390
1390
  return simd8float32_tpl<SIMDLevel::ARM_NEON>{
@@ -1393,14 +1393,14 @@ simd8float32_tpl<SIMDLevel::ARM_NEON> getodd(
1393
1393
 
1394
1394
  // 3 cycles
1395
1395
  // if the lanes are a = [a0 a1] and b = [b0 b1], return [a0 b0]
1396
- simd8float32_tpl<SIMDLevel::ARM_NEON> getlow128(
1396
+ inline simd8float32_tpl<SIMDLevel::ARM_NEON> getlow128(
1397
1397
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
1398
1398
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
1399
1399
  return simd8float32_tpl<SIMDLevel::ARM_NEON>{
1400
1400
  float32x4x2_t{a.data.val[0], b.data.val[0]}};
1401
1401
  }
1402
1402
 
1403
- simd8float32_tpl<SIMDLevel::ARM_NEON> gethigh128(
1403
+ inline simd8float32_tpl<SIMDLevel::ARM_NEON> gethigh128(
1404
1404
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& a,
1405
1405
  const simd8float32_tpl<SIMDLevel::ARM_NEON>& b) {
1406
1406
  return simd8float32_tpl<SIMDLevel::ARM_NEON>{
@@ -0,0 +1,232 @@
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 <algorithm>
11
+ #include <cstring>
12
+
13
+ #include <faiss/impl/ScalarQuantizer.h>
14
+ #include <faiss/utils/hamming.h>
15
+
16
+ /** Helpers shared by IndexSQFastScan and IndexIVFSQFastScan.
17
+ *
18
+ * Both re-quantise a higher-precision scalar quantizer down to 4-bit nibbles
19
+ * for the SIMD scan and keep the original codes to rerank with, so the
20
+ * classification of quantizer types and the nibble packing have to agree
21
+ * between them.
22
+ */
23
+
24
+ namespace faiss {
25
+ namespace sq_fastscan {
26
+
27
+ inline size_t roundup(size_t a, size_t b) {
28
+ return (a + b - 1) / b * b;
29
+ }
30
+
31
+ inline bool is_native_4bit(ScalarQuantizer::QuantizerType qtype) {
32
+ return qtype == ScalarQuantizer::QT_4bit ||
33
+ qtype == ScalarQuantizer::QT_4bit_uniform;
34
+ }
35
+
36
+ inline bool needs_rerank(ScalarQuantizer::QuantizerType qtype) {
37
+ return qtype == ScalarQuantizer::QT_6bit ||
38
+ qtype == ScalarQuantizer::QT_8bit ||
39
+ qtype == ScalarQuantizer::QT_8bit_uniform ||
40
+ qtype == ScalarQuantizer::QT_8bit_direct ||
41
+ qtype == ScalarQuantizer::QT_8bit_direct_signed;
42
+ }
43
+
44
+ inline bool is_fallback(ScalarQuantizer::QuantizerType qtype) {
45
+ return !is_native_4bit(qtype) && !needs_rerank(qtype);
46
+ }
47
+
48
+ inline bool is_uniform_range(ScalarQuantizer::QuantizerType qtype) {
49
+ return qtype == ScalarQuantizer::QT_8bit_uniform ||
50
+ qtype == ScalarQuantizer::QT_8bit_direct ||
51
+ qtype == ScalarQuantizer::QT_8bit_direct_signed;
52
+ }
53
+
54
+ inline void get_uniform_range(
55
+ const ScalarQuantizer& sq,
56
+ float& vmin,
57
+ float& vdiff) {
58
+ if (sq.qtype == ScalarQuantizer::QT_8bit_direct) {
59
+ vmin = 0;
60
+ vdiff = 255;
61
+ } else if (sq.qtype == ScalarQuantizer::QT_8bit_direct_signed) {
62
+ vmin = -128;
63
+ vdiff = 255;
64
+ } else {
65
+ vmin = sq.trained[0];
66
+ vdiff = sq.trained[1];
67
+ }
68
+ }
69
+
70
+ /// Bits per dimension the quantizer stores.
71
+ inline int sq_bits(ScalarQuantizer::QuantizerType qtype) {
72
+ if (qtype == ScalarQuantizer::QT_6bit) {
73
+ return 6;
74
+ }
75
+ return is_native_4bit(qtype) ? 4 : 8;
76
+ }
77
+
78
+ /// Bits left over once the scan has taken its 4. Zero for a native 4-bit
79
+ /// code, which needs no rerank and therefore stores nothing extra.
80
+ inline int sq_rerank_bits(ScalarQuantizer::QuantizerType qtype) {
81
+ return sq_bits(qtype) - 4;
82
+ }
83
+
84
+ /// Bytes per vector needed to hold the leftover bits of every dimension.
85
+ inline size_t sq_rerank_size(int d, ScalarQuantizer::QuantizerType qtype) {
86
+ const int bits = sq_rerank_bits(qtype);
87
+ return bits > 0 ? (size_t(d) * bits + 7) / 8 : 0;
88
+ }
89
+
90
+ /// The code for one value at the quantizer's own width.
91
+ inline int sq_code_of(float x, float vmin, float vdiff, int bits) {
92
+ const int maxv = (1 << bits) - 1;
93
+ const float f = (vdiff > 0) ? (x - vmin) / vdiff : 0.0f;
94
+ return std::min(maxv, std::max(0, int(f * float(maxv))));
95
+ }
96
+
97
+ /// Per-dimension range of the trained quantizer, resolved once for a batch.
98
+ struct SQRanges {
99
+ SQRanges(const ScalarQuantizer& sq, int d)
100
+ : uniform(is_uniform_range(sq.qtype) ||
101
+ sq.qtype == ScalarQuantizer::QT_4bit_uniform) {
102
+ if (uniform) {
103
+ get_uniform_range(sq, vmin_s, vdiff_s);
104
+ } else {
105
+ vmin_arr = sq.trained.data();
106
+ vdiff_arr = sq.trained.data() + d;
107
+ }
108
+ }
109
+
110
+ float vmin(int m) const {
111
+ return uniform ? vmin_s : vmin_arr[m];
112
+ }
113
+ float vdiff(int m) const {
114
+ return uniform ? vdiff_s : vdiff_arr[m];
115
+ }
116
+
117
+ const bool uniform;
118
+ const float* vmin_arr = nullptr;
119
+ const float* vdiff_arr = nullptr;
120
+ float vmin_s = 0, vdiff_s = 0;
121
+ };
122
+
123
+ /** Split each code into the top 4 bits the SIMD kernel scans and the rest,
124
+ * which rerank keeps. `hi` is packed two dimensions per byte as
125
+ * pq4_pack_codes expects. `lo` is bit-packed at `bits - 4` per dimension.
126
+ * Total width is the quantizer's own, not that width plus 4.
127
+ */
128
+ inline void float_to_split_codes(
129
+ const float* x,
130
+ uint8_t* hi,
131
+ uint8_t* lo,
132
+ idx_t n,
133
+ int d,
134
+ size_t M2,
135
+ const ScalarQuantizer& sq) {
136
+ const int bits = sq_bits(sq.qtype);
137
+ const int lo_bits = bits - 4;
138
+ const size_t hi_stride = M2 / 2;
139
+ const size_t lo_stride = sq_rerank_size(d, sq.qtype);
140
+ const SQRanges ranges(sq, d);
141
+ for (idx_t i = 0; i < n; i++) {
142
+ const float* xi = x + i * d;
143
+ uint8_t* hdst = hi + i * hi_stride;
144
+ memset(hdst, 0, hi_stride);
145
+ uint8_t* ldst = lo + i * lo_stride;
146
+ memset(ldst, 0, lo_stride);
147
+ BitstringWriter bsw(ldst, lo_stride);
148
+ for (int m = 0; m < d; m++) {
149
+ const int code =
150
+ sq_code_of(xi[m], ranges.vmin(m), ranges.vdiff(m), bits);
151
+ const int h = code >> lo_bits;
152
+ hdst[m / 2] |= (m & 1) ? uint8_t(h << 4) : uint8_t(h);
153
+ bsw.write(code & ((1 << lo_bits) - 1), lo_bits);
154
+ }
155
+ }
156
+ }
157
+
158
+ /// Rebuild the code values of one vector from its two halves.
159
+ inline void split_to_codes(
160
+ const uint8_t* hi,
161
+ const uint8_t* lo,
162
+ int d,
163
+ ScalarQuantizer::QuantizerType qtype,
164
+ int* codes_out) {
165
+ const int lo_bits = sq_rerank_bits(qtype);
166
+ BitstringReader bsr(lo, sq_rerank_size(d, qtype));
167
+ for (int m = 0; m < d; m++) {
168
+ const int h = (m & 1) ? (hi[m / 2] >> 4) : (hi[m / 2] & 0xF);
169
+ codes_out[m] = (h << lo_bits) | int(bsr.read(lo_bits));
170
+ }
171
+ }
172
+
173
+ inline void float_to_4bit_nibbles(
174
+ const float* x,
175
+ uint8_t* nibbles,
176
+ idx_t n,
177
+ int d,
178
+ int M2,
179
+ const ScalarQuantizer& sq) {
180
+ const bool is_uniform = is_uniform_range(sq.qtype) ||
181
+ sq.qtype == ScalarQuantizer::QT_4bit_uniform;
182
+ const float* vmin_arr = nullptr;
183
+ const float* vdiff_arr = nullptr;
184
+ float vmin_s = 0, inv_vdiff_s = 0;
185
+
186
+ if (is_uniform) {
187
+ float vdiff;
188
+ get_uniform_range(sq, vmin_s, vdiff);
189
+ inv_vdiff_s = (vdiff > 0) ? (1.0f / vdiff) : 0;
190
+ } else {
191
+ vmin_arr = sq.trained.data();
192
+ vdiff_arr = sq.trained.data() + d;
193
+ }
194
+
195
+ const int half = M2 / 2;
196
+ for (idx_t i = 0; i < n; i++) {
197
+ const float* xi = x + i * d;
198
+ uint8_t* dst = nibbles + i * half;
199
+ memset(dst, 0, half);
200
+ for (int m = 0; m + 1 < d; m += 2) {
201
+ float f0, f1;
202
+ if (is_uniform) {
203
+ f0 = (xi[m] - vmin_s) * inv_vdiff_s;
204
+ f1 = (xi[m + 1] - vmin_s) * inv_vdiff_s;
205
+ } else {
206
+ f0 = (vdiff_arr[m] > 0) ? (xi[m] - vmin_arr[m]) / vdiff_arr[m]
207
+ : 0;
208
+ f1 = (vdiff_arr[m + 1] > 0)
209
+ ? (xi[m + 1] - vmin_arr[m + 1]) / vdiff_arr[m + 1]
210
+ : 0;
211
+ }
212
+ uint8_t lo = (uint8_t)std::min(15, std::max(0, (int)(f0 * 15.0f)));
213
+ uint8_t hi = (uint8_t)std::min(15, std::max(0, (int)(f1 * 15.0f)));
214
+ dst[m / 2] = lo | (hi << 4);
215
+ }
216
+ if (d & 1) {
217
+ float f;
218
+ if (is_uniform) {
219
+ f = (xi[d - 1] - vmin_s) * inv_vdiff_s;
220
+ } else {
221
+ f = (vdiff_arr[d - 1] > 0)
222
+ ? (xi[d - 1] - vmin_arr[d - 1]) / vdiff_arr[d - 1]
223
+ : 0;
224
+ }
225
+ dst[(d - 1) / 2] =
226
+ (uint8_t)std::min(15, std::max(0, (int)(f * 15.0f)));
227
+ }
228
+ }
229
+ }
230
+
231
+ } // namespace sq_fastscan
232
+ } // namespace faiss
@@ -25,7 +25,8 @@ size_t ZeroCopyIOReader::get_data_view(void** ptr, size_t size, size_t nitems) {
25
25
  actual_size = total_ - rp_;
26
26
  }
27
27
 
28
- size_t actual_nitems = (actual_size + size - 1) / size;
28
+ // Whole items only, so that reading a truncated vector fails.
29
+ size_t actual_nitems = actual_size / size;
29
30
  if (actual_nitems == 0) {
30
31
  return 0;
31
32
  }
@@ -35,17 +35,20 @@
35
35
  #include <faiss/IndexIVFPQR.h>
36
36
  #include <faiss/IndexIVFRaBitQ.h>
37
37
  #include <faiss/IndexIVFRaBitQFastScan.h>
38
+ #include <faiss/IndexIVFSQFastScan.h>
38
39
  #include <faiss/IndexIVFSpectralHash.h>
39
40
  #include <faiss/IndexLSH.h>
40
41
  #include <faiss/IndexLattice.h>
41
42
  #include <faiss/IndexNSG.h>
42
43
  #include <faiss/IndexPQ.h>
43
44
  #include <faiss/IndexPQFastScan.h>
45
+ #include <faiss/IndexPiPNN.h>
44
46
  #include <faiss/IndexPreTransform.h>
45
47
  #include <faiss/IndexRaBitQ.h>
46
48
  #include <faiss/IndexRaBitQFastScan.h>
47
49
  #include <faiss/IndexRefine.h>
48
50
  #include <faiss/IndexRowwiseMinMax.h>
51
+ #include <faiss/IndexSQFastScan.h>
49
52
  #include <faiss/IndexScalarQuantizer.h>
50
53
  #include <faiss/VectorTransform.h>
51
54
 
@@ -54,6 +57,10 @@
54
57
  #include <faiss/IndexBinaryHash.h>
55
58
  #include <faiss/IndexBinaryIVF.h>
56
59
 
60
+ #ifdef FAISS_ENABLE_DISK_IVF
61
+ #include <faiss/IndexIVFRaBitQSSD.h>
62
+ #endif
63
+
57
64
  #ifdef FAISS_ENABLE_SVS
58
65
  #include <faiss/svs/IndexSVSFlat.h>
59
66
  #include <faiss/svs/IndexSVSIVF.h>
@@ -177,6 +184,11 @@ std::map<std::string, ScalarQuantizer::QuantizerType> sq_types = {
177
184
  };
178
185
  const std::string sq_pattern =
179
186
  "(SQ0|SQ4|SQ8|SQ6|SQfp16|SQbf16|SQ8_direct_signed|SQ8_direct|SQtqmse1|SQtqmse2|SQtqmse3|SQtqmse4|SQtqmse8|SQtq2|SQtq3|SQtq4|SQtq5)";
187
+ const std::string sq_fs_pattern =
188
+ "(SQ4|SQ6|SQ8|SQ8_direct_signed|SQ8_direct)fs(_[0-9]+)?";
189
+ // All SQ types with "fs" suffix for IndexIVFSQFastScan, optional _bbs
190
+ const std::string ivf_sq_fs_pattern =
191
+ "(SQ0|SQ4|SQ8|SQ6|SQfp16|SQbf16|SQ8_direct_signed|SQ8_direct|SQtqmse1|SQtqmse2|SQtqmse3|SQtqmse4|SQtqmse8|SQtq2|SQtq3|SQtq4|SQtq5)fs(_[0-9]+)?";
180
192
 
181
193
  std::map<std::string, AdditiveQuantizer::Search_type_t> aq_search_type = {
182
194
  {"_Nfloat", AdditiveQuantizer::ST_norm_float},
@@ -239,8 +251,11 @@ VectorTransform* parse_VectorTransform(const std::string& description, int d) {
239
251
  if (match("RR([0-9]+)?")) {
240
252
  return new RandomRotationMatrix(d, mres_to_int(sm[1], d));
241
253
  }
242
- if (match("HR([0-9]+)?")) {
243
- return new HadamardRotation(d, mres_to_int(sm[1], 12345));
254
+ // "HR" pads d to a power of 2, "HRk" to a multiple of 64 (the FHT-Kac
255
+ // rotation, see HadamardRotation). An optional seed follows: "HRk7".
256
+ if (match("HR(k?)([0-9]+)?")) {
257
+ return new HadamardRotation(
258
+ d, mres_to_int(sm[2], 12345), sm[1].length() == 0);
244
259
  }
245
260
  if (match("ITQ([0-9]+)?")) {
246
261
  return new ITQTransform(d, mres_to_int(sm[1], d), sm[1].length() > 0);
@@ -310,6 +325,11 @@ Index* parse_coarse_quantizer(
310
325
  int R = std::stoi(sm[2]);
311
326
  return new IndexNSGFlat(d, R, mt);
312
327
  }
328
+ if (match("IVF([0-9]+[kM]?)_PiPNN([0-9]*)")) {
329
+ nlist = parse_nlist(sm[1].str());
330
+ int R = mres_to_int(sm[2], 64);
331
+ return new IndexPiPNNFlat(d, R, mt);
332
+ }
313
333
  #ifdef FAISS_ENABLE_SVS
314
334
  if (match("IVF([0-9]+[kM]?)_SVSVamana([0-9]*)(_.+)?")) {
315
335
  nlist = parse_nlist(sm[1].str());
@@ -374,7 +394,10 @@ IndexIVF* parse_IndexIVF(
374
394
  auto match = [&sm, &code_string](const std::string pattern) {
375
395
  return re_match(code_string, pattern, sm);
376
396
  };
377
- auto get_q = [&quantizer] { return quantizer.release(); };
397
+ // we cannot release the quantizer until we know no error happens and
398
+ // destroys the owning index since in this path the index does not own
399
+ // quantizer
400
+ auto get_q = [&quantizer] { return quantizer.get(); };
378
401
  int d = quantizer->d;
379
402
 
380
403
  if (match("Flat")) {
@@ -389,6 +412,17 @@ IndexIVF* parse_IndexIVF(
389
412
  return new IndexIVFFlatPanorama(
390
413
  get_q(), d, nlist, nlevels, mt, own_il, bs);
391
414
  }
415
+ if (match(ivf_sq_fs_pattern)) {
416
+ int bbs = mres_to_int(sm[2], 32, 1);
417
+ return new IndexIVFSQFastScan(
418
+ get_q(),
419
+ d,
420
+ nlist,
421
+ sq_types[sm[1].str()],
422
+ mt,
423
+ bbs,
424
+ /*by_residual=*/true);
425
+ }
392
426
  if (match(sq_pattern)) {
393
427
  return new IndexIVFScalarQuantizer(
394
428
  get_q(),
@@ -510,6 +544,18 @@ IndexIVF* parse_IndexIVF(
510
544
  }
511
545
  return index_ivf;
512
546
  }
547
+ #ifdef FAISS_ENABLE_DISK_IVF
548
+ // "RaBitQSSD<nb_bits>_<mem_dim>". Call attach_file() before add/search.
549
+ if (match("RaBitQSSD([2-9])_([0-9]+)")) {
550
+ uint8_t nb_bits = mres_to_int(sm[1]);
551
+ size_t mem_dim = mres_to_int(sm[2]);
552
+ auto* index_ivf = new IndexIVFRaBitQSSD(
553
+ quantizer.get(), d, nlist, mem_dim, nb_bits, mt);
554
+ quantizer.release();
555
+ index_ivf->own_invlists = own_il;
556
+ return index_ivf;
557
+ }
558
+ #endif
513
559
  // IndexIVFRaBitQ with optional nb_bits (1-9)
514
560
  // Accepts: "RaBitQ" (default 1-bit) or "RaBitQ{nb_bits}" (e.g., "RaBitQ4")
515
561
  if (match("RaBitQ([1-9])?")) {
@@ -637,6 +683,30 @@ IndexNSG* parse_IndexNSG(
637
683
  return nullptr;
638
684
  }
639
685
 
686
+ /***************************************************************
687
+ * Parse IndexPiPNN
688
+ */
689
+
690
+ IndexPiPNN* parse_IndexPiPNN(
691
+ const std::string& code_string,
692
+ int d,
693
+ MetricType mt,
694
+ int pipnn_R) {
695
+ std::smatch sm;
696
+ auto match = [&sm, &code_string](const std::string& pattern) {
697
+ return re_match(code_string, pattern, sm);
698
+ };
699
+
700
+ if (match("Flat|")) {
701
+ return new IndexPiPNNFlat(d, pipnn_R, mt);
702
+ }
703
+ if (match(sq_pattern)) {
704
+ return new IndexPiPNNSQ(d, sq_types[sm[1].str()], pipnn_R, mt);
705
+ }
706
+
707
+ return nullptr;
708
+ }
709
+
640
710
  #ifdef FAISS_ENABLE_SVS
641
711
  /***************************************************************
642
712
  * Parse IndexSVS
@@ -831,6 +901,12 @@ Index* parse_other_indexes(
831
901
  return new IndexLattice(d, M, nbit, r2);
832
902
  }
833
903
 
904
+ // IndexSQFastScan (must be checked before IndexScalarQuantizer)
905
+ if (match(sq_fs_pattern)) {
906
+ int bbs = mres_to_int(sm[2], 32, 1);
907
+ return new IndexSQFastScan(d, sq_types[sm[1].str()], metric, bbs);
908
+ }
909
+
834
910
  // IndexScalarQuantizer
835
911
  if (match(sq_pattern)) {
836
912
  return new IndexScalarQuantizer(d, sq_types[description], metric);
@@ -1156,6 +1232,28 @@ std::unique_ptr<Index> index_factory_sub(
1156
1232
  return std::unique_ptr<Index>(index);
1157
1233
  }
1158
1234
 
1235
+ // PiPNN variants (separator "," or "_", as for HNSW and NSG)
1236
+ if (re_match(description, "PiPNN([0-9]*)([,_].*)?", sm)) {
1237
+ int pipnn_R = mres_to_int(sm[1], 64);
1238
+ // We also accept empty code string (synonym of Flat)
1239
+ std::string code_string =
1240
+ sm[2].length() > 0 ? sm[2].str().substr(1) : "";
1241
+ if (verbose) {
1242
+ printf("parsing PiPNN string %s code_string=%s pipnn_R=%d\n",
1243
+ description.c_str(),
1244
+ code_string.c_str(),
1245
+ pipnn_R);
1246
+ }
1247
+
1248
+ IndexPiPNN* index = parse_IndexPiPNN(code_string, d, metric, pipnn_R);
1249
+ FAISS_THROW_IF_NOT_FMT(
1250
+ index,
1251
+ "could not parse PiPNN code description %s in %s",
1252
+ code_string.c_str(),
1253
+ description.c_str());
1254
+ return std::unique_ptr<Index>(index);
1255
+ }
1256
+
1159
1257
  // IndexRowwiseMinMax, fp32 version
1160
1258
  if (description.compare(0, 7, "MinMax,") == 0) {
1161
1259
  size_t comma = description.find(',');
@@ -1182,8 +1280,8 @@ std::unique_ptr<Index> index_factory_sub(
1182
1280
 
1183
1281
  // IndexIVF
1184
1282
  {
1185
- size_t nlist;
1186
- bool use_2layer;
1283
+ size_t nlist = 0;
1284
+ bool use_2layer = false;
1187
1285
  size_t comma = description.find(',');
1188
1286
  std::string coarse_string = description.substr(0, comma);
1189
1287
  // Match coarse quantizer part first
@@ -1207,7 +1305,8 @@ std::unique_ptr<Index> index_factory_sub(
1207
1305
  description.c_str());
1208
1306
  int M = std::stoi(sm[1].str()), nbit = mres_to_int(sm[2], 8, 1);
1209
1307
  Index2Layer* index_2l =
1210
- new Index2Layer(quantizer.release(), nlist, M, nbit);
1308
+ new Index2Layer(quantizer.get(), nlist, M, nbit);
1309
+ (void)quantizer.release(); // now owned by index_2l
1211
1310
  index_2l->q1.own_fields = true;
1212
1311
  index_2l->q1.quantizer_trains_alone =
1213
1312
  get_trains_alone(index_2l->q1.quantizer);
@@ -1222,6 +1321,7 @@ std::unique_ptr<Index> index_factory_sub(
1222
1321
  "could not parse code description %s in %s",
1223
1322
  code_description.c_str(),
1224
1323
  description.c_str());
1324
+ (void)quantizer.release(); // now owned by index_ivf
1225
1325
  return std::unique_ptr<Index>(fix_ivf_fields(index_ivf));
1226
1326
  }
1227
1327
  }
@@ -93,7 +93,7 @@ void DirectMap::add_single_id(idx_t id, idx_t list_no, size_t offset) {
93
93
  }
94
94
 
95
95
  if (type == Array) {
96
- assert(id == array.size());
96
+ assert(static_cast<size_t>(id) == array.size());
97
97
  if (list_no >= 0) {
98
98
  array.push_back(lo_build(list_no, offset));
99
99
  } else {