forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcmul-microkernel-tester.h
261 lines (231 loc) · 12.1 KB
/
vcmul-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
// Copyright 2023 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 <random>
#include <vector>
#include <gtest/gtest.h>
#include <fp16/fp16.h>
#include "xnnpack.h"
#include "xnnpack/isa-checks.h"
#include "xnnpack/microfnptr.h"
#include "replicable_random_device.h"
class VCMulMicrokernelTester {
public:
VCMulMicrokernelTester& 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_;
}
VCMulMicrokernelTester& inplace_a(bool inplace_a) {
this->inplace_a_ = inplace_a;
return *this;
}
bool inplace_a() const {
return this->inplace_a_;
}
VCMulMicrokernelTester& inplace_b(bool inplace_b) {
this->inplace_b_ = inplace_b;
return *this;
}
bool inplace_b() const {
return this->inplace_b_;
}
VCMulMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void Test(xnn_f16_vbinary_ukernel_fn vcmul, xnn_init_f16_default_params_fn init_params = nullptr) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32rdist(1.0f, 10.0f);
std::uniform_real_distribution<float> f32idist(0.01f, 0.1f);
std::vector<xnn_float16> a(2 * batch_size() + XNN_EXTRA_BYTES / sizeof(xnn_float16));
std::vector<xnn_float16> b(2 * batch_size() + XNN_EXTRA_BYTES / sizeof(xnn_float16));
std::vector<xnn_float16> y(2 * batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(xnn_float16) : 0));
std::vector<float> y_ref(2 * batch_size());
std::fill(a.begin(), a.end(), std::nanf(""));
std::fill(b.begin(), b.end(), std::nanf(""));
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate_n(a.begin(), batch_size(), [&]() { return f32rdist(rng); });
std::generate_n(a.begin() + batch_size(), batch_size(), [&]() { return f32idist(rng); });
std::generate_n(b.begin(), batch_size(), [&]() { return f32rdist(rng); });
std::generate_n(b.begin() + batch_size(), batch_size(), [&]() { return f32idist(rng); });
if (inplace_a()) {
std::copy(a.cbegin(), a.cend(), y.begin());
} else if (inplace_b()) {
std::copy(b.cbegin(), b.cend(), y.begin());
} else {
std::fill(y.begin(), y.end(), std::nanf(""));
}
const xnn_float16* a_data = inplace_a() ? y.data() : a.data();
const xnn_float16* b_data = inplace_b() ? y.data() : b.data();
// Compute reference results.
for (size_t i = 0; i < batch_size(); i++) {
float a0 = a_data[i];
float b0 = b_data[i];
float a1 = a_data[i + batch_size()];
float b1 = b_data[i + batch_size()];
y_ref[i] = a0 * b0 - a1 * b1;
y_ref[i + batch_size()] = a0 * b1 + a1 * b0;
}
// Prepare parameters.
xnn_f16_default_params params;
if (init_params != nullptr) {
init_params(¶ms);
}
// Call optimized micro-kernel.
vcmul(batch_size() * sizeof(xnn_float16), a_data, b_data, y.data(), init_params != nullptr ? ¶ms : nullptr);
// Verify results.
for (size_t i = 0; i < batch_size(); i++) {
const float tolerance = std::abs(y_ref[i]) * 1.0e-2f;
EXPECT_NEAR(y[i], y_ref[i], tolerance)
<< "at " << i << " / " << batch_size();
}
}
}
void Test(xnn_f32_vbinary_ukernel_fn vcmul, xnn_init_f32_default_params_fn init_params = nullptr) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32rdist(1.0f, 10.0f);
std::uniform_real_distribution<float> f32idist(0.01f, 0.1f);
std::vector<float> a(2 * batch_size() + XNN_EXTRA_BYTES / sizeof(float));
std::vector<float> b(2 * batch_size() + XNN_EXTRA_BYTES / sizeof(float));
std::vector<float> y(2 * batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
std::vector<double> y_ref(2 * batch_size());
std::fill(a.begin(), a.end(), std::nanf(""));
std::fill(b.begin(), b.end(), std::nanf(""));
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate_n(a.begin(), batch_size(), [&]() { return f32rdist(rng); });
std::generate_n(a.begin() + batch_size(), batch_size(), [&]() { return f32idist(rng); });
std::generate_n(b.begin(), batch_size(), [&]() { return f32rdist(rng); });
std::generate_n(b.begin() + batch_size(), batch_size(), [&]() { return f32idist(rng); });
if (inplace_a()) {
std::copy(a.cbegin(), a.cend(), y.begin());
} else if (inplace_b()) {
std::copy(b.cbegin(), b.cend(), y.begin());
} else {
std::fill(y.begin(), y.end(), nanf(""));
}
const float* a_data = inplace_a() ? y.data() : a.data();
const float* b_data = inplace_b() ? y.data() : b.data();
// Compute reference results.
for (size_t i = 0; i < batch_size(); i++) {
y_ref[i] = double(a_data[i]) * double(b_data[i]) - double(a_data[i + batch_size()]) * double(b_data[i + batch_size()]);
y_ref[i + batch_size()] = double(a_data[i]) * double(b_data[i + batch_size()]) + double(a_data[i + batch_size()]) * double(b_data[i]);
}
// Prepare parameters.
xnn_f32_default_params params;
if (init_params != nullptr) {
init_params(¶ms);
}
// Call optimized micro-kernel.
vcmul(batch_size() * sizeof(float), a_data, b_data, y.data(), init_params != nullptr ? ¶ms : nullptr);
// Verify results.
for (size_t i = 0; i < batch_size(); i++) {
EXPECT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-4f)
<< "at " << i << " / " << batch_size();
}
}
}
private:
size_t batch_size_{1};
bool inplace_a_{false};
bool inplace_b_{false};
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>(); \
VCMulMicrokernelTester() \
.batch_size(batch_tile* batch_scale) \
.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) { \
VCMulMicrokernelTester().batch_size(batch_size).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++) { \
VCMulMicrokernelTester().batch_size(batch_size).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) { \
VCMulMicrokernelTester().batch_size(batch_size).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) { \
VCMulMicrokernelTester() \
.batch_size(batch_size) \
.inplace_a(true) \
.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) { \
VCMulMicrokernelTester() \
.batch_size(batch_size) \
.inplace_b(true) \
.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) { \
VCMulMicrokernelTester() \
.batch_size(batch_size) \
.inplace_a(true) \
.inplace_b(true) \
.Test(__VA_ARGS__); \
} \
}