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
@@ -5,6 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ #include <faiss/utils/distances.h>
8
9
  #include <faiss/utils/rabitq_simd.h>
9
10
  #include <immintrin.h>
10
11
  #include <limits>
@@ -248,7 +249,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX2>(
248
249
  __m256i sum_256 = _mm256_setzero_si256();
249
250
  for (; offset + step <= size; offset += step) {
250
251
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
251
- for (int j = 0; j < qb; j++) {
252
+ for (size_t j = 0; j < qb; j++) {
252
253
  __m256i v_q = _mm256_loadu_si256(
253
254
  (const __m256i*)(query + j * size + offset));
254
255
  __m256i v_and = _mm256_and_si256(v_q, v_x);
@@ -262,7 +263,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX2>(
262
263
  __m128i sum_128 = _mm_setzero_si128();
263
264
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
264
265
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
265
- for (int j = 0; j < qb; j++) {
266
+ for (size_t j = 0; j < qb; j++) {
266
267
  __m128i v_q = _mm_loadu_si128(
267
268
  (const __m128i*)(query + j * size + offset));
268
269
  __m128i v_and = _mm_and_si128(v_q, v_x);
@@ -275,7 +276,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX2>(
275
276
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
276
277
  uint64_t yv;
277
278
  std::memcpy(&yv, data + offset, sizeof(yv));
278
- for (int j = 0; j < qb; j++) {
279
+ for (size_t j = 0; j < qb; j++) {
279
280
  uint64_t qv;
280
281
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
281
282
  sum += popcount64(qv & yv) << j;
@@ -283,7 +284,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX2>(
283
284
  }
284
285
  for (; offset < size; ++offset) {
285
286
  const uint8_t yv = *(data + offset);
286
- for (int j = 0; j < qb; j++) {
287
+ for (size_t j = 0; j < qb; j++) {
287
288
  const uint8_t qv = *(query + j * size + offset);
288
289
  sum += popcount32(qv & yv) << j;
289
290
  }
@@ -307,7 +308,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
307
308
  for (; offset + step <= size; offset += step) {
308
309
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
309
310
  pop_256 = _mm256_add_epi64(pop_256, popcount_256(v_x));
310
- for (int j = 0; j < qb; j++) {
311
+ for (size_t j = 0; j < qb; j++) {
311
312
  __m256i v_q = _mm256_loadu_si256(
312
313
  (const __m256i*)(query + j * size + offset));
313
314
  __m256i v_and = _mm256_and_si256(v_q, v_x);
@@ -324,7 +325,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
324
325
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
325
326
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
326
327
  pop_128 = _mm_add_epi64(pop_128, popcount_128(v_x));
327
- for (int j = 0; j < qb; j++) {
328
+ for (size_t j = 0; j < qb; j++) {
328
329
  __m128i v_q = _mm_loadu_si128(
329
330
  (const __m128i*)(query + j * size + offset));
330
331
  __m128i v_and = _mm_and_si128(v_q, v_x);
@@ -339,7 +340,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
339
340
  uint64_t yv;
340
341
  std::memcpy(&yv, data + offset, sizeof(yv));
341
342
  popcount_sum += popcount64(yv);
342
- for (int j = 0; j < qb; j++) {
343
+ for (size_t j = 0; j < qb; j++) {
343
344
  uint64_t qv;
344
345
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
345
346
  dot_product += popcount64(qv & yv) << j;
@@ -348,7 +349,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
348
349
  for (; offset < size; ++offset) {
349
350
  const uint8_t yv = *(data + offset);
350
351
  popcount_sum += popcount32(yv);
351
- for (int j = 0; j < qb; j++) {
352
+ for (size_t j = 0; j < qb; j++) {
352
353
  const uint8_t qv = *(query + j * size + offset);
353
354
  dot_product += popcount32(qv & yv) << j;
354
355
  }
@@ -368,7 +369,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX2>(
368
369
  __m256i sum_256 = _mm256_setzero_si256();
369
370
  for (; offset + step <= size; offset += step) {
370
371
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
371
- for (int j = 0; j < qb; j++) {
372
+ for (size_t j = 0; j < qb; j++) {
372
373
  __m256i v_q = _mm256_loadu_si256(
373
374
  (const __m256i*)(query + j * size + offset));
374
375
  __m256i v_xor = _mm256_xor_si256(v_q, v_x);
@@ -382,7 +383,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX2>(
382
383
  __m128i sum_128 = _mm_setzero_si128();
383
384
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
384
385
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
385
- for (int j = 0; j < qb; j++) {
386
+ for (size_t j = 0; j < qb; j++) {
386
387
  __m128i v_q = _mm_loadu_si128(
387
388
  (const __m128i*)(query + j * size + offset));
388
389
  __m128i v_xor = _mm_xor_si128(v_q, v_x);
@@ -395,7 +396,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX2>(
395
396
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
396
397
  uint64_t yv;
397
398
  std::memcpy(&yv, data + offset, sizeof(yv));
398
- for (int j = 0; j < qb; j++) {
399
+ for (size_t j = 0; j < qb; j++) {
399
400
  uint64_t qv;
400
401
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
401
402
  sum += popcount64(qv ^ yv) << j;
@@ -403,7 +404,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX2>(
403
404
  }
404
405
  for (; offset < size; ++offset) {
405
406
  const auto yv = *(data + offset);
406
- for (int j = 0; j < qb; j++) {
407
+ for (size_t j = 0; j < qb; j++) {
407
408
  const auto qv = *(query + j * size + offset);
408
409
  sum += popcount32(qv ^ yv) << j;
409
410
  }
@@ -522,6 +523,43 @@ inline float ip_1exbit_avx2(
522
523
  return result;
523
524
  }
524
525
 
526
+ /// Bit-plane extraction for ex_bits in [2, 7]: plane b of 8 codes packed in
527
+ /// a uint64 is _pext_u64(codes, masks[b]), and has weight 2^b.
528
+ struct BitplaneTablesAVX2 {
529
+ uint64_t masks[7];
530
+ __m256 weights[8]; ///< weights[ex_bits] = 2^ex_bits, for the sign bit
531
+
532
+ explicit BitplaneTablesAVX2(size_t ex_bits) {
533
+ for (size_t b = 0; b < ex_bits; b++) {
534
+ uint64_t m = 0;
535
+ for (int j = 0; j < 8; j++) {
536
+ m |= (1ULL << (b + j * ex_bits));
537
+ }
538
+ masks[b] = m;
539
+ weights[b] = _mm256_set1_ps(static_cast<float>(1u << b));
540
+ }
541
+ weights[ex_bits] = _mm256_set1_ps(static_cast<float>(1u << ex_bits));
542
+ }
543
+ };
544
+
545
+ /// recon plus the extra-bit values of the 8 codes packed in ex64
546
+ inline __m256 add_bitplanes_avx2(
547
+ __m256 recon,
548
+ uint64_t ex64,
549
+ const BitplaneTablesAVX2& t,
550
+ size_t ex_bits) {
551
+ const __m256i bit_pos = _mm256_setr_epi32(1, 2, 4, 8, 16, 32, 64, 128);
552
+ const __m256i zero = _mm256_setzero_si256();
553
+ for (size_t b = 0; b < ex_bits; b++) {
554
+ auto plane = static_cast<uint8_t>(_pext_u64(ex64, t.masks[b]));
555
+ __m256i p_cmp = _mm256_cmpgt_epi32(
556
+ _mm256_and_si256(_mm256_set1_epi32(plane), bit_pos), zero);
557
+ recon = _mm256_add_ps(
558
+ recon, _mm256_and_ps(_mm256_castsi256_ps(p_cmp), t.weights[b]));
559
+ }
560
+ return recon;
561
+ }
562
+
525
563
  inline float ip_bitplane_avx2(
526
564
  const uint8_t* __restrict sign_bits,
527
565
  const uint8_t* __restrict ex_code,
@@ -530,42 +568,22 @@ inline float ip_bitplane_avx2(
530
568
  size_t ex_bits,
531
569
  float cb) {
532
570
  __m256 acc = _mm256_setzero_ps();
533
- const __m256 v_one = _mm256_set1_ps(1.0f);
534
571
  const __m256i bit_pos = _mm256_setr_epi32(1, 2, 4, 8, 16, 32, 64, 128);
535
572
  const __m256i zero = _mm256_setzero_si256();
536
573
  const __m256 v_cb = _mm256_set1_ps(cb);
537
-
538
- uint64_t pext_masks[7];
539
- __m256 v_weights[8];
540
- for (size_t b = 0; b < ex_bits; b++) {
541
- uint64_t m = 0;
542
- for (int j = 0; j < 8; j++) {
543
- m |= (1ULL << (b + j * ex_bits));
544
- }
545
- pext_masks[b] = m;
546
- v_weights[b] = _mm256_set1_ps(static_cast<float>(1u << b));
547
- }
548
- v_weights[ex_bits] = _mm256_set1_ps(static_cast<float>(1u << ex_bits));
574
+ const BitplaneTablesAVX2 tables(ex_bits);
549
575
 
550
576
  size_t i = 0;
551
577
  for (; i + 8 <= d; i += 8) {
552
578
  __m256i sb_cmp = _mm256_cmpgt_epi32(
553
579
  _mm256_and_si256(_mm256_set1_epi32(sign_bits[i / 8]), bit_pos),
554
580
  zero);
555
- __m256 recon = _mm256_mul_ps(
556
- _mm256_and_ps(_mm256_castsi256_ps(sb_cmp), v_one),
557
- v_weights[ex_bits]);
581
+ __m256 recon = _mm256_and_ps(
582
+ _mm256_castsi256_ps(sb_cmp), tables.weights[ex_bits]);
558
583
 
559
584
  uint64_t ex64 = 0;
560
585
  memcpy(&ex64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
561
-
562
- for (size_t b = 0; b < ex_bits; b++) {
563
- auto plane = static_cast<uint8_t>(_pext_u64(ex64, pext_masks[b]));
564
- __m256i p_cmp = _mm256_cmpgt_epi32(
565
- _mm256_and_si256(_mm256_set1_epi32(plane), bit_pos), zero);
566
- __m256 p_f = _mm256_and_ps(_mm256_castsi256_ps(p_cmp), v_one);
567
- recon = _mm256_fmadd_ps(p_f, v_weights[b], recon);
568
- }
586
+ recon = add_bitplanes_avx2(recon, ex64, tables, ex_bits);
569
587
 
570
588
  __m256 rq = _mm256_loadu_ps(rotated_q + i);
571
589
  acc = _mm256_fmadd_ps(rq, _mm256_add_ps(recon, v_cb), acc);
@@ -576,6 +594,62 @@ inline float ip_bitplane_avx2(
576
594
  return result;
577
595
  }
578
596
 
597
+ // sum_i rotated_q[i] * ex_code[i] for one-byte extra codes.
598
+ inline float ex_ip_8bit_avx2(
599
+ const uint8_t* __restrict ex_code,
600
+ const float* __restrict rotated_q,
601
+ size_t d) {
602
+ __m256 acc0 = _mm256_setzero_ps();
603
+ __m256 acc1 = _mm256_setzero_ps();
604
+ size_t i = 0;
605
+ for (; i + 16 <= d; i += 16) {
606
+ const __m128i bytes =
607
+ _mm_loadu_si128(reinterpret_cast<const __m128i*>(ex_code + i));
608
+ acc0 = _mm256_fmadd_ps(
609
+ _mm256_loadu_ps(rotated_q + i),
610
+ _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(bytes)),
611
+ acc0);
612
+ acc1 = _mm256_fmadd_ps(
613
+ _mm256_loadu_ps(rotated_q + i + 8),
614
+ _mm256_cvtepi32_ps(
615
+ _mm256_cvtepu8_epi32(_mm_srli_si128(bytes, 8))),
616
+ acc1);
617
+ }
618
+ for (; i + 8 <= d; i += 8) {
619
+ const __m128i bytes =
620
+ _mm_loadl_epi64(reinterpret_cast<const __m128i*>(ex_code + i));
621
+ acc0 = _mm256_fmadd_ps(
622
+ _mm256_loadu_ps(rotated_q + i),
623
+ _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(bytes)),
624
+ acc0);
625
+ }
626
+ return hsum_avx2(_mm256_add_ps(acc0, acc1)) +
627
+ ex_ip_scalar(ex_code, rotated_q, i, d, 8);
628
+ }
629
+
630
+ // sum_i rotated_q[i] * ex_i for ex_bits in [2, 7]: ip_bitplane_avx2 without
631
+ // the sign bits, and without reading past the codes.
632
+ inline float ex_ip_bitplane_avx2(
633
+ const uint8_t* __restrict ex_code,
634
+ const float* __restrict rotated_q,
635
+ size_t d,
636
+ size_t ex_bits) {
637
+ __m256 acc = _mm256_setzero_ps();
638
+ const BitplaneTablesAVX2 tables(ex_bits);
639
+
640
+ const size_t total_bytes = (d * ex_bits + 7) / 8;
641
+ size_t i = 0;
642
+ for (; i + 8 <= d; i += 8) {
643
+ const uint64_t ex64 =
644
+ load_ex_code_window(ex_code, (i / 8) * ex_bits, total_bytes);
645
+ const __m256 recon =
646
+ add_bitplanes_avx2(_mm256_setzero_ps(), ex64, tables, ex_bits);
647
+ acc = _mm256_fmadd_ps(_mm256_loadu_ps(rotated_q + i), recon, acc);
648
+ }
649
+
650
+ return hsum_avx2(acc) + ex_ip_scalar(ex_code, rotated_q, i, d, ex_bits);
651
+ }
652
+
579
653
  } // namespace
580
654
 
581
655
  template <>
@@ -636,4 +710,25 @@ float compute_inner_product<SIMDLevel::AVX2>(
636
710
  return ip_scalar(sign_bits, ex_code, rotated_q, 0, d, ex_bits, cb);
637
711
  }
638
712
 
713
+ template <>
714
+ float compute_inner_product_with_sign_ip<SIMDLevel::AVX2>(
715
+ const uint8_t* __restrict ex_code,
716
+ const float* __restrict rotated_q,
717
+ size_t d,
718
+ size_t ex_bits,
719
+ float cb,
720
+ float sign_ip,
721
+ float q_sum) {
722
+ float ex_ip = 0.0f;
723
+ if (ex_bits == 8) {
724
+ ex_ip = ex_ip_8bit_avx2(ex_code, rotated_q, d);
725
+ } else if (ex_bits == 1) {
726
+ // One extra bit per dim has the sign-bit layout.
727
+ ex_ip = fvec_masked_sum<SIMDLevel::AVX2>(rotated_q, ex_code, d);
728
+ } else if (ex_bits >= 2 && ex_bits <= 7) {
729
+ ex_ip = ex_ip_bitplane_avx2(ex_code, rotated_q, d, ex_bits);
730
+ }
731
+ return add_sign_and_bias_terms(ex_ip, ex_bits, cb, sign_ip, q_sum);
732
+ }
733
+
639
734
  } // namespace faiss::rabitq::multibit
@@ -5,6 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ #include <faiss/utils/distances.h>
8
9
  #include <faiss/utils/rabitq_simd.h>
9
10
  #include <immintrin.h>
10
11
  #include <limits>
@@ -336,7 +337,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
336
337
  __m512i sum_512 = _mm512_setzero_si512();
337
338
  for (; offset + step <= size; offset += step) {
338
339
  __m512i v_x = _mm512_loadu_si512((const __m512i*)(data + offset));
339
- for (int j = 0; j < qb; j++) {
340
+ for (size_t j = 0; j < qb; j++) {
340
341
  __m512i v_q = _mm512_loadu_si512(
341
342
  (const __m512i*)(query + j * size + offset));
342
343
  __m512i v_and = _mm512_and_si512(v_q, v_x);
@@ -351,7 +352,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
351
352
  __m256i sum_256 = _mm256_setzero_si256();
352
353
  for (; offset + step <= size; offset += step) {
353
354
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
354
- for (int j = 0; j < qb; j++) {
355
+ for (size_t j = 0; j < qb; j++) {
355
356
  __m256i v_q = _mm256_loadu_si256(
356
357
  (const __m256i*)(query + j * size + offset));
357
358
  __m256i v_and = _mm256_and_si256(v_q, v_x);
@@ -365,7 +366,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
365
366
  __m128i sum_128 = _mm_setzero_si128();
366
367
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
367
368
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
368
- for (int j = 0; j < qb; j++) {
369
+ for (size_t j = 0; j < qb; j++) {
369
370
  __m128i v_q = _mm_loadu_si128(
370
371
  (const __m128i*)(query + j * size + offset));
371
372
  __m128i v_and = _mm_and_si128(v_q, v_x);
@@ -378,7 +379,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
378
379
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
379
380
  uint64_t yv;
380
381
  std::memcpy(&yv, data + offset, sizeof(yv));
381
- for (int j = 0; j < qb; j++) {
382
+ for (size_t j = 0; j < qb; j++) {
382
383
  uint64_t qv;
383
384
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
384
385
  sum += popcount64(qv & yv) << j;
@@ -386,7 +387,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
386
387
  }
387
388
  for (; offset < size; ++offset) {
388
389
  const auto yv = *(data + offset);
389
- for (int j = 0; j < qb; j++) {
390
+ for (size_t j = 0; j < qb; j++) {
390
391
  const auto qv = *(query + j * size + offset);
391
392
  sum += popcount32(qv & yv) << j;
392
393
  }
@@ -410,7 +411,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
410
411
  for (; offset + step <= size; offset += step) {
411
412
  __m512i v_x = _mm512_loadu_si512((const __m512i*)(data + offset));
412
413
  pop_512 = _mm512_add_epi64(pop_512, popcount_512(v_x));
413
- for (int j = 0; j < qb; j++) {
414
+ for (size_t j = 0; j < qb; j++) {
414
415
  __m512i v_q = _mm512_loadu_si512(
415
416
  (const __m512i*)(query + j * size + offset));
416
417
  __m512i v_and = _mm512_and_si512(v_q, v_x);
@@ -428,7 +429,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
428
429
  for (; offset + step <= size; offset += step) {
429
430
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
430
431
  pop_256 = _mm256_add_epi64(pop_256, popcount_256(v_x));
431
- for (int j = 0; j < qb; j++) {
432
+ for (size_t j = 0; j < qb; j++) {
432
433
  __m256i v_q = _mm256_loadu_si256(
433
434
  (const __m256i*)(query + j * size + offset));
434
435
  __m256i v_and = _mm256_and_si256(v_q, v_x);
@@ -445,7 +446,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
445
446
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
446
447
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
447
448
  pop_128 = _mm_add_epi64(pop_128, popcount_128(v_x));
448
- for (int j = 0; j < qb; j++) {
449
+ for (size_t j = 0; j < qb; j++) {
449
450
  __m128i v_q = _mm_loadu_si128(
450
451
  (const __m128i*)(query + j * size + offset));
451
452
  __m128i v_and = _mm_and_si128(v_q, v_x);
@@ -460,7 +461,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
460
461
  uint64_t yv;
461
462
  std::memcpy(&yv, data + offset, sizeof(yv));
462
463
  popcount_sum += popcount64(yv);
463
- for (int j = 0; j < qb; j++) {
464
+ for (size_t j = 0; j < qb; j++) {
464
465
  uint64_t qv;
465
466
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
466
467
  dot_product += popcount64(qv & yv) << j;
@@ -469,7 +470,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
469
470
  for (; offset < size; ++offset) {
470
471
  const auto yv = *(data + offset);
471
472
  popcount_sum += popcount32(yv);
472
- for (int j = 0; j < qb; j++) {
473
+ for (size_t j = 0; j < qb; j++) {
473
474
  const auto qv = *(query + j * size + offset);
474
475
  dot_product += popcount32(qv & yv) << j;
475
476
  }
@@ -489,7 +490,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
489
490
  __m512i sum_512 = _mm512_setzero_si512();
490
491
  for (; offset + step <= size; offset += step) {
491
492
  __m512i v_x = _mm512_loadu_si512((const __m512i*)(data + offset));
492
- for (int j = 0; j < qb; j++) {
493
+ for (size_t j = 0; j < qb; j++) {
493
494
  __m512i v_q = _mm512_loadu_si512(
494
495
  (const __m512i*)(query + j * size + offset));
495
496
  __m512i v_xor = _mm512_xor_si512(v_q, v_x);
@@ -504,7 +505,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
504
505
  __m256i sum_256 = _mm256_setzero_si256();
505
506
  for (; offset + step <= size; offset += step) {
506
507
  __m256i v_x = _mm256_loadu_si256((const __m256i*)(data + offset));
507
- for (int j = 0; j < qb; j++) {
508
+ for (size_t j = 0; j < qb; j++) {
508
509
  __m256i v_q = _mm256_loadu_si256(
509
510
  (const __m256i*)(query + j * size + offset));
510
511
  __m256i v_xor = _mm256_xor_si256(v_q, v_x);
@@ -518,7 +519,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
518
519
  __m128i sum_128 = _mm_setzero_si128();
519
520
  for (size_t step = 128 / 8; offset + step <= size; offset += step) {
520
521
  __m128i v_x = _mm_loadu_si128((const __m128i*)(data + offset));
521
- for (int j = 0; j < qb; j++) {
522
+ for (size_t j = 0; j < qb; j++) {
522
523
  __m128i v_q = _mm_loadu_si128(
523
524
  (const __m128i*)(query + j * size + offset));
524
525
  __m128i v_xor = _mm_xor_si128(v_q, v_x);
@@ -531,7 +532,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
531
532
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
532
533
  uint64_t yv;
533
534
  std::memcpy(&yv, data + offset, sizeof(yv));
534
- for (int j = 0; j < qb; j++) {
535
+ for (size_t j = 0; j < qb; j++) {
535
536
  uint64_t qv;
536
537
  std::memcpy(&qv, query + j * size + offset, sizeof(qv));
537
538
  sum += popcount64(qv ^ yv) << j;
@@ -539,7 +540,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
539
540
  }
540
541
  for (; offset < size; ++offset) {
541
542
  const auto yv = *(data + offset);
542
- for (int j = 0; j < qb; j++) {
543
+ for (size_t j = 0; j < qb; j++) {
543
544
  const auto qv = *(query + j * size + offset);
544
545
  sum += popcount32(qv ^ yv) << j;
545
546
  }
@@ -659,6 +660,43 @@ inline float ip_1exbit_avx512(
659
660
  return result;
660
661
  }
661
662
 
663
+ /// Bit-plane extraction for ex_bits in [2, 7]: plane b of 8 codes packed in
664
+ /// a uint64 is _pext_u64(codes, masks[b]), and has weight 2^b.
665
+ struct BitplaneTablesAVX512 {
666
+ uint64_t masks[7];
667
+ __m512 weights[8]; ///< weights[ex_bits] = 2^ex_bits, for the sign bit
668
+
669
+ explicit BitplaneTablesAVX512(size_t ex_bits) {
670
+ for (size_t b = 0; b < ex_bits; b++) {
671
+ uint64_t m = 0;
672
+ for (int j = 0; j < 8; j++) {
673
+ m |= (1ULL << (b + j * ex_bits));
674
+ }
675
+ masks[b] = m;
676
+ weights[b] = _mm512_set1_ps(static_cast<float>(1u << b));
677
+ }
678
+ weights[ex_bits] = _mm512_set1_ps(static_cast<float>(1u << ex_bits));
679
+ }
680
+ };
681
+
682
+ /// recon plus the extra-bit values of 16 codes, the first 8 packed in lo64
683
+ /// and the next 8 in hi64
684
+ inline __m512 add_bitplanes_avx512(
685
+ __m512 recon,
686
+ uint64_t lo64,
687
+ uint64_t hi64,
688
+ const BitplaneTablesAVX512& t,
689
+ size_t ex_bits) {
690
+ for (size_t b = 0; b < ex_bits; b++) {
691
+ const uint32_t plane =
692
+ static_cast<uint32_t>(_pext_u64(lo64, t.masks[b])) |
693
+ (static_cast<uint32_t>(_pext_u64(hi64, t.masks[b])) << 8);
694
+ recon = _mm512_mask_add_ps(
695
+ recon, static_cast<__mmask16>(plane), recon, t.weights[b]);
696
+ }
697
+ return recon;
698
+ }
699
+
662
700
  // Bit-plane kernel for ex_bits >= 2. It scores 16 dims per pass.
663
701
  // Each plane acts as a mask for one weighted add. Reads can run
664
702
  // past the codes into the row tail, which stays in bounds.
@@ -671,39 +709,20 @@ inline float ip_bitplane_avx512(
671
709
  float cb) {
672
710
  __m512 acc = _mm512_setzero_ps();
673
711
  const __m512 v_cb = _mm512_set1_ps(cb);
674
-
675
- uint64_t pext_masks[7];
676
- __m512 v_weights[8];
677
- for (size_t b = 0; b < ex_bits; b++) {
678
- uint64_t m = 0;
679
- for (int j = 0; j < 8; j++) {
680
- m |= (1ULL << (b + j * ex_bits));
681
- }
682
- pext_masks[b] = m;
683
- v_weights[b] = _mm512_set1_ps(static_cast<float>(1u << b));
684
- }
685
- v_weights[ex_bits] = _mm512_set1_ps(static_cast<float>(1u << ex_bits));
712
+ const BitplaneTablesAVX512 tables(ex_bits);
686
713
 
687
714
  size_t i = 0;
688
715
  for (; i + 16 <= d; i += 16) {
689
716
  uint16_t sb = 0;
690
717
  memcpy(&sb, sign_bits + (i / 8), sizeof(uint16_t));
691
718
  __m512 recon = _mm512_maskz_mov_ps(
692
- static_cast<__mmask16>(sb), v_weights[ex_bits]);
719
+ static_cast<__mmask16>(sb), tables.weights[ex_bits]);
693
720
 
694
721
  uint64_t lo64 = 0;
695
722
  uint64_t hi64 = 0;
696
723
  memcpy(&lo64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
697
724
  memcpy(&hi64, ex_code + ((i / 8) + 1) * ex_bits, sizeof(uint64_t));
698
-
699
- for (size_t b = 0; b < ex_bits; b++) {
700
- const uint32_t plane =
701
- static_cast<uint32_t>(_pext_u64(lo64, pext_masks[b])) |
702
- (static_cast<uint32_t>(_pext_u64(hi64, pext_masks[b]))
703
- << 8);
704
- recon = _mm512_mask_add_ps(
705
- recon, static_cast<__mmask16>(plane), recon, v_weights[b]);
706
- }
725
+ recon = add_bitplanes_avx512(recon, lo64, hi64, tables, ex_bits);
707
726
 
708
727
  __m512 rq = _mm512_loadu_ps(rotated_q + i);
709
728
  acc = _mm512_fmadd_ps(rq, _mm512_add_ps(recon, v_cb), acc);
@@ -716,17 +735,12 @@ inline float ip_bitplane_avx512(
716
735
  if (i + 8 <= d) {
717
736
  const __mmask16 low8 = static_cast<__mmask16>(0x00ff);
718
737
  __m512 recon = _mm512_maskz_mov_ps(
719
- static_cast<__mmask16>(sign_bits[i / 8]), v_weights[ex_bits]);
738
+ static_cast<__mmask16>(sign_bits[i / 8]),
739
+ tables.weights[ex_bits]);
720
740
 
721
741
  uint64_t lo64 = 0;
722
742
  memcpy(&lo64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
723
-
724
- for (size_t b = 0; b < ex_bits; b++) {
725
- const uint32_t plane =
726
- static_cast<uint32_t>(_pext_u64(lo64, pext_masks[b]));
727
- recon = _mm512_mask_add_ps(
728
- recon, static_cast<__mmask16>(plane), recon, v_weights[b]);
729
- }
743
+ recon = add_bitplanes_avx512(recon, lo64, 0, tables, ex_bits);
730
744
 
731
745
  __m512 rq = _mm512_maskz_loadu_ps(low8, rotated_q + i);
732
746
  acc = _mm512_fmadd_ps(
@@ -739,6 +753,82 @@ inline float ip_bitplane_avx512(
739
753
  return result;
740
754
  }
741
755
 
756
+ // sum_i rotated_q[i] * ex_code[i] for one-byte extra codes. Two accumulators,
757
+ // so that consecutive FMAs do not wait on each other.
758
+ inline float ex_ip_8bit_avx512(
759
+ const uint8_t* __restrict ex_code,
760
+ const float* __restrict rotated_q,
761
+ size_t d) {
762
+ __m512 acc0 = _mm512_setzero_ps();
763
+ __m512 acc1 = _mm512_setzero_ps();
764
+ auto step = [&](__m512& acc, size_t i) {
765
+ const __m128i bytes =
766
+ _mm_loadu_si128(reinterpret_cast<const __m128i*>(ex_code + i));
767
+ acc = _mm512_fmadd_ps(
768
+ _mm512_loadu_ps(rotated_q + i),
769
+ _mm512_cvtepi32_ps(_mm512_cvtepu8_epi32(bytes)),
770
+ acc);
771
+ };
772
+ size_t i = 0;
773
+ for (; i + 32 <= d; i += 32) {
774
+ step(acc0, i);
775
+ step(acc1, i + 16);
776
+ }
777
+ if (i + 16 <= d) {
778
+ step(acc0, i);
779
+ i += 16;
780
+ }
781
+ // Half-width step: the upper 8 lanes see zero codes and a masked load.
782
+ if (i + 8 <= d) {
783
+ const __m128i bytes =
784
+ _mm_loadl_epi64(reinterpret_cast<const __m128i*>(ex_code + i));
785
+ acc1 = _mm512_fmadd_ps(
786
+ _mm512_maskz_loadu_ps(0x00ff, rotated_q + i),
787
+ _mm512_cvtepi32_ps(_mm512_cvtepu8_epi32(bytes)),
788
+ acc1);
789
+ i += 8;
790
+ }
791
+ return _mm512_reduce_add_ps(_mm512_add_ps(acc0, acc1)) +
792
+ ex_ip_scalar(ex_code, rotated_q, i, d, 8);
793
+ }
794
+
795
+ // sum_i rotated_q[i] * ex_i for ex_bits in [2, 7]: ip_bitplane_avx512
796
+ // without the sign bits, and without reading past the codes.
797
+ inline float ex_ip_bitplane_avx512(
798
+ const uint8_t* __restrict ex_code,
799
+ const float* __restrict rotated_q,
800
+ size_t d,
801
+ size_t ex_bits) {
802
+ __m512 acc = _mm512_setzero_ps();
803
+ const BitplaneTablesAVX512 tables(ex_bits);
804
+
805
+ const size_t total_bytes = (d * ex_bits + 7) / 8;
806
+ size_t i = 0;
807
+ for (; i + 16 <= d; i += 16) {
808
+ const uint64_t lo64 =
809
+ load_ex_code_window(ex_code, (i / 8) * ex_bits, total_bytes);
810
+ const uint64_t hi64 = load_ex_code_window(
811
+ ex_code, ((i / 8) + 1) * ex_bits, total_bytes);
812
+ const __m512 recon = add_bitplanes_avx512(
813
+ _mm512_setzero_ps(), lo64, hi64, tables, ex_bits);
814
+ acc = _mm512_fmadd_ps(_mm512_loadu_ps(rotated_q + i), recon, acc);
815
+ }
816
+
817
+ // Half-width step, as in ip_bitplane_avx512.
818
+ if (i + 8 <= d) {
819
+ const uint64_t lo64 =
820
+ load_ex_code_window(ex_code, (i / 8) * ex_bits, total_bytes);
821
+ const __m512 recon = add_bitplanes_avx512(
822
+ _mm512_setzero_ps(), lo64, 0, tables, ex_bits);
823
+ acc = _mm512_fmadd_ps(
824
+ _mm512_maskz_loadu_ps(0x00ff, rotated_q + i), recon, acc);
825
+ i += 8;
826
+ }
827
+
828
+ return _mm512_reduce_add_ps(acc) +
829
+ ex_ip_scalar(ex_code, rotated_q, i, d, ex_bits);
830
+ }
831
+
742
832
  } // namespace
743
833
 
744
834
  template <>
@@ -781,4 +871,25 @@ float compute_inner_product<SIMDLevel::AVX512>(
781
871
  return ip_scalar(sign_bits, ex_code, rotated_q, 0, d, ex_bits, cb);
782
872
  }
783
873
 
874
+ template <>
875
+ float compute_inner_product_with_sign_ip<SIMDLevel::AVX512>(
876
+ const uint8_t* __restrict ex_code,
877
+ const float* __restrict rotated_q,
878
+ size_t d,
879
+ size_t ex_bits,
880
+ float cb,
881
+ float sign_ip,
882
+ float q_sum) {
883
+ float ex_ip = 0.0f;
884
+ if (ex_bits == 8) {
885
+ ex_ip = ex_ip_8bit_avx512(ex_code, rotated_q, d);
886
+ } else if (ex_bits == 1) {
887
+ // One extra bit per dim has the sign-bit layout.
888
+ ex_ip = fvec_masked_sum<SIMDLevel::AVX512>(rotated_q, ex_code, d);
889
+ } else if (ex_bits >= 2 && ex_bits <= 7) {
890
+ ex_ip = ex_ip_bitplane_avx512(ex_code, rotated_q, d, ex_bits);
891
+ }
892
+ return add_sign_and_bias_terms(ex_ip, ex_bits, cb, sign_ip, q_sum);
893
+ }
894
+
784
895
  } // namespace faiss::rabitq::multibit
@@ -52,19 +52,19 @@ bool SIMDConfig::has_dynamic_dispatch() {
52
52
  #define HWCAP_SVE (1 << 22)
53
53
  #endif
54
54
 
55
- static bool has_sve() {
55
+ [[maybe_unused]] static bool has_sve() {
56
56
  return (getauxval(AT_HWCAP) & HWCAP_SVE) != 0;
57
57
  }
58
58
 
59
59
  #elif defined(__APPLE__)
60
60
  // Apple Silicon does NOT support SVE
61
- static bool has_sve() {
61
+ [[maybe_unused]] static bool has_sve() {
62
62
  return false;
63
63
  }
64
64
 
65
65
  #else
66
66
  // Other aarch64 platforms: conservatively report no SVE
67
- static bool has_sve() {
67
+ [[maybe_unused]] static bool has_sve() {
68
68
  return false;
69
69
  }
70
70