forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvbinary-microkernel-tester.h
472 lines (417 loc) · 22.8 KB
/
vbinary-microkernel-tester.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <type_traits>
#include <gtest/gtest.h>
#include "xnnpack/isa-checks.h"
#include "xnnpack/math.h"
#include "xnnpack/microfnptr.h"
class VBinaryMicrokernelTester {
public:
enum class OpType {
Add,
CopySign,
RCopySign,
Div,
RDiv,
Max,
Min,
Mul,
Sub,
RSub,
SqrDiff,
Prelu,
RPrelu,
};
template <typename A, typename B, typename Result>
void reference_op_impl(const A* a, const B* b, Result* result, size_t n, OpType op_type) const {
size_t stride_b = broadcast_b() ? 0 : 1;
for (size_t i = 0; i < n; ++i) {
switch (op_type) {
case OpType::Add:
result[i] = a[i] + b[i * stride_b];
break;
case OpType::CopySign:
result[i] = std::copysign(a[i], b[i * stride_b]);
break;
case OpType::RCopySign:
result[i] = std::copysign(b[i * stride_b], a[i]);
break;
case OpType::Div:
result[i] = a[i] / b[i * stride_b];
break;
case OpType::RDiv:
result[i] = b[i * stride_b] / a[i];
break;
case OpType::Max:
result[i] = std::max(a[i], b[i * stride_b]);
break;
case OpType::Min:
result[i] = std::min(a[i], b[i * stride_b]);
break;
case OpType::Mul:
if (std::is_integral<A>::value && std::is_integral<B>::value) {
// Overflow is the expected behavior.
int64_t result_wide = static_cast<int64_t>(a[i]) * static_cast<int64_t>(b[i * stride_b]);
result[i] = result_wide & ((static_cast<int64_t>(1) << (sizeof(Result) * 8)) - 1);
} else {
result[i] = a[i] * b[i * stride_b];
}
break;
case OpType::Prelu:
result[i] = a[i] < 0 ? static_cast<Result>(a[i] * b[i * stride_b]) : static_cast<Result>(a[i]);
break;
case OpType::RPrelu:
result[i] = b[i * stride_b] < 0 ? static_cast<Result>(a[i] * b[i * stride_b]) : static_cast<Result>(b[i * stride_b]);
break;
case OpType::SqrDiff: {
const double diff = static_cast<double>(a[i]) - static_cast<double>(b[i * stride_b]);
result[i] = diff * diff;
break;
}
case OpType::Sub:
result[i] = a[i] - b[i * stride_b];
break;
case OpType::RSub:
result[i] = b[i * stride_b] - a[i];
break;
}
}
}
VBinaryMicrokernelTester& batch_size(size_t batch_size) {
assert(batch_size != 0);
this->batch_size_ = batch_size;
return *this;
}
size_t batch_size() const { return this->batch_size_; }
VBinaryMicrokernelTester& inplace_a(bool inplace_a) {
this->inplace_a_ = inplace_a;
return *this;
}
bool inplace_a() const { return this->inplace_a_; }
VBinaryMicrokernelTester& inplace_b(bool inplace_b) {
this->inplace_b_ = inplace_b;
return *this;
}
bool inplace_b() const { return this->inplace_b_; }
VBinaryMicrokernelTester& broadcast_b(bool broadcast_b) {
this->broadcast_b_ = broadcast_b;
return *this;
}
bool broadcast_b() const { return this->broadcast_b_; }
VBinaryMicrokernelTester& a_scale(float a_scale) {
assert(a_scale > 0.0f);
assert(std::isnormal(a_scale));
this->a_scale_ = a_scale;
return *this;
}
float a_scale() const { return this->a_scale_; }
VBinaryMicrokernelTester& a_zero_point(uint8_t a_zero_point) {
this->a_zero_point_ = a_zero_point;
return *this;
}
uint8_t a_zero_point() const { return this->a_zero_point_; }
VBinaryMicrokernelTester& b_scale(float b_scale) {
assert(b_scale > 0.0f);
assert(std::isnormal(b_scale));
this->b_scale_ = b_scale;
return *this;
}
float b_scale() const { return this->b_scale_; }
VBinaryMicrokernelTester& b_zero_point(uint8_t b_zero_point) {
this->b_zero_point_ = b_zero_point;
return *this;
}
uint8_t b_zero_point() const { return this->b_zero_point_; }
VBinaryMicrokernelTester& y_scale(float y_scale) {
assert(y_scale > 0.0f);
assert(std::isnormal(y_scale));
this->y_scale_ = y_scale;
return *this;
}
float y_scale() const { return this->y_scale_; }
VBinaryMicrokernelTester& y_zero_point(uint8_t y_zero_point) {
this->y_zero_point_ = y_zero_point;
return *this;
}
uint8_t y_zero_point() const { return this->y_zero_point_; }
VBinaryMicrokernelTester& qmin(uint8_t qmin) {
this->qmin_ = qmin;
return *this;
}
uint8_t qmin() const { return this->qmin_; }
VBinaryMicrokernelTester& qmax(uint8_t qmax) {
this->qmax_ = qmax;
return *this;
}
uint8_t qmax() const { return this->qmax_; }
VBinaryMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const { return this->iterations_; }
void Test(xnn_f16_vbinary_ukernel_fn vbinary, OpType op_type,
xnn_init_f16_default_params_fn init_params = nullptr) const;
void Test(xnn_f16_vbinary_minmax_ukernel_fn vbinary_minmax, OpType op_type,
xnn_init_f16_minmax_params_fn init_params) const;
void Test(xnn_f32_vbinary_ukernel_fn vbinary, OpType op_type,
xnn_init_f32_default_params_fn init_params = nullptr) const;
void Test(xnn_f32_vbinary_minmax_ukernel_fn vbinary_minmax, OpType op_type,
xnn_init_f32_minmax_params_fn init_params) const;
void Test(xnn_s32_vbinary_ukernel_fn vbinary, OpType op_type,
xnn_init_s32_default_params_fn init_params = nullptr) const;
void Test(xnn_qu8_vadd_minmax_ukernel_fn vadd_minmax,
xnn_init_qu8_add_minmax_params_fn init_params) const;
void Test(xnn_qu8_vmul_minmax_ukernel_fn vmul_minmax,
xnn_init_qu8_mul_minmax_params_fn init_params) const;
void Test(xnn_qs8_vadd_minmax_ukernel_fn vadd_minmax,
xnn_init_qs8_add_minmax_params_fn init_params) const;
void Test(xnn_qs8_vmul_minmax_ukernel_fn vmul_minmax,
xnn_init_qs8_mul_minmax_params_fn init_params) const;
private:
size_t batch_size_{1};
bool inplace_a_{false};
bool inplace_b_{false};
bool broadcast_b_{false};
float a_scale_{0.75f};
float b_scale_{1.25f};
float y_scale_{0.96875f};
uint8_t a_zero_point_{121};
uint8_t b_zero_point_{127};
uint8_t y_zero_point_{133};
uint8_t qmin_{0};
uint8_t qmax_{255};
size_t iterations_{15};
};
#define XNN_TEST_BINARY_BATCH_EQ(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, batch_eq) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
VBinaryMicrokernelTester() \
.batch_size(batch_tile* batch_scale) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
}
#define XNN_TEST_BINARY_BATCH_DIV(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, batch_div) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
if (batch_tile == 1 && batch_scale == 1) return; \
for (size_t batch_size = batch_tile * batch_scale * 2; \
batch_size < batch_tile * batch_scale * 10; \
batch_size += batch_tile * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_BATCH_LT(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, batch_lt) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
if (batch_tile == 1 && batch_scale == 1) return; \
for (size_t batch_size = batch_scale; \
batch_size < batch_tile * batch_scale; batch_size++) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_BATCH_GT(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, batch_gt) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
const size_t batch_end = batch_tile == 1 ? 10 : batch_tile * 2; \
const size_t batch_step = batch_scale == 1 ? 1 : batch_tile * 2; \
for (size_t batch_size = batch_tile + 1; batch_size < batch_end; \
batch_size += batch_step) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_INPLACE_A(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, inplace_a) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.inplace_a(true) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_INPLACE_B(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, inplace_b) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.inplace_b(true) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_INPLACE_A_AND_B(ukernel, arch_flags, batch_tile, \
is_binaryc, datatype, ...) \
TEST(ukernel, inplace_a_and_b) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.inplace_a(true) \
.inplace_b(true) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_A_ZERO_POINT(ukernel, arch_flags, batch_tile, \
is_binaryc, datatype, ...) \
TEST(ukernel, a_zero_point) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (int32_t a_zero_point = -128; a_zero_point <= 127; \
a_zero_point += 51) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.a_zero_point(a_zero_point) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_B_ZERO_POINT(ukernel, arch_flags, batch_tile, \
is_binaryc, datatype, ...) \
TEST(ukernel, b_zero_point) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (int32_t b_zero_point = -128; b_zero_point <= 127; \
b_zero_point += 51) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.b_zero_point(b_zero_point) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_Y_ZERO_POINT(ukernel, arch_flags, batch_tile, \
is_binaryc, datatype, ...) \
TEST(ukernel, y_zero_point) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (int32_t y_zero_point = -128; y_zero_point <= 127; \
y_zero_point += 51) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.y_zero_point(y_zero_point) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_A_SCALE(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, a_scale) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (float a_scale = 0.1f; a_scale <= 10.0f; a_scale *= 3.14f) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.a_scale(a_scale) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_B_SCALE(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, b_scale) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (float b_scale = 0.1f; b_scale <= 10.0f; b_scale *= 3.14f) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.b_scale(b_scale) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_Y_SCALE(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, y_scale) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
for (float y_scale = 0.1f; y_scale <= 10.0f; y_scale *= 3.14f) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.y_scale(y_scale) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
} \
}
#define XNN_TEST_BINARY_QMIN(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, qmin) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.qmin(128) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}
#define XNN_TEST_BINARY_QMAX(ukernel, arch_flags, batch_tile, is_binaryc, \
datatype, ...) \
TEST(ukernel, qmax) { \
TEST_REQUIRES_ARCH_FLAGS(arch_flags); \
const size_t batch_scale = get_batch_scale<datatype>(); \
for (size_t batch_size = 1; batch_size <= batch_tile * batch_scale * 5; \
batch_size += std::max(1, batch_tile - 1) * batch_scale) { \
VBinaryMicrokernelTester() \
.batch_size(batch_size) \
.qmax(128) \
.broadcast_b(is_binaryc) \
.Test(__VA_ARGS__); \
} \
}