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 <random>
16 #include <vector>
17
18 #include <fp16.h>
19
20
21 #include <xnnpack.h>
22 #include <xnnpack/aligned-allocator.h>
23 #include <xnnpack/microfnptr.h>
24 #include <xnnpack/microparams-init.h>
25
26
27 class GAvgPoolCWMicrokernelTester {
28 public:
29 enum class Variant {
30 Native,
31 Scalar,
32 };
33
elements(size_t elements)34 inline GAvgPoolCWMicrokernelTester& elements(size_t elements) {
35 assert(elements != 0);
36 this->elements_ = elements;
37 return *this;
38 }
39
elements()40 inline size_t elements() const {
41 return this->elements_;
42 }
43
channels(size_t channels)44 inline GAvgPoolCWMicrokernelTester& channels(size_t channels) {
45 assert(channels != 0);
46 this->channels_ = channels;
47 return *this;
48 }
49
channels()50 inline size_t channels() const {
51 return this->channels_;
52 }
53
qmin(uint8_t qmin)54 inline GAvgPoolCWMicrokernelTester& qmin(uint8_t qmin) {
55 this->qmin_ = qmin;
56 return *this;
57 }
58
qmin()59 inline uint8_t qmin() const {
60 return this->qmin_;
61 }
62
qmax(uint8_t qmax)63 inline GAvgPoolCWMicrokernelTester& qmax(uint8_t qmax) {
64 this->qmax_ = qmax;
65 return *this;
66 }
67
qmax()68 inline uint8_t qmax() const {
69 return this->qmax_;
70 }
71
iterations(size_t iterations)72 inline GAvgPoolCWMicrokernelTester& iterations(size_t iterations) {
73 this->iterations_ = iterations;
74 return *this;
75 }
76
iterations()77 inline size_t iterations() const {
78 return this->iterations_;
79 }
80
81
82 void Test(xnn_f32_gavgpool_cw_ukernel_function gavgpool, Variant variant = Variant::Native) const {
83 std::random_device random_device;
84 auto rng = std::mt19937(random_device());
85 std::uniform_real_distribution<float> f32dist;
86
87 std::vector<float> x(elements() * channels() + XNN_EXTRA_BYTES / sizeof(float));
88 std::vector<float> y(channels());
89 std::vector<float> y_ref(channels());
90 for (size_t iteration = 0; iteration < iterations(); iteration++) {
91 std::generate(x.begin(), x.end(), [&]() { return f32dist(rng); });
92 std::fill(y.begin(), y.end(), std::nanf(""));
93
94 // Compute reference results, without clamping.
95 for (size_t i = 0; i < channels(); i++) {
96 float acc = 0.0f;
97 for (size_t j = 0; j < elements(); j++) {
98 acc += x[i * elements() + j];
99 }
100 y_ref[i] = acc / float(elements());
101 }
102
103 // Compute clamping parameters.
104 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
105 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
106 const float accumulated_range = accumulated_max - accumulated_min;
107 const float y_min = accumulated_min + float(qmin()) / 255.0f * accumulated_range;
108 const float y_max = accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range;
109
110 // Prepare parameters.
111 union xnn_f32_gavgpool_params params;
112 switch (variant) {
113 case Variant::Native:
114 xnn_init_f32_gavgpool_params(
115 ¶ms, 1.0f / float(elements()), y_min, y_max, elements());
116 break;
117 case Variant::Scalar:
118 xnn_init_scalar_f32_gavgpool_params(
119 ¶ms, 1.0f / float(elements()), y_min, y_max, elements());
120 break;
121 }
122
123 // Clamp reference results.
124 for (float& y_value : y_ref) {
125 y_value = std::max(std::min(y_value, y_max), y_min);
126 }
127
128 // Call optimized micro-kernel.
129 gavgpool(elements() * sizeof(float), channels(), x.data(), y.data(), ¶ms);
130
131 // Verify results.
132 for (size_t i = 0; i < channels(); i++) {
133 ASSERT_LE(y[i], y_max)
134 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
135 ASSERT_GE(y[i], y_min)
136 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
137 ASSERT_NEAR(y[i], y_ref[i], std::abs(y_ref[i]) * 1.0e-6f)
138 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
139 }
140 }
141 }
142
Test(xnn_f16_gavgpool_cw_ukernel_function gavgpool,xnn_init_f16_gavgpool_neonfp16arith_params_fn init_params)143 void Test(xnn_f16_gavgpool_cw_ukernel_function gavgpool, xnn_init_f16_gavgpool_neonfp16arith_params_fn init_params) const {
144 std::random_device random_device;
145 auto rng = std::mt19937(random_device());
146 std::uniform_real_distribution<float> f32dist(0.1f, 10.0f);
147
148 std::vector<uint16_t> x(elements() * channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
149 std::vector<uint16_t> y(channels());
150 std::vector<float> y_ref(channels());
151 for (size_t iteration = 0; iteration < iterations(); iteration++) {
152 std::generate(x.begin(), x.end(), [&]() { return fp16_ieee_from_fp32_value(f32dist(rng)); });
153 std::fill(y.begin(), y.end(), UINT16_C(0x7E00) /* NaN */);
154
155 // Compute reference results, without clamping.
156 for (size_t i = 0; i < channels(); i++) {
157 float acc = 0.0f;
158 for (size_t j = 0; j < elements(); j++) {
159 acc += fp16_ieee_to_fp32_value(x[i * elements() + j]);
160 }
161 y_ref[i] = acc / float(elements());
162 }
163
164 // Compute clamping parameters.
165 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
166 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
167 const float accumulated_range = accumulated_max - accumulated_min;
168 const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin())));
169 const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax())));
170
171 // Prepare parameters.
172 union xnn_f16_gavgpool_params params;
173 init_params(
174 ¶ms, fp16_ieee_from_fp32_value(1.0f / float(elements())), fp16_ieee_from_fp32_value(y_min), fp16_ieee_from_fp32_value(y_max), elements());
175
176 // Clamp reference results.
177 for (float& y_value : y_ref) {
178 y_value = std::max(std::min(y_value, y_max), y_min);
179 }
180
181 // Call optimized micro-kernel.
182 gavgpool(elements() * sizeof(uint16_t), channels(), x.data(), y.data(), ¶ms);
183
184 // Verify results.
185 for (size_t i = 0; i < channels(); i++) {
186 ASSERT_LE(fp16_ieee_to_fp32_value(y[i]), y_max)
187 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
188 ASSERT_GE(fp16_ieee_to_fp32_value(y[i]), y_min)
189 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
190 ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i]), y_ref[i], 1.0e-2f * std::abs(y_ref[i]))
191 << "at position " << i << ", elements = " << elements() << ", channels = " << channels();
192 }
193 }
194 }
195
196 private:
197 size_t elements_{1};
198 size_t channels_{1};
199 uint8_t qmin_{0};
200 uint8_t qmax_{255};
201 size_t iterations_{15};
202 };
203