XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [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> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 19 | #include <xnnpack/params-init.h> |
Frank Barchard | e0601b5 | 2019-10-25 17:43:34 -0700 | [diff] [blame] | 20 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class HSwishMicrokernelTester { |
| 24 | public: |
| 25 | enum class Variant { |
| 26 | Native, |
| 27 | Scalar, |
| 28 | }; |
| 29 | |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 30 | inline HSwishMicrokernelTester& batch_size(size_t batch_size) { |
| 31 | assert(batch_size != 0); |
| 32 | this->batch_size_ = batch_size; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 33 | return *this; |
| 34 | } |
| 35 | |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 36 | inline size_t batch_size() const { |
| 37 | return this->batch_size_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | inline HSwishMicrokernelTester& inplace(bool inplace) { |
| 41 | this->inplace_ = inplace; |
| 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | inline bool inplace() const { |
| 46 | return this->inplace_; |
| 47 | } |
| 48 | |
| 49 | inline HSwishMicrokernelTester& iterations(size_t iterations) { |
| 50 | this->iterations_ = iterations; |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | inline size_t iterations() const { |
| 55 | return this->iterations_; |
| 56 | } |
| 57 | |
| 58 | void Test(xnn_f32_hswish_ukernel_function hswish, Variant variant = Variant::Native) const { |
| 59 | std::random_device random_device; |
| 60 | auto rng = std::mt19937(random_device()); |
| 61 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), rng); |
| 62 | |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 63 | std::vector<float> x(batch_size() + XNN_EXTRA_BYTES / sizeof(float)); |
| 64 | std::vector<float> y(batch_size() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); |
| 65 | std::vector<float> y_ref(batch_size()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 66 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 67 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 68 | if (inplace()) { |
| 69 | std::generate(y.begin(), y.end(), std::ref(f32rng)); |
| 70 | } else { |
| 71 | std::fill(y.begin(), y.end(), std::nanf("")); |
| 72 | } |
| 73 | const float* x_data = inplace() ? y.data() : x.data(); |
| 74 | |
| 75 | // Prepare micro-kernel parameters. |
| 76 | union xnn_f32_hswish_params params = { }; |
| 77 | switch (variant) { |
| 78 | case Variant::Native: |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 79 | params = xnn_init_f32_hswish_params(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 80 | break; |
| 81 | case Variant::Scalar: |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 82 | params = xnn_init_scalar_f32_hswish_params(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | |
| 86 | // Compute reference results. |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 87 | for (size_t i = 0; i < batch_size(); i++) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 88 | y_ref[i] = x_data[i] * std::max(std::min(x_data[i] + 3.0f, 6.0f), 0.0f) / 6.0f; |
| 89 | } |
| 90 | |
| 91 | // Call optimized micro-kernel. |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 92 | hswish(batch_size() * sizeof(float), x_data, y.data(), ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 93 | |
| 94 | // Verify results. |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 95 | for (size_t i = 0; i < batch_size(); i++) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 96 | ASSERT_NEAR(y_ref[i], y[i], std::abs(y_ref[i]) * 1.0e-6f) |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 97 | << "at position " << i << ", batch_size = " << batch_size(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private: |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 103 | size_t batch_size_{1}; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 104 | bool inplace_{false}; |
Marat Dukhan | 662faa0 | 2019-12-09 22:48:16 -0800 | [diff] [blame] | 105 | size_t iterations_{5}; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 106 | }; |