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
@@ -0,0 +1,431 @@
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
+ // AVX-512 kernels of the block engine (04_simd.md). Compiled with
9
+ // AVX-512 flags (FAISS_SIMD_AVX512_SRC) and -ffp-contract=off.
10
+ //
11
+ // One zmm register holds 16 floats: the 16 values below a node of the trees,
12
+ // or 16 accumulator entries. The 81 children (p, q) of a node are 6 rows of
13
+ // 16 lanes (ChildTable); a row gathers a[p], b[q] and acc[p | q] with
14
+ // permutexvar. Directed rounding uses the embedded rounding of AVX-512
15
+ // (_mm512_add_round_ps), which gives the same bounds as the TwoSum code of
16
+ // the generic kernels or tighter ones.
17
+
18
+ #include <faiss/impl/optimal_kmeans/BlockCombiner-inl.h>
19
+
20
+ #include <immintrin.h>
21
+
22
+ namespace faiss {
23
+ namespace optimal_kmeans {
24
+
25
+ namespace {
26
+
27
+ constexpr int kRD = _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC;
28
+ constexpr int kRU = _MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC;
29
+ constexpr int kRN = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC;
30
+
31
+ inline __m512i load_i(const int32_t* p) {
32
+ return _mm512_load_si512(p);
33
+ }
34
+
35
+ inline __m512i load_i(const int64_t* p) {
36
+ return _mm512_load_si512(p);
37
+ }
38
+
39
+ /// next float above x, for all lanes (-0 -> smallest positive, inf -> inf)
40
+ inline __m512 nextup_ps(__m512 x) {
41
+ x = _mm512_add_ps(x, _mm512_setzero_ps()); // -0 -> +0
42
+ __m512i b = _mm512_castps_si512(x);
43
+ const __mmask16 neg = _mm512_movepi32_mask(b);
44
+ const __mmask16 inf =
45
+ _mm512_cmp_ps_mask(x, _mm512_set1_ps(kInf), _CMP_EQ_OQ);
46
+ const __m512i one = _mm512_set1_epi32(1);
47
+ b = _mm512_mask_add_epi32(b, __mmask16(~neg & ~inf), b, one);
48
+ b = _mm512_mask_sub_epi32(b, neg, b, one);
49
+ return _mm512_castsi512_ps(b);
50
+ }
51
+
52
+ /*********************************************************
53
+ * 16 x 16 transposed reduction: red16(v)[j] = min (max) of the 16 lanes of
54
+ * v[j], in 4 steps of permutex2var + min
55
+ *********************************************************/
56
+
57
+ struct RedTables {
58
+ alignas(64) int32_t i1[4][16];
59
+ alignas(64) int32_t i2[4][16];
60
+ RedTables() {
61
+ for (int s = 0, w = 16; s < 4; s++, w /= 2) {
62
+ const int G = 16 / w, h = w / 2;
63
+ for (int i = 0; i < 16; i++) {
64
+ const int g = i / h, o = i % h;
65
+ if (g < G) {
66
+ i1[s][i] = g * w + o;
67
+ i2[s][i] = g * w + h + o;
68
+ } else {
69
+ i1[s][i] = 16 + (g - G) * w + o;
70
+ i2[s][i] = 16 + (g - G) * w + h + o;
71
+ }
72
+ }
73
+ }
74
+ }
75
+ };
76
+
77
+ const RedTables& red_tables() {
78
+ static const RedTables t;
79
+ return t;
80
+ }
81
+
82
+ template <bool MAX>
83
+ inline __m512 red16(__m512* v) {
84
+ const RedTables& R = red_tables();
85
+ for (int s = 0, cnt = 16; s < 4; s++, cnt /= 2) {
86
+ const __m512i a = load_i(R.i1[s]), b = load_i(R.i2[s]);
87
+ for (int j = 0; j < cnt / 2; j++) {
88
+ const __m512 x = _mm512_permutex2var_ps(v[2 * j], a, v[2 * j + 1]);
89
+ const __m512 y = _mm512_permutex2var_ps(v[2 * j], b, v[2 * j + 1]);
90
+ v[j] = MAX ? _mm512_max_ps(x, y) : _mm512_min_ps(x, y);
91
+ }
92
+ }
93
+ return v[0];
94
+ }
95
+
96
+ /*********************************************************
97
+ * Dense tile: the 81 candidates of 16 outputs in registers. A sorting
98
+ * network of lexicographic minima, smallest split on ties.
99
+ *********************************************************/
100
+
101
+ struct Cand16 {
102
+ __m512 cost;
103
+ __m512i split; // p (32-bit)
104
+ };
105
+
106
+ inline Cand16 mk16(
107
+ __m512 in1,
108
+ __m512 in2,
109
+ __m512i left,
110
+ __m512i right,
111
+ __mmask16 active = 0xffff) {
112
+ const __m512 a = _mm512_permutexvar_ps(left, in1);
113
+ const __m512 b = _mm512_permutexvar_ps(right, in2);
114
+ return {_mm512_mask_add_ps(_mm512_set1_ps(kInf), active, a, b), left};
115
+ }
116
+
117
+ inline Cand16 perm2(Cand16 a, Cand16 b, __m512i idx) {
118
+ return {_mm512_permutex2var_ps(a.cost, idx, b.cost),
119
+ _mm512_permutex2var_epi32(a.split, idx, b.split)};
120
+ }
121
+
122
+ inline Cand16 blend16(Cand16 dest, Cand16 src, __m512i idx, __mmask16 m) {
123
+ return {_mm512_mask_permutexvar_ps(dest.cost, m, idx, src.cost),
124
+ _mm512_mask_permutexvar_epi32(dest.split, m, idx, src.split)};
125
+ }
126
+
127
+ /// lexicographic min, a wins ties (a has the smaller splits)
128
+ inline Cand16 omin16(Cand16 a, Cand16 b, __mmask16 m = 0xffff) {
129
+ const __mmask16 imp =
130
+ _mm512_mask_cmp_ps_mask(m, b.cost, a.cost, _CMP_LT_OQ);
131
+ return {_mm512_mask_min_ps(a.cost, m, b.cost, a.cost),
132
+ _mm512_mask_mov_epi32(a.split, imp, b.split)};
133
+ }
134
+
135
+ #define S16 _mm512_setr_epi32
136
+ /// out[z] = lexicographic min over p subset of z of (a[p] + b[z ^ p], p)
137
+ inline Cand16 combo16(const float* cost1, const float* cost2) {
138
+ const __m512 input1 = _mm512_loadu_ps(cost1);
139
+ const __m512 input2 = _mm512_loadu_ps(cost2);
140
+ const Cand16 c0 = {_mm512_set1_ps(kInf), _mm512_setzero_si512()};
141
+ // clang-format off
142
+ const Cand16 c1 = mk16(input1, input2, S16(0, 0, 1, 0, 2, 0, 1, 2, 3, 0, 4, 0, 1, 4, 5, 0),
143
+ S16(0, 1, 0, 2, 0, 3, 2, 1, 0, 4, 0, 5, 4, 1, 0, 6));
144
+ const Cand16 c2 = mk16(input1, input2, S16(2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 8, 0, 1, 8),
145
+ S16(4, 2, 0, 7, 6, 5, 4, 3, 2, 1, 0, 8, 0, 9, 8, 1));
146
+ const Cand16 c3 = mk16(input1, input2, S16(9, 0, 2, 8, 10, 0, 1, 2, 3, 8, 9, 10, 11, 0, 4, 8),
147
+ S16(0, 10, 8, 2, 0, 11, 10, 9, 8, 3, 2, 1, 0, 12, 8, 4));
148
+ const Cand16 c4 = mk16(input1, input2, S16(12, 0, 1, 4, 5, 8, 9, 12, 13, 0, 2, 4, 6, 8, 10, 12),
149
+ S16(0, 13, 12, 9, 8, 5, 4, 1, 0, 14, 12, 10, 8, 6, 4, 2));
150
+ const Cand16 c5 = mk16(input1, input2, S16(14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14),
151
+ S16(0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
152
+ const Cand16 c6 = mk16(input1, input2, S16(15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
153
+ S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 0x0001);
154
+ Cand16 left1 = perm2(c0, c5, S16(15, 18, 20, 22, 24, 26, 28, 30, 7, 0, 0, 0, 11, 0, 0, 0));
155
+ left1 = blend16(left1, c2, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 0, 0, 0, 0), 0x0e00);
156
+ left1 = blend16(left1, c3, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 10), 0xe000);
157
+ Cand16 right1 = perm2(c5, c2, S16(1, 3, 5, 7, 9, 11, 13, 15, 19, 21, 23, 25, 0, 0, 0, 0));
158
+ right1 = blend16(right1, c3, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 9, 11), 0xf000);
159
+ const Cand16 v1 = omin16(left1, right1);
160
+ Cand16 left2 = perm2(v1, c0, S16(0, 2, 4, 6, 29, 0, 0, 0, 30, 0, 0, 0, 8, 10, 12, 14));
161
+ left2 = blend16(left2, c4, S16(0, 0, 0, 0, 0, 2, 4, 6, 0, 10, 12, 14, 0, 0, 0, 0), 0x0ee0);
162
+ Cand16 right2 = perm2(v1, c4, S16(1, 3, 5, 7, 17, 19, 21, 23, 25, 27, 29, 31, 9, 11, 13, 15));
163
+ const Cand16 v2 = omin16(left2, right2);
164
+ Cand16 left3 = perm2(v2, c0, S16(0, 2, 4, 6, 8, 10, 19, 0, 21, 0, 22, 0, 25, 0, 26, 0));
165
+ left3 = blend16(left3, c1, S16(0, 0, 0, 0, 0, 0, 0, 6, 0, 12, 0, 0, 0, 0, 0, 0), 0x0280);
166
+ left3 = blend16(left3, c2, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0), 0x2800);
167
+ left3 = blend16(left3, c3, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2), 0x8000);
168
+ Cand16 right3 = perm2(v2, c1, S16(1, 3, 5, 7, 9, 11, 21, 23, 27, 29, 31, 0, 0, 0, 0, 0));
169
+ right3 = blend16(right3, c2, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 15, 0, 0), 0x3800);
170
+ right3 = blend16(right3, c3, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3), 0xc000);
171
+ const Cand16 v3 = omin16(left3, right3);
172
+ Cand16 left4 = perm2(c0, c3, S16(12, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8));
173
+ left4 = blend16(left4, v3, S16(0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 0, 0, 0, 0), 0x0fe4);
174
+ left4 = blend16(left4, v2, S16(0, 0, 0, 12, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 0x0018);
175
+ Cand16 right4 = perm2(c3, v3, S16(13, 15, 17, 0, 0, 19, 21, 23, 25, 27, 29, 31, 0, 0, 0, 0));
176
+ right4 = blend16(right4, v2, S16(0, 0, 0, 13, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 0x0018);
177
+ right4 = blend16(right4, c1, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 9, 0), 0x7000);
178
+ right4 = blend16(right4, c2, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11), 0x8000);
179
+ const Cand16 v4 = omin16(left4, right4);
180
+ Cand16 left5 = perm2(c0, v4, S16(0, 28, 29, 23, 30, 24, 25, 19, 31, 26, 27, 20, 16, 21, 22, 18));
181
+ Cand16 right5 = perm2(c1, c2, S16(0, 2, 4, 8, 10, 14, 18, 26, 28, 0, 0, 0, 0, 0, 0, 0));
182
+ right5 = blend16(right5, c3, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 12, 0, 0, 0, 0), 0x0e00);
183
+ right5 = blend16(right5, v4, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), 0x1000);
184
+ right5 = blend16(right5, c4, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0), 0x2000);
185
+ right5 = blend16(right5, c5, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 0x4000);
186
+ right5 = blend16(right5, c6, S16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 0x8000);
187
+ // clang-format on
188
+ Cand16 v5 = omin16(left5, right5);
189
+ const Cand16 remaining = blend16(v5, c4, _mm512_setzero_si512(), 0x1000);
190
+ v5 = omin16(v5, remaining, 0x1000);
191
+ return v5;
192
+ }
193
+ #undef S16
194
+
195
+ /// lexicographic merge of 16 (cost, hi | local split) into ac, as
196
+ inline void merge16(
197
+ float* ac,
198
+ uint64_t* as,
199
+ __m512 c,
200
+ __m512i loc,
201
+ uint64_t hi) {
202
+ const __m512 o = _mm512_loadu_ps(ac);
203
+ __mmask16 better = _mm512_cmp_ps_mask(c, o, _CMP_LT_OQ);
204
+ const __mmask16 eq = _mm512_cmp_ps_mask(c, o, _CMP_EQ_OQ);
205
+ const __m512i vhi = _mm512_set1_epi64(int64_t(hi));
206
+ const __m512i f0 = _mm512_or_si512(
207
+ _mm512_cvtepu32_epi64(_mm512_castsi512_si256(loc)), vhi);
208
+ const __m512i f1 = _mm512_or_si512(
209
+ _mm512_cvtepu32_epi64(_mm512_extracti64x4_epi64(loc, 1)), vhi);
210
+ if (eq) {
211
+ const __mmask8 t0 = _mm512_mask_cmplt_epu64_mask(
212
+ __mmask8(eq), f0, _mm512_loadu_si512(as));
213
+ const __mmask8 t1 = _mm512_mask_cmplt_epu64_mask(
214
+ __mmask8(eq >> 8), f1, _mm512_loadu_si512(as + 8));
215
+ better |= __mmask16(t0) | __mmask16(__mmask16(t1) << 8);
216
+ }
217
+ if (better) {
218
+ _mm512_mask_storeu_ps(ac, better, c);
219
+ _mm512_mask_storeu_epi64(as, __mmask8(better), f0);
220
+ _mm512_mask_storeu_epi64(as + 8, __mmask8(better >> 8), f1);
221
+ }
222
+ }
223
+
224
+ /*********************************************************
225
+ * The kernels
226
+ *********************************************************/
227
+
228
+ template <>
229
+ struct BlockKernels<SIMDLevel::AVX512> {
230
+ static int stage(const StageInputs& in, uint32_t* list) {
231
+ const ChildTable& T = child_table();
232
+ const __m512 la = _mm512_loadu_ps(in.ta);
233
+ const __m512 lb = _mm512_loadu_ps(in.tb);
234
+ const __m512 hu = _mm512_loadu_ps(in.tu);
235
+ const __m512 hus = _mm512_loadu_ps(in.tus);
236
+ const __m512i hm0 = _mm512_loadu_si512(in.tm);
237
+ const __m512i hm1 = _mm512_loadu_si512(in.tm + 8);
238
+ const __m512i vb = _mm512_set1_epi64(int64_t(in.split_base));
239
+ const uint16_t* act = in.mirror ? T.mirror : T.active;
240
+ int cnt = 0;
241
+ for (int j = 0; j < 6; j++) {
242
+ const __m512i out = load_i(T.out[j]);
243
+ const __m512 sa = _mm512_permutexvar_ps(load_i(T.left[j]), la);
244
+ const __m512 sb = _mm512_permutexvar_ps(load_i(T.right[j]), lb);
245
+ const __m512 su = _mm512_permutexvar_ps(out, hu);
246
+ const __m512 lo = _mm512_add_round_ps(sa, sb, kRD);
247
+ // kept unless lo > U (unordered: kept)
248
+ __mmask16 mk = _mm512_mask_cmp_ps_mask(act[j], lo, su, _CMP_NGT_UQ);
249
+ const __mmask16 ge = _mm512_mask_cmp_ps_mask(
250
+ mk, lo, _mm512_permutexvar_ps(out, hus), _CMP_GE_OQ);
251
+ if (ge) {
252
+ // ties would lose: max output split <= smallest split
253
+ const __m512i ml = _mm512_permutex2var_epi64(
254
+ hm0, load_i(T.out64[j][0]), hm1);
255
+ const __m512i mh = _mm512_permutex2var_epi64(
256
+ hm0, load_i(T.out64[j][1]), hm1);
257
+ const __m512i cl = _mm512_or_si512(
258
+ vb, _mm512_slli_epi64(load_i(T.left64[j][0]), in.sh));
259
+ const __m512i ch = _mm512_or_si512(
260
+ vb, _mm512_slli_epi64(load_i(T.left64[j][1]), in.sh));
261
+ const __mmask16 tie_lose =
262
+ __mmask16(_mm512_mask_cmple_epu64_mask(
263
+ __mmask8(ge), ml, cl)) |
264
+ __mmask16(
265
+ __mmask16(_mm512_mask_cmple_epu64_mask(
266
+ __mmask8(ge >> 8), mh, ch))
267
+ << 8);
268
+ mk &= __mmask16(~tie_lose);
269
+ }
270
+ _mm512_storeu_si512(
271
+ list + cnt,
272
+ _mm512_maskz_compress_epi32(mk, load_i(T.eidx[j])));
273
+ cnt += popcount32(mk);
274
+ }
275
+ return cnt;
276
+ }
277
+
278
+ static int bottom(
279
+ const float* a,
280
+ const float* b,
281
+ float* ac,
282
+ uint64_t* as,
283
+ uint64_t split_base,
284
+ bool mirror,
285
+ uint64_t* passes) {
286
+ const ChildTable& T = child_table();
287
+ const __m512 la = _mm512_loadu_ps(a);
288
+ const __m512 lb = _mm512_loadu_ps(b);
289
+ const __m512 hu = _mm512_loadu_ps(ac);
290
+ const uint16_t* act = mirror ? T.mirror : T.active;
291
+ __mmask16 m[6];
292
+ __mmask16 any = 0;
293
+ alignas(64) float sums[96];
294
+ for (int j = 0; j < 6; j++) {
295
+ const __m512 sa = _mm512_permutexvar_ps(load_i(T.left[j]), la);
296
+ const __m512 sb = _mm512_permutexvar_ps(load_i(T.right[j]), lb);
297
+ const __m512 su = _mm512_permutexvar_ps(load_i(T.out[j]), hu);
298
+ const __m512 c = _mm512_add_round_ps(sa, sb, kRN);
299
+ m[j] = _mm512_mask_cmp_ps_mask(act[j], c, su, _CMP_LE_OQ);
300
+ any |= m[j];
301
+ _mm512_store_ps(sums + 16 * j, c);
302
+ }
303
+ if (any == 0) {
304
+ return 0;
305
+ }
306
+ // rare: candidates <= acc (at the time of the test), in order
307
+ int nimp = 0;
308
+ for (int j = 0; j < 6; j++) {
309
+ uint32_t mm = m[j];
310
+ while (mm) {
311
+ const int e = 16 * j + __builtin_ctz(mm);
312
+ mm &= mm - 1;
313
+ const int p = T.P[e], z = p | T.Q[e];
314
+ if (sums[e] <= ac[z]) {
315
+ (*passes)++;
316
+ nimp += lex_update(
317
+ sums[e], split_base | uint64_t(p), ac + z, as + z);
318
+ }
319
+ }
320
+ }
321
+ return nimp;
322
+ }
323
+
324
+ static uint64_t dense_tile(
325
+ const float* a,
326
+ const float* b,
327
+ uint64_t Tt,
328
+ bool mirror,
329
+ float* ac,
330
+ uint64_t* as,
331
+ uint64_t split_base) {
332
+ if (Tt >= (uint64_t(1) << 28)) { // local splits need > 32 bits
333
+ return BlockKernels<SIMDLevel::NONE>::dense_tile(
334
+ a, b, Tt, mirror, ac, as, split_base);
335
+ }
336
+ const uint64_t hb = Tt ? (uint64_t(1) << highbit(Tt)) : 0;
337
+ uint64_t nk = 0;
338
+ Cand16 run;
339
+ uint64_t X = 0;
340
+ do { // X increasing: earlier candidates have smaller splits
341
+ if (!(mirror && (X & hb))) {
342
+ Cand16 c = combo16(a + (X << 4), b + ((Tt ^ X) << 4));
343
+ c.split = _mm512_or_si512(
344
+ c.split, _mm512_set1_epi32(int(uint32_t(X << 4))));
345
+ run = nk == 0 ? c : omin16(run, c);
346
+ nk++;
347
+ }
348
+ X = (X - Tt) & Tt;
349
+ } while (X != 0);
350
+ if (nk) {
351
+ merge16(ac, as, run.cost, run.split, split_base);
352
+ }
353
+ return nk;
354
+ }
355
+
356
+ template <int KIND>
357
+ static void level4(
358
+ const float* src,
359
+ const float* wlo,
360
+ const float* wup,
361
+ uint64_t ng,
362
+ float* out) {
363
+ const __m512 vwlo = _mm512_loadu_ps(wlo);
364
+ auto one = [&](uint64_t g) {
365
+ __m512 x = _mm512_loadu_ps(src + (g << 4));
366
+ if (KIND == 1) {
367
+ x = nextup_ps(x);
368
+ }
369
+ const __m512 wv = _mm512_add_ps(vwlo, _mm512_set1_ps(wup[g]));
370
+ return KIND ? _mm512_sub_round_ps(x, wv, kRU)
371
+ : _mm512_sub_round_ps(x, wv, kRD);
372
+ };
373
+ uint64_t g = 0;
374
+ for (; g + 16 <= ng; g += 16) {
375
+ __m512 v[16];
376
+ for (int j = 0; j < 16; j++) {
377
+ v[j] = one(g + j);
378
+ }
379
+ _mm512_storeu_ps(out + g, red16<KIND != 0>(v));
380
+ }
381
+ for (; g < ng; g++) {
382
+ out[g] = KIND ? _mm512_reduce_max_ps(one(g))
383
+ : _mm512_reduce_min_ps(one(g));
384
+ }
385
+ }
386
+
387
+ template <bool MAX>
388
+ static void reduce16(const float* in, uint64_t nout, float* out) {
389
+ uint64_t g = 0;
390
+ for (; g + 16 <= nout; g += 16) {
391
+ __m512 v[16];
392
+ for (int j = 0; j < 16; j++) {
393
+ v[j] = _mm512_loadu_ps(in + ((g + j) << 4));
394
+ }
395
+ _mm512_storeu_ps(out + g, red16<MAX>(v));
396
+ }
397
+ for (; g < nout; g++) {
398
+ const __m512 x = _mm512_loadu_ps(in + (g << 4));
399
+ out[g] = MAX ? _mm512_reduce_max_ps(x) : _mm512_reduce_min_ps(x);
400
+ }
401
+ }
402
+
403
+ static void max16_u64(const uint64_t* in, uint64_t nout, uint64_t* out) {
404
+ for (uint64_t g = 0; g < nout; g++) {
405
+ const __m512i x = _mm512_max_epu64(
406
+ _mm512_loadu_si512(in + (g << 4)),
407
+ _mm512_loadu_si512(in + (g << 4) + 8));
408
+ out[g] = _mm512_reduce_max_epu64(x);
409
+ }
410
+ }
411
+ };
412
+
413
+ } // namespace
414
+
415
+ template <>
416
+ OptimalKMeansBlockCombiner* make_block_combiner<SIMDLevel::AVX512>(
417
+ int n,
418
+ int L,
419
+ uint64_t s_hi,
420
+ const float* A,
421
+ const float* B,
422
+ bool sym,
423
+ float* acc_cost,
424
+ uint64_t* acc_split,
425
+ const OptimalKMeansCombineParams& params) {
426
+ return new BlockCombiner<SIMDLevel::AVX512>(
427
+ n, L, s_hi, A, B, sym, acc_cost, acc_split, params);
428
+ }
429
+
430
+ } // namespace optimal_kmeans
431
+ } // namespace faiss