forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreduce-microkernel-tester.h
177 lines (151 loc) · 5.4 KB
/
reduce-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
// 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 <limits>
#include <random>
#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#include "xnnpack.h"
#include "xnnpack/math.h"
#include "xnnpack/microfnptr.h"
#include "xnnpack/microparams.h"
#include "replicable_random_device.h"
class ReduceMicrokernelTester {
public:
enum class OpType {
Max,
Min,
MinMax,
};
ReduceMicrokernelTester& 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_;
}
ReduceMicrokernelTester& iterations(size_t iterations) {
this->iterations_ = iterations;
return *this;
}
size_t iterations() const {
return this->iterations_;
}
void Test(xnn_f16_reduce_ukernel_fn reduce, OpType op_type, xnn_init_f16_default_params_fn init_params = nullptr) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist(-1.0f, 1.0f);
std::vector<xnn_float16> input(batch_size() + XNN_EXTRA_BYTES / sizeof(xnn_float16));
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
// Compute reference results.
std::vector<xnn_float16>::iterator min, max;
std::tie(min, max) = std::minmax_element(input.begin(), input.begin() + batch_size());
// Prepare parameters.
xnn_f16_default_params params;
if (init_params != nullptr) {
init_params(¶ms);
}
// Call optimized micro-kernel.
xnn_float16 output[2] = {std::nanf(""), std::nanf("")};
reduce(batch_size() * sizeof(xnn_float16), input.data(), output, init_params != nullptr ? ¶ms : nullptr);
// Verify results.
switch (op_type) {
case OpType::Max:
EXPECT_EQ(output[0], *max)
<< "with batch " << batch_size();
break;
case OpType::Min:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
break;
case OpType::MinMax:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
EXPECT_EQ(output[1], *max)
<< "with batch " << batch_size();
break;
}
}
}
void Test(xnn_f32_reduce_ukernel_fn reduce, OpType op_type, xnn_init_f32_default_params_fn init_params = nullptr) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_real_distribution<float> f32dist(-1.0f, 1.0f);
std::vector<float> input(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); });
// Compute reference results.
std::vector<float>::iterator min, max;
std::tie(min, max) = std::minmax_element(input.begin(), input.begin() + batch_size());
// Prepare parameters.
xnn_f32_default_params params;
if (init_params != nullptr) {
init_params(¶ms);
}
// Call optimized micro-kernel.
float output[2] = {std::nanf(""), std::nanf("")};
reduce(batch_size() * sizeof(float), input.data(), output, init_params != nullptr ? ¶ms : nullptr);
// Verify results.
switch (op_type) {
case OpType::Max:
EXPECT_EQ(output[0], *max)
<< "with batch " << batch_size();
break;
case OpType::Min:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
break;
case OpType::MinMax:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
EXPECT_EQ(output[1], *max)
<< "with batch " << batch_size();
break;
}
}
}
void Test(xnn_u8_reduce_ukernel_fn reduce, OpType op_type) const {
xnnpack::ReplicableRandomDevice rng;
std::uniform_int_distribution<int32_t> u8dist(
std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
std::vector<uint8_t> input(batch_size() + XNN_EXTRA_BYTES / sizeof(uint8_t));
for (size_t iteration = 0; iteration < iterations(); iteration++) {
std::generate(input.begin(), input.end(), [&]() { return u8dist(rng); });
// Compute reference results.
std::vector<uint8_t>::iterator min, max;
std::tie(min, max) = std::minmax_element(input.begin(), input.begin() + batch_size());
// Call optimized micro-kernel.
uint8_t output[2] = {0xAA, 0xAA};
reduce(batch_size() * sizeof(uint8_t), input.data(), output, nullptr);
// Verify results.
switch (op_type) {
case OpType::Max:
EXPECT_EQ(output[0], *max)
<< "with batch " << batch_size();
break;
case OpType::Min:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
break;
case OpType::MinMax:
EXPECT_EQ(output[0], *min)
<< "with batch " << batch_size();
EXPECT_EQ(output[1], *max)
<< "with batch " << batch_size();
break;
}
}
}
private:
size_t batch_size_{1};
size_t iterations_{15};
};