• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HSwishMicrokernelTester {
24  public:
25   enum class Variant {
26     Native,
27     Scalar,
28   };
29 
batch_size(size_t batch_size)30   inline HSwishMicrokernelTester& batch_size(size_t batch_size) {
31     assert(batch_size != 0);
32     this->batch_size_ = batch_size;
33     return *this;
34   }
35 
batch_size()36   inline size_t batch_size() const {
37     return this->batch_size_;
38   }
39 
inplace(bool inplace)40   inline HSwishMicrokernelTester& inplace(bool inplace) {
41     this->inplace_ = inplace;
42     return *this;
43   }
44 
inplace()45   inline bool inplace() const {
46     return this->inplace_;
47   }
48 
iterations(size_t iterations)49   inline HSwishMicrokernelTester& iterations(size_t iterations) {
50     this->iterations_ = iterations;
51     return *this;
52   }
53 
iterations()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 
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());
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:
79           params = xnn_init_f32_hswish_params();
80           break;
81         case Variant::Scalar:
82           params = xnn_init_scalar_f32_hswish_params();
83           break;
84       }
85 
86       // Compute reference results.
87       for (size_t i = 0; i < batch_size(); i++) {
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.
92       hswish(batch_size() * sizeof(float), x_data, y.data(), &params);
93 
94       // Verify results.
95       for (size_t i = 0; i < batch_size(); i++) {
96         ASSERT_NEAR(y_ref[i], y[i], std::abs(y_ref[i]) * 1.0e-6f)
97           << "at position " << i << ", batch_size = " << batch_size();
98       }
99     }
100   }
101 
102  private:
103   size_t batch_size_{1};
104   bool inplace_{false};
105   size_t iterations_{5};
106 };
107