blob: 59d7a2324a0a54efb5b0cc6305e85d6d8e67cd9f [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 <cstddef>
13#include <cstdlib>
14#include <functional>
15#include <random>
16#include <vector>
17
Frank Barchard2a1049c2020-06-03 02:31:27 -070018#include <fp16.h>
19
XNNPACK Teamb455b122019-09-27 18:10:33 -070020#include <xnnpack.h>
21#include <xnnpack/AlignedAllocator.h>
22#include <xnnpack/pack.h>
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070023#include <xnnpack/params-init.h>
Frank Barcharde0601b52019-10-25 17:43:34 -070024#include <xnnpack/params.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070025
26
27class VMulCAddCMicrokernelTester {
28 public:
29 enum class Variant {
30 Native,
31 Scalar,
32 };
33
Marat Dukhan49e6ee92019-11-06 15:55:29 -080034 inline VMulCAddCMicrokernelTester& channel_tile(size_t channel_tile) {
35 this->channel_tile_ = channel_tile;
XNNPACK Teamb455b122019-09-27 18:10:33 -070036 return *this;
37 }
38
Marat Dukhan49e6ee92019-11-06 15:55:29 -080039 inline size_t channel_tile() const {
40 return this->channel_tile_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070041 }
42
Marat Dukhan49e6ee92019-11-06 15:55:29 -080043 inline VMulCAddCMicrokernelTester& channels(size_t channels) {
44 assert(channels != 0);
45 this->channels_ = channels;
XNNPACK Teamb455b122019-09-27 18:10:33 -070046 return *this;
47 }
48
Marat Dukhan49e6ee92019-11-06 15:55:29 -080049 inline size_t channels() const {
50 return this->channels_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070051 }
52
Marat Dukhan49e6ee92019-11-06 15:55:29 -080053 inline size_t packed_channels() const {
54 return channels() % channel_tile() == 0 ? channels() : (channels() / channel_tile() + 1) * channel_tile();
XNNPACK Teamb455b122019-09-27 18:10:33 -070055 }
56
Marat Dukhan49e6ee92019-11-06 15:55:29 -080057 inline VMulCAddCMicrokernelTester& rows(size_t rows) {
58 assert(rows != 0);
59 this->rows_ = rows;
XNNPACK Teamb455b122019-09-27 18:10:33 -070060 return *this;
61 }
62
Marat Dukhan49e6ee92019-11-06 15:55:29 -080063 inline size_t rows() const {
64 return this->rows_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070065 }
66
Marat Dukhan49e6ee92019-11-06 15:55:29 -080067 inline VMulCAddCMicrokernelTester& input_stride(size_t input_stride) {
68 this->input_stride_ = input_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -070069 return *this;
70 }
71
Marat Dukhan49e6ee92019-11-06 15:55:29 -080072 inline size_t input_stride() const {
73 return this->input_stride_ == 0 ? channels() : this->input_stride_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070074 }
75
Marat Dukhan49e6ee92019-11-06 15:55:29 -080076 inline VMulCAddCMicrokernelTester& output_stride(size_t output_stride) {
77 this->output_stride_ = output_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -070078 return *this;
79 }
80
Marat Dukhan49e6ee92019-11-06 15:55:29 -080081 inline size_t output_stride() const {
82 return this->output_stride_ == 0 ? channels() : this->output_stride_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070083 }
84
85 inline VMulCAddCMicrokernelTester& inplace(bool inplace) {
86 this->inplace_ = inplace;
87 return *this;
88 }
89
90 inline bool inplace() const {
91 return this->inplace_;
92 }
93
94 inline VMulCAddCMicrokernelTester& qmin(uint8_t qmin) {
95 this->qmin_ = qmin;
96 return *this;
97 }
98
99 inline uint8_t qmin() const {
100 return this->qmin_;
101 }
102
103 inline VMulCAddCMicrokernelTester& qmax(uint8_t qmax) {
104 this->qmax_ = qmax;
105 return *this;
106 }
107
108 inline uint8_t qmax() const {
109 return this->qmax_;
110 }
111
112 inline VMulCAddCMicrokernelTester& iterations(size_t iterations) {
113 this->iterations_ = iterations;
114 return *this;
115 }
116
117 inline size_t iterations() const {
118 return this->iterations_;
119 }
120
Frank Barchard2a1049c2020-06-03 02:31:27 -0700121 void Test(xnn_f16_vmulcaddc_ukernel_function vmulcaddc, Variant variant = Variant::Native) const {
122 std::random_device random_device;
123 auto rng = std::mt19937(random_device());
124 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
125 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
126
127 if (inplace()) {
128 ASSERT_EQ(input_stride(), output_stride());
129 }
130
131 std::vector<uint16_t> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
132 std::vector<uint16_t> scale(channels());
133 std::vector<uint16_t> bias(channels());
134 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_w(packed_channels() * 2);
135 std::vector<uint16_t> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0));
136 std::vector<float> y_ref(rows() * channels());
137
138 for (size_t iteration = 0; iteration < iterations(); iteration++) {
139 std::generate(scale.begin(), scale.end(), std::ref(f16rng));
140 std::generate(bias.begin(), bias.end(), std::ref(f16rng));
141 std::generate(x.begin(), x.end(), std::ref(f16rng));
142 if (inplace()) {
143 std::copy(x.cbegin(), x.cend(), y.begin());
144 } else {
145 std::fill(y.begin(), y.end(), UINT16_C(0x7E00) /* NaN */);
146 }
147 const uint16_t* x_data = inplace() ? y.data() : x.data();
148
149 std::fill(packed_w.begin(), packed_w.end(), UINT16_C(0x7E00) /* NaN */);
150 xnn_pack_f16_vmulcaddc_w(channels(), channel_tile(),
151 scale.data(), bias.data(), packed_w.data());
152
153 // Compute reference results.
154 for (size_t i = 0; i < rows(); i++) {
155 for (size_t j = 0; j < channels(); j++) {
156 y_ref[i * channels() + j] = fp16_ieee_to_fp32_value(x_data[i * input_stride() + j]) * fp16_ieee_to_fp32_value(scale[j]) + fp16_ieee_to_fp32_value(bias[j]);
157 }
158 }
159 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
160 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
161 const float accumulated_range = accumulated_max - accumulated_min;
162 const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax())));
163 const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin())));
164
165 for (float& y_value : y_ref) {
166 y_value = std::max(std::min(y_value, y_max), y_min);
167 }
168
169 // Prepare parameters.
170 xnn_f16_minmax_params params = xnn_init_f16_minmax_params(
171 fp16_ieee_from_fp32_value(y_min),
172 fp16_ieee_from_fp32_value(y_max));
173
174 // Call optimized micro-kernel.
175 vmulcaddc(rows(), channels() * sizeof(uint16_t),
176 x_data, input_stride() * sizeof(uint16_t),
177 packed_w.data(),
178 y.data(), output_stride() * sizeof(uint16_t),
179 &params);
180
181 // Verify results.
182 for (size_t i = 0; i < rows(); i++) {
183 for (size_t j = 0; j < channels(); j++) {
184 ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i * output_stride() + j]), y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-2f)
185 << "at pixel " << i << " / " << rows()
186 << ", channel = " << j << " / " << channels();
187 }
188 }
189 }
190 }
191
XNNPACK Teamb455b122019-09-27 18:10:33 -0700192 void Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc, Variant variant = Variant::Native) const {
193 std::random_device random_device;
194 auto rng = std::mt19937(random_device());
195 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
196
197 if (inplace()) {
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800198 ASSERT_EQ(input_stride(), output_stride());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700199 }
200
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800201 std::vector<float> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float));
202 std::vector<float> scale(channels());
203 std::vector<float> bias(channels());
Marat Dukhan9594db02019-12-05 14:32:37 -0800204 std::vector<float, AlignedAllocator<float, 64>> packed_w(packed_channels() * 2);
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800205 std::vector<float> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0));
206 std::vector<float> y_ref(rows() * channels());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700207 for (size_t iteration = 0; iteration < iterations(); iteration++) {
208 std::generate(scale.begin(), scale.end(), std::ref(f32rng));
209 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
210 std::generate(x.begin(), x.end(), std::ref(f32rng));
211 if (inplace()) {
212 std::copy(x.cbegin(), x.cend(), y.begin());
213 } else {
214 std::fill(y.begin(), y.end(), nanf(""));
215 }
216 const float* x_data = inplace() ? y.data() : x.data();
217
218 std::fill(packed_w.begin(), packed_w.end(), nanf(""));
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800219 xnn_pack_f32_vmulcaddc_w(channels(), channel_tile(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700220 scale.data(), bias.data(), packed_w.data());
221
222 // Compute reference results.
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800223 for (size_t i = 0; i < rows(); i++) {
224 for (size_t j = 0; j < channels(); j++) {
225 y_ref[i * channels() + j] = x_data[i * input_stride() + j] * scale[j] + bias[j];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700226 }
227 }
228 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend());
229 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend());
230 const float accumulated_range = accumulated_max - accumulated_min;
231 const float y_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax());
232 const float y_min = accumulated_min + accumulated_range / 255.0f * float(qmin());
233 for (float& y_value : y_ref) {
234 y_value = std::max<float>(std::min<float>(y_value, y_max), y_min);
235 }
236
Frank Barchard9f3a8432020-06-02 13:59:35 -0700237 // Prepare parameters.
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700238 xnn_f32_minmax_params params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700239 switch (variant) {
240 case Variant::Native:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700241 params = xnn_init_f32_minmax_params(y_min, y_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700242 break;
243 case Variant::Scalar:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700244 params = xnn_init_scalar_f32_minmax_params(y_min, y_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700245 break;
246 }
247
248 // Call optimized micro-kernel.
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800249 vmulcaddc(rows(), channels() * sizeof(float),
250 x_data, input_stride() * sizeof(float),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700251 packed_w.data(),
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800252 y.data(), output_stride() * sizeof(float),
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700253 &params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700254
255 // Verify results.
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800256 for (size_t i = 0; i < rows(); i++) {
257 for (size_t j = 0; j < channels(); j++) {
258 ASSERT_NEAR(y[i * output_stride() + j], y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-6f)
259 << "at pixel " << i << " / " << rows()
260 << ", channel = " << j << " / " << channels();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700261 }
262 }
263 }
264 }
265
266 private:
Marat Dukhan49e6ee92019-11-06 15:55:29 -0800267 size_t channel_tile_{1};
268 size_t channels_{1};
269 size_t rows_{1};
270 size_t input_stride_{0};
271 size_t output_stride_{0};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700272 bool inplace_{false};
273 uint8_t qmin_{0};
274 uint8_t qmax_{255};
275 size_t iterations_{15};
276};