blob: 975c6e1274455dc6c0322d7f47ab92aa2ac2bea6 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// 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>
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070021#include <xnnpack/params-init.h>
Frank Barcharde0601b52019-10-25 17:43:34 -070022#include <xnnpack/params.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070023
24
Marat Dukhan1f29b802020-05-15 23:46:39 -070025class GAvgPoolCWMicrokernelTester {
XNNPACK Teamb455b122019-09-27 18:10:33 -070026 public:
27 enum class Variant {
28 Native,
29 Scalar,
30 };
31
Marat Dukhan1f29b802020-05-15 23:46:39 -070032 inline GAvgPoolCWMicrokernelTester& elements(size_t elements) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070033 assert(elements != 0);
34 this->elements_ = elements;
35 return *this;
36 }
37
38 inline size_t elements() const {
39 return this->elements_;
40 }
41
Marat Dukhan1f29b802020-05-15 23:46:39 -070042 inline GAvgPoolCWMicrokernelTester& channels(size_t channels) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070043 assert(channels != 0);
44 this->channels_ = channels;
45 return *this;
46 }
47
48 inline size_t channels() const {
49 return this->channels_;
50 }
51
Marat Dukhan1f29b802020-05-15 23:46:39 -070052 inline GAvgPoolCWMicrokernelTester& qmin(uint8_t qmin) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070053 this->qmin_ = qmin;
54 return *this;
55 }
56
57 inline uint8_t qmin() const {
58 return this->qmin_;
59 }
60
Marat Dukhan1f29b802020-05-15 23:46:39 -070061 inline GAvgPoolCWMicrokernelTester& qmax(uint8_t qmax) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070062 this->qmax_ = qmax;
63 return *this;
64 }
65
66 inline uint8_t qmax() const {
67 return this->qmax_;
68 }
69
Marat Dukhan1f29b802020-05-15 23:46:39 -070070 inline GAvgPoolCWMicrokernelTester& iterations(size_t iterations) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070071 this->iterations_ = iterations;
72 return *this;
73 }
74
75 inline size_t iterations() const {
76 return this->iterations_;
77 }
78
79
Marat Dukhan1f29b802020-05-15 23:46:39 -070080 void Test(xnn_f32_gavgpool_cw_ukernel_function gavgpool, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -070081 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
Frank Barchard9f3a8432020-06-02 13:59:35 -0700108 // Prepare parameters.
XNNPACK Teamb455b122019-09-27 18:10:33 -0700109 union xnn_f32_gavgpool_params params = { };
110 switch (variant) {
111 case Variant::Native:
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -0700112 params = xnn_init_f32_gavgpool_params(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700113 1.0f / float(elements()), y_min, y_max, elements());
114 break;
115 case Variant::Scalar:
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -0700116 params = xnn_init_scalar_f32_gavgpool_params(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700117 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};