blob: 86032719742e8b7455dafa2db17ad9c316481d1b [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 VBinOpMicrokernelTester {
24 public:
25 enum class OpType {
26 Add,
Marat Dukhan77ca6302019-12-06 12:48:15 -080027 Div,
Marat Dukhan403b7d42019-12-05 12:49:11 -080028 Max,
29 Min,
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080030 Mul,
31 Sub,
32 };
33
34 enum class Variant {
35 Native,
36 Scalar,
37 };
38
39 inline VBinOpMicrokernelTester& batch_size(size_t batch_size) {
40 assert(batch_size != 0);
41 this->batch_size_ = batch_size;
42 return *this;
43 }
44
45 inline size_t batch_size() const {
46 return this->batch_size_;
47 }
48
49 inline VBinOpMicrokernelTester& inplace_a(bool inplace_a) {
50 this->inplace_a_ = inplace_a;
51 return *this;
52 }
53
54 inline bool inplace_a() const {
55 return this->inplace_a_;
56 }
57
58 inline VBinOpMicrokernelTester& inplace_b(bool inplace_b) {
59 this->inplace_b_ = inplace_b;
60 return *this;
61 }
62
63 inline bool inplace_b() const {
64 return this->inplace_b_;
65 }
66
67 inline VBinOpMicrokernelTester& qmin(uint8_t qmin) {
68 this->qmin_ = qmin;
69 return *this;
70 }
71
72 inline uint8_t qmin() const {
73 return this->qmin_;
74 }
75
76 inline VBinOpMicrokernelTester& qmax(uint8_t qmax) {
77 this->qmax_ = qmax;
78 return *this;
79 }
80
81 inline uint8_t qmax() const {
82 return this->qmax_;
83 }
84
85 inline VBinOpMicrokernelTester& iterations(size_t iterations) {
86 this->iterations_ = iterations;
87 return *this;
88 }
89
90 inline size_t iterations() const {
91 return this->iterations_;
92 }
93
Marat Dukhan1e782c42019-11-21 17:02:40 -080094 void Test(xnn_f32_vbinary_ukernel_function vbinary, OpType op_type, Variant variant = Variant::Native) const {
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080095 std::random_device random_device;
96 auto rng = std::mt19937(random_device());
Marat Dukhan77ca6302019-12-06 12:48:15 -080097 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.01f, 1.0f), rng);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -080098
99 std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
100 std::vector<float> b(batch_size() + XNN_EXTRA_BYTES / sizeof(float));
101 std::vector<float> y(batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
102 std::vector<float> y_ref(batch_size());
103 for (size_t iteration = 0; iteration < iterations(); iteration++) {
104 std::generate(a.begin(), a.end(), std::ref(f32rng));
105 std::generate(b.begin(), b.end(), std::ref(f32rng));
106 if (inplace_a() || inplace_b()) {
107 std::generate(y.begin(), y.end(), std::ref(f32rng));
108 } else {
109 std::fill(y.begin(), y.end(), nanf(""));
110 }
111 const float* a_data = inplace_a() ? y.data() : a.data();
112 const float* b_data = inplace_b() ? y.data() : b.data();
113
114 // Compute reference results.
115 for (size_t i = 0; i < batch_size(); i++) {
116 switch (op_type) {
117 case OpType::Add:
118 y_ref[i] = a_data[i] + b_data[i];
119 break;
Marat Dukhan77ca6302019-12-06 12:48:15 -0800120 case OpType::Div:
121 y_ref[i] = a_data[i] / b_data[i];
122 break;
Marat Dukhan403b7d42019-12-05 12:49:11 -0800123 case OpType::Max:
124 y_ref[i] = std::max<float>(a_data[i], b_data[i]);
125 break;
126 case OpType::Min:
127 y_ref[i] = std::min<float>(a_data[i], b_data[i]);
128 break;
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800129 case OpType::Mul:
130 y_ref[i] = a_data[i] * b_data[i];
131 break;
132 case OpType::Sub:
133 y_ref[i] = a_data[i] - b_data[i];
134 break;
135 }
136 }
137 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
138 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
139 const float accumulated_range = accumulated_max - accumulated_min;
140 const float y_max = accumulated_range > 0.0f ?
141 (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) :
142 +std::numeric_limits<float>::infinity();
143 const float y_min = accumulated_range > 0.0f ?
144 (accumulated_min + accumulated_range / 255.0f * float(qmin())) :
145 -std::numeric_limits<float>::infinity();
146 for (size_t i = 0; i < batch_size(); i++) {
147 y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min);
148 }
149
150 // Prepare output parameters.
151 xnn_f32_output_params output_params = { };
152 switch (variant) {
153 case Variant::Native:
154 output_params = xnn_init_f32_output_params(y_min, y_max);
155 break;
156 case Variant::Scalar:
157 output_params = xnn_init_scalar_f32_output_params(y_min, y_max);
158 break;
159 }
160
161 // Call optimized micro-kernel.
Marat Dukhan1e782c42019-11-21 17:02:40 -0800162 vbinary(batch_size() * sizeof(float), a_data, b_data, y.data(), &output_params);
Marat Dukhanc07cb7f2019-11-14 15:32:05 -0800163
164 // Verify results.
165 for (size_t i = 0; i < batch_size(); i++) {
166 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
167 << "at " << i << " / " << batch_size();
168 }
169 }
170 }
171
172 private:
173 size_t batch_size_{1};
174 bool inplace_a_{false};
175 bool inplace_b_{false};
176 uint8_t qmin_{0};
177 uint8_t qmax_{255};
178 size_t iterations_{15};
179};