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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/faiss/extconf.rb +1 -0
- data/ext/faiss/index.cpp +42 -0
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/IVFlib.cpp +48 -51
- data/vendor/faiss/faiss/IVFlib.h +6 -2
- data/vendor/faiss/faiss/Index.cpp +2 -2
- data/vendor/faiss/faiss/Index.h +3 -3
- data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
- data/vendor/faiss/faiss/IndexBinary.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
- data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
- data/vendor/faiss/faiss/IndexHNSW.h +2 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
- data/vendor/faiss/faiss/IndexIDMap.h +10 -5
- data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
- data/vendor/faiss/faiss/IndexIVF.h +37 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
- data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
- data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
- data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
- data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
- data/vendor/faiss/faiss/VectorTransform.h +24 -1
- data/vendor/faiss/faiss/factory_tools.cpp +19 -1
- data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
- data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
- data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
- data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
- data/vendor/faiss/faiss/impl/HNSW.h +2 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
- data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
- data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
- data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
- data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
- data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
- data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
- data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
- data/vendor/faiss/faiss/impl/io.cpp +1 -0
- data/vendor/faiss/faiss/impl/io_macros.h +2 -2
- data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
- data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
- data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
- data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
- data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
- data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
- data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
- data/vendor/faiss/faiss/index_factory.cpp +30 -6
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
- data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
- data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
- data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
- data/vendor/faiss/faiss/utils/distances.cpp +246 -34
- data/vendor/faiss/faiss/utils/distances.h +145 -2
- data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
- data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
- data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
- data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
- data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
- data/vendor/faiss/faiss/utils/utils.cpp +4 -0
- metadata +20 -1
|
@@ -0,0 +1,1157 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
// Private header: the engine that combines one block (03_pruned.md).
|
|
11
|
+
//
|
|
12
|
+
// The engine is a template on the SIMD level. Its inner kernels are the
|
|
13
|
+
// static functions of BlockKernels<SL>: the primary template below is plain
|
|
14
|
+
// C++ (SIMDLevel::NONE); BlockCombiner-avx512.cpp specializes it for AVX-512
|
|
15
|
+
// and instantiates the engine with it. Everything else is shared.
|
|
16
|
+
//
|
|
17
|
+
// ODR: the TUs that include this header are compiled with different SIMD
|
|
18
|
+
// flags, so everything except make_block_combiner is in an anonymous
|
|
19
|
+
// namespace (internal linkage): the linker must not merge an AVX-512 copy of
|
|
20
|
+
// an inline function into the generic code.
|
|
21
|
+
|
|
22
|
+
#include <faiss/impl/optimal_kmeans/OptimalKMeans.h>
|
|
23
|
+
|
|
24
|
+
#include <faiss/impl/FaissAssert.h>
|
|
25
|
+
#include <faiss/impl/platform_macros.h>
|
|
26
|
+
#include <faiss/utils/popcount.h>
|
|
27
|
+
#include <faiss/utils/simd_levels.h>
|
|
28
|
+
|
|
29
|
+
#include <algorithm>
|
|
30
|
+
#include <atomic>
|
|
31
|
+
#include <cmath>
|
|
32
|
+
#include <cstdint>
|
|
33
|
+
#include <cstring>
|
|
34
|
+
#include <limits>
|
|
35
|
+
#include <memory>
|
|
36
|
+
#include <mutex>
|
|
37
|
+
#include <vector>
|
|
38
|
+
|
|
39
|
+
namespace faiss {
|
|
40
|
+
namespace optimal_kmeans {
|
|
41
|
+
|
|
42
|
+
/// engine for one SIMD level; specialized in OptimalKMeans.cpp (NONE) and
|
|
43
|
+
/// BlockCombiner-avx512.cpp (AVX512)
|
|
44
|
+
template <SIMDLevel SL>
|
|
45
|
+
OptimalKMeansBlockCombiner* make_block_combiner(
|
|
46
|
+
int n,
|
|
47
|
+
int L,
|
|
48
|
+
uint64_t s_hi,
|
|
49
|
+
const float* A,
|
|
50
|
+
const float* B,
|
|
51
|
+
bool sym,
|
|
52
|
+
float* acc_cost,
|
|
53
|
+
uint64_t* acc_split,
|
|
54
|
+
const OptimalKMeansCombineParams& params);
|
|
55
|
+
|
|
56
|
+
template <>
|
|
57
|
+
OptimalKMeansBlockCombiner* make_block_combiner<SIMDLevel::NONE>(
|
|
58
|
+
int n,
|
|
59
|
+
int L,
|
|
60
|
+
uint64_t s_hi,
|
|
61
|
+
const float* A,
|
|
62
|
+
const float* B,
|
|
63
|
+
bool sym,
|
|
64
|
+
float* acc_cost,
|
|
65
|
+
uint64_t* acc_split,
|
|
66
|
+
const OptimalKMeansCombineParams& params);
|
|
67
|
+
|
|
68
|
+
#ifdef COMPILE_SIMD_AVX512
|
|
69
|
+
template <>
|
|
70
|
+
OptimalKMeansBlockCombiner* make_block_combiner<SIMDLevel::AVX512>(
|
|
71
|
+
int n,
|
|
72
|
+
int L,
|
|
73
|
+
uint64_t s_hi,
|
|
74
|
+
const float* A,
|
|
75
|
+
const float* B,
|
|
76
|
+
bool sym,
|
|
77
|
+
float* acc_cost,
|
|
78
|
+
uint64_t* acc_split,
|
|
79
|
+
const OptimalKMeansCombineParams& params);
|
|
80
|
+
#endif
|
|
81
|
+
|
|
82
|
+
namespace {
|
|
83
|
+
|
|
84
|
+
constexpr float kInf = std::numeric_limits<float>::infinity();
|
|
85
|
+
constexpr float kMax = std::numeric_limits<float>::max();
|
|
86
|
+
|
|
87
|
+
/*********************************************************
|
|
88
|
+
* Small helpers
|
|
89
|
+
*********************************************************/
|
|
90
|
+
|
|
91
|
+
inline int highbit(uint64_t x) {
|
|
92
|
+
return 63 - __builtin_clzll(x);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// i-th sub-group of mask (bits of i deposited at the set bits of mask)
|
|
96
|
+
inline uint64_t deposit_bits(uint64_t i, uint64_t mask) {
|
|
97
|
+
uint64_t r = 0;
|
|
98
|
+
for (uint64_t m = mask; m && i; m &= m - 1, i >>= 1) {
|
|
99
|
+
if (i & 1) {
|
|
100
|
+
r |= m & (~m + 1);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return r;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// inverse of deposit_bits: the bits of x at the set bits of mask, packed
|
|
107
|
+
inline uint64_t extract_bits(uint64_t x, uint64_t mask) {
|
|
108
|
+
uint64_t r = 0;
|
|
109
|
+
int k = 0;
|
|
110
|
+
for (uint64_t m = mask; m; m &= m - 1, k++) {
|
|
111
|
+
if (x & m & (~m + 1)) {
|
|
112
|
+
r |= uint64_t(1) << k;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return r;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
inline uint64_t pow3(int k) {
|
|
119
|
+
uint64_t r = 1;
|
|
120
|
+
while (k-- > 0) {
|
|
121
|
+
r *= 3;
|
|
122
|
+
}
|
|
123
|
+
return r;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// lexicographic update: (c, s) replaces (*ac, *as) if strictly better
|
|
127
|
+
inline bool lex_update(float c, uint64_t s, float* ac, uint64_t* as) {
|
|
128
|
+
if (c < *ac || (c == *ac && s < *as)) {
|
|
129
|
+
*ac = c;
|
|
130
|
+
*as = s;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
inline float next_up(float x) {
|
|
137
|
+
return std::nextafter(x + 0.0f, kInf); // -0 -> +0 first
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
inline float next_down(float x) {
|
|
141
|
+
return std::nextafter(x + 0.0f, -kInf);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Additions and subtractions rounded down or up, without changing the
|
|
145
|
+
* rounding mode: the rounded-to-nearest result plus the sign of its exact
|
|
146
|
+
* error (TwoSum). An overflow to +inf rounds down to FLT_MAX, an overflow to
|
|
147
|
+
* -inf rounds up to -FLT_MAX. */
|
|
148
|
+
inline float add_rd(float a, float b) {
|
|
149
|
+
float s = a + b;
|
|
150
|
+
if (!std::isfinite(s)) {
|
|
151
|
+
return (s == kInf && std::isfinite(a) && std::isfinite(b)) ? kMax : s;
|
|
152
|
+
}
|
|
153
|
+
float bb = s - a;
|
|
154
|
+
float err = (a - (s - bb)) + (b - bb);
|
|
155
|
+
return err < 0 ? next_down(s) : s;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
inline float add_ru(float a, float b) {
|
|
159
|
+
float s = a + b;
|
|
160
|
+
if (!std::isfinite(s)) {
|
|
161
|
+
return (s == -kInf && std::isfinite(a) && std::isfinite(b)) ? -kMax : s;
|
|
162
|
+
}
|
|
163
|
+
float bb = s - a;
|
|
164
|
+
float err = (a - (s - bb)) + (b - bb);
|
|
165
|
+
return err > 0 ? next_up(s) : s;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
inline float sub_rd(float a, float b) {
|
|
169
|
+
return add_rd(a, -b);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
inline float sub_ru(float a, float b) {
|
|
173
|
+
return add_ru(a, -b);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/*********************************************************
|
|
177
|
+
* The 81 children of a node
|
|
178
|
+
*********************************************************/
|
|
179
|
+
|
|
180
|
+
/** Deciding 4 more points has 3^4 = 81 outcomes: the pairs (p, q) of 4-bit
|
|
181
|
+
* disjoint sub-groups, ordered by z = p | q then by increasing p, packed in
|
|
182
|
+
* 6 rows of 16 (the last row has one entry). */
|
|
183
|
+
struct ChildTable {
|
|
184
|
+
alignas(64) int32_t left[6][16]; // p
|
|
185
|
+
alignas(64) int32_t right[6][16]; // q
|
|
186
|
+
alignas(64) int32_t out[6][16]; // p | q
|
|
187
|
+
alignas(64) int32_t eidx[6][16]; // entry number 16 * row + lane
|
|
188
|
+
// out and left as 64-bit lanes (lanes 0-7 and 8-15)
|
|
189
|
+
alignas(64) int64_t out64[6][2][8];
|
|
190
|
+
alignas(64) int64_t left64[6][2][8];
|
|
191
|
+
uint16_t active[6]; // valid lanes
|
|
192
|
+
uint16_t mirror[6]; // lanes kept below a mirrored node: z == 0 or the
|
|
193
|
+
// highest point of z goes to B (q)
|
|
194
|
+
uint8_t P[96], Q[96];
|
|
195
|
+
int n_active = 0, n_mirror = 0;
|
|
196
|
+
|
|
197
|
+
ChildTable() {
|
|
198
|
+
std::memset(this, 0, sizeof(*this));
|
|
199
|
+
int e = 0;
|
|
200
|
+
for (int z = 0; z < 16; z++) {
|
|
201
|
+
int p = 0;
|
|
202
|
+
do {
|
|
203
|
+
int q = z ^ p, row = e / 16, lane = e % 16;
|
|
204
|
+
left[row][lane] = p;
|
|
205
|
+
right[row][lane] = q;
|
|
206
|
+
out[row][lane] = z;
|
|
207
|
+
eidx[row][lane] = e;
|
|
208
|
+
out64[row][lane / 8][lane % 8] = z;
|
|
209
|
+
left64[row][lane / 8][lane % 8] = p;
|
|
210
|
+
active[row] |= uint16_t(1u << lane);
|
|
211
|
+
n_active++;
|
|
212
|
+
if (z == 0 || ((q >> highbit(z)) & 1)) {
|
|
213
|
+
mirror[row] |= uint16_t(1u << lane);
|
|
214
|
+
n_mirror++;
|
|
215
|
+
}
|
|
216
|
+
P[e] = uint8_t(p);
|
|
217
|
+
Q[e] = uint8_t(q);
|
|
218
|
+
e++;
|
|
219
|
+
p = (p - z) & z;
|
|
220
|
+
} while (p != 0);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
inline const ChildTable& child_table() {
|
|
226
|
+
static const ChildTable t;
|
|
227
|
+
return t;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/*********************************************************
|
|
231
|
+
* Kernels (generic version)
|
|
232
|
+
*********************************************************/
|
|
233
|
+
|
|
234
|
+
/** Inputs of the test of the 81 children of a node (u, v) at level k. */
|
|
235
|
+
struct StageInputs {
|
|
236
|
+
const float* ta; ///< 16 level-(k-4) minima of A' below u
|
|
237
|
+
const float* tb; ///< 16 level-(k-4) minima of B' below v
|
|
238
|
+
const float* tu; ///< 16 maxima of U = ru(nextup(acc) - W) of the outputs
|
|
239
|
+
const float* tus; ///< 16 maxima of Us = ru(acc - W)
|
|
240
|
+
const uint64_t* tm; ///< 16 maxima of the accumulator splits
|
|
241
|
+
uint64_t split_base; ///< smallest split of child (p, q) is split_base | p
|
|
242
|
+
///< << sh
|
|
243
|
+
int sh; ///< k - 4
|
|
244
|
+
bool mirror; ///< mirrored node: only the mirror lanes
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
template <SIMDLevel SL>
|
|
248
|
+
struct BlockKernels {
|
|
249
|
+
/** Test the 81 children of a node; write the entry numbers of the kept
|
|
250
|
+
* children to list (at most 81 + 15 slots) and return their number.
|
|
251
|
+
* A child is skipped if lo > max U (no candidate can be <= acc), or if
|
|
252
|
+
* lo >= max Us and every output split is <= the child's smallest split
|
|
253
|
+
* (no candidate < acc, and a tie would lose). */
|
|
254
|
+
static int stage(const StageInputs& in, uint32_t* list) {
|
|
255
|
+
const ChildTable& T = child_table();
|
|
256
|
+
int cnt = 0;
|
|
257
|
+
for (int e = 0; e < 81; e++) {
|
|
258
|
+
int row = e / 16, lane = e % 16;
|
|
259
|
+
uint16_t act = in.mirror ? T.mirror[row] : T.active[row];
|
|
260
|
+
if (!((act >> lane) & 1)) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
int p = T.P[e], q = T.Q[e], z = p | q;
|
|
264
|
+
float lo = add_rd(in.ta[p], in.tb[q]);
|
|
265
|
+
if (lo > in.tu[z]) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (lo >= in.tus[z] &&
|
|
269
|
+
in.tm[z] <= (in.split_base | (uint64_t(p) << in.sh))) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
list[cnt++] = uint32_t(e);
|
|
273
|
+
}
|
|
274
|
+
return cnt;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The 81 candidates of a level-4 node: a[p] + b[q] (rounded to nearest)
|
|
278
|
+
* merged into the 16 accumulator entries ac / as (lexicographic, split =
|
|
279
|
+
* split_base | p). Returns the number of candidates that changed the
|
|
280
|
+
* accumulator; *passes counts those that were <= acc. */
|
|
281
|
+
static int bottom(
|
|
282
|
+
const float* a,
|
|
283
|
+
const float* b,
|
|
284
|
+
float* ac,
|
|
285
|
+
uint64_t* as,
|
|
286
|
+
uint64_t split_base,
|
|
287
|
+
bool mirror,
|
|
288
|
+
uint64_t* passes) {
|
|
289
|
+
const ChildTable& T = child_table();
|
|
290
|
+
int nimp = 0;
|
|
291
|
+
for (int e = 0; e < 81; e++) {
|
|
292
|
+
int row = e / 16, lane = e % 16;
|
|
293
|
+
uint16_t act = mirror ? T.mirror[row] : T.active[row];
|
|
294
|
+
if (!((act >> lane) & 1)) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
int p = T.P[e], q = T.Q[e], z = p | q;
|
|
298
|
+
float c = a[p] + b[q];
|
|
299
|
+
if (c <= ac[z]) {
|
|
300
|
+
(*passes)++;
|
|
301
|
+
nimp += lex_update(c, split_base | uint64_t(p), ac + z, as + z);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return nimp;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Dense evaluation of the output tile Tt (16 outputs (Tt << 4) | z):
|
|
308
|
+
* all the candidates a[(X << 4) | p] + b[((Tt ^ X) << 4) | q] for X
|
|
309
|
+
* sub-group of Tt, with split split_base | (X << 4) | p, merged into
|
|
310
|
+
* ac[0..15], as[0..15]. mirror: skip the X that contain the highest bit
|
|
311
|
+
* of Tt. Returns the number of (X, 81 candidates) evaluated. */
|
|
312
|
+
static uint64_t dense_tile(
|
|
313
|
+
const float* a,
|
|
314
|
+
const float* b,
|
|
315
|
+
uint64_t Tt,
|
|
316
|
+
bool mirror,
|
|
317
|
+
float* ac,
|
|
318
|
+
uint64_t* as,
|
|
319
|
+
uint64_t split_base) {
|
|
320
|
+
const ChildTable& T = child_table();
|
|
321
|
+
const uint64_t hb = Tt ? (uint64_t(1) << highbit(Tt)) : 0;
|
|
322
|
+
uint64_t nk = 0;
|
|
323
|
+
uint64_t X = 0;
|
|
324
|
+
do {
|
|
325
|
+
if (!(mirror && (X & hb))) {
|
|
326
|
+
const float* ax = a + (X << 4);
|
|
327
|
+
const float* bx = b + ((Tt ^ X) << 4);
|
|
328
|
+
const uint64_t sx = split_base | (X << 4);
|
|
329
|
+
for (int e = 0; e < 81; e++) {
|
|
330
|
+
int p = T.P[e], q = T.Q[e];
|
|
331
|
+
lex_update(
|
|
332
|
+
ax[p] + bx[q],
|
|
333
|
+
sx | uint64_t(p),
|
|
334
|
+
ac + (p | q),
|
|
335
|
+
as + (p | q));
|
|
336
|
+
}
|
|
337
|
+
nk++;
|
|
338
|
+
}
|
|
339
|
+
X = (X - Tt) & Tt;
|
|
340
|
+
} while (X != 0);
|
|
341
|
+
return nk;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Level-4 reduction of 16 consecutive values of each of ng groups,
|
|
345
|
+
* after subtracting the offsets W (wlo[x] for the 4 low points, wup[g]
|
|
346
|
+
* for the others): KIND 0: min of rd(x - W); 1: max of ru(nextup(x) - W);
|
|
347
|
+
* 2: max of ru(x - W). */
|
|
348
|
+
template <int KIND>
|
|
349
|
+
static void level4(
|
|
350
|
+
const float* src,
|
|
351
|
+
const float* wlo,
|
|
352
|
+
const float* wup,
|
|
353
|
+
uint64_t ng,
|
|
354
|
+
float* out) {
|
|
355
|
+
for (uint64_t g = 0; g < ng; g++) {
|
|
356
|
+
float r = KIND == 0 ? kInf : -kInf;
|
|
357
|
+
for (int j = 0; j < 16; j++) {
|
|
358
|
+
float x = src[g * 16 + j];
|
|
359
|
+
float w = wlo[j] + wup[g];
|
|
360
|
+
if (KIND == 0) {
|
|
361
|
+
r = std::min(r, sub_rd(x, w));
|
|
362
|
+
} else if (KIND == 1) {
|
|
363
|
+
r = std::max(r, sub_ru(next_up(x), w));
|
|
364
|
+
} else {
|
|
365
|
+
r = std::max(r, sub_ru(x, w));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
out[g] = r;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/// out[g] = min (MAX: max) of in[16 g ... 16 g + 15]
|
|
373
|
+
template <bool MAX>
|
|
374
|
+
static void reduce16(const float* in, uint64_t nout, float* out) {
|
|
375
|
+
for (uint64_t g = 0; g < nout; g++) {
|
|
376
|
+
float r = in[g * 16];
|
|
377
|
+
for (int j = 1; j < 16; j++) {
|
|
378
|
+
r = MAX ? std::max(r, in[g * 16 + j])
|
|
379
|
+
: std::min(r, in[g * 16 + j]);
|
|
380
|
+
}
|
|
381
|
+
out[g] = r;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/// out[g] = max of in[16 g ... 16 g + 15]
|
|
386
|
+
static void max16_u64(const uint64_t* in, uint64_t nout, uint64_t* out) {
|
|
387
|
+
for (uint64_t g = 0; g < nout; g++) {
|
|
388
|
+
uint64_t r = in[g * 16];
|
|
389
|
+
for (int j = 1; j < 16; j++) {
|
|
390
|
+
r = std::max(r, in[g * 16 + j]);
|
|
391
|
+
}
|
|
392
|
+
out[g] = r;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/*********************************************************
|
|
398
|
+
* The engine
|
|
399
|
+
*********************************************************/
|
|
400
|
+
|
|
401
|
+
template <SIMDLevel SL>
|
|
402
|
+
struct BlockCombiner : OptimalKMeansBlockCombiner {
|
|
403
|
+
using Kern = BlockKernels<SL>;
|
|
404
|
+
|
|
405
|
+
struct Pair {
|
|
406
|
+
uint64_t l, r;
|
|
407
|
+
uint32_t ia, ib; // slice numbers among the sub-groups of s_hi
|
|
408
|
+
bool dense;
|
|
409
|
+
bool mirror; // sym and l == r == 0: mirrored candidates are skipped
|
|
410
|
+
int64_t prio;
|
|
411
|
+
float lb;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
struct Item { // a region of outputs and a range of its nodes
|
|
415
|
+
uint32_t R;
|
|
416
|
+
uint64_t n0, n1; // node = pair rank << popcount(R) | division of R
|
|
417
|
+
bool priv; // the region has several items: private accumulator
|
|
418
|
+
uint64_t weight;
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/// per-thread state of run_items
|
|
422
|
+
struct Worker {
|
|
423
|
+
std::vector<float> ut, us; // region max trees of U and Us
|
|
424
|
+
std::vector<uint64_t> um; // region max tree of the splits
|
|
425
|
+
std::vector<float> pc; // private accumulator
|
|
426
|
+
std::vector<uint64_t> ps;
|
|
427
|
+
float* ac = nullptr;
|
|
428
|
+
uint64_t* as = nullptr;
|
|
429
|
+
const float *a = nullptr, *b = nullptr, *TA = nullptr, *TB = nullptr;
|
|
430
|
+
uint64_t hi = 0;
|
|
431
|
+
bool dirty = true;
|
|
432
|
+
uint64_t nimp = 0; // accumulator updates since the trees were built
|
|
433
|
+
OptimalKMeansStats st;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// inputs
|
|
437
|
+
int n, L;
|
|
438
|
+
uint64_t s_hi;
|
|
439
|
+
const float *A, *B;
|
|
440
|
+
bool sym;
|
|
441
|
+
float* acc_c;
|
|
442
|
+
uint64_t* acc_s;
|
|
443
|
+
OptimalKMeansCombineParams P;
|
|
444
|
+
const ChildTable& tab = child_table();
|
|
445
|
+
|
|
446
|
+
// output tables of the whole combination, for the context guesses
|
|
447
|
+
const uint64_t* ctx_s = nullptr;
|
|
448
|
+
|
|
449
|
+
// state
|
|
450
|
+
bool pruned = false;
|
|
451
|
+
std::vector<float> snap_c;
|
|
452
|
+
std::vector<uint64_t> snap_s;
|
|
453
|
+
OptimalKMeansStats st; // setup counters
|
|
454
|
+
|
|
455
|
+
// offsets
|
|
456
|
+
float w[64] = {0};
|
|
457
|
+
float wlo16[16] = {0};
|
|
458
|
+
std::vector<float> wup; // W of the points 4 .. L-1, indexed by z >> 4
|
|
459
|
+
|
|
460
|
+
// slice trees (levels 4, 8, ..., K) and slice minima
|
|
461
|
+
int K = 0;
|
|
462
|
+
size_t toff[16] = {0};
|
|
463
|
+
size_t tsz = 0;
|
|
464
|
+
uint32_t nsub = 1;
|
|
465
|
+
std::vector<float> treeA, treeB, minA, minB;
|
|
466
|
+
const float *pTB = nullptr, *pminB = nullptr;
|
|
467
|
+
float maxU_block = kInf, maxUs_block = kInf;
|
|
468
|
+
uint64_t maxS_block = 0;
|
|
469
|
+
|
|
470
|
+
// pairs, regions and items
|
|
471
|
+
std::vector<Pair> pairs;
|
|
472
|
+
uint64_t n_pairs_total = 0, n_skipped = 0;
|
|
473
|
+
int t = 0, Lp = 0, KR = 0;
|
|
474
|
+
size_t uoff[16] = {0};
|
|
475
|
+
size_t usz = 1;
|
|
476
|
+
std::unique_ptr<std::mutex[]> locks;
|
|
477
|
+
// the most promising pair may be run alone in a first phase, then the
|
|
478
|
+
// other pairs are sampled again against the improved accumulator
|
|
479
|
+
bool resample = false;
|
|
480
|
+
int phase = 0;
|
|
481
|
+
std::vector<Item> items; // of the current phase
|
|
482
|
+
std::atomic<size_t> next_item{0}; // first item not claimed yet
|
|
483
|
+
|
|
484
|
+
BlockCombiner(
|
|
485
|
+
int n_in,
|
|
486
|
+
int L_in,
|
|
487
|
+
uint64_t s_hi_in,
|
|
488
|
+
const float* A_in,
|
|
489
|
+
const float* B_in,
|
|
490
|
+
bool sym_in,
|
|
491
|
+
float* acc_cost,
|
|
492
|
+
uint64_t* acc_split,
|
|
493
|
+
const OptimalKMeansCombineParams& params)
|
|
494
|
+
: n(n_in),
|
|
495
|
+
L(L_in),
|
|
496
|
+
s_hi(s_hi_in),
|
|
497
|
+
A(A_in),
|
|
498
|
+
B(B_in),
|
|
499
|
+
sym(sym_in),
|
|
500
|
+
acc_c(acc_cost),
|
|
501
|
+
acc_s(acc_split),
|
|
502
|
+
P(params) {
|
|
503
|
+
pruned = P.pruned && L >= 4;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
uint64_t N() const {
|
|
507
|
+
return uint64_t(1) << L;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/*********************** guess pass */
|
|
511
|
+
|
|
512
|
+
/* For a group S and each of its gbits highest points p, the best known
|
|
513
|
+
* division of S \ {p} with p added on either side. Block points: the
|
|
514
|
+
* division is read in the accumulator (already improved for the smaller
|
|
515
|
+
* z). Context points: in the output of the block s_hi ^ (bit of p), if
|
|
516
|
+
* set_context gave it. With the context and >= 2 context points, the
|
|
517
|
+
* context guesses are good enough: gbits = guess_bits_ctx. */
|
|
518
|
+
void guess_pass() {
|
|
519
|
+
const int gbits = ctx_s && popcount64(s_hi) >= 2 ? P.guess_bits_ctx
|
|
520
|
+
: P.guess_bits;
|
|
521
|
+
uint64_t ncand = 0, nimp = 0;
|
|
522
|
+
for (uint64_t z = 0; z < N(); z++) {
|
|
523
|
+
const uint64_t S = (s_hi << L) | z;
|
|
524
|
+
float bc = acc_c[z];
|
|
525
|
+
uint64_t bs = acc_s[z];
|
|
526
|
+
bool changed = false;
|
|
527
|
+
uint64_t bits = S;
|
|
528
|
+
for (int cnt = 0; bits && cnt < gbits; cnt++) {
|
|
529
|
+
const int p = highbit(bits);
|
|
530
|
+
const uint64_t bit = uint64_t(1) << p;
|
|
531
|
+
bits ^= bit;
|
|
532
|
+
const uint64_t Tm = S ^ bit;
|
|
533
|
+
uint64_t Lt;
|
|
534
|
+
if (p >= L) {
|
|
535
|
+
if (!ctx_s) {
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
Lt = ctx_s[Tm] & Tm;
|
|
539
|
+
} else {
|
|
540
|
+
Lt = acc_s[z ^ bit] & Tm;
|
|
541
|
+
}
|
|
542
|
+
const uint64_t Rt = Tm ^ Lt;
|
|
543
|
+
ncand += 2;
|
|
544
|
+
if (lex_update(A[Lt | bit] + B[Rt], Lt | bit, &bc, &bs)) {
|
|
545
|
+
changed = true;
|
|
546
|
+
nimp++;
|
|
547
|
+
}
|
|
548
|
+
if (lex_update(A[Lt] + B[Rt | bit], Lt, &bc, &bs)) {
|
|
549
|
+
changed = true;
|
|
550
|
+
nimp++;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (changed) {
|
|
554
|
+
acc_c[z] = bc;
|
|
555
|
+
acc_s[z] = bs;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
st.guess_candidates += ncand;
|
|
559
|
+
st.guess_improved += nimp;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/*********************** offsets */
|
|
563
|
+
|
|
564
|
+
void fit_offsets() {
|
|
565
|
+
const uint64_t mask = N() - 1;
|
|
566
|
+
const uint64_t nsamp = std::min<uint64_t>(N(), 1 << 14);
|
|
567
|
+
double wd[64], m = 0;
|
|
568
|
+
for (int i = 0; i < L; i++) {
|
|
569
|
+
double s = 0;
|
|
570
|
+
uint64_t c = 0;
|
|
571
|
+
const uint64_t bit = uint64_t(1) << i;
|
|
572
|
+
for (uint64_t j = 0; j < nsamp; j++) {
|
|
573
|
+
const uint64_t z =
|
|
574
|
+
(nsamp == N() ? j : (j * 2654435761ull + (j >> 7))) &
|
|
575
|
+
mask & ~bit;
|
|
576
|
+
const float a = acc_c[z], b = acc_c[z | bit];
|
|
577
|
+
if (std::isfinite(a) && std::isfinite(b)) {
|
|
578
|
+
double d = double(b) - double(a);
|
|
579
|
+
if (std::isfinite(d)) {
|
|
580
|
+
s += d;
|
|
581
|
+
c++;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
wd[i] = c ? s / double(c) : 0.0;
|
|
586
|
+
if (!std::isfinite(wd[i])) {
|
|
587
|
+
wd[i] = 0;
|
|
588
|
+
}
|
|
589
|
+
m += std::fabs(wd[i]);
|
|
590
|
+
}
|
|
591
|
+
// quantize to multiples of q: sum |w_i| < 2^22 q, so that every sum
|
|
592
|
+
// W(X) of offsets is exact in float
|
|
593
|
+
const bool ok = m > 1e-30 && m < 1e30;
|
|
594
|
+
int e = 0;
|
|
595
|
+
std::frexp(m, &e);
|
|
596
|
+
const double q = std::ldexp(1.0, e - 21);
|
|
597
|
+
for (int i = 0; i < L; i++) {
|
|
598
|
+
w[i] = ok ? float(std::rint(wd[i] / q) * q) : 0.f;
|
|
599
|
+
}
|
|
600
|
+
for (int x = 0; x < 16; x++) {
|
|
601
|
+
float s = 0;
|
|
602
|
+
for (int i = 0; i < 4 && i < L; i++) {
|
|
603
|
+
if ((x >> i) & 1) {
|
|
604
|
+
s += w[i];
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
wlo16[x] = s;
|
|
608
|
+
}
|
|
609
|
+
const uint64_t nu = uint64_t(1) << (L - 4);
|
|
610
|
+
wup.resize(nu);
|
|
611
|
+
wup[0] = 0.f;
|
|
612
|
+
for (uint64_t u = 1; u < nu; u++) {
|
|
613
|
+
wup[u] = wup[u & (u - 1)] + w[4 + __builtin_ctzll(u)];
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/*********************** trees */
|
|
618
|
+
|
|
619
|
+
void build_trees() {
|
|
620
|
+
K = (L / 4) * 4;
|
|
621
|
+
size_t off = 0;
|
|
622
|
+
for (int k = 4, i = 0; k <= K; k += 4, i++) {
|
|
623
|
+
toff[i] = off;
|
|
624
|
+
off += size_t(1) << (L - k);
|
|
625
|
+
}
|
|
626
|
+
tsz = off;
|
|
627
|
+
nsub = uint32_t(1) << popcount64(s_hi);
|
|
628
|
+
const int nlev = K / 4;
|
|
629
|
+
auto build = [&](const float* table,
|
|
630
|
+
std::vector<float>& tree,
|
|
631
|
+
std::vector<float>& mins) {
|
|
632
|
+
tree.resize(size_t(nsub) * tsz);
|
|
633
|
+
mins.resize(nsub);
|
|
634
|
+
for (uint32_t si = 0; si < nsub; si++) {
|
|
635
|
+
const uint64_t hs = deposit_bits(si, s_hi);
|
|
636
|
+
const float* src = table + (hs << L);
|
|
637
|
+
float* tr = tree.data() + size_t(si) * tsz;
|
|
638
|
+
Kern::template level4<0>(
|
|
639
|
+
src, wlo16, wup.data(), uint64_t(1) << (L - 4), tr);
|
|
640
|
+
for (int i = 1; i < nlev; i++) {
|
|
641
|
+
Kern::template reduce16<false>(
|
|
642
|
+
tr + toff[i - 1],
|
|
643
|
+
uint64_t(1) << (L - 4 - 4 * i),
|
|
644
|
+
tr + toff[i]);
|
|
645
|
+
}
|
|
646
|
+
const float* top = tr + toff[nlev - 1];
|
|
647
|
+
float mn = top[0];
|
|
648
|
+
for (size_t j = 1; j < (size_t(1) << (L - K)); j++) {
|
|
649
|
+
mn = std::min(mn, top[j]);
|
|
650
|
+
}
|
|
651
|
+
mins[si] = mn;
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
build(A, treeA, minA);
|
|
655
|
+
if (!sym) {
|
|
656
|
+
build(B, treeB, minB);
|
|
657
|
+
}
|
|
658
|
+
pTB = sym ? treeA.data() : treeB.data();
|
|
659
|
+
pminB = sym ? minA.data() : minB.data();
|
|
660
|
+
// block maxima of U and Us, and of the splits
|
|
661
|
+
const uint64_t ng = uint64_t(1) << (L - 4);
|
|
662
|
+
std::vector<float> tmp(std::min<uint64_t>(ng, 4096));
|
|
663
|
+
float mx = -kInf, mxs = -kInf;
|
|
664
|
+
for (uint64_t g0 = 0; g0 < ng; g0 += tmp.size()) {
|
|
665
|
+
const uint64_t cnt = std::min<uint64_t>(tmp.size(), ng - g0);
|
|
666
|
+
Kern::template level4<1>(
|
|
667
|
+
acc_c + (g0 << 4), wlo16, wup.data() + g0, cnt, tmp.data());
|
|
668
|
+
for (uint64_t j = 0; j < cnt; j++) {
|
|
669
|
+
mx = std::max(mx, tmp[j]);
|
|
670
|
+
}
|
|
671
|
+
Kern::template level4<2>(
|
|
672
|
+
acc_c + (g0 << 4), wlo16, wup.data() + g0, cnt, tmp.data());
|
|
673
|
+
for (uint64_t j = 0; j < cnt; j++) {
|
|
674
|
+
mxs = std::max(mxs, tmp[j]);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
maxU_block = mx;
|
|
678
|
+
maxUs_block = mxs;
|
|
679
|
+
uint64_t ms = 0;
|
|
680
|
+
for (uint64_t z = 0; z < N(); z++) {
|
|
681
|
+
ms = std::max(ms, acc_s[z]);
|
|
682
|
+
}
|
|
683
|
+
maxS_block = ms;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/// region trees of the current accumulator of region R (2^Lp outputs)
|
|
687
|
+
void build_region_trees(Worker& W, uint32_t R) const {
|
|
688
|
+
const uint64_t ng = uint64_t(1) << (Lp - 4);
|
|
689
|
+
const float* wu = wup.data() + (uint64_t(R) << (Lp - 4));
|
|
690
|
+
Kern::template level4<1>(W.ac, wlo16, wu, ng, W.ut.data() + uoff[0]);
|
|
691
|
+
Kern::template level4<2>(W.ac, wlo16, wu, ng, W.us.data() + uoff[0]);
|
|
692
|
+
Kern::max16_u64(W.as, ng, W.um.data() + uoff[0]);
|
|
693
|
+
for (int i = 1; i < KR / 4; i++) {
|
|
694
|
+
const uint64_t n2 = uint64_t(1) << (Lp - 4 * (i + 1));
|
|
695
|
+
Kern::template reduce16<true>(
|
|
696
|
+
W.ut.data() + uoff[i - 1], n2, W.ut.data() + uoff[i]);
|
|
697
|
+
Kern::template reduce16<true>(
|
|
698
|
+
W.us.data() + uoff[i - 1], n2, W.us.data() + uoff[i]);
|
|
699
|
+
Kern::max16_u64(
|
|
700
|
+
W.um.data() + uoff[i - 1], n2, W.um.data() + uoff[i]);
|
|
701
|
+
}
|
|
702
|
+
W.dirty = false;
|
|
703
|
+
W.nimp = 0;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/// skip test of one node with lower bound lo and smallest split ms
|
|
707
|
+
/// against entry `at` of level i of the region trees
|
|
708
|
+
bool skip1(const Worker& W, int i, uint64_t at, float lo, uint64_t ms)
|
|
709
|
+
const {
|
|
710
|
+
if (lo > W.ut[uoff[i] + at]) {
|
|
711
|
+
return true;
|
|
712
|
+
}
|
|
713
|
+
return lo >= W.us[uoff[i] + at] && W.um[uoff[i] + at] <= ms;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/*********************** pruned depth-first search */
|
|
717
|
+
|
|
718
|
+
void bottom(Worker& W, uint64_t u, uint64_t v, bool mirror) {
|
|
719
|
+
const uint64_t loc = (u | v) & ((uint64_t(1) << (Lp - 4)) - 1);
|
|
720
|
+
uint64_t passes = 0;
|
|
721
|
+
int nimp = Kern::bottom(
|
|
722
|
+
W.a + (u << 4),
|
|
723
|
+
W.b + (v << 4),
|
|
724
|
+
W.ac + (loc << 4),
|
|
725
|
+
W.as + (loc << 4),
|
|
726
|
+
W.hi | (u << 4),
|
|
727
|
+
mirror,
|
|
728
|
+
&passes);
|
|
729
|
+
W.nimp += nimp;
|
|
730
|
+
W.st.candidates_evaluated += mirror ? tab.n_mirror : tab.n_active;
|
|
731
|
+
W.st.bottom_nodes++;
|
|
732
|
+
W.st.bottom_passes += passes;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/// node (u, v) at level k >= 8, kept: test its 81 children at level k - 4
|
|
736
|
+
void stage(Worker& W, int k, uint64_t u, uint64_t v, bool mirror) {
|
|
737
|
+
const int i = (k - 4) / 4 - 1; // tree index of level k - 4
|
|
738
|
+
const uint64_t loc = (u | v) & ((uint64_t(1) << (Lp - k)) - 1);
|
|
739
|
+
StageInputs in;
|
|
740
|
+
in.ta = W.TA + toff[i] + (u << 4);
|
|
741
|
+
in.tb = W.TB + toff[i] + (v << 4);
|
|
742
|
+
in.tu = W.ut.data() + uoff[i] + (loc << 4);
|
|
743
|
+
in.tus = W.us.data() + uoff[i] + (loc << 4);
|
|
744
|
+
in.tm = W.um.data() + uoff[i] + (loc << 4);
|
|
745
|
+
in.split_base = W.hi | (u << k);
|
|
746
|
+
in.sh = k - 4;
|
|
747
|
+
in.mirror = mirror;
|
|
748
|
+
uint32_t list[96 + 16];
|
|
749
|
+
const int cnt = Kern::stage(in, list);
|
|
750
|
+
W.st.nodes_tested += mirror ? tab.n_mirror : tab.n_active;
|
|
751
|
+
W.st.nodes_kept += cnt;
|
|
752
|
+
for (int x = 0; x < cnt; x++) {
|
|
753
|
+
const uint32_t e = list[x];
|
|
754
|
+
const uint64_t cu = (u << 4) | tab.P[e], cv = (v << 4) | tab.Q[e];
|
|
755
|
+
const bool cm = mirror && e == 0;
|
|
756
|
+
if (k == 8) {
|
|
757
|
+
bottom(W, cu, cv, cm);
|
|
758
|
+
} else {
|
|
759
|
+
stage(W, k - 4, cu, cv, cm);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/// node (u, v) at the top of the region (level Lp)
|
|
765
|
+
void enter_node(Worker& W, uint64_t u, uint64_t v, bool mirror) {
|
|
766
|
+
const int r = Lp % 4, k0 = Lp - r;
|
|
767
|
+
if (r == 0) {
|
|
768
|
+
if (k0 == 4) {
|
|
769
|
+
bottom(W, u, v, mirror);
|
|
770
|
+
} else {
|
|
771
|
+
stage(W, k0, u, v, mirror);
|
|
772
|
+
}
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
// the r highest points one at a time, then 4 at a time
|
|
776
|
+
const int i = k0 / 4 - 1;
|
|
777
|
+
const float *ta = W.TA + toff[i], *tb = W.TB + toff[i];
|
|
778
|
+
for (uint32_t z = 0; z < (1u << r); z++) {
|
|
779
|
+
uint32_t p = 0;
|
|
780
|
+
do {
|
|
781
|
+
const uint32_t q = z ^ p;
|
|
782
|
+
if (!mirror || z == 0 || ((q >> highbit(z)) & 1)) {
|
|
783
|
+
const uint64_t cu = (u << r) | p, cv = (v << r) | q;
|
|
784
|
+
W.st.nodes_tested++;
|
|
785
|
+
if (!skip1(W,
|
|
786
|
+
i,
|
|
787
|
+
(cu | cv) & ((1u << r) - 1),
|
|
788
|
+
add_rd(ta[cu], tb[cv]),
|
|
789
|
+
W.hi | (cu << k0))) {
|
|
790
|
+
W.st.nodes_kept++;
|
|
791
|
+
const bool cm = mirror && z == 0;
|
|
792
|
+
if (k0 == 4) {
|
|
793
|
+
bottom(W, cu, cv, cm);
|
|
794
|
+
} else {
|
|
795
|
+
stage(W, k0, cu, cv, cm);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
p = (p - z) & z;
|
|
800
|
+
} while (p != 0);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/*********************** dense node */
|
|
805
|
+
|
|
806
|
+
/// all candidates of node (u, v) at level Lp, by output tiles of 16
|
|
807
|
+
void dense_node(Worker& W, uint64_t u, uint64_t v, bool mirror) {
|
|
808
|
+
const float* a = W.a + (u << Lp);
|
|
809
|
+
const float* b = W.b + (v << Lp);
|
|
810
|
+
const uint64_t sb = W.hi | (u << Lp);
|
|
811
|
+
uint64_t nk = 0;
|
|
812
|
+
for (uint64_t Tt = 0; Tt < (uint64_t(1) << (Lp - 4)); Tt++) {
|
|
813
|
+
nk += Kern::dense_tile(
|
|
814
|
+
a, b, Tt, mirror, W.ac + (Tt << 4), W.as + (Tt << 4), sb);
|
|
815
|
+
}
|
|
816
|
+
W.st.candidates_evaluated += 81 * nk;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/// L < 4: plain loops
|
|
820
|
+
void scalar_node(Worker& W) {
|
|
821
|
+
uint64_t nc = 0;
|
|
822
|
+
for (uint64_t z = 0; z < N(); z++) {
|
|
823
|
+
uint64_t x = 0;
|
|
824
|
+
do {
|
|
825
|
+
lex_update(W.a[x] + W.b[z ^ x], W.hi | x, W.ac + z, W.as + z);
|
|
826
|
+
nc++;
|
|
827
|
+
x = (x - z) & z;
|
|
828
|
+
} while (x != 0);
|
|
829
|
+
}
|
|
830
|
+
W.st.candidates_evaluated += nc;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/*********************** pairs */
|
|
834
|
+
|
|
835
|
+
/// fraction of the candidates of 'samples' random level-4 nodes of the
|
|
836
|
+
/// pair that are < acc
|
|
837
|
+
double sample_rate(const Pair& p, uint64_t seed) const {
|
|
838
|
+
uint64_t x = seed * 0x9E3779B97F4A7C15ull + 0x632BE59BD9B4E019ull;
|
|
839
|
+
auto rnd = [&]() {
|
|
840
|
+
x ^= x >> 12;
|
|
841
|
+
x ^= x << 25;
|
|
842
|
+
x ^= x >> 27;
|
|
843
|
+
return x * 0x2545F4914F6CDD1Dull;
|
|
844
|
+
};
|
|
845
|
+
uint64_t pass = 0, tot = 0;
|
|
846
|
+
const float *a = A + (p.l << L), *b = B + (p.r << L);
|
|
847
|
+
for (int s = 0; s < P.samples; s++) {
|
|
848
|
+
uint64_t u = 0, v = 0;
|
|
849
|
+
for (int bi = 0; bi < L - 4; bi++) {
|
|
850
|
+
const uint64_t r3 = rnd() % 3;
|
|
851
|
+
if (r3 == 1) {
|
|
852
|
+
u |= uint64_t(1) << bi;
|
|
853
|
+
} else if (r3 == 2) {
|
|
854
|
+
v |= uint64_t(1) << bi;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
for (int e = 0; e < 81; e++) {
|
|
858
|
+
int pp = tab.P[e], qq = tab.Q[e];
|
|
859
|
+
pass += a[(u << 4) | pp] + b[(v << 4) | qq] <
|
|
860
|
+
acc_c[((u | v) << 4) | (pp | qq)];
|
|
861
|
+
}
|
|
862
|
+
tot += 81;
|
|
863
|
+
}
|
|
864
|
+
return tot ? double(pass) / double(tot) : 1.0;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
void make_pairs() {
|
|
868
|
+
const int ms = popcount64(s_hi);
|
|
869
|
+
nsub = uint32_t(1) << ms;
|
|
870
|
+
std::vector<int64_t> hist;
|
|
871
|
+
if (pruned) {
|
|
872
|
+
hist.resize(nsub);
|
|
873
|
+
for (uint64_t z = 0; z < N(); z++) {
|
|
874
|
+
const uint64_t h = acc_s[z] >> L;
|
|
875
|
+
if ((h & ~s_hi) == 0) {
|
|
876
|
+
hist[extract_bits(h, s_hi)]++;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
for (uint32_t ia = 0; ia < nsub; ia++) {
|
|
881
|
+
const uint64_t l = deposit_bits(ia, s_hi), r = s_hi ^ l;
|
|
882
|
+
n_pairs_total++;
|
|
883
|
+
if (sym && l > r) {
|
|
884
|
+
continue; // mirrored pair: same costs, larger splits
|
|
885
|
+
}
|
|
886
|
+
Pair p;
|
|
887
|
+
p.l = l;
|
|
888
|
+
p.r = r;
|
|
889
|
+
p.ia = ia;
|
|
890
|
+
p.ib = uint32_t(extract_bits(r, s_hi));
|
|
891
|
+
p.mirror = sym && l == 0 && r == 0;
|
|
892
|
+
p.dense = !pruned;
|
|
893
|
+
p.prio = 0;
|
|
894
|
+
p.lb = -kInf;
|
|
895
|
+
if (pruned) {
|
|
896
|
+
p.lb = add_rd(minA[p.ia], pminB[p.ib]);
|
|
897
|
+
if (p.lb > maxU_block ||
|
|
898
|
+
(p.lb >= maxUs_block && maxS_block <= (l << L))) {
|
|
899
|
+
n_skipped++;
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
// most promising first: entries whose split comes from this
|
|
903
|
+
// slice
|
|
904
|
+
p.prio = hist[p.ia] + (sym ? hist[p.ib] : 0);
|
|
905
|
+
}
|
|
906
|
+
pairs.push_back(p);
|
|
907
|
+
}
|
|
908
|
+
if (pruned) {
|
|
909
|
+
std::stable_sort(
|
|
910
|
+
pairs.begin(),
|
|
911
|
+
pairs.end(),
|
|
912
|
+
[](const Pair& x, const Pair& y) {
|
|
913
|
+
if (x.prio != y.prio) {
|
|
914
|
+
return x.prio > y.prio;
|
|
915
|
+
}
|
|
916
|
+
return x.lb < y.lb;
|
|
917
|
+
});
|
|
918
|
+
bool anyd = false;
|
|
919
|
+
for (size_t i = 0; i < pairs.size(); i++) {
|
|
920
|
+
pairs[i].dense = P.dense_rate < 1 &&
|
|
921
|
+
sample_rate(pairs[i], s_hi * 1000003ull + i) >
|
|
922
|
+
P.dense_rate;
|
|
923
|
+
anyd |= pairs[i].dense;
|
|
924
|
+
}
|
|
925
|
+
// run the most promising pair first and sample the others again
|
|
926
|
+
if (anyd && pairs.size() > 1) {
|
|
927
|
+
pairs[0].dense = false;
|
|
928
|
+
resample = true;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/// items of the pairs [pb, pe)
|
|
934
|
+
void make_items(size_t pb, size_t pe, int nthreads_hint) {
|
|
935
|
+
const uint64_t np = pe - pb;
|
|
936
|
+
if (np == 0) {
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
const uint64_t nreg = uint64_t(1) << t;
|
|
940
|
+
const uint64_t tot = np * pow3(t);
|
|
941
|
+
const uint64_t target = std::max<uint64_t>(
|
|
942
|
+
1, tot / (uint64_t(std::max(1, nthreads_hint)) * 4));
|
|
943
|
+
for (uint64_t R = 0; R < nreg; R++) {
|
|
944
|
+
const uint64_t nn = np << popcount64(R);
|
|
945
|
+
const uint64_t nch = (nn + target - 1) / target;
|
|
946
|
+
for (uint64_t c = 0; c < nch; c++) {
|
|
947
|
+
Item it;
|
|
948
|
+
it.R = uint32_t(R);
|
|
949
|
+
it.n0 = (pb << popcount64(R)) + nn * c / nch;
|
|
950
|
+
it.n1 = (pb << popcount64(R)) + nn * (c + 1) / nch;
|
|
951
|
+
it.priv = nch > 1;
|
|
952
|
+
it.weight = it.n1 - it.n0;
|
|
953
|
+
if (it.n1 > it.n0) {
|
|
954
|
+
items.push_back(it);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/*********************** OptimalKMeansBlockCombiner */
|
|
961
|
+
|
|
962
|
+
void set_context(const uint64_t* split) override {
|
|
963
|
+
ctx_s = split;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
void setup() override {
|
|
967
|
+
snap_c.assign(acc_c, acc_c + N());
|
|
968
|
+
snap_s.assign(acc_s, acc_s + N());
|
|
969
|
+
if (pruned) {
|
|
970
|
+
guess_pass();
|
|
971
|
+
fit_offsets();
|
|
972
|
+
build_trees();
|
|
973
|
+
}
|
|
974
|
+
make_pairs();
|
|
975
|
+
Lp = P.region_level >= 4 && P.region_level < L ? P.region_level & ~3
|
|
976
|
+
: L;
|
|
977
|
+
t = L - Lp;
|
|
978
|
+
KR = (Lp / 4) * 4;
|
|
979
|
+
size_t off = 0;
|
|
980
|
+
for (int k = 4, i = 0; k <= KR; k += 4, i++) {
|
|
981
|
+
uoff[i] = off;
|
|
982
|
+
off += size_t(1) << (Lp - k);
|
|
983
|
+
}
|
|
984
|
+
usz = std::max<size_t>(off, 1);
|
|
985
|
+
locks.reset(new std::mutex[uint64_t(1) << t]);
|
|
986
|
+
st.pairs_total += n_pairs_total;
|
|
987
|
+
st.pairs_skipped += n_skipped;
|
|
988
|
+
st.candidates_total += n_pairs_total * pow3(L);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
size_t next_phase() override {
|
|
992
|
+
items.clear();
|
|
993
|
+
next_item = 0;
|
|
994
|
+
const size_t np = pairs.size();
|
|
995
|
+
if (phase == 0) {
|
|
996
|
+
make_items(0, resample ? 1 : np, P.nthreads_hint);
|
|
997
|
+
} else if (phase == 1 && resample) {
|
|
998
|
+
// sample the dense pairs again against the improved accumulator
|
|
999
|
+
for (size_t i = 1; i < np; i++) {
|
|
1000
|
+
if (pairs[i].dense) {
|
|
1001
|
+
pairs[i].dense =
|
|
1002
|
+
sample_rate(
|
|
1003
|
+
pairs[i], s_hi * 1000003ull + i + 7777) >
|
|
1004
|
+
P.dense_rate;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
make_items(1, np, P.nthreads_hint);
|
|
1008
|
+
}
|
|
1009
|
+
phase++;
|
|
1010
|
+
// heaviest first
|
|
1011
|
+
std::stable_sort(
|
|
1012
|
+
items.begin(), items.end(), [](const Item& x, const Item& y) {
|
|
1013
|
+
return x.weight > y.weight;
|
|
1014
|
+
});
|
|
1015
|
+
return items.size();
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
void run_item(Worker& W, const Item& it) {
|
|
1019
|
+
const uint32_t R = it.R;
|
|
1020
|
+
const int pr = popcount64(R);
|
|
1021
|
+
const uint64_t npp = uint64_t(1) << pr;
|
|
1022
|
+
const size_t rsz = size_t(1) << Lp;
|
|
1023
|
+
if (it.priv) {
|
|
1024
|
+
std::lock_guard<std::mutex> lk(locks[R]);
|
|
1025
|
+
W.pc.assign(
|
|
1026
|
+
acc_c + (size_t(R) << Lp), acc_c + (size_t(R) << Lp) + rsz);
|
|
1027
|
+
W.ps.assign(
|
|
1028
|
+
acc_s + (size_t(R) << Lp), acc_s + (size_t(R) << Lp) + rsz);
|
|
1029
|
+
W.ac = W.pc.data();
|
|
1030
|
+
W.as = W.ps.data();
|
|
1031
|
+
} else {
|
|
1032
|
+
W.ac = acc_c + (size_t(R) << Lp);
|
|
1033
|
+
W.as = acc_s + (size_t(R) << Lp);
|
|
1034
|
+
}
|
|
1035
|
+
const uint64_t hbR = R ? (uint64_t(1) << highbit(R)) : 0;
|
|
1036
|
+
int64_t cur = -1;
|
|
1037
|
+
W.dirty = true; // the region trees belong to the previous item
|
|
1038
|
+
for (uint64_t nd = it.n0; nd < it.n1; nd++) {
|
|
1039
|
+
const uint64_t pi = nd >> pr;
|
|
1040
|
+
const uint64_t ut = deposit_bits(nd & (npp - 1), R), vt = R ^ ut;
|
|
1041
|
+
const Pair& p = pairs[pi];
|
|
1042
|
+
if (p.mirror && (ut & hbR)) {
|
|
1043
|
+
continue; // mirrored division of the region points
|
|
1044
|
+
}
|
|
1045
|
+
const bool mirror = p.mirror && R == 0;
|
|
1046
|
+
const bool use_pruned = !p.dense && pruned;
|
|
1047
|
+
if (int64_t(pi) != cur) {
|
|
1048
|
+
cur = int64_t(pi);
|
|
1049
|
+
W.a = A + (p.l << L);
|
|
1050
|
+
W.b = B + (p.r << L);
|
|
1051
|
+
W.hi = p.l << L;
|
|
1052
|
+
if (use_pruned) {
|
|
1053
|
+
W.TA = treeA.data() + size_t(p.ia) * tsz;
|
|
1054
|
+
W.TB = pTB + size_t(p.ib) * tsz;
|
|
1055
|
+
// stale trees stay valid (acc only decreases)
|
|
1056
|
+
if (W.dirty ||
|
|
1057
|
+
W.nimp > (uint64_t(1)
|
|
1058
|
+
<< std::max(Lp - P.rebuild_shift, 0))) {
|
|
1059
|
+
build_region_trees(W, R);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
if (L < 4) {
|
|
1064
|
+
scalar_node(W);
|
|
1065
|
+
} else if (!use_pruned) {
|
|
1066
|
+
dense_node(W, ut, vt, mirror);
|
|
1067
|
+
} else {
|
|
1068
|
+
if (t > 0) {
|
|
1069
|
+
const int i = Lp / 4 - 1;
|
|
1070
|
+
W.st.nodes_tested++;
|
|
1071
|
+
if (skip1(W,
|
|
1072
|
+
i,
|
|
1073
|
+
0,
|
|
1074
|
+
add_rd(W.TA[toff[i] + ut], W.TB[toff[i] + vt]),
|
|
1075
|
+
W.hi | (ut << Lp))) {
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
W.st.nodes_kept++;
|
|
1079
|
+
}
|
|
1080
|
+
enter_node(W, ut, vt, mirror);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
if (it.priv) {
|
|
1084
|
+
std::lock_guard<std::mutex> lk(locks[R]);
|
|
1085
|
+
float* dc = acc_c + (size_t(R) << Lp);
|
|
1086
|
+
uint64_t* ds = acc_s + (size_t(R) << Lp);
|
|
1087
|
+
for (size_t j = 0; j < rsz; j++) {
|
|
1088
|
+
lex_update(W.pc[j], W.ps[j], dc + j, ds + j);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
void init_worker(Worker& W) const {
|
|
1094
|
+
if (pruned) {
|
|
1095
|
+
W.ut.resize(usz);
|
|
1096
|
+
W.us.resize(usz);
|
|
1097
|
+
W.um.resize(usz);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
void run_items(size_t i0, size_t i1, OptimalKMeansStats* stats) override {
|
|
1102
|
+
FAISS_THROW_IF_NOT(i0 <= i1 && i1 <= items.size());
|
|
1103
|
+
Worker W;
|
|
1104
|
+
init_worker(W);
|
|
1105
|
+
for (size_t i = i0; i < i1; i++) {
|
|
1106
|
+
run_item(W, items[i]);
|
|
1107
|
+
W.st.items++;
|
|
1108
|
+
}
|
|
1109
|
+
if (stats) {
|
|
1110
|
+
stats->add(W.st);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
size_t run_pending_items(OptimalKMeansStats* stats) override {
|
|
1115
|
+
Worker W;
|
|
1116
|
+
init_worker(W);
|
|
1117
|
+
size_t nrun = 0;
|
|
1118
|
+
for (;;) {
|
|
1119
|
+
const size_t i = next_item.fetch_add(1);
|
|
1120
|
+
if (i >= items.size()) {
|
|
1121
|
+
break;
|
|
1122
|
+
}
|
|
1123
|
+
run_item(W, items[i]);
|
|
1124
|
+
W.st.items++;
|
|
1125
|
+
nrun++;
|
|
1126
|
+
}
|
|
1127
|
+
if (stats) {
|
|
1128
|
+
stats->add(W.st);
|
|
1129
|
+
}
|
|
1130
|
+
return nrun;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
void finish(OptimalKMeansStats* stats) override {
|
|
1134
|
+
if (!stats) {
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
for (const Pair& p : pairs) {
|
|
1138
|
+
if (p.dense || !pruned) {
|
|
1139
|
+
st.pairs_dense++;
|
|
1140
|
+
} else {
|
|
1141
|
+
st.pairs_pruned++;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
uint64_t nimp = 0;
|
|
1145
|
+
for (uint64_t z = 0; z < N(); z++) {
|
|
1146
|
+
nimp += std::memcmp(acc_c + z, &snap_c[z], 4) != 0 ||
|
|
1147
|
+
acc_s[z] != snap_s[z];
|
|
1148
|
+
}
|
|
1149
|
+
st.entries_improved += nimp;
|
|
1150
|
+
stats->add(st);
|
|
1151
|
+
}
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
} // namespace
|
|
1155
|
+
|
|
1156
|
+
} // namespace optimal_kmeans
|
|
1157
|
+
} // namespace faiss
|