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
|
// Copyright 2022 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 <gtest/gtest.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <random>
#include <vector>
#include <xnnpack.h>
#include <xnnpack/microfnptr.h>
class VLShiftMicrokernelTester {
public:
inline VLShiftMicrokernelTester& batch(size_t batch) {
assert(batch != 0);
this->batch_ = batch;
return *this;
}
inline size_t batch() const {
return this->batch_;
}
inline VLShiftMicrokernelTester& shift(uint32_t shift) {
assert(shift < 32);
this->shift_ = shift;
return *this;
}
inline uint32_t shift() const {
return this->shift_;
}
inline VLShiftMicrokernelTester& inplace(bool inplace) {
this->inplace_ = inplace;
return *this;
}
inline bool inplace() const {
return this->inplace_;
}
inline VLShiftMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
inline size_t iterations() const {
return this->iterations_;
}
void Test(xnn_s16_vlshift_ukernel_function vlshift) const {
std::random_device random_device;
auto rng = std::mt19937(random_device());
auto i16rng = std::bind(std::uniform_int_distribution<int16_t>(), std::ref(rng));
std::vector<int16_t> x(batch() + XNN_EXTRA_BYTES / sizeof(int16_t));
std::vector<int16_t> y(batch() + (inplace() ? XNN_EXTRA_BYTES / sizeof(int16_t) : 0));
std::vector<int16_t> y_ref(batch());
const int16_t* x_data = inplace() ? y.data() : x.data();
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(x.begin(), x.end(), std::ref(i16rng));
std::generate(y.begin(), y.end(), std::ref(i16rng));
std::generate(y_ref.begin(), y_ref.end(), std::ref(i16rng));
// Compute reference results.
for (size_t n = 0; n < batch(); n++) {
const uint16_t i = static_cast<uint16_t>(x_data[n]);
uint16_t value = i << shift();
y_ref[n] = reinterpret_cast<uint16_t>(value);
}
// Call optimized micro-kernel.
vlshift(batch(), x_data, shift(), y.data());
// Verify results.
for (size_t n = 0; n < batch(); n++) {
ASSERT_EQ(y[n], y_ref[n])
<< ", shift " << shift()
<< ", batch " << n << " / " << batch();
}
}
}
private:
size_t batch_{1};
uint32_t shift_{12};
bool inplace_{false};
size_t iterations_{15};
};
|