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
@@ -0,0 +1,326 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #include <faiss/impl/pipnn/kernels.h>
9
+
10
+ #include <algorithm>
11
+ #include <cmath>
12
+ #include <limits>
13
+ #include <memory>
14
+ #include <typeinfo>
15
+ #include <vector>
16
+
17
+ #include <faiss/IndexScalarQuantizer.h>
18
+ #include <faiss/impl/FaissAssert.h>
19
+ #include <faiss/utils/Heap.h>
20
+
21
+ extern "C" {
22
+
23
+ // this is to keep the clang syntax checker happy
24
+ #ifndef FINTEGER
25
+ #define FINTEGER int
26
+ #endif
27
+
28
+ /* declare BLAS functions, see http://www.netlib.org/clapack/cblas/ */
29
+
30
+ int sgemm_(
31
+ const char* transa,
32
+ const char* transb,
33
+ FINTEGER* m,
34
+ FINTEGER* n,
35
+ FINTEGER* k,
36
+ const float* alpha,
37
+ const float* a,
38
+ FINTEGER* lda,
39
+ const float* b,
40
+ FINTEGER* ldb,
41
+ float* beta,
42
+ float* c,
43
+ FINTEGER* ldc);
44
+
45
+ int ssyrk_(
46
+ const char* uplo,
47
+ const char* trans,
48
+ FINTEGER* n,
49
+ FINTEGER* k,
50
+ float* alpha,
51
+ float* a,
52
+ FINTEGER* lda,
53
+ float* beta,
54
+ float* c,
55
+ FINTEGER* ldc);
56
+ }
57
+
58
+ namespace faiss {
59
+ namespace pipnn {
60
+
61
+ namespace {
62
+
63
+ FINTEGER to_finteger(size_t v, const char* what) {
64
+ FAISS_THROW_IF_NOT_FMT(
65
+ v <= size_t(std::numeric_limits<FINTEGER>::max()),
66
+ "PiPNN kernel: %s=%zu exceeds the BLAS integer range",
67
+ what,
68
+ v);
69
+ return static_cast<FINTEGER>(v);
70
+ }
71
+
72
+ float nan_to_inf(float v) {
73
+ return std::isnan(v) ? std::numeric_limits<float>::infinity() : v;
74
+ }
75
+
76
+ /// Top-k in (dist, index) order on a Faiss max-heap. Empty slots are
77
+ /// (+inf, INT32_MAX), so +inf offers still fill them, in index order.
78
+ using TopK = CMax<float, int32_t>;
79
+ constexpr int32_t kEmpty = std::numeric_limits<int32_t>::max();
80
+
81
+ void topk_init(float* D, int32_t* I, size_t k) {
82
+ std::fill(D, D + k, std::numeric_limits<float>::infinity());
83
+ std::fill(I, I + k, kEmpty);
84
+ }
85
+
86
+ void topk_offer(float* D, int32_t* I, size_t k, float v, int32_t id) {
87
+ if (TopK::cmp2(D[0], v, I[0], id)) {
88
+ heap_replace_top<TopK>(k, D, I, v, id);
89
+ }
90
+ }
91
+
92
+ /// Sorts the slots by (dist, index); slots never filled become (+inf, -1).
93
+ void topk_finish(float* D, int32_t* I, size_t k) {
94
+ heap_reorder<TopK>(k, D, I);
95
+ std::replace(I, I + k, kEmpty, int32_t(-1));
96
+ }
97
+
98
+ } // namespace
99
+
100
+ void stripe_assign(
101
+ const float* points,
102
+ size_t S,
103
+ const float* leaders,
104
+ const float* leader_norms,
105
+ size_t L,
106
+ size_t d,
107
+ Ranking ranking,
108
+ int fanout,
109
+ float* scratch,
110
+ uint16_t* out) {
111
+ FAISS_THROW_IF_NOT_FMT(
112
+ L >= 1 && L <= 65535,
113
+ "stripe_assign: L=%zu must be in [1, 65535]",
114
+ L);
115
+ FAISS_THROW_IF_NOT_FMT(
116
+ fanout >= 1 && size_t(fanout) <= L,
117
+ "stripe_assign: fanout=%d must be in [1, L=%zu]",
118
+ fanout,
119
+ L);
120
+ FAISS_THROW_IF_NOT_MSG(d >= 1, "stripe_assign: d must be >= 1");
121
+ FAISS_THROW_IF_NOT_MSG(
122
+ ranking == Ranking::IP || leader_norms,
123
+ "stripe_assign: leader_norms is required for the L2 and Angle rankings");
124
+ if (S == 0) {
125
+ return;
126
+ }
127
+ FINTEGER li = to_finteger(L, "L");
128
+ FINTEGER si = to_finteger(S, "S");
129
+ FINTEGER di = to_finteger(d, "d");
130
+
131
+ // Column-major BLAS sees row-major scratch (S x L) as L x S, so "T", "N"
132
+ // gives scratch[i * L + j] = alpha * p_i.l_j + beta * scratch[i * L + j].
133
+ float alpha = -1;
134
+ float beta = 0; // BLAS does not read C when beta == 0
135
+ if (ranking == Ranking::L2) {
136
+ for (size_t i = 0; i < S; i++) {
137
+ std::copy(leader_norms, leader_norms + L, scratch + i * L);
138
+ }
139
+ alpha = -2;
140
+ beta = 1;
141
+ }
142
+ sgemm_("T",
143
+ "N",
144
+ &li,
145
+ &si,
146
+ &di,
147
+ &alpha,
148
+ leaders,
149
+ &di,
150
+ points,
151
+ &di,
152
+ &beta,
153
+ scratch,
154
+ &li);
155
+
156
+ if (ranking == Ranking::Angle) {
157
+ // A zero-norm leader has no direction: rank it +inf, not the -inf of
158
+ // a positive dot product divided by 0.
159
+ for (size_t i = 0; i < S; i++) {
160
+ float* row = scratch + i * L;
161
+ for (size_t j = 0; j < L; j++) {
162
+ const float len = std::sqrt(leader_norms[j]);
163
+ row[j] = len > 0 ? row[j] / len
164
+ : std::numeric_limits<float>::infinity();
165
+ }
166
+ }
167
+ }
168
+
169
+ const size_t f = size_t(fanout);
170
+ std::vector<float> D(f);
171
+ std::vector<int32_t> I(f);
172
+ for (size_t i = 0; i < S; i++) {
173
+ topk_init(D.data(), I.data(), f);
174
+ const float* row = scratch + i * L;
175
+ for (size_t j = 0; j < L; j++) {
176
+ topk_offer(D.data(), I.data(), f, nan_to_inf(row[j]), int32_t(j));
177
+ }
178
+ topk_finish(D.data(), I.data(), f);
179
+ // L >= fanout and every offer precedes an empty slot, so all fanout
180
+ // slots are filled, even when the whole row is +inf.
181
+ for (size_t r = 0; r < f; r++) {
182
+ out[i * f + r] = uint16_t(I[r]);
183
+ }
184
+ }
185
+ }
186
+
187
+ namespace {
188
+
189
+ /// Distance from a Gram entry: L2 max(0, ni + nj - 2g), IP -g. NaN is mapped
190
+ /// to +inf before the L2 clamp, which would otherwise turn it into 0.
191
+ float gram_to_distance(bool is_l2, float ni, float nj, float g) {
192
+ if (is_l2) {
193
+ const float v = ni + nj - 2 * g;
194
+ if (std::isnan(v)) {
195
+ return std::numeric_limits<float>::infinity();
196
+ }
197
+ return v < 0 ? 0 : v;
198
+ }
199
+ return nan_to_inf(-g);
200
+ }
201
+
202
+ /// Lower triangle of X X^T, diagonal included, into row-major gram (s x s);
203
+ /// the strict upper triangle is not written.
204
+ void gram_lower(const float* X, size_t s, size_t d, float* gram) {
205
+ // Column-major BLAS reads X as A = X^T (d x s); the upper triangle of
206
+ // C = A^T A ("U", "T") is the row-major lower triangle of gram.
207
+ FINTEGER si = to_finteger(s, "s");
208
+ FINTEGER di = to_finteger(d, "d");
209
+ float one = 1;
210
+ float zero = 0; // BLAS does not read C when beta == 0
211
+ ssyrk_("U",
212
+ "T",
213
+ &si,
214
+ &di,
215
+ &one,
216
+ const_cast<float*>(X),
217
+ &di,
218
+ &zero,
219
+ gram,
220
+ &si);
221
+ }
222
+
223
+ } // namespace
224
+
225
+ void leaf_knn(
226
+ const float* X,
227
+ const float* norms,
228
+ size_t s,
229
+ size_t d,
230
+ MetricType metric,
231
+ int k,
232
+ float* gram,
233
+ int32_t* nn_idx,
234
+ float* nn_dist) {
235
+ FAISS_THROW_IF_NOT_MSG(
236
+ metric == METRIC_L2 || metric == METRIC_INNER_PRODUCT,
237
+ "leaf_knn: metric must be METRIC_L2 or METRIC_INNER_PRODUCT");
238
+ FAISS_THROW_IF_NOT_MSG(k >= 1, "leaf_knn: k must be >= 1");
239
+ FAISS_THROW_IF_NOT_MSG(d >= 1, "leaf_knn: d must be >= 1");
240
+ FAISS_THROW_IF_NOT_MSG(
241
+ metric == METRIC_INNER_PRODUCT || norms,
242
+ "leaf_knn: norms is required for METRIC_L2");
243
+ const size_t kk = size_t(k);
244
+ for (size_t i = 0; i < s; i++) {
245
+ topk_init(nn_dist + i * kk, nn_idx + i * kk, kk);
246
+ }
247
+ if (s >= 2) {
248
+ gram_lower(X, s, d, gram);
249
+ }
250
+ const bool is_l2 = metric == METRIC_L2;
251
+ for (size_t i = 1; i < s; i++) {
252
+ const float* gi = gram + i * s;
253
+ const float ni = is_l2 ? norms[i] : 0;
254
+ for (size_t j = 0; j < i; j++) {
255
+ const float nj = is_l2 ? norms[j] : 0;
256
+ const float v = gram_to_distance(is_l2, ni, nj, gi[j]);
257
+ topk_offer(nn_dist + i * kk, nn_idx + i * kk, kk, v, int32_t(j));
258
+ topk_offer(nn_dist + j * kk, nn_idx + j * kk, kk, v, int32_t(i));
259
+ }
260
+ }
261
+ for (size_t i = 0; i < s; i++) {
262
+ topk_finish(nn_dist + i * kk, nn_idx + i * kk, kk);
263
+ }
264
+ }
265
+
266
+ void pairwise_distances(
267
+ const float* X,
268
+ const float* norms,
269
+ size_t n,
270
+ size_t d,
271
+ MetricType metric,
272
+ float* out) {
273
+ FAISS_THROW_IF_NOT_MSG(
274
+ metric == METRIC_L2 || metric == METRIC_INNER_PRODUCT,
275
+ "pairwise_distances: metric must be METRIC_L2 or METRIC_INNER_PRODUCT");
276
+ FAISS_THROW_IF_NOT_MSG(d >= 1, "pairwise_distances: d must be >= 1");
277
+ FAISS_THROW_IF_NOT_MSG(
278
+ metric == METRIC_INNER_PRODUCT || norms,
279
+ "pairwise_distances: norms is required for METRIC_L2");
280
+ if (n == 0) {
281
+ return;
282
+ }
283
+ // Row i reads only its lower part of the Gram scratch; mirrored writes of
284
+ // earlier rows go to their strict upper triangle.
285
+ gram_lower(X, n, d, out);
286
+ const bool is_l2 = metric == METRIC_L2;
287
+ for (size_t i = 0; i < n; i++) {
288
+ float* oi = out + i * n;
289
+ const float ni = is_l2 ? norms[i] : 0;
290
+ for (size_t j = 0; j < i; j++) {
291
+ const float nj = is_l2 ? norms[j] : 0;
292
+ const float v = gram_to_distance(is_l2, ni, nj, oi[j]);
293
+ oi[j] = v;
294
+ out[j * n + i] = v;
295
+ }
296
+ oi[i] = gram_to_distance(is_l2, ni, ni, oi[i]);
297
+ }
298
+ }
299
+
300
+ void gather_rows(
301
+ const Index& storage,
302
+ const int32_t* ids,
303
+ size_t k,
304
+ float* out) {
305
+ const size_t d = static_cast<size_t>(storage.d);
306
+ if (typeid(storage) == typeid(IndexScalarQuantizer)) {
307
+ // Not reconstruct: ScalarQuantizer::decode opens an OpenMP region, and
308
+ // a nested region inside the build's dynamic loops crashes libomp.
309
+ const auto& sq = static_cast<const IndexScalarQuantizer&>(storage);
310
+ std::unique_ptr<ScalarQuantizer::SQuantizer> quant(
311
+ sq.sq.select_quantizer());
312
+ for (size_t i = 0; i < k; i++) {
313
+ quant->decode_vector(
314
+ sq.codes.data() +
315
+ static_cast<size_t>(ids[i]) * sq.code_size,
316
+ out + i * d);
317
+ }
318
+ return;
319
+ }
320
+ for (size_t i = 0; i < k; i++) {
321
+ storage.reconstruct(ids[i], out + i * d);
322
+ }
323
+ }
324
+
325
+ } // namespace pipnn
326
+ } // namespace faiss
@@ -0,0 +1,92 @@
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 <faiss/MetricType.h>
11
+ #include <faiss/utils/AlignedTable.h>
12
+ #include <cstddef>
13
+ #include <cstdint>
14
+
15
+ /*
16
+ * Each kernel makes one BLAS call (sgemm_ or ssyrk_), and results are bitwise
17
+ * reproducible across thread counts only if that call runs single-threaded.
18
+ * So call the kernels inside an OpenMP parallel region, or from serial code
19
+ * only when omp_get_max_threads() == 1, and keep BLAS from threading on its
20
+ * own: MKL_NUM_THREADS unset or 1, OPENBLAS_NUM_THREADS=1 for pthreads
21
+ * OpenBLAS, VECLIB_MAXIMUM_THREADS=1 on macOS, OMP_NUM_THREADS=1 for an
22
+ * OpenMP BLAS in a Faiss build without OpenMP. Inputs should be 64-byte
23
+ * aligned: some BLAS round differently on differently aligned buffers.
24
+ */
25
+
26
+ namespace faiss {
27
+
28
+ struct Index;
29
+
30
+ namespace pipnn {
31
+
32
+ /// Points per stripe item of the partition kernel. Changing it changes graphs.
33
+ constexpr size_t kStripe = 1024;
34
+
35
+ enum class Ranking : uint8_t { L2 = 0, IP = 1, Angle = 2 };
36
+
37
+ /// out (S x fanout): each point's nearest leaders in (rank, index) order, with
38
+ /// rank L2 ||l||^2 - 2 p.l, IP -p.l, Angle -p.l / ||l|| (+inf if ||l|| = 0).
39
+ /// NaN ranks +inf. scratch: S * L floats; leader_norms may be null for IP.
40
+ void stripe_assign(
41
+ const float* points,
42
+ size_t S,
43
+ const float* leaders,
44
+ const float* leader_norms,
45
+ size_t L,
46
+ size_t d,
47
+ Ranking ranking,
48
+ int fanout,
49
+ float* scratch,
50
+ uint16_t* out);
51
+
52
+ /// k nearest neighbours (s x k) of each leaf point in (dist, index) order;
53
+ /// unfilled slots are -1 / +inf. gram: s * s floats; norms null for IP only.
54
+ void leaf_knn(
55
+ const float* X,
56
+ const float* norms,
57
+ size_t s,
58
+ size_t d,
59
+ MetricType metric,
60
+ int k,
61
+ float* gram,
62
+ int32_t* nn_idx,
63
+ float* nn_dist);
64
+
65
+ /// n x n distances (L2 clamped at 0, IP -dot); out is also the Gram scratch.
66
+ /// The L2 diagonal is small and non-negative, but not necessarily 0.
67
+ void pairwise_distances(
68
+ const float* X,
69
+ const float* norms,
70
+ size_t n,
71
+ size_t d,
72
+ MetricType metric,
73
+ float* out);
74
+
75
+ /// Rows ids[0..k) of `storage` as floats into out (k x d). SQ rows are decoded
76
+ /// directly; other types use reconstruct(), which must not open OpenMP regions.
77
+ void gather_rows(
78
+ const Index& storage,
79
+ const int32_t* ids,
80
+ size_t k,
81
+ float* out);
82
+
83
+ /// Grows a per-thread scratch table and never shrinks it: AlignedTable::resize
84
+ /// reallocates whenever the rounded capacity changes, shrinking included.
85
+ inline void ensure_size(AlignedTable<float, 64>& table, size_t n) {
86
+ if (table.size() < n) {
87
+ table.resize(n);
88
+ }
89
+ }
90
+
91
+ } // namespace pipnn
92
+ } // namespace faiss
@@ -8,8 +8,11 @@
8
8
  #pragma once
9
9
 
10
10
  // basic int types and size_t
11
+ #include <atomic>
12
+ #include <cerrno>
11
13
  #include <cstdint>
12
14
  #include <cstdio>
15
+ #include <type_traits>
13
16
 
14
17
  #ifdef _WIN32
15
18
 
@@ -35,9 +38,6 @@
35
38
  (((*(p)) = _aligned_malloc((s), (a))), *(p) ? 0 : errno)
36
39
  #define posix_memalign_free _aligned_free
37
40
 
38
- // aligned should be in front of the declaration
39
- #define ALIGNED(x) __declspec(align(x))
40
-
41
41
  // redefine the GCC intrinsics with Windows equivalents
42
42
  #ifdef _MSC_VER
43
43
 
@@ -104,14 +104,6 @@ inline int __builtin_clzll(uint64_t x) {
104
104
  #endif // _MSC_VER
105
105
 
106
106
  #define FAISS_ALWAYS_INLINE __forceinline
107
-
108
- // MSVC uses pragma pack instead of __attribute__((packed))
109
- // Use FAISS_PACK_STRUCTS_BEGIN/END to wrap packed structure definitions
110
- #define FAISS_PACKED
111
- #define FAISS_RESTRICT __restrict
112
- #define FAISS_PACK_STRUCTS_BEGIN __pragma(pack(push, 1))
113
- #define FAISS_PACK_STRUCTS_END __pragma(pack(pop))
114
-
115
107
  #else
116
108
  /*******************************************************
117
109
  * Linux and OSX
@@ -120,22 +112,6 @@ inline int __builtin_clzll(uint64_t x) {
120
112
  #define FAISS_API
121
113
  #define posix_memalign_free free
122
114
 
123
- // aligned should be *in front* of the declaration, for compatibility with
124
- // windows
125
- #ifdef SWIG
126
- #define ALIGNED(x)
127
- #define FAISS_PACKED
128
- #define FAISS_RESTRICT
129
- #else
130
- #define ALIGNED(x) __attribute__((aligned(x)))
131
- #define FAISS_PACKED __attribute__((packed))
132
- #define FAISS_RESTRICT __restrict
133
- #endif
134
-
135
- // On non-Windows, FAISS_PACKED handles packing, so these are no-ops
136
- #define FAISS_PACK_STRUCTS_BEGIN
137
- #define FAISS_PACK_STRUCTS_END
138
-
139
115
  #define FAISS_ALWAYS_INLINE __attribute__((always_inline)) inline
140
116
 
141
117
  #endif
@@ -146,6 +122,50 @@ inline int __builtin_clzll(uint64_t x) {
146
122
  #define FAISS_DEPRECATED(msg)
147
123
  #endif // GCC or Clang
148
124
 
125
+ // Never inlined, so floating-point results do not depend on the compile
126
+ // flags of the call site (e.g. under LTO).
127
+ #if defined(_MSC_VER)
128
+ #define FAISS_NOINLINE __declspec(noinline)
129
+ #elif defined(__clang__)
130
+ #define FAISS_NOINLINE __attribute__((noinline))
131
+ #elif defined(__GNUC__)
132
+ #define FAISS_NOINLINE __attribute__((noinline, noclone))
133
+ #else
134
+ #define FAISS_NOINLINE
135
+ #endif
136
+
137
+ namespace faiss {
138
+ namespace detail {
139
+
140
+ template <typename T>
141
+ inline void atomic_fetch_add_relaxed(T& target, T value) {
142
+ #if defined(__cpp_lib_atomic_ref) && __cpp_lib_atomic_ref >= 201806L
143
+ std::atomic_ref<T>(target).fetch_add(value, std::memory_order_relaxed);
144
+ #else
145
+ if constexpr (std::is_floating_point_v<T>) {
146
+ // The _n builtins reject a floating-point type, so accumulate with the
147
+ // generic compare-exchange builtin instead.
148
+ T expected;
149
+ __atomic_load(&target, &expected, __ATOMIC_RELAXED);
150
+ T desired;
151
+ do {
152
+ desired = expected + value;
153
+ } while (!__atomic_compare_exchange(
154
+ &target,
155
+ &expected,
156
+ &desired,
157
+ true,
158
+ __ATOMIC_RELAXED,
159
+ __ATOMIC_RELAXED));
160
+ } else {
161
+ __atomic_fetch_add(&target, value, __ATOMIC_RELAXED);
162
+ }
163
+ #endif
164
+ }
165
+
166
+ } // namespace detail
167
+ } // namespace faiss
168
+
149
169
  // Localized enablement of imprecise floating point operations
150
170
  // You need to use all 3 macros to cover all compilers.
151
171
  #if defined(_MSC_VER)
@@ -201,6 +221,33 @@ inline int __builtin_clzll(uint64_t x) {
201
221
 
202
222
  // clang-format on
203
223
 
224
+ /*******************************************************
225
+ * Packing and alignment
226
+ *******************************************************/
227
+
228
+ // ALIGNED goes *in front* of the declaration, for compatibility with MSVC.
229
+ // swigfaiss.swig defines _MSC_VER under SWIGWIN, so check SWIG first.
230
+ #if defined(SWIG)
231
+ #define ALIGNED(x)
232
+ #define FAISS_PACKED
233
+ #define FAISS_RESTRICT
234
+ #define FAISS_PACK_STRUCTS_BEGIN
235
+ #define FAISS_PACK_STRUCTS_END
236
+ #elif defined(_MSC_VER) // MSVC and clang-cl
237
+ #define ALIGNED(x) __declspec(align(x))
238
+ // Wrap packed structs in FAISS_PACK_STRUCTS_BEGIN/END for MSVC.
239
+ #define FAISS_PACKED
240
+ #define FAISS_RESTRICT __restrict
241
+ #define FAISS_PACK_STRUCTS_BEGIN __pragma(pack(push, 1))
242
+ #define FAISS_PACK_STRUCTS_END __pragma(pack(pop))
243
+ #else // GCC and Clang, including MinGW
244
+ #define ALIGNED(x) __attribute__((aligned(x)))
245
+ #define FAISS_PACKED __attribute__((packed))
246
+ #define FAISS_RESTRICT __restrict
247
+ #define FAISS_PACK_STRUCTS_BEGIN
248
+ #define FAISS_PACK_STRUCTS_END
249
+ #endif
250
+
204
251
  /*******************************************************
205
252
  * BIGENDIAN specific macros
206
253
  *******************************************************/
@@ -226,3 +273,20 @@ inline int __builtin_clzll(uint64_t x) {
226
273
  #define FAISS_MAYBE_UNUSED [[maybe_unused]]
227
274
  #define FAISS_FINAL final
228
275
  #endif
276
+
277
+ /*******************************************************
278
+ * OpenMP dynamic scheduling
279
+ *******************************************************/
280
+
281
+ // Use as schedule(FAISS_OMP_DYNAMIC) or schedule(FAISS_OMP_DYNAMIC, chunk).
282
+ // OpenMP 5.0 makes schedule(dynamic) nonmonotonic, and libomp 12 runs
283
+ // nonmonotonic dynamic loops with its static-steal scheduler, which crashes
284
+ // intermittently under load (libomp itself keeps it off by default). MSVC's
285
+ // OpenMP 2.0 has no schedule modifiers.
286
+ // clang-format off
287
+ #ifdef _MSC_VER
288
+ #define FAISS_OMP_DYNAMIC dynamic
289
+ #else
290
+ #define FAISS_OMP_DYNAMIC monotonic : dynamic
291
+ #endif
292
+ // clang-format on
@@ -7,8 +7,6 @@
7
7
 
8
8
  // -*- c++ -*-
9
9
 
10
- #ifdef COMPILE_SIMD_AVX512
11
-
12
10
  #include <faiss/impl/PolysemousTraining.h>
13
11
  #include <faiss/impl/polysemous_training/dispatch.h>
14
12
  #include <faiss/utils/popcount.h>
@@ -280,5 +278,3 @@ double distances_cost_update<SIMDLevel::AVX512>(
280
278
 
281
279
  } // namespace polysemous_training
282
280
  } // namespace faiss
283
-
284
- #endif // COMPILE_SIMD_AVX512