forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathibilinear-microkernel-tester.h
432 lines (374 loc) · 17.6 KB
/
ibilinear-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
// 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 <limits>
#include <random>
#include <vector>
#include <gtest/gtest.h>
#include <fp16/fp16.h>
#include "xnnpack.h"
#include "xnnpack/aligned-allocator.h"
#include "xnnpack/math.h"
#include "xnnpack/microfnptr.h"
#include "replicable_random_device.h"
class IBilinearMicrokernelTester {
public:
IBilinearMicrokernelTester& pixels(uint32_t pixels) {
assert(pixels >= 1);
this->pixels_ = pixels;
return *this;
}
uint32_t pixels() const {
return this->pixels_;
}
IBilinearMicrokernelTester& channels(uint32_t channels) {
assert(channels >= 1);
this->channels_ = channels;
return *this;
}
uint32_t channels() const {
return this->channels_;
}
IBilinearMicrokernelTester& input_offset(uint32_t input_offset) {
this->input_offset_ = input_offset;
return *this;
}
uint32_t input_offset() const {
return this->input_offset_;
}
IBilinearMicrokernelTester& output_stride(uint32_t output_stride) {
assert(output_stride != 0);
this->output_stride_ = output_stride;
return *this;
}
uint32_t output_stride() const {
if (this->output_stride_ == 0) {
return channels();
} else {
assert(this->output_stride_ >= channels());
return this->output_stride_;
}
}
IBilinearMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
IBilinearMicrokernelTester& input_stride(uint32_t input_stride) {
assert(input_stride != 0);
this->input_stride_ = input_stride;
return *this;
}
uint32_t input_stride() const {
if (this->input_stride_ == 0) {
return 4 * pixels();
} else {
assert(this->input_stride_ >= 4 * pixels());
return this->input_stride_;
}
}
void Test(xnn_f16_ibilinear_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist(0.1f, 1.0f);
std::vector<const xnn_float16*> indirection(pixels() * 4);
std::vector<xnn_float16> input(XNN_EXTRA_BYTES / sizeof(xnn_float16) + indirection.size() * channels());
std::vector<xnn_float16, AlignedAllocator<xnn_float16, 64>> packed_weights(pixels() * 2);
std::vector<xnn_float16> output((pixels() - 1) * output_stride() + channels());
std::vector<float> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return f32dist(rng); });
std::fill(output.begin(), output.end(), std::nanf(""));
for (size_t i = 0; i < indirection.size(); i++) {
indirection[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const float alpha_h = packed_weights[i * 2 + 0];
const float alpha_v = packed_weights[i * 2 + 1];
output_ref[i * channels() + c] =
indirection[i * 4 + 0][c + input_offset()] * (1.0f - alpha_h) * (1.0f - alpha_v) +
indirection[i * 4 + 1][c + input_offset()] * alpha_h * (1.0f - alpha_v) +
indirection[i * 4 + 2][c + input_offset()] * (1.0f - alpha_h) * alpha_v +
indirection[i * 4 + 3][c + input_offset()] * alpha_h * alpha_v;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels() * sizeof(xnn_float16),
reinterpret_cast<const xnn_float16**>(indirection.data()), input_offset() * sizeof(xnn_float16),
packed_weights.data(), output.data(),
(output_stride() - channels()) * sizeof(xnn_float16));
// Verify results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
ASSERT_NEAR(
output[i * output_stride() + c],
output_ref[i * channels() + c],
std::abs(output_ref[i * channels() + c]) * 1.0e-2f)
<< "pixel " << i << " / " << pixels() << ", channel " << c << " / " << channels();
}
}
}
}
void Test(xnn_f32_ibilinear_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist;
std::vector<const float*> indirection(pixels() * 4);
std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + indirection.size() * channels());
std::vector<float, AlignedAllocator<float, 64>> packed_weights(pixels() * 2);
std::vector<float> output((pixels() - 1) * output_stride() + channels());
std::vector<float> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return f32dist(rng); });
std::fill(output.begin(), output.end(), nanf(""));
for (size_t i = 0; i < indirection.size(); i++) {
indirection[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const float alpha_h = packed_weights[i * 2 + 0];
const float alpha_v = packed_weights[i * 2 + 1];
output_ref[i * channels() + c] =
indirection[i * 4 + 0][c + input_offset()] * (1.0f - alpha_h) * (1.0f - alpha_v) +
indirection[i * 4 + 1][c + input_offset()] * alpha_h * (1.0f - alpha_v) +
indirection[i * 4 + 2][c + input_offset()] * (1.0f - alpha_h) * alpha_v +
indirection[i * 4 + 3][c + input_offset()] * alpha_h * alpha_v;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels() * sizeof(float),
indirection.data(), input_offset() * sizeof(float),
packed_weights.data(), output.data(),
(output_stride() - channels()) * sizeof(float));
// Verify results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
EXPECT_NEAR(
output_ref[i * channels() + c],
output[i * output_stride() + c],
std::abs(output_ref[i * channels() + c]) * 1.0e-4)
<< "pixel " << i << " / " << pixels() << ", channel " << c << " / " << channels();
}
}
}
}
void Test(xnn_s8_ibilinear_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> i8dist(
std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max());
std::uniform_int_distribution<int16_t> w11dist(0, 2047);
std::vector<const int8_t*> indirection(pixels() * 4);
std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) + indirection.size() * channels());
std::vector<int16_t, AlignedAllocator<int16_t, 64>> packed_weights(pixels() * 2);
std::vector<int8_t> output((pixels() - 1) * output_stride() + channels());
std::vector<int8_t> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return i8dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return w11dist(rng); });
std::fill(output.begin(), output.end(), INT8_C(0xFA));
for (size_t i = 0; i < indirection.size(); i++) {
indirection[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const int32_t alpha_h = packed_weights[i * 2 + 0];
const int32_t alpha_v = packed_weights[i * 2 + 1];
const int32_t acc = math_asr_s32(
int32_t(indirection[i * 4 + 0][c + input_offset()]) * (2048 - alpha_h) * (2048 - alpha_v) +
int32_t(indirection[i * 4 + 1][c + input_offset()]) * alpha_h * (2048 - alpha_v) +
int32_t(indirection[i * 4 + 2][c + input_offset()]) * (2048 - alpha_h) * alpha_v +
int32_t(indirection[i * 4 + 3][c + input_offset()]) * alpha_h * alpha_v +
2097152, 22);
ASSERT_GE(acc, std::numeric_limits<int8_t>::min());
ASSERT_LE(acc, std::numeric_limits<int8_t>::max());
output_ref[i * channels() + c] = (int8_t) acc;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels() * sizeof(int8_t),
indirection.data(), input_offset() * sizeof(int8_t),
packed_weights.data(), output.data(),
(output_stride() - channels()) * sizeof(int8_t));
// Verify results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
EXPECT_EQ(int32_t(output_ref[i * channels() + c]), int32_t(output[i * output_stride() + c]))
<< "pixel " << i << " / " << pixels() << ", channel " << c << " / " << channels();
}
}
}
}
void Test(xnn_u8_ibilinear_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> u8dist(
std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
std::uniform_int_distribution<int16_t> w11dist(0, 2047);
std::vector<const uint8_t*> indirection(pixels() * 4);
std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) + indirection.size() * channels());
std::vector<int16_t, AlignedAllocator<int16_t, 64>> packed_weights(pixels() * 2);
std::vector<uint8_t> output((pixels() - 1) * output_stride() + channels());
std::vector<uint8_t> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return u8dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return w11dist(rng); });
std::fill(output.begin(), output.end(), UINT8_C(0xFA));
for (size_t i = 0; i < indirection.size(); i++) {
indirection[i] = input.data() + i * channels() - input_offset();
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const uint32_t alpha_h = uint32_t(int32_t(packed_weights[i * 2 + 0]));
const uint32_t alpha_v = uint32_t(int32_t(packed_weights[i * 2 + 1]));
const uint32_t acc = (2097152 +
int32_t(indirection[i * 4 + 0][c + input_offset()]) * (2048 - alpha_h) * (2048 - alpha_v) +
int32_t(indirection[i * 4 + 1][c + input_offset()]) * alpha_h * (2048 - alpha_v) +
int32_t(indirection[i * 4 + 2][c + input_offset()]) * (2048 - alpha_h) * alpha_v +
int32_t(indirection[i * 4 + 3][c + input_offset()]) * alpha_h * alpha_v) >> 22;
ASSERT_LE(acc, std::numeric_limits<uint8_t>::max());
output_ref[i * channels() + c] = (uint8_t) acc;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels() * sizeof(uint8_t),
indirection.data(), input_offset() * sizeof(uint8_t),
packed_weights.data(), output.data(),
(output_stride() - channels()) * sizeof(uint8_t));
// Verify results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
EXPECT_EQ(uint32_t(output_ref[i * channels() + c]), uint32_t(output[i * output_stride() + c]))
<< "pixel " << i << " / " << pixels() << ", channel " << c << " / " << channels();
}
}
}
}
void TestCHW(xnn_f16_ibilinear_chw_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist(0.1f, 1.0f);
std::vector<const xnn_float16*> indirection(pixels() * 2);
std::vector<xnn_float16> input(XNN_EXTRA_BYTES / sizeof(xnn_float16) + (channels() - 1) * input_stride() + 4 * pixels());
std::vector<xnn_float16, AlignedAllocator<xnn_float16, 64>> packed_weights(pixels() * 2);
std::vector<xnn_float16> output(pixels() * channels());
std::vector<float> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return f32dist(rng); });
std::fill(output.begin(), output.end(), std::nanf(""));
// Indirection will point to the even ("left") pixels of the input.
// The kernels will expect "right" pixels to be placed right next to them.
for (size_t i = 0; i < indirection.size(); i++) {
const xnn_float16* left_corner = input.data() + 2 * i - input_offset();
indirection[i] = left_corner;
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const float alpha_h = packed_weights[i * 2 + 0];
const float alpha_v = packed_weights[i * 2 + 1];
// `c * pixels() + i` because the output is NCHW.
output_ref[c * pixels() + i] =
// `c * indirection.size()` because the input is NCHW.
(indirection[i * 2 + 0] + 0)[c * input_stride() + input_offset()] * (1.0f - alpha_h) * (1.0f - alpha_v) +
(indirection[i * 2 + 0] + 1)[c * input_stride() + input_offset()] * alpha_h * (1.0f - alpha_v) +
(indirection[i * 2 + 1] + 0)[c * input_stride() + input_offset()] * (1.0f - alpha_h) * alpha_v +
(indirection[i * 2 + 1] + 1)[c * input_stride() + input_offset()] * alpha_h * alpha_v;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels(),
reinterpret_cast<const xnn_float16**>(indirection.data()), input_offset() * sizeof(xnn_float16),
packed_weights.data(), output.data(), input_stride() * sizeof(xnn_float16));
// Verify results.
for (size_t c = 0; c < channels(); c++) {
for (size_t i = 0; i < pixels(); i++) {
ASSERT_NEAR(
output[c * pixels() + i],
output_ref[c * pixels() + i],
std::abs(output_ref[c * pixels() + i]) * 1.0e-2f)
<< "i = " << i << ", channel = " << c;
}
}
}
}
void TestCHW(xnn_f32_ibilinear_chw_ukernel_fn ibilinear) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist;
std::vector<const float*> indirection(pixels() * 2);
std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + (channels() - 1) * input_stride() + 4 * pixels());
std::vector<float, AlignedAllocator<float, 64>> packed_weights(pixels() * 2);
std::vector<float> output(pixels() * channels());
std::vector<float> output_ref(pixels() * channels());
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
std::generate(packed_weights.begin(), packed_weights.end(), [&]() { return f32dist(rng); });
std::fill(output.begin(), output.end(), nanf(""));
// Indirection will point to the even ("left") pixels of the input.
// The kernels will expect "right" pixels to be placed right next to them.
for (size_t i = 0; i < indirection.size(); i++) {
const float* left_corner = input.data() + 2 * i - input_offset();
indirection[i] = left_corner;
}
std::shuffle(indirection.begin(), indirection.end(), rng);
// Compute reference results.
for (size_t i = 0; i < pixels(); i++) {
for (size_t c = 0; c < channels(); c++) {
const float alpha_h = packed_weights[i * 2 + 0];
const float alpha_v = packed_weights[i * 2 + 1];
// `c * pixels() + i` because the output is NCHW.
output_ref[c * pixels() + i] =
// `c * indirection.size()` because the input is NCHW.
(indirection[i * 2 + 0] + 0)[c * input_stride() + input_offset()] * (1.0f - alpha_h) * (1.0f - alpha_v) +
(indirection[i * 2 + 0] + 1)[c * input_stride() + input_offset()] * alpha_h * (1.0f - alpha_v) +
(indirection[i * 2 + 1] + 0)[c * input_stride() + input_offset()] * (1.0f - alpha_h) * alpha_v +
(indirection[i * 2 + 1] + 1)[c * input_stride() + input_offset()] * alpha_h * alpha_v;
}
}
// Call optimized micro-kernel.
ibilinear(
pixels(), channels(),
indirection.data(), input_offset() * sizeof(float),
packed_weights.data(), output.data(), input_stride() * sizeof(float));
// Verify results.
for (size_t c = 0; c < channels(); c++) {
for (size_t i = 0; i < pixels(); i++) {
EXPECT_NEAR(
output_ref[c * pixels() + i],
output[c * pixels() + i],
std::abs(output_ref[c * pixels() + i]) * 1.0e-4)
<< "i = " << i << ", channel = " << c;
}
}
}
}
private:
uint32_t channels_{1};
uint32_t pixels_{1};
uint32_t output_stride_{0};
uint32_t input_stride_{0};
uint32_t input_offset_{0};
size_t iterations_{3};
};