faiss 0.6.5 → 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 (157) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/ext/faiss/extconf.rb +1 -0
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/IVFlib.cpp +48 -51
  7. data/vendor/faiss/faiss/IVFlib.h +6 -2
  8. data/vendor/faiss/faiss/Index.cpp +2 -2
  9. data/vendor/faiss/faiss/Index.h +3 -3
  10. data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
  11. data/vendor/faiss/faiss/IndexBinary.h +2 -2
  12. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  13. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
  14. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
  15. data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
  16. data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
  17. data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
  18. data/vendor/faiss/faiss/IndexHNSW.h +2 -5
  19. data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
  20. data/vendor/faiss/faiss/IndexIDMap.h +10 -5
  21. data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
  22. data/vendor/faiss/faiss/IndexIVF.h +37 -1
  23. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
  24. data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
  25. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
  26. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
  27. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
  28. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  29. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  30. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
  31. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
  32. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  33. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  34. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
  35. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
  36. data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
  37. data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
  38. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  39. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  40. data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
  41. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
  42. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
  43. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  44. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  45. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  46. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  47. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
  48. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
  49. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  50. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
  51. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
  52. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  53. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  54. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
  55. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
  56. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
  57. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  58. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  59. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  60. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  61. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
  62. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
  63. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  64. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  65. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  66. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  67. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  68. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
  69. data/vendor/faiss/faiss/impl/HNSW.h +2 -2
  70. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  71. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  72. data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
  73. data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
  74. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  75. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  76. data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
  77. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
  78. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  79. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  80. data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
  81. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
  82. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  83. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
  84. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
  85. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
  86. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
  87. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
  88. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  89. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
  90. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  91. data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
  92. data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
  93. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  94. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  95. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  96. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  97. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  98. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  99. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  100. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  101. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  102. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  103. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  104. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  105. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  106. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  107. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
  108. data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
  109. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
  110. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  111. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  112. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
  113. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
  114. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  115. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
  116. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
  117. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
  118. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
  119. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
  120. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
  121. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  122. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  123. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  124. data/vendor/faiss/faiss/index_factory.cpp +30 -6
  125. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  126. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  127. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  128. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
  129. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  130. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  131. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  132. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  133. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  134. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  135. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  136. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  137. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  138. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  139. data/vendor/faiss/faiss/utils/distances.cpp +246 -34
  140. data/vendor/faiss/faiss/utils/distances.h +145 -2
  141. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  142. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  143. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  144. data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
  145. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
  146. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  147. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
  148. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
  149. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  150. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
  151. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  152. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
  153. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
  154. data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
  155. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  156. data/vendor/faiss/faiss/utils/utils.cpp +4 -0
  157. metadata +20 -1
@@ -164,7 +164,7 @@ inline uint64_t bitwise_and_dot_product<SIMDLevel::NONE>(
164
164
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
165
165
  uint64_t yv;
166
166
  std::memcpy(&yv, data + offset, sizeof(yv));
167
- for (int j = 0; j < qb; j++) {
167
+ for (size_t j = 0; j < qb; j++) {
168
168
  uint64_t qv;
169
169
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
170
170
  sum += popcount64(qv & yv) << j;
@@ -172,7 +172,7 @@ inline uint64_t bitwise_and_dot_product<SIMDLevel::NONE>(
172
172
  }
173
173
  for (; offset < size; ++offset) {
174
174
  const auto yv = *(data + offset);
175
- for (int j = 0; j < qb; j++) {
175
+ for (size_t j = 0; j < qb; j++) {
176
176
  const auto qv = *(query + j * size + offset);
177
177
  sum += popcount32(qv & yv) << j;
178
178
  }
@@ -194,7 +194,7 @@ inline BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
194
194
  uint64_t yv;
195
195
  std::memcpy(&yv, data + offset, sizeof(yv));
196
196
  popcount_sum += popcount64(yv);
197
- for (int j = 0; j < qb; j++) {
197
+ for (size_t j = 0; j < qb; j++) {
198
198
  uint64_t qv;
199
199
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
200
200
  dot_product += popcount64(qv & yv) << j;
@@ -203,7 +203,7 @@ inline BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
203
203
  for (; offset < size; ++offset) {
204
204
  const auto yv = *(data + offset);
205
205
  popcount_sum += popcount32(yv);
206
- for (int j = 0; j < qb; j++) {
206
+ for (size_t j = 0; j < qb; j++) {
207
207
  const auto qv = *(query + j * size + offset);
208
208
  dot_product += popcount32(qv & yv) << j;
209
209
  }
@@ -222,7 +222,7 @@ inline uint64_t bitwise_xor_dot_product<SIMDLevel::NONE>(
222
222
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
223
223
  uint64_t yv;
224
224
  std::memcpy(&yv, data + offset, sizeof(yv));
225
- for (int j = 0; j < qb; j++) {
225
+ for (size_t j = 0; j < qb; j++) {
226
226
  uint64_t qv;
227
227
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
228
228
  sum += popcount64(qv ^ yv) << j;
@@ -230,7 +230,7 @@ inline uint64_t bitwise_xor_dot_product<SIMDLevel::NONE>(
230
230
  }
231
231
  for (; offset < size; ++offset) {
232
232
  const auto yv = *(data + offset);
233
- for (int j = 0; j < qb; j++) {
233
+ for (size_t j = 0; j < qb; j++) {
234
234
  const auto qv = *(query + j * size + offset);
235
235
  sum += popcount32(qv ^ yv) << j;
236
236
  }
@@ -440,4 +440,118 @@ inline float compute_inner_product<SIMDLevel::NONE>(
440
440
  return ip_scalar(sign_bits, ex_code, rotated_q, 0, d, ex_bits, cb);
441
441
  }
442
442
 
443
+ /**
444
+ * compute_inner_product with the sign term supplied by the caller:
445
+ *
446
+ * (1 << ex_bits) * sign_ip + sum_i rotated_q[i] * ex_i + cb * q_sum
447
+ *
448
+ * With sign_ip = fvec_masked_sum(rotated_q, sign_bits, d) and
449
+ * q_sum = sum_i rotated_q[i], this equals
450
+ * compute_inner_product(sign_bits, ex_code, rotated_q, d, ex_bits, cb) up to
451
+ * float rounding. It serves codes whose sign bits are not stored next to
452
+ * ex_code, e.g. split between memory and disk: the caller accumulates the
453
+ * sign term piece by piece with fvec_masked_sum. q_sum depends on the
454
+ * query only, so it can be computed once per query.
455
+ *
456
+ * Unlike compute_inner_product, this reads only the (d * ex_bits + 7) / 8
457
+ * bytes of ex_code and never the bytes after them.
458
+ *
459
+ * @param ex_code packed extra-bit codes (ex_bits bits/dim); may be null
460
+ * when ex_bits == 0
461
+ * @param rotated_q rotated query vector (float[d])
462
+ * @param d dimensionality
463
+ * @param ex_bits number of extra bits per dimension, in [0, 8]
464
+ * @param cb constant bias: -(2^ex_bits - 0.5)
465
+ * @param sign_ip sum_i rotated_q[i] * sign_i
466
+ * @param q_sum sum_i rotated_q[i]
467
+ * @return inner product value
468
+ */
469
+ template <SIMDLevel SL = SINGLE_SIMD_LEVEL>
470
+ float compute_inner_product_with_sign_ip(
471
+ const uint8_t* __restrict ex_code,
472
+ const float* __restrict rotated_q,
473
+ size_t d,
474
+ size_t ex_bits,
475
+ float cb,
476
+ float sign_ip,
477
+ float q_sum);
478
+
479
+ /// SIMD levels with a split inner-product kernel.
480
+ constexpr int RABITQ_SPLIT_IP_SIMD_LEVELS = (1 << int(SIMDLevel::NONE)) |
481
+ (1 << int(SIMDLevel::AVX2)) | (1 << int(SIMDLevel::AVX512));
482
+
483
+ /// Load the code bytes at ex_code + offset into the low bytes of a uint64,
484
+ /// reading 8 bytes when they all lie within the first total_bytes bytes of
485
+ /// ex_code and only the remaining ones otherwise.
486
+ static inline uint64_t load_ex_code_window(
487
+ const uint8_t* ex_code,
488
+ size_t offset,
489
+ size_t total_bytes) {
490
+ uint64_t window = 0;
491
+ if (offset + sizeof(window) <= total_bytes) {
492
+ memcpy(&window, ex_code + offset, sizeof(window));
493
+ } else {
494
+ memcpy(&window, ex_code + offset, total_bytes - offset);
495
+ }
496
+ return window;
497
+ }
498
+
499
+ /// Scalar sum over [start, d) of rotated_q[i] * ex_i, for ex_bits in [1, 8].
500
+ /// Reads only the (d * ex_bits + 7) / 8 bytes of ex_code. Also serves as the
501
+ /// tail handler of the SIMD kernels.
502
+ static inline float ex_ip_scalar(
503
+ const uint8_t* __restrict ex_code,
504
+ const float* __restrict rotated_q,
505
+ size_t start,
506
+ size_t d,
507
+ size_t ex_bits) {
508
+ float result = 0.0f;
509
+ if (ex_bits == 8) {
510
+ for (size_t i = start; i < d; ++i) {
511
+ result += rotated_q[i] * static_cast<float>(ex_code[i]);
512
+ }
513
+ return result;
514
+ }
515
+ const unsigned code_mask = (1u << ex_bits) - 1;
516
+ for (size_t i = start; i < d; i++) {
517
+ const size_t bit_pos = i * ex_bits;
518
+ const size_t byte_idx = bit_pos / 8;
519
+ const size_t bit_offset = bit_pos % 8;
520
+ // With ex_bits <= 8 a code spans at most two bytes.
521
+ unsigned raw = ex_code[byte_idx];
522
+ if (bit_offset + ex_bits > 8) {
523
+ raw |= static_cast<unsigned>(ex_code[byte_idx + 1]) << 8;
524
+ }
525
+ const unsigned ex_val = (raw >> bit_offset) & code_mask;
526
+ result += rotated_q[i] * static_cast<float>(ex_val);
527
+ }
528
+ return result;
529
+ }
530
+
531
+ /// The terms compute_inner_product_with_sign_ip adds to the extra-bit inner
532
+ /// product ex_ip.
533
+ static inline float add_sign_and_bias_terms(
534
+ float ex_ip,
535
+ size_t ex_bits,
536
+ float cb,
537
+ float sign_ip,
538
+ float q_sum) {
539
+ return static_cast<float>(1u << ex_bits) * sign_ip + ex_ip + cb * q_sum;
540
+ }
541
+
542
+ template <>
543
+ inline float compute_inner_product_with_sign_ip<SIMDLevel::NONE>(
544
+ const uint8_t* __restrict ex_code,
545
+ const float* __restrict rotated_q,
546
+ size_t d,
547
+ size_t ex_bits,
548
+ float cb,
549
+ float sign_ip,
550
+ float q_sum) {
551
+ const float ex_ip = ex_bits == 0
552
+ ? 0.0f
553
+ : ex_ip_scalar(ex_code, rotated_q, 0, d, ex_bits);
554
+ return add_sign_and_bias_terms(ex_ip, ex_bits, cb, sign_ip, q_sum);
555
+ }
556
+
443
557
  } // namespace faiss::rabitq::multibit
@@ -768,14 +768,13 @@ void exhaustive_L2sqr_blas_cmax_sve(
768
768
  size_t nx,
769
769
  size_t ny,
770
770
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
771
- const float* y_norms) {
771
+ const float* y_norms,
772
+ size_t bs_x,
773
+ size_t bs_y) {
772
774
  // BLAS does not like empty matrices
773
775
  if (nx == 0 || ny == 0)
774
776
  return;
775
777
 
776
- /* block sizes */
777
- const size_t bs_x = distance_compute_blas_query_bs;
778
- const size_t bs_y = distance_compute_blas_database_bs;
779
778
  // const size_t bs_x = 16, bs_y = 16;
780
779
  std::unique_ptr<float[]> ip_block(new float[bs_x * bs_y]);
781
780
  std::unique_ptr<float[]> x_norms(new float[nx]);
@@ -952,9 +951,41 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
952
951
  size_t nx,
953
952
  size_t ny,
954
953
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
955
- const float* y_norms) {
954
+ const float* y_norms,
955
+ size_t query_block_size,
956
+ size_t database_block_size) {
956
957
  Fp32QueryTiles queries{x, d};
957
- exhaustive_L2sqr_blas_cmax_sve(queries, y, d, nx, ny, res, y_norms);
958
+ exhaustive_L2sqr_blas_cmax_sve(
959
+ queries,
960
+ y,
961
+ d,
962
+ nx,
963
+ ny,
964
+ res,
965
+ y_norms,
966
+ query_block_size,
967
+ database_block_size);
968
+ }
969
+
970
+ template <>
971
+ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
972
+ const float* x,
973
+ const float* y,
974
+ size_t d,
975
+ size_t nx,
976
+ size_t ny,
977
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
978
+ const float* y_norms) {
979
+ exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
980
+ x,
981
+ y,
982
+ d,
983
+ nx,
984
+ ny,
985
+ res,
986
+ y_norms,
987
+ distance_compute_blas_query_bs,
988
+ distance_compute_blas_database_bs);
958
989
  }
959
990
 
960
991
  template <>
@@ -967,7 +998,16 @@ void exhaustive_L2sqr_blas_cmax_fp16<SIMDLevel::ARM_SVE>(
967
998
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
968
999
  const float* y_norms) {
969
1000
  Fp16QueryTiles queries(x, d, nx);
970
- exhaustive_L2sqr_blas_cmax_sve(queries, y, d, nx, ny, res, y_norms);
1001
+ exhaustive_L2sqr_blas_cmax_sve(
1002
+ queries,
1003
+ y,
1004
+ d,
1005
+ nx,
1006
+ ny,
1007
+ res,
1008
+ y_norms,
1009
+ distance_compute_blas_query_bs,
1010
+ distance_compute_blas_database_bs);
971
1011
  }
972
1012
 
973
1013
  } // namespace faiss
@@ -208,7 +208,10 @@ float VectorDistance<METRIC_Canberra, SL>::operator()(
208
208
  float accu = 0;
209
209
  for (size_t i = 0; i < this->d; i++) {
210
210
  float xi = x[i], yi = y[i];
211
- accu += fabs(xi - yi) / (fabs(xi) + fabs(yi));
211
+ float denominator = fabs(xi) + fabs(yi);
212
+ if (denominator != 0.0f) {
213
+ accu += fabs(xi - yi) / denominator;
214
+ }
212
215
  }
213
216
  return accu;
214
217
  }
@@ -230,12 +233,13 @@ template <>
230
233
  float VectorDistance<METRIC_JensenShannon, SL>::operator()(
231
234
  const float* x,
232
235
  const float* y) const {
236
+ constexpr float kLn2 = 0.6931471805599453f;
233
237
  float accu = 0;
234
238
  for (size_t i = 0; i < this->d; i++) {
235
239
  float xi = x[i], yi = y[i];
236
240
  float mi = 0.5 * (xi + yi);
237
- float kl1 = -xi * log(mi / xi);
238
- float kl2 = -yi * log(mi / yi);
241
+ float kl1 = xi > 0 ? (yi == 0 ? xi * kLn2 : -xi * log(mi / xi)) : 0.0f;
242
+ float kl2 = yi > 0 ? (xi == 0 ? yi * kLn2 : -yi * log(mi / yi)) : 0.0f;
239
243
  accu += kl1 + kl2;
240
244
  }
241
245
  return 0.5 * accu;
@@ -1250,15 +1250,14 @@ void exhaustive_L2sqr_blas_cmax_avx2(
1250
1250
  size_t nx,
1251
1251
  size_t ny,
1252
1252
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1253
- const float* y_norms) {
1253
+ const float* y_norms,
1254
+ size_t bs_x,
1255
+ size_t bs_y) {
1254
1256
  // BLAS does not like empty matrices
1255
1257
  if (nx == 0 || ny == 0) {
1256
1258
  return;
1257
1259
  }
1258
1260
 
1259
- /* block sizes */
1260
- const size_t bs_x = distance_compute_blas_query_bs;
1261
- const size_t bs_y = distance_compute_blas_database_bs;
1262
1261
  // const size_t bs_x = 16, bs_y = 16;
1263
1262
  std::unique_ptr<float[]> ip_block(new float[bs_x * bs_y]);
1264
1263
  std::unique_ptr<float[]> x_norms(new float[nx]);
@@ -1468,9 +1467,41 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX2>(
1468
1467
  size_t nx,
1469
1468
  size_t ny,
1470
1469
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1471
- const float* y_norms) {
1470
+ const float* y_norms,
1471
+ size_t query_block_size,
1472
+ size_t database_block_size) {
1472
1473
  Fp32QueryTiles queries{x, d};
1473
- exhaustive_L2sqr_blas_cmax_avx2(queries, y, d, nx, ny, res, y_norms);
1474
+ exhaustive_L2sqr_blas_cmax_avx2(
1475
+ queries,
1476
+ y,
1477
+ d,
1478
+ nx,
1479
+ ny,
1480
+ res,
1481
+ y_norms,
1482
+ query_block_size,
1483
+ database_block_size);
1484
+ }
1485
+
1486
+ template <>
1487
+ void exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX2>(
1488
+ const float* x,
1489
+ const float* y,
1490
+ size_t d,
1491
+ size_t nx,
1492
+ size_t ny,
1493
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
1494
+ const float* y_norms) {
1495
+ exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX2>(
1496
+ x,
1497
+ y,
1498
+ d,
1499
+ nx,
1500
+ ny,
1501
+ res,
1502
+ y_norms,
1503
+ distance_compute_blas_query_bs,
1504
+ distance_compute_blas_database_bs);
1474
1505
  }
1475
1506
 
1476
1507
  template <>
@@ -1483,7 +1514,16 @@ void exhaustive_L2sqr_blas_cmax_fp16<SIMDLevel::AVX2>(
1483
1514
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1484
1515
  const float* y_norms) {
1485
1516
  Fp16QueryTiles queries(x, d, nx);
1486
- exhaustive_L2sqr_blas_cmax_avx2(queries, y, d, nx, ny, res, y_norms);
1517
+ exhaustive_L2sqr_blas_cmax_avx2(
1518
+ queries,
1519
+ y,
1520
+ d,
1521
+ nx,
1522
+ ny,
1523
+ res,
1524
+ y_norms,
1525
+ distance_compute_blas_query_bs,
1526
+ distance_compute_blas_database_bs);
1487
1527
  }
1488
1528
 
1489
1529
  } // namespace faiss
@@ -677,7 +677,7 @@ void fvec_L2sqr_ny<SIMDLevel::AVX512>(
677
677
 
678
678
  template <>
679
679
  size_t fvec_L2sqr_ny_nearest_D2<SIMDLevel::AVX512>(
680
- float* distances_tmp_buffer,
680
+ float* /*distances_tmp_buffer*/,
681
681
  const float* x,
682
682
  const float* y,
683
683
  size_t ny) {
@@ -769,7 +769,7 @@ size_t fvec_L2sqr_ny_nearest_D2<SIMDLevel::AVX512>(
769
769
 
770
770
  template <>
771
771
  size_t fvec_L2sqr_ny_nearest_D4<SIMDLevel::AVX512>(
772
- float* distances_tmp_buffer,
772
+ float* /*distances_tmp_buffer*/,
773
773
  const float* x,
774
774
  const float* y,
775
775
  size_t ny) {
@@ -866,7 +866,7 @@ size_t fvec_L2sqr_ny_nearest_D4<SIMDLevel::AVX512>(
866
866
 
867
867
  template <>
868
868
  size_t fvec_L2sqr_ny_nearest_D8<SIMDLevel::AVX512>(
869
- float* distances_tmp_buffer,
869
+ float* /*distances_tmp_buffer*/,
870
870
  const float* x,
871
871
  const float* y,
872
872
  size_t ny) {
@@ -1170,15 +1170,14 @@ void exhaustive_L2sqr_blas_cmax_avx512(
1170
1170
  size_t nx,
1171
1171
  size_t ny,
1172
1172
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1173
- const float* y_norms) {
1173
+ const float* y_norms,
1174
+ size_t bs_x,
1175
+ size_t bs_y) {
1174
1176
  // BLAS does not like empty matrices
1175
1177
  if (nx == 0 || ny == 0) {
1176
1178
  return;
1177
1179
  }
1178
1180
 
1179
- /* block sizes */
1180
- const size_t bs_x = distance_compute_blas_query_bs;
1181
- const size_t bs_y = distance_compute_blas_database_bs;
1182
1181
  std::unique_ptr<float[]> ip_block(new float[bs_x * bs_y]);
1183
1182
  std::unique_ptr<float[]> x_norms(new float[nx]);
1184
1183
  std::unique_ptr<float[]> del2;
@@ -1381,9 +1380,41 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX512>(
1381
1380
  size_t nx,
1382
1381
  size_t ny,
1383
1382
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1384
- const float* y_norms) {
1383
+ const float* y_norms,
1384
+ size_t query_block_size,
1385
+ size_t database_block_size) {
1385
1386
  Fp32QueryTiles queries{x, d};
1386
- exhaustive_L2sqr_blas_cmax_avx512(queries, y, d, nx, ny, res, y_norms);
1387
+ exhaustive_L2sqr_blas_cmax_avx512(
1388
+ queries,
1389
+ y,
1390
+ d,
1391
+ nx,
1392
+ ny,
1393
+ res,
1394
+ y_norms,
1395
+ query_block_size,
1396
+ database_block_size);
1397
+ }
1398
+
1399
+ template <>
1400
+ void exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX512>(
1401
+ const float* x,
1402
+ const float* y,
1403
+ size_t d,
1404
+ size_t nx,
1405
+ size_t ny,
1406
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
1407
+ const float* y_norms) {
1408
+ exhaustive_L2sqr_blas_cmax<SIMDLevel::AVX512>(
1409
+ x,
1410
+ y,
1411
+ d,
1412
+ nx,
1413
+ ny,
1414
+ res,
1415
+ y_norms,
1416
+ distance_compute_blas_query_bs,
1417
+ distance_compute_blas_database_bs);
1387
1418
  }
1388
1419
 
1389
1420
  template <>
@@ -1396,7 +1427,16 @@ void exhaustive_L2sqr_blas_cmax_fp16<SIMDLevel::AVX512>(
1396
1427
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
1397
1428
  const float* y_norms) {
1398
1429
  Fp16QueryTiles queries(x, d, nx);
1399
- exhaustive_L2sqr_blas_cmax_avx512(queries, y, d, nx, ny, res, y_norms);
1430
+ exhaustive_L2sqr_blas_cmax_avx512(
1431
+ queries,
1432
+ y,
1433
+ d,
1434
+ nx,
1435
+ ny,
1436
+ res,
1437
+ y_norms,
1438
+ distance_compute_blas_query_bs,
1439
+ distance_compute_blas_database_bs);
1400
1440
  }
1401
1441
 
1402
1442
  } // namespace faiss
@@ -149,14 +149,14 @@ void compute_PQ_dis_tables_dsub2<THE_SIMD_LEVEL>(
149
149
  FAISS_THROW_IF_NOT(ksub % 8 == 0);
150
150
 
151
151
  for (size_t m0 = 0; m0 < M; m0 += 4) {
152
- int m1 = std::min(M, m0 + 4);
153
- for (int k0 = 0; k0 < ksub; k0 += 8) {
152
+ size_t m1 = std::min(M, m0 + 4);
153
+ for (size_t k0 = 0; k0 < ksub; k0 += 8) {
154
154
  simd8float32_tpl<THE_SIMD_LEVEL> centroids[8];
155
155
  for (int k = 0; k < 8; k++) {
156
156
  ALIGNED(32) float centroid[8];
157
157
  size_t wp = 0;
158
158
  size_t rp = (m0 * ksub + k + k0) * 2;
159
- for (int m = m0; m < m1; m++) {
159
+ for (size_t m = m0; m < m1; m++) {
160
160
  centroid[wp++] = all_centroids[rp];
161
161
  centroid[wp++] = all_centroids[rp + 1];
162
162
  rp += 2 * ksub;
@@ -106,6 +106,19 @@ void exhaustive_L2sqr_blas_cmax(
106
106
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
107
107
  const float* y_norms);
108
108
 
109
+ /// Per-call block sizes; the original overload retains its global defaults.
110
+ template <SIMDLevel>
111
+ void exhaustive_L2sqr_blas_cmax(
112
+ const float* x,
113
+ const float* y,
114
+ size_t d,
115
+ size_t nx,
116
+ size_t ny,
117
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
118
+ const float* y_norms,
119
+ size_t query_block_size,
120
+ size_t database_block_size);
121
+
109
122
  /// Same as exhaustive_L2sqr_blas_cmax for packed IEEE binary16 queries.
110
123
  template <SIMDLevel>
111
124
  void exhaustive_L2sqr_blas_cmax_fp16(
@@ -66,6 +66,21 @@ using simd8float32 = simd8float32_tpl<THE_SL_256>;
66
66
  * SIMD routines when vals is an aligned array of uint16_t
67
67
  ******************************************************************/
68
68
 
69
+ // Forward declarations of histogram functions defined below, outside
70
+ // simd_partitioning
71
+ void local_simd_histogram_8(
72
+ const uint16_t* data,
73
+ int n,
74
+ uint16_t min,
75
+ int shift,
76
+ int* hist);
77
+ void local_simd_histogram_16(
78
+ const uint16_t* data,
79
+ int n,
80
+ uint16_t min,
81
+ int shift,
82
+ int* hist);
83
+
69
84
  namespace simd_partitioning {
70
85
 
71
86
  void find_minimax(
@@ -338,7 +353,7 @@ uint16_t simd_partition_fuzzy_with_bounds(
338
353
  IFV printf(
339
354
  " override: thresh=%d n_eq_1=%" PRId64 "\n", thresh, n_eq_1);
340
355
  } else {
341
- assert(n_eq_1 <= n_eq);
356
+ assert(static_cast<size_t>(n_eq_1) <= n_eq);
342
357
  }
343
358
 
344
359
  size_t wp = simd_compress_array<C>(vals, ids, n, thresh, n_eq_1);
@@ -357,20 +372,6 @@ uint16_t simd_partition_fuzzy_with_bounds(
357
372
  return thresh;
358
373
  }
359
374
 
360
- // Forward declarations of histogram functions defined below
361
- void local_simd_histogram_8(
362
- const uint16_t* data,
363
- int n,
364
- uint16_t min,
365
- int shift,
366
- int* hist);
367
- void local_simd_histogram_16(
368
- const uint16_t* data,
369
- int n,
370
- uint16_t min,
371
- int shift,
372
- int* hist);
373
-
374
375
  template <class C>
375
376
  uint16_t simd_partition_fuzzy_with_bounds_histogram(
376
377
  uint16_t* vals,