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,129 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include <vector>
11
+
12
+ #include <faiss/IndexFastScan.h>
13
+ #include <faiss/IndexFlatCodes.h>
14
+ #include <faiss/IndexScalarQuantizer.h>
15
+ #include <faiss/impl/ScalarQuantizer.h>
16
+
17
+ namespace faiss {
18
+
19
+ /** Fast scan version of IndexScalarQuantizer.
20
+ *
21
+ * Supported quantizer types:
22
+ * - QT_4bit, QT_4bit_uniform: native 4-bit codes mapped onto the
23
+ * PQ4 FastScan SIMD (vpshufb / equivalent).
24
+ *
25
+ * - QT_6bit, QT_8bit, QT_8bit_uniform, QT_8bit_direct,
26
+ * QT_8bit_direct_signed: re-quantised to 4-bit for the scan, reranked
27
+ * against the originals in orig_codes.
28
+ *
29
+ * - All other types (QT_fp16, QT_bf16): use IndexScalarQuantizer directly.
30
+ */
31
+ struct IndexSQFastScan : IndexFastScan {
32
+ ScalarQuantizer sq;
33
+
34
+ /// Overselection ratio for reranking.
35
+ float rerank_factor = 2;
36
+
37
+ /// Low nibbles of the 8-bit codes, two dimensions per byte, indexed by
38
+ /// id. The high nibbles live in the packed scan codes. Empty for the
39
+ /// native 4-bit types, which need no rerank.
40
+ std::vector<uint8_t> lo_codes;
41
+
42
+ /** Constructor.
43
+ *
44
+ * @param d dimensionality of input vectors
45
+ * @param qtype any native 4-bit or reranked type (see above)
46
+ * @param metric distance metric (METRIC_L2 or METRIC_INNER_PRODUCT)
47
+ * @param bbs block size for SIMD processing (multiple of 32)
48
+ */
49
+ IndexSQFastScan(
50
+ int d,
51
+ ScalarQuantizer::QuantizerType qtype,
52
+ MetricType metric = METRIC_L2,
53
+ int bbs = 32);
54
+
55
+ IndexSQFastScan();
56
+
57
+ /// Build from an existing IndexScalarQuantizer
58
+ explicit IndexSQFastScan(const IndexScalarQuantizer& orig, int bbs = 32);
59
+
60
+ void train(idx_t n, const float* x) override;
61
+
62
+ void add(idx_t n, const float* x) override;
63
+
64
+ void reset() override;
65
+
66
+ void search(
67
+ idx_t n,
68
+ const float* x,
69
+ idx_t k,
70
+ float* distances,
71
+ idx_t* labels,
72
+ const SearchParameters* params = nullptr) const override;
73
+
74
+ void compute_codes(uint8_t* codes, idx_t n, const float* x) const override;
75
+
76
+ void check_compatible_for_merge(const Index& otherIndex) const override;
77
+
78
+ void compute_float_LUT(
79
+ float* lut,
80
+ idx_t n,
81
+ const float* x,
82
+ const FastScanDistancePostProcessing& context) const override;
83
+
84
+ void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override;
85
+
86
+ void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override;
87
+
88
+ void reconstruct(idx_t key, float* recons) const override;
89
+
90
+ void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override;
91
+
92
+ void add_sa_codes(idx_t n, const uint8_t* codes, const idx_t* xids)
93
+ override;
94
+
95
+ void permute_entries(const idx_t* perm);
96
+
97
+ FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const;
98
+
99
+ DistanceComputer* get_distance_computer() const override;
100
+
101
+ void range_search(
102
+ idx_t n,
103
+ const float* x,
104
+ float radius,
105
+ RangeSearchResult* result,
106
+ const SearchParameters* params = nullptr) const override;
107
+
108
+ size_t remove_ids(const IDSelector& sel) override;
109
+
110
+ void merge_from(Index& otherIndex, idx_t add_id = 0) override;
111
+
112
+ void search1(
113
+ const float* x,
114
+ ResultHandler& handler,
115
+ SearchParameters* params = nullptr) const override;
116
+
117
+ size_t sa_code_size() const override;
118
+
119
+ void fill_sa_code(
120
+ const CodePacker& packer,
121
+ idx_t id,
122
+ uint8_t* scratch,
123
+ int* values,
124
+ uint8_t* code_out) const;
125
+
126
+ size_t fast_scan_code_size() const override;
127
+ };
128
+
129
+ } // namespace faiss
@@ -193,6 +193,16 @@ void IndexShardsTemplate<IndexT>::add_with_ids(
193
193
  syncWithSubIndexes();
194
194
  }
195
195
 
196
+ template <typename IndexT>
197
+ size_t IndexShardsTemplate<IndexT>::remove_ids(const IDSelector& sel) {
198
+ FAISS_THROW_IF_MSG(
199
+ successive_ids, "remove_ids needs successive_ids = false");
200
+ const idx_t before = this->ntotal;
201
+ this->runOnIndex([&sel](int, IndexT* index) { index->remove_ids(sel); });
202
+ syncWithSubIndexes();
203
+ return before - this->ntotal;
204
+ }
205
+
196
206
  template <typename IndexT>
197
207
  void IndexShardsTemplate<IndexT>::search(
198
208
  idx_t n,
@@ -91,6 +91,11 @@ struct IndexShardsTemplate : public ThreadedIndex<IndexT> {
91
91
 
92
92
  void train(idx_t n, const component_t* x) override;
93
93
 
94
+ /// Removes the ids from every shard, and returns the sum of the counts.
95
+ /// Needs successive_ids = false: with successive ids, the ids of a shard
96
+ /// depend on the sizes of the shards before it.
97
+ size_t remove_ids(const IDSelector& sel) override;
98
+
94
99
  bool successive_ids;
95
100
 
96
101
  /// Synchronize the top-level index (IndexShards) with data in the
@@ -22,6 +22,7 @@
22
22
  #include <faiss/impl/ClusteringHelpers.h>
23
23
  #include <faiss/impl/FaissAssert.h>
24
24
  #include <faiss/impl/PdxLayout.h>
25
+ #include <faiss/impl/platform_macros.h>
25
26
  #include <faiss/impl/simd_dispatch.h>
26
27
  #include <faiss/utils/distances.h>
27
28
  #include <faiss/utils/random.h>
@@ -266,9 +267,9 @@ float adapt_d_prime(
266
267
  }
267
268
 
268
269
  /// Pre-loop setup: subsample, rotate, Forgy init, build ADSampling table,
269
- /// size the label scratch. Returned `sampled_x_owner` keeps the subsampled
270
- /// buffer alive when subsampling occurred (otherwise empty).
271
- std::unique_ptr<uint8_t[]> setup_train_state(
270
+ /// and size the label scratch. The subsampled input only needs to remain alive
271
+ /// through the rotation and is released before the iteration loop starts.
272
+ void setup_train_state(
272
273
  TrainState& state,
273
274
  std::vector<int64_t>& labels64,
274
275
  std::vector<float>& hassign,
@@ -276,10 +277,13 @@ std::unique_ptr<uint8_t[]> setup_train_state(
276
277
  int d,
277
278
  int k,
278
279
  idx_t n,
279
- const float* x) {
280
- const size_t line_size = sizeof(float) * static_cast<size_t>(d);
280
+ const void* x,
281
+ NumericType numeric_type) {
282
+ const bool fp16 = numeric_type == NumericType::Float16;
283
+ const size_t line_size =
284
+ (fp16 ? sizeof(uint16_t) : sizeof(float)) * static_cast<size_t>(d);
281
285
  idx_t nx = n;
282
- const uint8_t* x_bytes = reinterpret_cast<const uint8_t*>(x);
286
+ const uint8_t* x_bytes = static_cast<const uint8_t*>(x);
283
287
  std::unique_ptr<uint8_t[]> sampled_x_owner;
284
288
  if (static_cast<size_t>(nx) >
285
289
  static_cast<size_t>(k) * cp.max_points_per_centroid) {
@@ -298,7 +302,6 @@ std::unique_ptr<uint8_t[]> setup_train_state(
298
302
  sampled_x_owner.reset(x_new);
299
303
  x_bytes = x_new;
300
304
  }
301
- const float* x_sampled = reinterpret_cast<const float*>(x_bytes);
302
305
 
303
306
  FAISS_THROW_IF_NOT_MSG(
304
307
  nx <= static_cast<idx_t>(std::numeric_limits<int>::max()),
@@ -315,7 +318,28 @@ std::unique_ptr<uint8_t[]> setup_train_state(
315
318
  }
316
319
 
317
320
  state.X_tilde.resize(static_cast<size_t>(state.n) * d);
318
- state.R->apply_noalloc(state.n, x_sampled, state.X_tilde.data());
321
+ if (!fp16) {
322
+ state.R->apply_noalloc(
323
+ state.n,
324
+ reinterpret_cast<const float*>(x_bytes),
325
+ state.X_tilde.data());
326
+ } else {
327
+ const size_t block_size =
328
+ std::min(static_cast<size_t>(state.n), cp.decode_block_size);
329
+ std::vector<float> decoded(block_size * d);
330
+ const uint16_t* x16 = reinterpret_cast<const uint16_t*>(x_bytes);
331
+ for (size_t i0 = 0; i0 < static_cast<size_t>(state.n);
332
+ i0 += block_size) {
333
+ const size_t i1 =
334
+ std::min(static_cast<size_t>(state.n), i0 + block_size);
335
+ fp16_to_fp32((i1 - i0) * d, x16 + i0 * d, decoded.data());
336
+ state.R->apply_noalloc(
337
+ static_cast<idx_t>(i1 - i0),
338
+ decoded.data(),
339
+ state.X_tilde.data() + i0 * d);
340
+ }
341
+ }
342
+ sampled_x_owner.reset();
319
343
 
320
344
  // Forgy init: pick k random rows from the rotated pool as initial
321
345
  // centroids. These remain in rotated space; un-rotation happens
@@ -351,8 +375,6 @@ std::unique_ptr<uint8_t[]> setup_train_state(
351
375
  hassign.assign(k, 0.0f);
352
376
 
353
377
  labels64.resize(state.n);
354
-
355
- return sampled_x_owner;
356
378
  }
357
379
 
358
380
  /// Un-rotate centroids into output buffer. R orthogonal, so
@@ -369,8 +391,8 @@ void untransform_centroids(
369
391
 
370
392
  } // namespace
371
393
 
372
- SuperKMeans::SuperKMeans(int d, int k, const SuperKMeansParameters& cp_in)
373
- : cp(cp_in), d(d), k(k) {
394
+ SuperKMeans::SuperKMeans(int d_in, int k_in, const SuperKMeansParameters& cp_in)
395
+ : cp(cp_in), d(d_in), k(k_in) {
374
396
  FAISS_THROW_IF_NOT_MSG(d > 0, "SuperKMeans: d must be positive");
375
397
  FAISS_THROW_IF_NOT_MSG(k > 0, "SuperKMeans: k must be positive");
376
398
  FAISS_THROW_IF_NOT_MSG(
@@ -411,15 +433,36 @@ SuperKMeans::SuperKMeans(int d, int k, const SuperKMeansParameters& cp_in)
411
433
  }
412
434
 
413
435
  void SuperKMeans::train(idx_t n, const float* x) {
436
+ train_ex(n, x, NumericType::Float32);
437
+ }
438
+
439
+ void SuperKMeans::train_ex(idx_t n, const void* x, NumericType numeric_type) {
440
+ FAISS_THROW_IF_NOT_MSG(
441
+ numeric_type == NumericType::Float32 ||
442
+ numeric_type == NumericType::Float16,
443
+ "SuperKMeans::train_ex: unsupported numeric type");
414
444
  FAISS_THROW_IF_NOT_MSG(n > 0, "SuperKMeans: n must be positive");
415
445
  FAISS_THROW_IF_MSG(x == nullptr, "SuperKMeans: x must not be null");
416
446
  FAISS_THROW_IF_NOT_MSG(
417
447
  n >= static_cast<idx_t>(k), "SuperKMeans: n must be >= k");
448
+ const bool fp16 = numeric_type == NumericType::Float16;
449
+ FAISS_THROW_IF_NOT_MSG(
450
+ !fp16 || cp.decode_block_size > 0,
451
+ "SuperKMeans: decode_block_size must be positive");
418
452
  if (cp.check_input_data_for_NaNs) {
419
- for (size_t i = 0; i < static_cast<size_t>(n) * d; i++) {
453
+ const size_t num_values = static_cast<size_t>(n) * d;
454
+ if (fp16) {
420
455
  FAISS_THROW_IF_NOT_MSG(
421
- std::isfinite(x[i]),
456
+ detail::fp16_all_finite(
457
+ num_values, static_cast<const uint16_t*>(x)),
422
458
  "SuperKMeans: input contains NaN's or Inf's");
459
+ } else {
460
+ const float* x32 = static_cast<const float*>(x);
461
+ for (size_t i = 0; i < num_values; i++) {
462
+ FAISS_THROW_IF_NOT_MSG(
463
+ std::isfinite(x32[i]),
464
+ "SuperKMeans: input contains NaN's or Inf's");
465
+ }
423
466
  }
424
467
  }
425
468
  if (cp.verbose && n < static_cast<idx_t>(k) * cp.min_points_per_centroid) {
@@ -435,8 +478,7 @@ void SuperKMeans::train(idx_t n, const float* x) {
435
478
  std::vector<int64_t> labels64;
436
479
  SuperKMeansAssignScratch assign_scratch;
437
480
  std::vector<float> hassign;
438
- [[maybe_unused]] auto sampled_x_owner =
439
- setup_train_state(state, labels64, hassign, cp, d, k, n, x);
481
+ setup_train_state(state, labels64, hassign, cp, d, k, n, x, numeric_type);
440
482
 
441
483
  iteration_stats.clear();
442
484
  iteration_stats.reserve(cp.niter);
@@ -625,7 +667,7 @@ void super_kmeans_assign_iteration(
625
667
  [[maybe_unused]] const int omp_chunk_local = cp.omp_chunk;
626
668
  int64_t tile_total = 0;
627
669
  int64_t tile_pruned = 0;
628
- #pragma omp parallel for schedule(dynamic, omp_chunk_local) \
670
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC, omp_chunk_local) \
629
671
  reduction(+ : tile_total) reduction(+ : tile_pruned)
630
672
  for (int i = 0; i < bx; ++i) {
631
673
  const float xnp_i = x_norms_partial[xi + i];
@@ -89,11 +89,16 @@ struct SuperKMeans {
89
89
 
90
90
  SuperKMeans(int d, int k, const SuperKMeansParameters& cp = {});
91
91
 
92
- /// Train on `n` row-major vectors of dimension `d`. Honors the applicable
93
- /// ClusteringParameters fields (niter, seed, verbose,
94
- /// max_points_per_centroid, use_faster_subsampling,
95
- /// check_input_data_for_NaNs).
92
+ /// Train on `n` row-major fp32 vectors of dimension `d`.
96
93
  void train(idx_t n, const float* x);
94
+
95
+ /** Train on vectors of the given numeric type.
96
+ *
97
+ * Float32 is the same as train(). Float16 takes packed IEEE binary16
98
+ * vectors and widens bounded blocks while rotating them into the fp32
99
+ * training workspace. Centroids and all iteration state remain fp32.
100
+ */
101
+ void train_ex(idx_t n, const void* x, NumericType numeric_type);
97
102
  };
98
103
 
99
104
  /// Reusable scratch for super_kmeans_assign_iteration; pass one instance across
@@ -17,6 +17,7 @@
17
17
 
18
18
  #include <faiss/IndexPQ.h>
19
19
  #include <faiss/impl/FaissAssert.h>
20
+ #include <faiss/impl/platform_macros.h>
20
21
  #include <faiss/utils/distances.h>
21
22
  #include <faiss/utils/random.h>
22
23
  #include <faiss/utils/utils.h>
@@ -423,13 +424,85 @@ static int next_power_of_2(int n) {
423
424
  return p;
424
425
  }
425
426
 
426
- // Generate three sign-flip vectors from the given seed.
427
+ // Smallest multiple of 64 >= n.
428
+ static int next_multiple_of_64(int n) {
429
+ return (n + 63) / 64 * 64;
430
+ }
431
+
432
+ static bool is_power_of_2(size_t n) {
433
+ return n > 0 && (n & (n - 1)) == 0;
434
+ }
435
+
436
+ // Largest power of 2 <= n, for n > 0.
437
+ static size_t prev_power_of_2(size_t n) {
438
+ size_t p = 1;
439
+ while (p * 2 <= n) {
440
+ p *= 2;
441
+ }
442
+ return p;
443
+ }
444
+
445
+ // Butterfly between the two halves of x (p even):
446
+ // (x_j, x_{j + p/2}) -> (x_j + x_{j + p/2}, x_j - x_{j + p/2}).
447
+ // Scales norms by sqrt(2).
448
+ static void kac_butterfly(float* x, size_t p) {
449
+ const size_t half = p / 2;
450
+ for (size_t j = 0; j < half; j++) {
451
+ float a = x[j];
452
+ float b = x[j + half];
453
+ x[j] = a + b;
454
+ x[j + half] = a - b;
455
+ }
456
+ }
457
+
458
+ // One round of the FHT-Kac rotation of a vector of p components: sign flips,
459
+ // normalized FWHT of the t components starting at offset, then the butterfly.
460
+ static void fht_kac_round(
461
+ float* x,
462
+ size_t p,
463
+ size_t t,
464
+ size_t offset,
465
+ const float* signs,
466
+ float fwht_scale) {
467
+ for (size_t j = 0; j < p; j++) {
468
+ x[j] *= signs[j];
469
+ }
470
+ fwht_inplace(x + offset, t);
471
+ for (size_t j = offset; j < offset + t; j++) {
472
+ x[j] *= fwht_scale;
473
+ }
474
+ kac_butterfly(x, p);
475
+ }
476
+
477
+ // Inverse of fht_kac_round, up to the sqrt(2) scaling of the butterfly: the
478
+ // butterfly, the normalized FWHT and the sign flips are each their own
479
+ // inverse, so they are applied in reverse order.
480
+ static void fht_kac_inverse_round(
481
+ float* x,
482
+ size_t p,
483
+ size_t t,
484
+ size_t offset,
485
+ const float* signs,
486
+ float fwht_scale) {
487
+ kac_butterfly(x, p);
488
+ fwht_inplace(x + offset, t);
489
+ for (size_t j = offset; j < offset + t; j++) {
490
+ x[j] *= fwht_scale;
491
+ }
492
+ for (size_t j = 0; j < p; j++) {
493
+ x[j] *= signs[j];
494
+ }
495
+ }
496
+
497
+ // Generate three sign-flip vectors from the given seed, and a fourth one
498
+ // after them if s4 is not null.
427
499
  static void generate_signs(
428
500
  uint32_t seed,
429
501
  size_t p,
430
502
  std::vector<float>& s1,
431
503
  std::vector<float>& s2,
432
- std::vector<float>& s3) {
504
+ std::vector<float>& s3,
505
+ std::vector<float>* s4) {
433
506
  FAISS_THROW_IF_NOT_MSG(
434
507
  p > 0, "number of Hadamard factors p must be positive");
435
508
  SplitMix64RandomGenerator rng(seed);
@@ -445,17 +518,36 @@ static void generate_signs(
445
518
  for (size_t j = 0; j < p; j++) {
446
519
  s3[j] = (rng.rand_int(2) == 0) ? -1.0f : 1.0f;
447
520
  }
521
+ if (s4) {
522
+ s4->resize(p);
523
+ for (size_t j = 0; j < p; j++) {
524
+ (*s4)[j] = (rng.rand_int(2) == 0) ? -1.0f : 1.0f;
525
+ }
526
+ }
448
527
  }
449
528
 
450
- HadamardRotation::HadamardRotation(int d, uint32_t seed_in)
451
- : VectorTransform(d, next_power_of_2(d)), seed(seed_in) {
529
+ HadamardRotation::HadamardRotation(
530
+ int d,
531
+ uint32_t seed_in,
532
+ bool pad_to_power_of_2_in)
533
+ : VectorTransform(
534
+ d,
535
+ pad_to_power_of_2_in ? next_power_of_2(d)
536
+ : next_multiple_of_64(d)),
537
+ seed(seed_in),
538
+ pad_to_power_of_2(pad_to_power_of_2_in) {
452
539
  init(seed_in);
453
540
  }
454
541
 
455
542
  void HadamardRotation::init(uint32_t seed_in) {
456
543
  seed = seed_in;
457
544
  is_trained = true;
458
- generate_signs(seed, d_out, signs1, signs2, signs3);
545
+ if (is_power_of_2(d_out)) {
546
+ generate_signs(seed, d_out, signs1, signs2, signs3, nullptr);
547
+ signs4.clear();
548
+ } else {
549
+ generate_signs(seed, d_out, signs1, signs2, signs3, &signs4);
550
+ }
459
551
  }
460
552
 
461
553
  void HadamardRotation::train(idx_t, const float*) {
@@ -477,11 +569,40 @@ void HadamardRotation::apply_noalloc(idx_t n, const float* x, float* xt) const {
477
569
  signs3.size() == p,
478
570
  "sign-flip vector 3 size must match output dimension");
479
571
 
572
+ if (!is_power_of_2(p)) {
573
+ // FHT-Kac rotation (pad_to_power_of_2 == false), see the header.
574
+ FAISS_THROW_IF_NOT_MSG(
575
+ signs4.size() == p,
576
+ "sign-flip vector 4 size must match output dimension");
577
+ const size_t t = prev_power_of_2(p);
578
+ const float fwht_scale = 1.0f / std::sqrt(static_cast<float>(t));
579
+
580
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
581
+ for (idx_t i = 0; i < n; i++) {
582
+ const float* xi = x + i * d;
583
+ float* xo = xt + i * p;
584
+
585
+ std::memcpy(xo, xi, d * sizeof(float));
586
+ for (size_t j = d; j < p; j++) {
587
+ xo[j] = 0.0f;
588
+ }
589
+ fht_kac_round(xo, p, t, 0, signs1.data(), fwht_scale);
590
+ fht_kac_round(xo, p, t, p - t, signs2.data(), fwht_scale);
591
+ fht_kac_round(xo, p, t, 0, signs3.data(), fwht_scale);
592
+ fht_kac_round(xo, p, t, p - t, signs4.data(), fwht_scale);
593
+ // The four butterflies scaled norms by sqrt(2)^4 = 4.
594
+ for (size_t j = 0; j < p; j++) {
595
+ xo[j] *= 0.25f;
596
+ }
597
+ }
598
+ return;
599
+ }
600
+
480
601
  // Each unnormalized FWHT scales norms by sqrt(p).
481
602
  // Three rounds scale by p^(3/2). Normalize once at the end.
482
603
  float total_scale = 1.0f / (p * std::sqrt(static_cast<float>(p)));
483
604
 
484
- #pragma omp parallel for schedule(dynamic)
605
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
485
606
  for (idx_t i = 0; i < n; i++) {
486
607
  const float* xi = x + i * d;
487
608
  float* xo = xt + i * p;
@@ -521,12 +642,36 @@ void HadamardRotation::reverse_transform(idx_t n, const float* xt, float* x)
521
642
  "HadamardRotation inverse requires equal input/output dimensions");
522
643
 
523
644
  const size_t p = d_out;
645
+
646
+ if (!is_power_of_2(p)) {
647
+ // Reverse of the FHT-Kac rotation: the rounds in reverse order.
648
+ FAISS_THROW_IF_NOT_MSG(
649
+ signs4.size() == p,
650
+ "sign-flip vector 4 size must match output dimension");
651
+ const size_t t = prev_power_of_2(p);
652
+ const float fwht_scale = 1.0f / std::sqrt(static_cast<float>(t));
653
+
654
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
655
+ for (idx_t i = 0; i < n; i++) {
656
+ float* xo = x + i * p;
657
+ std::memcpy(xo, xt + i * p, p * sizeof(float));
658
+ fht_kac_inverse_round(xo, p, t, p - t, signs4.data(), fwht_scale);
659
+ fht_kac_inverse_round(xo, p, t, 0, signs3.data(), fwht_scale);
660
+ fht_kac_inverse_round(xo, p, t, p - t, signs2.data(), fwht_scale);
661
+ fht_kac_inverse_round(xo, p, t, 0, signs1.data(), fwht_scale);
662
+ for (size_t j = 0; j < p; j++) {
663
+ xo[j] *= 0.25f;
664
+ }
665
+ }
666
+ return;
667
+ }
668
+
524
669
  // Reverse of apply_noalloc: three unnormalized FWHT rounds scale norms
525
670
  // by (sqrt(p))^3 = p*sqrt(p); the forward pass cancels this with
526
671
  // total_scale = 1/(p*sqrt(p)), so the inverse applies the same factor.
527
672
  const float inverse_scale = 1.0f / (p * std::sqrt(static_cast<float>(p)));
528
673
 
529
- #pragma omp parallel for schedule(dynamic)
674
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
530
675
  for (idx_t i = 0; i < n; i++) {
531
676
  const float* xi = xt + i * p;
532
677
  float* xo = x + i * p;
@@ -561,6 +706,9 @@ void HadamardRotation::check_identical(const VectorTransform& other) const {
561
706
  "HadamardRotation output dimensions must match");
562
707
  FAISS_THROW_IF_NOT_MSG(
563
708
  seed == hr->seed, "HadamardRotation seeds must match");
709
+ FAISS_THROW_IF_NOT_MSG(
710
+ pad_to_power_of_2 == hr->pad_to_power_of_2,
711
+ "HadamardRotation padding modes must match");
564
712
  }
565
713
 
566
714
  /*********************************************
@@ -718,7 +866,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
718
866
  }
719
867
 
720
868
  std::vector<double> covd(d_in * d_in);
721
- for (size_t i = 0; i < d_in * d_in; i++) {
869
+ for (size_t i = 0; i < static_cast<size_t>(d_in) * d_in; i++) {
722
870
  covd[i] = cov[i];
723
871
  }
724
872
 
@@ -726,7 +874,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
726
874
 
727
875
  eig(d_in, covd.data(), eigenvaluesd.data(), verbose);
728
876
 
729
- for (size_t i = 0; i < d_in * d_in; i++) {
877
+ for (size_t i = 0; i < static_cast<size_t>(d_in) * d_in; i++) {
730
878
  PCAMat[i] = covd[i];
731
879
  }
732
880
  eigenvalues.resize(d_in);
@@ -773,7 +921,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
773
921
  }
774
922
 
775
923
  std::vector<double> gramd(n * n);
776
- for (size_t i = 0; i < n * n; i++) {
924
+ for (idx_t i = 0; i < n * n; i++) {
777
925
  gramd[i] = gram[i];
778
926
  }
779
927
 
@@ -785,7 +933,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
785
933
 
786
934
  PCAMat.resize(d_in * n);
787
935
 
788
- for (size_t i = 0; i < n * n; i++) {
936
+ for (idx_t i = 0; i < n * n; i++) {
789
937
  gram[i] = gramd[i];
790
938
  }
791
939
 
@@ -129,14 +129,37 @@ struct RandomRotationMatrix : LinearTransform {
129
129
  /** Three rounds of random sign-flip + Fast Walsh-Hadamard Transform.
130
130
  * Produces a pseudo-random rotation in O(d log d) time.
131
131
  * d_out is the smallest power of 2 >= d_in (zero-padded as needed).
132
+ *
133
+ * With pad_to_power_of_2 = false, d_out is the smallest multiple of 64 >= d_in
134
+ * instead, which avoids e.g. padding d = 1536 to 2048. When that d_out is not
135
+ * a power of 2, the transform runs four rounds. Each round flips the signs of
136
+ * all d_out components, applies a normalized FWHT to the first (rounds 1 and
137
+ * 3) or the last (rounds 2 and 4) t of them, t being the largest power of 2
138
+ * <= d_out, and then mixes the two halves of the vector with the butterfly
139
+ * (x_j, x_{j + d_out/2}) -> (x_j + x_{j + d_out/2}, x_j - x_{j + d_out/2})
140
+ * / sqrt(2). This is the FHT-Kac rotation of the RaBitQ library.
141
+ *
142
+ * In index_factory: "HR" (power of 2) or "HRk" (multiple of 64), optionally
143
+ * followed by the seed, e.g. "HRk7".
132
144
  */
133
145
  struct HadamardRotation : VectorTransform {
134
146
  uint32_t seed{};
135
147
 
148
+ /// d_out is the smallest power of 2 >= d_in if true (the default), the
149
+ /// smallest multiple of 64 >= d_in if false
150
+ bool pad_to_power_of_2 = true;
151
+
136
152
  /// Sign-flip vectors, each of size d_out, generated from seed.
137
153
  std::vector<float> signs1, signs2, signs3;
138
154
 
139
- explicit HadamardRotation(int d, uint32_t seed = 12345);
155
+ /// Sign-flip vector of the fourth round. Only used, and only non-empty,
156
+ /// when d_out is not a power of 2.
157
+ std::vector<float> signs4;
158
+
159
+ explicit HadamardRotation(
160
+ int d,
161
+ uint32_t seed = 12345,
162
+ bool pad_to_power_of_2 = true);
140
163
 
141
164
  void init(uint32_t seed_in);
142
165
 
@@ -9,6 +9,8 @@
9
9
 
10
10
  #include <faiss/clone_index.h>
11
11
 
12
+ #include <memory>
13
+
12
14
  #include <faiss/impl/FaissAssert.h>
13
15
 
14
16
  #include <faiss/Index2Layer.h>
@@ -37,6 +39,7 @@
37
39
  #include <faiss/IndexNSG.h>
38
40
  #include <faiss/IndexPQ.h>
39
41
  #include <faiss/IndexPQFastScan.h>
42
+ #include <faiss/IndexPiPNN.h>
40
43
  #include <faiss/IndexPreTransform.h>
41
44
  #include <faiss/IndexRaBitQ.h>
42
45
  #include <faiss/IndexRefine.h>
@@ -170,6 +173,17 @@ IndexNSG* clone_IndexNSG(const IndexNSG* insg) {
170
173
  }
171
174
  }
172
175
 
176
+ static IndexPiPNN* clone_IndexPiPNN(const IndexPiPNN* ipipnn) {
177
+ TRYCLONE(IndexPiPNNFlat, ipipnn)
178
+ TRYCLONE(IndexPiPNNSQ, ipipnn) {
179
+ FAISS_THROW_IF_NOT_FMT(
180
+ typeid(*ipipnn) == typeid(IndexPiPNN),
181
+ "clone not supported for this type of IndexPiPNN: %s",
182
+ typeid(*ipipnn).name());
183
+ return new IndexPiPNN(*ipipnn);
184
+ }
185
+ }
186
+
173
187
  IndexRowwiseMinMaxBase* clone_IndexRowwiseMinMax(
174
188
  const IndexRowwiseMinMaxBase* irmmb) {
175
189
  TRYCLONE(IndexRowwiseMinMaxFP16, irmmb)
@@ -356,6 +370,16 @@ Index* Cloner::clone_Index(const Index* index) {
356
370
  res->own_fields = true;
357
371
  res->storage = clone_Index(insg->storage);
358
372
  return res;
373
+ } else if (
374
+ const IndexPiPNN* ipipnn = dynamic_cast<const IndexPiPNN*>(index)) {
375
+ // make sure we don't get a GPU index here
376
+ std::unique_ptr<Index> storage(
377
+ ipipnn->storage ? Cloner::clone_Index(ipipnn->storage)
378
+ : nullptr);
379
+ IndexPiPNN* res = clone_IndexPiPNN(ipipnn);
380
+ res->storage = storage.release();
381
+ res->own_fields = true;
382
+ return res;
359
383
  } else if (
360
384
  const IndexNNDescent* innd =
361
385
  dynamic_cast<const IndexNNDescent*>(index)) {