blob: 823571cd4b0917f05fac60c54b7b09eb3e9fc056 [file] [log] [blame]
Marat Dukhanc07cb7f2019-11-14 15:32:05 -08001// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6#pragma once
7
8#include <gtest/gtest.h>
9
10#include <algorithm>
11#include <cassert>
12#include <cstddef>
13#include <cstdlib>
14#include <functional>
15#include <random>
16#include <vector>
17
18#include <xnnpack.h>
19#include <xnnpack/params-init.h>
20#include <xnnpack/params.h>
21
22
23class VBinOpCMicrokernelTester {
24 public:
25 enum class OpType {
26 AddC,
Marat Dukhan77ca6302019-12-06 12:48:15 -080027 DivC,
28 RDivC,
Marat Dukhan403b7d42019-12-05 12:49:11 -080029 MaxC,
30 MinC,
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080031 MulC,
32 SubC,
33 RSubC,
34 };
35
36 enum class Variant {
37 Native,
38 Scalar,
39 };
40
41 inline VBinOpCMicrokernelTester& batch_size(size_t batch_size) {
42 assert(batch_size != 0);
43 this->batch_size_ = batch_size;
44 return *this;
45 }
46
47 inline size_t batch_size() const {
48 return this->batch_size_;
49 }
50
51 inline VBinOpCMicrokernelTester& inplace(bool inplace) {
52 this->inplace_ = inplace;
53 return *this;
54 }
55
56 inline bool inplace() const {
57 return this->inplace_;
58 }
59
60 inline VBinOpCMicrokernelTester& qmin(uint8_t qmin) {
61 this->qmin_ = qmin;
62 return *this;
63 }
64
65 inline uint8_t qmin() const {
66 return this->qmin_;
67 }
68
69 inline VBinOpCMicrokernelTester& qmax(uint8_t qmax) {
70 this->qmax_ = qmax;
71 return *this;
72 }
73
74 inline uint8_t qmax() const {
75 return this->qmax_;
76 }
77
78 inline VBinOpCMicrokernelTester& iterations(size_t iterations) {
79 this->iterations_ = iterations;
80 return *this;
81 }
82
83 inline size_t iterations() const {
84 return this->iterations_;
85 }
86
Frank Barchardd793f6c2020-05-08 13:37:43 -070087 void Test(xnn_f16_vbinary_ukernel_function vbinaryc, OpType op_type, Variant variant = Variant::Native) const {
88 std::random_device random_device;
89 auto rng = std::mt19937(random_device());
90 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.01f, 1.0f), rng);
91 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
92
93 std::vector<uint16_t> a(batch_size() + XNN_EXTRA_BYTES / sizeof(uint16_t));
94 const uint16_t b = f16rng();
95 std::vector<uint16_t> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0));
96 std::vector<float> y_ref(batch_size());
97 for (size_t iteration = 0; iteration < iterations(); iteration++) {
98 std::generate(a.begin(), a.end(), std::ref(f16rng));
99 if (inplace()) {
100 std::generate(y.begin(), y.end(), std::ref(f16rng));
101 } else {
102 std::fill(y.begin(), y.end(), nanf(""));
103 }
104 const uint16_t* a_data = inplace() ? y.data() : a.data();
105
106 // Compute reference results.
107 for (size_t i = 0; i < batch_size(); i++) {
108 switch (op_type) {
109 case OpType::AddC:
110 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) + fp16_ieee_to_fp32_value(b);
111 break;
112 case OpType::DivC:
113 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) / fp16_ieee_to_fp32_value(b);
114 break;
115 case OpType::RDivC:
116 y_ref[i] = fp16_ieee_to_fp32_value(b) / fp16_ieee_to_fp32_value(a_data[i]);
117 break;
118 case OpType::MaxC:
119 y_ref[i] = std::max<float>(fp16_ieee_to_fp32_value(a_data[i]), fp16_ieee_to_fp32_value(b));
120 break;
121 case OpType::MinC:
122 y_ref[i] = std::min<float>(fp16_ieee_to_fp32_value(a_data[i]), fp16_ieee_to_fp32_value(b));
123 break;
124 case OpType::MulC:
125 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) * fp16_ieee_to_fp32_value(b);
126 break;
127 case OpType::SubC:
128 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) - fp16_ieee_to_fp32_value(b);
129 break;
130 case OpType::RSubC:
131 y_ref[i] = fp16_ieee_to_fp32_value(b) - fp16_ieee_to_fp32_value(a_data[i]);
132 break;
133 }
134 }
135 // Call optimized micro-kernel.
136 vbinaryc(batch_size() * sizeof(uint16_t), a_data, &b, y.data(), nullptr);
137
138 // Verify results.
139 for (size_t i = 0; i < batch_size(); i++) {
140 ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i]), y_ref[i], std::abs(y_ref[i]) * 1.0e-2f)
141 << "at " << i << " / " << batch_size();
142 }
143 }
144 }
145
146 void Test(xnn_f16_vbinary_minmax_ukernel_function vbinaryc_minmax, OpType op_type, Variant variant = Variant::Native) const {
147 std::random_device random_device;
148 auto rng = std::mt19937(random_device());
149 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.01f, 1.0f), rng);
150 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
151
152 std::vector<uint16_t> a(batch_size() + XNN_EXTRA_BYTES / sizeof(uint16_t));
153 const uint16_t b = f16rng();
154 std::vector<uint16_t> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0));
155 std::vector<float> y_ref(batch_size());
156 for (size_t iteration = 0; iteration < iterations(); iteration++) {
157 std::generate(a.begin(), a.end(), std::ref(f16rng));
158 if (inplace()) {
159 std::generate(y.begin(), y.end(), std::ref(f16rng));
160 } else {
161 std::fill(y.begin(), y.end(), nanf(""));
162 }
163 const uint16_t* a_data = inplace() ? y.data() : a.data();
164
165 // Compute reference results.
166 for (size_t i = 0; i < batch_size(); i++) {
167 switch (op_type) {
168 case OpType::AddC:
169 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) + fp16_ieee_to_fp32_value(b);
170 break;
171 case OpType::DivC:
172 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) / fp16_ieee_to_fp32_value(b);
173 break;
174 case OpType::RDivC:
175 y_ref[i] = fp16_ieee_to_fp32_value(b) / fp16_ieee_to_fp32_value(a_data[i]);
176 break;
177 case OpType::MaxC:
178 y_ref[i] = std::max<float>(fp16_ieee_to_fp32_value(a_data[i]), fp16_ieee_to_fp32_value(b));
179 break;
180 case OpType::MinC:
181 y_ref[i] = std::min<float>(fp16_ieee_to_fp32_value(a_data[i]), fp16_ieee_to_fp32_value(b));
182 break;
183 case OpType::MulC:
184 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) * fp16_ieee_to_fp32_value(b);
185 break;
186 case OpType::SubC:
187 y_ref[i] = fp16_ieee_to_fp32_value(a_data[i]) - fp16_ieee_to_fp32_value(b);
188 break;
189 case OpType::RSubC:
190 y_ref[i] = fp16_ieee_to_fp32_value(b) - fp16_ieee_to_fp32_value(a_data[i]);
191 break;
192 }
193 }
194 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
195 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
196 const float accumulated_range = accumulated_max - accumulated_min;
197 const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_range > 0.0f ?
198 (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) :
199 +std::numeric_limits<float>::infinity()));
200 const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_range > 0.0f ?
201 (accumulated_min + accumulated_range / 255.0f * float(qmin())) :
202 -std::numeric_limits<float>::infinity()));
203 for (size_t i = 0; i < batch_size(); i++) {
204 y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min);
205 }
206
207 // Prepare output parameters.
208 xnn_f16_minmax_params params = { };
209 params = xnn_init_f16_minmax_params(
210 fp16_ieee_from_fp32_value(y_min),
211 fp16_ieee_from_fp32_value(y_max));
212
213 // Call optimized micro-kernel.
214 vbinaryc_minmax(batch_size() * sizeof(uint16_t), a_data, &b, y.data(), &params);
215
216 // Verify results.
217 for (size_t i = 0; i < batch_size(); i++) {
218 ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i]), y_ref[i], std::abs(y_ref[i]) * 1.0e-2f)
219 << "at " << i << " / " << batch_size();
220 }
221 }
222 }
223
Marat Dukhan1e782c42019-11-21 17:02:40 -0800224 void Test(xnn_f32_vbinary_ukernel_function vbinaryc, OpType op_type, Variant variant = Variant::Native) const {
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800225 std::random_device random_device;
226 auto rng = std::mt19937(random_device());
227 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
228
229 std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
230 const float b = f32rng();
231 std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
232 std::vector<float> y_ref(batch_size());
233 for (size_t iteration = 0; iteration < iterations(); iteration++) {
234 std::generate(a.begin(), a.end(), std::ref(f32rng));
235 if (inplace()) {
236 std::generate(y.begin(), y.end(), std::ref(f32rng));
237 } else {
238 std::fill(y.begin(), y.end(), nanf(""));
239 }
240 const float* a_data = inplace() ? y.data() : a.data();
241
242 // Compute reference results.
243 for (size_t i = 0; i < batch_size(); i++) {
244 switch (op_type) {
245 case OpType::AddC:
246 y_ref[i] = a_data[i] + b;
247 break;
Marat Dukhan77ca6302019-12-06 12:48:15 -0800248 case OpType::DivC:
249 y_ref[i] = a_data[i] / b;
250 break;
251 case OpType::RDivC:
252 y_ref[i] = b / a_data[i];
253 break;
Marat Dukhan403b7d42019-12-05 12:49:11 -0800254 case OpType::MaxC:
255 y_ref[i] = std::max<float>(a_data[i], b);
256 break;
257 case OpType::MinC:
258 y_ref[i] = std::min<float>(a_data[i], b);
259 break;
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800260 case OpType::MulC:
261 y_ref[i] = a_data[i] * b;
262 break;
263 case OpType::SubC:
264 y_ref[i] = a_data[i] - b;
265 break;
266 case OpType::RSubC:
267 y_ref[i] = b - a_data[i];
268 break;
269 }
270 }
Marat Dukhan91cd2b72020-04-09 23:57:31 -0700271 // Call optimized micro-kernel.
272 vbinaryc(batch_size() * sizeof(float), a_data, &b, y.data(), nullptr);
273
274 // Verify results.
275 for (size_t i = 0; i < batch_size(); i++) {
276 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
277 << "at " << i << " / " << batch_size();
278 }
279 }
280 }
281
282 void Test(xnn_f32_vbinary_minmax_ukernel_function vbinaryc_minmax, OpType op_type, Variant variant = Variant::Native) const {
283 std::random_device random_device;
284 auto rng = std::mt19937(random_device());
285 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
286
287 std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
288 const float b = f32rng();
289 std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
290 std::vector<float> y_ref(batch_size());
291 for (size_t iteration = 0; iteration < iterations(); iteration++) {
292 std::generate(a.begin(), a.end(), std::ref(f32rng));
293 if (inplace()) {
294 std::generate(y.begin(), y.end(), std::ref(f32rng));
295 } else {
296 std::fill(y.begin(), y.end(), nanf(""));
297 }
298 const float* a_data = inplace() ? y.data() : a.data();
299
300 // Compute reference results.
301 for (size_t i = 0; i < batch_size(); i++) {
302 switch (op_type) {
303 case OpType::AddC:
304 y_ref[i] = a_data[i] + b;
305 break;
306 case OpType::DivC:
307 y_ref[i] = a_data[i] / b;
308 break;
309 case OpType::RDivC:
310 y_ref[i] = b / a_data[i];
311 break;
312 case OpType::MaxC:
313 y_ref[i] = std::max<float>(a_data[i], b);
314 break;
315 case OpType::MinC:
316 y_ref[i] = std::min<float>(a_data[i], b);
317 break;
318 case OpType::MulC:
319 y_ref[i] = a_data[i] * b;
320 break;
321 case OpType::SubC:
322 y_ref[i] = a_data[i] - b;
323 break;
324 case OpType::RSubC:
325 y_ref[i] = b - a_data[i];
326 break;
327 }
328 }
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800329 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
330 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
331 const float accumulated_range = accumulated_max - accumulated_min;
332 const float y_max = accumulated_range > 0.0f ?
333 (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) :
334 +std::numeric_limits<float>::infinity();
335 const float y_min = accumulated_range > 0.0f ?
336 (accumulated_min + accumulated_range / 255.0f * float(qmin())) :
337 -std::numeric_limits<float>::infinity();
338 for (size_t i = 0; i < batch_size(); i++) {
339 y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min);
340 }
341
342 // Prepare output parameters.
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700343 xnn_f32_minmax_params params = { };
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800344 switch (variant) {
345 case Variant::Native:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700346 params = xnn_init_f32_minmax_params(y_min, y_max);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800347 break;
348 case Variant::Scalar:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700349 params = xnn_init_scalar_f32_minmax_params(y_min, y_max);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800350 break;
351 }
352
353 // Call optimized micro-kernel.
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700354 vbinaryc_minmax(batch_size() * sizeof(float), a_data, &b, y.data(), &params);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800355
356 // Verify results.
357 for (size_t i = 0; i < batch_size(); i++) {
358 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
359 << "at " << i << " / " << batch_size();
360 }
361 }
362 }
363
364 private:
365 size_t batch_size_{1};
366 bool inplace_{false};
367 uint8_t qmin_{0};
368 uint8_t qmax_{255};
369 size_t iterations_{15};
370};