-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathbench_hamming_computer.cpp
314 lines (257 loc) · 9.27 KB
/
bench_hamming_computer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cstdio>
#include <vector>
#include <cinttypes>
#include <faiss/impl/FaissAssert.h>
#include <faiss/utils/hamming.h>
#include <faiss/utils/random.h>
#include <faiss/utils/utils.h>
using namespace faiss;
// These implementations are currently slower than HammingComputerDefault so
// they are not in the main faiss anymore.
struct HammingComputerM8 {
const uint64_t* a;
int n;
HammingComputerM8() = default;
HammingComputerM8(const uint8_t* a8, int code_size) {
set(a8, code_size);
}
void set(const uint8_t* a8, int code_size) {
assert(code_size % 8 == 0);
a = (uint64_t*)a8;
n = code_size / 8;
}
int hamming(const uint8_t* b8) const {
const uint64_t* b = (uint64_t*)b8;
int accu = 0;
for (int i = 0; i < n; i++)
accu += popcount64(a[i] ^ b[i]);
return accu;
}
inline int get_code_size() const {
return n * 8;
}
};
struct HammingComputerM4 {
const uint32_t* a;
int n;
HammingComputerM4() = default;
HammingComputerM4(const uint8_t* a4, int code_size) {
set(a4, code_size);
}
void set(const uint8_t* a4, int code_size) {
assert(code_size % 4 == 0);
a = (uint32_t*)a4;
n = code_size / 4;
}
int hamming(const uint8_t* b8) const {
const uint32_t* b = (uint32_t*)b8;
int accu = 0;
for (int i = 0; i < n; i++)
accu += popcount64(a[i] ^ b[i]);
return accu;
}
inline int get_code_size() const {
return n * 4;
}
};
template <class T>
void hamming_cpt_test(
int code_size,
uint8_t* data1,
uint8_t* data2,
int n,
int* rst) {
T computer(data1, code_size);
for (int i = 0; i < n; i++) {
rst[i] = computer.hamming(data2);
data2 += code_size;
}
}
template <int CODE_SIZE_IN_BITS>
void hamming_func_test(
const uint8_t* const x1,
const uint8_t* const x2,
const size_t n1,
const size_t n2,
uint64_t& sumv,
uint64_t& xorv) {
constexpr size_t CODE_SIZE_IN_BYTES = CODE_SIZE_IN_BITS / 8;
double t0 = faiss::getmillisecs();
uint64_t sumx = 0;
uint64_t xorx = 0;
const size_t nruns = 10;
for (size_t irun = 0; irun < 10; irun++) {
#pragma omp parallel reduction(+ : sumx, xorx)
{
#pragma omp for
for (size_t i = 0; i < n1; i++) {
uint64_t local_sum = 0;
uint64_t local_xor = 0;
const uint64_t* data1_ptr =
(const uint64_t*)(x1 + i * CODE_SIZE_IN_BYTES);
for (size_t j = 0; j < n2; j++) {
const uint64_t* data2_ptr =
(const uint64_t*)(x2 + j * CODE_SIZE_IN_BYTES);
uint64_t code = faiss::hamming<CODE_SIZE_IN_BITS>(
data1_ptr, data2_ptr);
local_sum += code;
local_xor ^= code;
}
sumx += local_sum;
xorx ^= local_xor;
}
}
}
sumv = sumx;
xorv = xorx;
double t1 = faiss::getmillisecs();
printf("hamming<%d>: %.3f msec, %" PRIX64 ", %" PRIX64 "\n",
CODE_SIZE_IN_BITS,
(t1 - t0) / nruns,
sumx,
xorx);
}
template <typename HammingComputerT, int CODE_SIZE_IN_BITS>
void hamming_computer_test(
const uint8_t* const x1,
const uint8_t* const x2,
const size_t n1,
const size_t n2,
uint64_t& sumv,
uint64_t& xorv) {
constexpr size_t CODE_SIZE_IN_BYTES = CODE_SIZE_IN_BITS / 8;
double t0 = faiss::getmillisecs();
uint64_t sumx = 0;
uint64_t xorx = 0;
const size_t nruns = 10;
for (size_t irun = 0; irun < nruns; irun++) {
sumx = 0;
xorx = 0;
#pragma omp parallel reduction(+ : sumx, xorx)
{
#pragma omp for
for (size_t i = 0; i < n1; i++) {
uint64_t local_sum = 0;
uint64_t local_xor = 0;
const uint8_t* data1_ptr = x1 + i * CODE_SIZE_IN_BYTES;
HammingComputerT hc(data1_ptr, CODE_SIZE_IN_BYTES);
for (size_t j = 0; j < n2; j++) {
const uint8_t* data2_ptr = x2 + j * CODE_SIZE_IN_BYTES;
uint64_t code = hc.hamming(data2_ptr);
local_sum += code;
local_xor ^= code;
}
sumx += local_sum;
xorx ^= local_xor;
}
}
}
sumv = sumx;
xorv = xorx;
double t1 = faiss::getmillisecs();
printf("HammingComputer<%zd>: %.3f msec, %" PRIX64 ", %" PRIX64 "\n",
CODE_SIZE_IN_BYTES,
(t1 - t0) / nruns,
sumx,
xorx);
}
int main() {
size_t n = 4 * 1000 * 1000;
std::vector<size_t> code_size = {128, 256, 512, 1000};
std::vector<uint8_t> x(n * code_size.back());
byte_rand(x.data(), n, 12345);
int nrun = 100;
for (size_t cs : code_size) {
printf("benchmark with code_size=%zd n=%zd nrun=%d\n", cs, n, nrun);
double tot_t1 = 0, tot_t2 = 0, tot_t3 = 0;
#pragma omp parallel reduction(+ : tot_t1, tot_t2, tot_t3)
{
std::vector<int> rst_m4(n);
std::vector<int> rst_m8(n);
std::vector<int> rst_default(n);
#pragma omp for
for (int run = 0; run < nrun; run++) {
double t0, t1, t2, t3;
t0 = getmillisecs();
// new implem from Zilliz
hamming_cpt_test<HammingComputerDefault>(
cs, x.data(), x.data(), n, rst_default.data());
t1 = getmillisecs();
// M8
hamming_cpt_test<HammingComputerM8>(
cs, x.data(), x.data(), n, rst_m8.data());
t2 = getmillisecs();
// M4
hamming_cpt_test<HammingComputerM4>(
cs, x.data(), x.data(), n, rst_m4.data());
t3 = getmillisecs();
tot_t1 += t1 - t0;
tot_t2 += t2 - t1;
tot_t3 += t3 - t2;
}
for (int i = 0; i < n; i++) {
FAISS_THROW_IF_NOT_FMT(
(rst_m4[i] == rst_m8[i] && rst_m4[i] == rst_default[i]),
"wrong result i=%d, m4 %d m8 %d default %d",
i,
rst_m4[i],
rst_m8[i],
rst_default[i]);
}
}
printf("Hamming_Dft implem: %.3f ms\n", tot_t1 / nrun);
printf("Hamming_M8 implem: %.3f ms\n", tot_t2 / nrun);
printf("Hamming_M4 implem: %.3f ms\n", tot_t3 / nrun);
}
// evaluate various hamming<>() function calls
const size_t MAX_HAMMING_FUNC_CODE_SIZE = 512;
const size_t n1 = 65536;
const size_t n2 = 16384;
std::vector<uint8_t> x1(n1 * MAX_HAMMING_FUNC_CODE_SIZE / 8);
std::vector<uint8_t> x2(n2 * MAX_HAMMING_FUNC_CODE_SIZE / 8);
byte_rand(x1.data(), x1.size(), 12345);
byte_rand(x2.data(), x2.size(), 23456);
// These two values serve as a kind of CRC.
uint64_t sumx = 0;
uint64_t xorx = 0;
hamming_func_test<64>(x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_func_test<128>(x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_func_test<256>(x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_func_test<384>(x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_func_test<512>(x1.data(), x2.data(), n1, n2, sumx, xorx);
// evaluate various HammingComputerXX
hamming_computer_test<faiss::HammingComputer4, 32>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::HammingComputer8, 64>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::HammingComputer16, 128>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::HammingComputer20, 160>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::HammingComputer32, 256>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::HammingComputer64, 512>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
// evaluate various GenHammingDistanceComputerXX
hamming_computer_test<faiss::GenHammingComputer8, 64>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputer16, 128>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputer32, 256>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputerM8, 64>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputerM8, 128>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputerM8, 256>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
hamming_computer_test<faiss::GenHammingComputerM8, 512>(
x1.data(), x2.data(), n1, n2, sumx, xorx);
return 0;
}