• 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 <cmath>
13 #include <cstddef>
14 #include <cstdlib>
15 #include <functional>
16 #include <random>
17 #include <vector>
18 
19 #include <xnnpack.h>
20 #include <xnnpack/AlignedAllocator.h>
21 #include <xnnpack/params-init.h>
22 #include <xnnpack/params.h>
23 
24 
25 class GAvgPoolCWMicrokernelTester {
26  public:
27   enum class Variant {
28     Native,
29     Scalar,
30   };
31 
elements(size_t elements)32   inline GAvgPoolCWMicrokernelTester& elements(size_t elements) {
33     assert(elements != 0);
34     this->elements_ = elements;
35     return *this;
36   }
37 
elements()38   inline size_t elements() const {
39     return this->elements_;
40   }
41 
channels(size_t channels)42   inline GAvgPoolCWMicrokernelTester& channels(size_t channels) {
43     assert(channels != 0);
44     this->channels_ = channels;
45     return *this;
46   }
47 
channels()48   inline size_t channels() const {
49     return this->channels_;
50   }
51 
qmin(uint8_t qmin)52   inline GAvgPoolCWMicrokernelTester& qmin(uint8_t qmin) {
53     this->qmin_ = qmin;
54     return *this;
55   }
56 
qmin()57   inline uint8_t qmin() const {
58     return this->qmin_;
59   }
60 
qmax(uint8_t qmax)61   inline GAvgPoolCWMicrokernelTester& qmax(uint8_t qmax) {
62     this->qmax_ = qmax;
63     return *this;
64   }
65 
qmax()66   inline uint8_t qmax() const {
67     return this->qmax_;
68   }
69 
iterations(size_t iterations)70   inline GAvgPoolCWMicrokernelTester& iterations(size_t iterations) {
71     this->iterations_ = iterations;
72     return *this;
73   }
74 
iterations()75   inline size_t iterations() const {
76     return this->iterations_;
77   }
78 
79 
80   void Test(xnn_f32_gavgpool_cw_ukernel_function gavgpool, 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>(), rng);
84 
85     std::vector<float> x(elements() * channels() + XNN_EXTRA_BYTES / sizeof(float));
86     std::vector<float> y(channels());
87     std::vector<float> y_ref(channels());
88     for (size_t iteration = 0; iteration < iterations(); iteration++) {
89       std::generate(x.begin(), x.end(), std::ref(f32rng));
90       std::fill(y.begin(), y.end(), std::nanf(""));
91 
92       // Compute reference results, without clamping.
93       for (size_t i = 0; i < channels(); i++) {
94         float acc = 0.0f;
95         for (size_t j = 0; j < elements(); j++) {
96           acc += x[i * elements() + j];
97         }
98         y_ref[i] = acc / float(elements());
99       }
100 
101       // Compute clamping parameters.
102       const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
103       const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
104       const float accumulated_range = accumulated_max - accumulated_min;
105       const float y_min = accumulated_min + float(qmin()) / 255.0f * accumulated_range;
106       const float y_max = accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range;
107 
108       // Prepare parameters.
109       union xnn_f32_gavgpool_params params;
110       switch (variant) {
111         case Variant::Native:
112           xnn_init_f32_gavgpool_params(
113             &params, 1.0f / float(elements()), y_min, y_max, elements());
114           break;
115         case Variant::Scalar:
116           xnn_init_scalar_f32_gavgpool_params(
117             &params, 1.0f / float(elements()), y_min, y_max, elements());
118           break;
119       }
120 
121       // Clamp reference results.
122       for (float& y_value : y_ref) {
123         y_value = std::max(std::min(y_value, y_max), y_min);
124       }
125 
126       // Call optimized micro-kernel.
127       gavgpool(elements() * sizeof(float), channels(), x.data(), y.data(), &params);
128 
129       // Verify results.
130       for (size_t i = 0; i < channels(); i++) {
131         ASSERT_LE(y[i], y_max)
132           << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
133         ASSERT_GE(y[i], y_min)
134           << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
135         ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
136           << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
137       }
138     }
139   }
140 
141  private:
142   size_t elements_{1};
143   size_t channels_{1};
144   uint8_t qmin_{0};
145   uint8_t qmax_{255};
146   size_t iterations_{15};
147 };
148