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