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 VBinOpCMicrokernelTester { |
| 24 | public: |
| 25 | enum class OpType { |
| 26 | AddC, |
| 27 | MulC, |
| 28 | SubC, |
| 29 | RSubC, |
| 30 | }; |
| 31 | |
| 32 | enum class Variant { |
| 33 | Native, |
| 34 | Scalar, |
| 35 | }; |
| 36 | |
| 37 | inline VBinOpCMicrokernelTester& batch_size(size_t batch_size) { |
| 38 | assert(batch_size != 0); |
| 39 | this->batch_size_ = batch_size; |
| 40 | return *this; |
| 41 | } |
| 42 | |
| 43 | inline size_t batch_size() const { |
| 44 | return this->batch_size_; |
| 45 | } |
| 46 | |
| 47 | inline VBinOpCMicrokernelTester& inplace(bool inplace) { |
| 48 | this->inplace_ = inplace; |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | inline bool inplace() const { |
| 53 | return this->inplace_; |
| 54 | } |
| 55 | |
| 56 | inline VBinOpCMicrokernelTester& qmin(uint8_t qmin) { |
| 57 | this->qmin_ = qmin; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | inline uint8_t qmin() const { |
| 62 | return this->qmin_; |
| 63 | } |
| 64 | |
| 65 | inline VBinOpCMicrokernelTester& qmax(uint8_t qmax) { |
| 66 | this->qmax_ = qmax; |
| 67 | return *this; |
| 68 | } |
| 69 | |
| 70 | inline uint8_t qmax() const { |
| 71 | return this->qmax_; |
| 72 | } |
| 73 | |
| 74 | inline VBinOpCMicrokernelTester& iterations(size_t iterations) { |
| 75 | this->iterations_ = iterations; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | inline size_t iterations() const { |
| 80 | return this->iterations_; |
| 81 | } |
| 82 | |
Marat Dukhan | 1e782c4 | 2019-11-21 17:02:40 -0800 | [diff] [blame^] | 83 | void Test(xnn_f32_vbinary_ukernel_function vbinaryc, OpType op_type, Variant variant = Variant::Native) const { |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 84 | std::random_device random_device; |
| 85 | auto rng = std::mt19937(random_device()); |
| 86 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); |
| 87 | |
| 88 | std::vector<float> a(batch_size() + XNN_EXTRA_BYTES / sizeof(float)); |
| 89 | const float b = f32rng(); |
| 90 | std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); |
| 91 | std::vector<float> y_ref(batch_size()); |
| 92 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 93 | std::generate(a.begin(), a.end(), std::ref(f32rng)); |
| 94 | if (inplace()) { |
| 95 | std::generate(y.begin(), y.end(), std::ref(f32rng)); |
| 96 | } else { |
| 97 | std::fill(y.begin(), y.end(), nanf("")); |
| 98 | } |
| 99 | const float* a_data = inplace() ? y.data() : a.data(); |
| 100 | |
| 101 | // Compute reference results. |
| 102 | for (size_t i = 0; i < batch_size(); i++) { |
| 103 | switch (op_type) { |
| 104 | case OpType::AddC: |
| 105 | y_ref[i] = a_data[i] + b; |
| 106 | break; |
| 107 | case OpType::MulC: |
| 108 | y_ref[i] = a_data[i] * b; |
| 109 | break; |
| 110 | case OpType::SubC: |
| 111 | y_ref[i] = a_data[i] - b; |
| 112 | break; |
| 113 | case OpType::RSubC: |
| 114 | y_ref[i] = b - a_data[i]; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); |
| 119 | const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); |
| 120 | const float accumulated_range = accumulated_max - accumulated_min; |
| 121 | const float y_max = accumulated_range > 0.0f ? |
| 122 | (accumulated_max - accumulated_range / 255.0f * float(255 - qmax())) : |
| 123 | +std::numeric_limits<float>::infinity(); |
| 124 | const float y_min = accumulated_range > 0.0f ? |
| 125 | (accumulated_min + accumulated_range / 255.0f * float(qmin())) : |
| 126 | -std::numeric_limits<float>::infinity(); |
| 127 | for (size_t i = 0; i < batch_size(); i++) { |
| 128 | y_ref[i] = std::max<float>(std::min<float>(y_ref[i], y_max), y_min); |
| 129 | } |
| 130 | |
| 131 | // Prepare output parameters. |
| 132 | xnn_f32_output_params output_params = { }; |
| 133 | switch (variant) { |
| 134 | case Variant::Native: |
| 135 | output_params = xnn_init_f32_output_params(y_min, y_max); |
| 136 | break; |
| 137 | case Variant::Scalar: |
| 138 | output_params = xnn_init_scalar_f32_output_params(y_min, y_max); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | // Call optimized micro-kernel. |
Marat Dukhan | 1e782c4 | 2019-11-21 17:02:40 -0800 | [diff] [blame^] | 143 | vbinaryc(batch_size() * sizeof(float), a_data, &b, y.data(), &output_params); |
Marat Dukhan | c07cb7f | 2019-11-14 15:32:05 -0800 | [diff] [blame] | 144 | |
| 145 | // Verify results. |
| 146 | for (size_t i = 0; i < batch_size(); i++) { |
| 147 | ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f) |
| 148 | << "at " << i << " / " << batch_size(); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | size_t batch_size_{1}; |
| 155 | bool inplace_{false}; |
| 156 | uint8_t qmin_{0}; |
| 157 | uint8_t qmax_{255}; |
| 158 | size_t iterations_{15}; |
| 159 | }; |