Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 1 | // 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 | |
| 23 | class VBinOpMicrokernelTester { |
| 24 | public: |
| 25 | enum class OpType { |
| 26 | Add, |
Marat Dukhan | 77ca630 | 2019-12-06 12:48:15 -0800 | [diff] [blame] | 27 | Div, |
Marat Dukhan | 403b7d4 | 2019-12-05 12:49:11 -0800 | [diff] [blame] | 28 | Max, |
| 29 | Min, |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 30 | 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 Dukhan | 1e782c4 | 2019-11-21 17:02:40 -0800 | [diff] [blame] | 94 | void Test(xnn_f32_vbinary_ukernel_function vbinary, OpType op_type, Variant variant = Variant::Native) const { |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 95 | std::random_device random_device; |
| 96 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 77ca630 | 2019-12-06 12:48:15 -0800 | [diff] [blame] | 97 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.01f, 1.0f), rng); |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 98 | |
| 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 Dukhan | 77ca630 | 2019-12-06 12:48:15 -0800 | [diff] [blame] | 120 | case OpType::Div: |
| 121 | y_ref[i] = a_data[i] / b_data[i]; |
| 122 | break; |
Marat Dukhan | 403b7d4 | 2019-12-05 12:49:11 -0800 | [diff] [blame] | 123 | 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 Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 129 | 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 | } |
Marat Dukhan | 91cd2b7 | 2020-04-09 23:57:31 -0700 | [diff] [blame] | 137 | |
| 138 | // Call optimized micro-kernel. |
| 139 | vbinary(batch_size() * sizeof(float), a_data, b_data, y.data(), nullptr); |
| 140 | |
| 141 | // Verify results. |
| 142 | for (size_t i = 0; i < batch_size(); i++) { |
| 143 | ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f) |
| 144 | << "at " << i << " / " << batch_size(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Test(xnn_f32_vbinary_minmax_ukernel_function vbinary_minmax, OpType op_type, Variant variant = Variant::Native) const { |
| 150 | std::random_device random_device; |
| 151 | auto rng = std::mt19937(random_device()); |
| 152 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.01f, 1.0f), rng); |
| 153 | |
| 154 | std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float)); |
| 155 | std::vector<float> b(batch_size() + XNN_EXTRA_BYTES / sizeof(float)); |
| 156 | std::vector<float> y(batch_size() + (inplace_a() || inplace_b() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); |
| 157 | std::vector<float> y_ref(batch_size()); |
| 158 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 159 | std::generate(a.begin(), a.end(), std::ref(f32rng)); |
| 160 | std::generate(b.begin(), b.end(), std::ref(f32rng)); |
| 161 | if (inplace_a() || inplace_b()) { |
| 162 | std::generate(y.begin(), y.end(), std::ref(f32rng)); |
| 163 | } else { |
| 164 | std::fill(y.begin(), y.end(), nanf("")); |
| 165 | } |
| 166 | const float* a_data = inplace_a() ? y.data() : a.data(); |
| 167 | const float* b_data = inplace_b() ? y.data() : b.data(); |
| 168 | |
| 169 | // Compute reference results. |
| 170 | for (size_t i = 0; i < batch_size(); i++) { |
| 171 | switch (op_type) { |
| 172 | case OpType::Add: |
| 173 | y_ref[i] = a_data[i] + b_data[i]; |
| 174 | break; |
| 175 | case OpType::Div: |
| 176 | y_ref[i] = a_data[i] / b_data[i]; |
| 177 | break; |
| 178 | case OpType::Max: |
| 179 | y_ref[i] = std::max<float>(a_data[i], b_data[i]); |
| 180 | break; |
| 181 | case OpType::Min: |
| 182 | y_ref[i] = std::min<float>(a_data[i], b_data[i]); |
| 183 | break; |
| 184 | case OpType::Mul: |
| 185 | y_ref[i] = a_data[i] * b_data[i]; |
| 186 | break; |
| 187 | case OpType::Sub: |
| 188 | y_ref[i] = a_data[i] - b_data[i]; |
| 189 | break; |
| 190 | } |
| 191 | } |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 192 | const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); |
| 193 | const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); |
| 194 | const float accumulated_range = accumulated_max - accumulated_min; |
| 195 | const float y_max = accumulated_range > 0.0f ? |
| 196 | (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) : |
| 197 | +std::numeric_limits<float>::infinity(); |
| 198 | const float y_min = accumulated_range > 0.0f ? |
| 199 | (accumulated_min + accumulated_range / 255.0f * float(qmin())) : |
| 200 | -std::numeric_limits<float>::infinity(); |
| 201 | for (size_t i = 0; i < batch_size(); i++) { |
| 202 | y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min); |
| 203 | } |
| 204 | |
| 205 | // Prepare output parameters. |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 206 | xnn_f32_minmax_params minmax_params = { }; |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 207 | switch (variant) { |
| 208 | case Variant::Native: |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 209 | minmax_params = xnn_init_f32_minmax_params(y_min, y_max); |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 210 | break; |
| 211 | case Variant::Scalar: |
Marat Dukhan | eb09a6b | 2020-04-08 17:34:32 -0700 | [diff] [blame] | 212 | minmax_params = xnn_init_scalar_f32_minmax_params(y_min, y_max); |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
| 216 | // Call optimized micro-kernel. |
Marat Dukhan | 91cd2b7 | 2020-04-09 23:57:31 -0700 | [diff] [blame] | 217 | vbinary_minmax(batch_size() * sizeof(float), a_data, b_data, y.data(), &minmax_params); |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 218 | |
| 219 | // Verify results. |
| 220 | for (size_t i = 0; i < batch_size(); i++) { |
| 221 | ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f) |
| 222 | << "at " << i << " / " << batch_size(); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | size_t batch_size_{1}; |
| 229 | bool inplace_a_{false}; |
| 230 | bool inplace_b_{false}; |
| 231 | uint8_t qmin_{0}; |
| 232 | uint8_t qmax_{255}; |
| 233 | size_t iterations_{15}; |
| 234 | }; |