blob: 0264ef6a5438bfc49925a8a61baf62984a89f265 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
8
9#pragma once
10
11#include <gtest/gtest.h>
12
13#include <algorithm>
14#include <cassert>
15#include <cmath>
16#include <cstddef>
17#include <cstdlib>
18#include <functional>
Marat Dukhan5ce30d92020-04-14 03:31:26 -070019#include <limits>
XNNPACK Teamb455b122019-09-27 18:10:33 -070020#include <random>
21#include <vector>
22
Frank Barchard0bb49a72020-06-04 11:35:11 -070023#include <fp16.h>
24
XNNPACK Teamb455b122019-09-27 18:10:33 -070025#include <xnnpack.h>
26#include <xnnpack/AlignedAllocator.h>
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070027#include <xnnpack/params-init.h>
Frank Barcharde0601b52019-10-25 17:43:34 -070028#include <xnnpack/params.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070029#include <xnnpack/requantization.h>
30
31
32class GAvgPoolMicrokernelTester {
33 public:
34 enum class Variant {
35 Native,
36 Scalar,
37 };
38
Marat Dukhana63a6fc2020-03-10 06:12:48 -070039 inline GAvgPoolMicrokernelTester& rows(size_t rows) {
40 assert(rows != 0);
41 this->rows_ = rows;
XNNPACK Teamb455b122019-09-27 18:10:33 -070042 return *this;
43 }
44
Marat Dukhana63a6fc2020-03-10 06:12:48 -070045 inline size_t rows() const {
46 return this->rows_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070047 }
48
Marat Dukhana63a6fc2020-03-10 06:12:48 -070049 inline GAvgPoolMicrokernelTester& channels(size_t channels) {
50 assert(channels != 0);
51 this->channels_ = channels;
XNNPACK Teamb455b122019-09-27 18:10:33 -070052 return *this;
53 }
54
Marat Dukhana63a6fc2020-03-10 06:12:48 -070055 inline size_t channels() const {
56 return this->channels_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070057 }
58
Marat Dukhana63a6fc2020-03-10 06:12:48 -070059 inline GAvgPoolMicrokernelTester& channel_tile(size_t channel_tile) {
60 assert(channel_tile != 0);
61 this->channel_tile_ = channel_tile;
XNNPACK Teamb455b122019-09-27 18:10:33 -070062 return *this;
63 }
64
Marat Dukhana63a6fc2020-03-10 06:12:48 -070065 inline size_t channel_tile() const {
66 return this->channel_tile_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070067 }
68
Marat Dukhana63a6fc2020-03-10 06:12:48 -070069 inline GAvgPoolMicrokernelTester& input_stride(size_t input_stride) {
70 assert(input_stride != 0);
71 this->input_stride_ = input_stride;
XNNPACK Teamb455b122019-09-27 18:10:33 -070072 return *this;
73 }
74
Marat Dukhana63a6fc2020-03-10 06:12:48 -070075 inline size_t input_stride() const {
76 if (this->input_stride_ == 0) {
77 return channels();
XNNPACK Teamb455b122019-09-27 18:10:33 -070078 } else {
Marat Dukhana63a6fc2020-03-10 06:12:48 -070079 assert(this->input_stride_ >= channels());
80 return this->input_stride_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070081 }
82 }
83
Marat Dukhana63a6fc2020-03-10 06:12:48 -070084 inline GAvgPoolMicrokernelTester& input_scale(float input_scale) {
85 assert(input_scale > 0.0f);
86 assert(std::isnormal(input_scale));
87 this->input_scale_ = input_scale;
XNNPACK Teamb455b122019-09-27 18:10:33 -070088 return *this;
89 }
90
Marat Dukhana63a6fc2020-03-10 06:12:48 -070091 inline float input_scale() const {
92 return this->input_scale_;
XNNPACK Teamb455b122019-09-27 18:10:33 -070093 }
94
Marat Dukhana63a6fc2020-03-10 06:12:48 -070095 inline GAvgPoolMicrokernelTester& input_zero_point(uint8_t input_zero_point) {
96 this->input_zero_point_ = input_zero_point;
XNNPACK Teamb455b122019-09-27 18:10:33 -070097 return *this;
98 }
99
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700100 inline uint8_t input_zero_point() const {
101 return this->input_zero_point_;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700102 }
103
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700104 inline GAvgPoolMicrokernelTester& output_scale(float output_scale) {
105 assert(output_scale > 0.0f);
106 assert(std::isnormal(output_scale));
107 this->output_scale_ = output_scale;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700108 return *this;
109 }
110
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700111 inline float output_scale() const {
112 return this->output_scale_;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700113 }
114
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700115 inline GAvgPoolMicrokernelTester& output_zero_point(uint8_t output_zero_point) {
116 this->output_zero_point_ = output_zero_point;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700117 return *this;
118 }
119
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700120 inline uint8_t output_zero_point() const {
121 return this->output_zero_point_;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700122 }
123
124 inline GAvgPoolMicrokernelTester& qmin(uint8_t qmin) {
125 this->qmin_ = qmin;
126 return *this;
127 }
128
129 inline uint8_t qmin() const {
130 return this->qmin_;
131 }
132
133 inline GAvgPoolMicrokernelTester& qmax(uint8_t qmax) {
134 this->qmax_ = qmax;
135 return *this;
136 }
137
138 inline uint8_t qmax() const {
139 return this->qmax_;
140 }
141
142 inline GAvgPoolMicrokernelTester& iterations(size_t iterations) {
143 this->iterations_ = iterations;
144 return *this;
145 }
146
147 inline size_t iterations() const {
148 return this->iterations_;
149 }
150
Marat Dukhan08b7a972020-07-14 18:17:29 -0700151 void Test(xnn_qu8_gavgpool_minmax_unipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700152 std::random_device random_device;
153 auto rng = std::mt19937(random_device());
Marat Dukhan5ce30d92020-04-14 03:31:26 -0700154 auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700155
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700156 std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) +
157 (rows() - 1) * input_stride() + channels());
158 std::vector<uint8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint8_t));
159 std::vector<uint8_t> output(channels());
160 std::vector<uint8_t> output_ref(channels());
161 std::vector<float> output_fp(channels());
162 std::vector<int32_t> accumulators(channels());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700163 for (size_t iteration = 0; iteration < iterations(); iteration++) {
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700164 std::generate(input.begin(), input.end(), std::ref(u8rng));
165 std::fill(output.begin(), output.end(), 0xA5);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700166
Frank Barchard9f3a8432020-06-02 13:59:35 -0700167 // Prepare parameters.
Marat Dukhan08b7a972020-07-14 18:17:29 -0700168 union xnn_qu8_avgpool_params quantization_params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700169 switch (variant) {
170 case Variant::Native:
Marat Dukhan08b7a972020-07-14 18:17:29 -0700171 quantization_params = xnn_init_qu8_avgpool_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700172 -int32_t(input_zero_point()) * int32_t(rows()),
173 input_scale() / (output_scale() * float(rows())),
174 output_zero_point(), qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700175 break;
176 case Variant::Scalar:
Marat Dukhan08b7a972020-07-14 18:17:29 -0700177 quantization_params = xnn_init_scalar_qu8_avgpool_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700178 -int32_t(input_zero_point()) * int32_t(rows()),
179 input_scale() / (output_scale() * float(rows())),
180 output_zero_point(), qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700181 break;
182 }
Marat Dukhan08b7a972020-07-14 18:17:29 -0700183 const union xnn_qu8_avgpool_params scalar_quantization_params =
184 xnn_init_scalar_qu8_avgpool_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700185 -int32_t(input_zero_point()) * int32_t(rows()),
186 input_scale() / (output_scale() * float(rows())),
187 output_zero_point(), qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700188
189 // Compute reference results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700190 for (size_t c = 0; c < channels(); c++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700191 int32_t acc = scalar_quantization_params.scalar.bias;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700192 for (size_t n = 0; n < rows(); n++) {
193 acc += input[n * input_stride() + c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700194 }
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700195 accumulators[c] = acc;
Marat Dukhan5b69f8b2020-07-24 15:26:48 -0700196 output_ref[c] = xnn_qu8_quantize_avgpool(acc, scalar_quantization_params);
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700197 output_fp[c] = float(acc) * (input_scale() / (output_scale() * float(rows()))) + float(output_zero_point());
198 output_fp[c] = std::min<float>(output_fp[c], float(qmax()));
199 output_fp[c] = std::max<float>(output_fp[c], float(qmin()));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700200 }
201
202 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700203 gavgpool_minmax(rows(), channels(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700204 input.data(), input_stride() * sizeof(uint8_t),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700205 zero.data(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700206 output.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700207 &quantization_params);
208
209 // Verify results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700210 for (size_t c = 0; c < channels(); c++) {
211 ASSERT_LE(uint32_t(output[c]), uint32_t(qmax()))
212 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
213 ASSERT_GE(uint32_t(output[c]), uint32_t(qmin()))
214 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
215 ASSERT_NEAR(float(int32_t(output[c])), output_fp[c], 0.5f)
216 << "at position " << c << ", rows = " << rows() << ", channels = " << channels()
217 << ", acc = " << accumulators[c];
218 ASSERT_EQ(uint32_t(output_ref[c]), uint32_t(output[c]))
219 << "at position " << c << ", rows = " << rows() << ", channels = " << channels()
220 << ", acc = " << accumulators[c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700221 }
222 }
223 }
224
Marat Dukhan159688f2020-08-06 10:34:29 -0700225 void Test(xnn_qu8_gavgpool_minmax_multipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
226 std::random_device random_device;
227 auto rng = std::mt19937(random_device());
228 auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng);
229
230 std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) +
231 (rows() - 1) * input_stride() + channels());
232 std::vector<int32_t, AlignedAllocator<int32_t, 64>> buffer(channels() + XNN_EXTRA_BYTES / sizeof(uint8_t));
233 std::vector<uint8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint8_t));
234 std::vector<uint8_t> output(channels());
235 std::vector<uint8_t> output_ref(channels());
236 std::vector<float> output_fp(channels());
237 std::vector<int32_t> accumulators(channels());
238 for (size_t iteration = 0; iteration < iterations(); iteration++) {
239 std::generate(input.begin(), input.end(), std::ref(u8rng));
240 std::fill(output.begin(), output.end(), 0xA5);
241
242 // Prepare parameters.
243 union xnn_qu8_avgpool_params quantization_params = { };
244 switch (variant) {
245 case Variant::Native:
246 quantization_params = xnn_init_qu8_avgpool_params(
247 -int32_t(input_zero_point()) * int32_t(rows()),
248 input_scale() / (output_scale() * float(rows())),
249 output_zero_point(), qmin(), qmax());
250 break;
251 case Variant::Scalar:
252 quantization_params = xnn_init_scalar_qu8_avgpool_params(
253 -int32_t(input_zero_point()) * int32_t(rows()),
254 input_scale() / (output_scale() * float(rows())),
255 output_zero_point(), qmin(), qmax());
256 break;
257 }
258 const union xnn_qu8_avgpool_params scalar_quantization_params =
259 xnn_init_scalar_qu8_avgpool_params(
260 -int32_t(input_zero_point()) * int32_t(rows()),
261 input_scale() / (output_scale() * float(rows())),
262 output_zero_point(), qmin(), qmax());
263
264 // Compute reference results.
265 for (size_t c = 0; c < channels(); c++) {
266 int32_t acc = scalar_quantization_params.scalar.bias;
267 for (size_t n = 0; n < rows(); n++) {
268 acc += input[n * input_stride() + c];
269 }
270
271 accumulators[c] = acc;
272 output_ref[c] = xnn_qu8_quantize_avgpool(acc, scalar_quantization_params);
273 output_fp[c] = float(acc) * (input_scale() / (output_scale() * float(rows()))) + float(output_zero_point());
274 output_fp[c] = std::min<float>(output_fp[c], float(qmax()));
275 output_fp[c] = std::max<float>(output_fp[c], float(qmin()));
276 }
277
278 // Call optimized micro-kernel.
279 gavgpool_minmax(rows(), channels(),
280 input.data(), input_stride() * sizeof(uint8_t),
281 zero.data(),
282 buffer.data(),
283 output.data(),
284 &quantization_params);
285
286 // Verify results.
287 for (size_t c = 0; c < channels(); c++) {
288 ASSERT_LE(uint32_t(output[c]), uint32_t(qmax()))
289 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
290 ASSERT_GE(uint32_t(output[c]), uint32_t(qmin()))
291 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
292 ASSERT_NEAR(float(int32_t(output[c])), output_fp[c], 0.5f)
293 << "at position " << c << ", rows = " << rows() << ", channels = " << channels()
294 << ", acc = " << accumulators[c];
295 ASSERT_EQ(uint32_t(output_ref[c]), uint32_t(output[c]))
296 << "at position " << c << ", rows = " << rows() << ", channels = " << channels()
297 << ", acc = " << accumulators[c];
298 }
299 }
300 }
301
Marat Dukhan4ed53f42020-08-06 01:12:55 -0700302 void Test(xnn_qs8_gavgpool_minmax_unipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
303 std::random_device random_device;
304 auto rng = std::mt19937(random_device());
305 auto i8rng = std::bind(
306 std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()), rng);
307
308 std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) +
309 (rows() - 1) * input_stride() + channels());
310 std::vector<int8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(int8_t));
311 std::vector<int8_t> output(channels());
312 std::vector<int8_t> output_ref(channels());
313 std::vector<float> output_fp(channels());
314 std::vector<int32_t> accumulators(channels());
315 for (size_t iteration = 0; iteration < iterations(); iteration++) {
316 std::generate(input.begin(), input.end(), std::ref(i8rng));
317 std::fill(output.begin(), output.end(), 0xA5);
318
319 // Prepare parameters.
320 union xnn_qs8_avgpool_params quantization_params = { };
321 switch (variant) {
322 case Variant::Native:
323 quantization_params = xnn_init_qs8_avgpool_params(
324 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
325 input_scale() / (output_scale() * float(rows())),
326 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
327 break;
328 case Variant::Scalar:
329 quantization_params = xnn_init_scalar_qs8_avgpool_params(
330 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
331 input_scale() / (output_scale() * float(rows())),
332 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
333 break;
334 }
335 const union xnn_qs8_avgpool_params scalar_quantization_params =
336 xnn_init_scalar_qs8_avgpool_params(
337 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
338 input_scale() / (output_scale() * float(rows())),
339 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
340
341 // Compute reference results.
342 for (size_t c = 0; c < channels(); c++) {
343 int32_t acc = scalar_quantization_params.scalar.bias;
344 for (size_t n = 0; n < rows(); n++) {
345 acc += input[n * input_stride() + c];
346 }
347 accumulators[c] = acc;
348 output_ref[c] = xnn_qs8_quantize_avgpool(acc, scalar_quantization_params);
349 output_fp[c] = float(acc) * (input_scale() / (output_scale() * float(rows()))) + float(output_zero_point() - 0x80);
350 output_fp[c] = std::min<float>(output_fp[c], float(qmax() - 0x80));
351 output_fp[c] = std::max<float>(output_fp[c], float(qmin() - 0x80));
352 }
353
354 // Call optimized micro-kernel.
355 gavgpool_minmax(rows(), channels(),
356 input.data(), input_stride() * sizeof(int8_t),
357 zero.data(),
358 output.data(),
359 &quantization_params);
360
361 // Verify results.
362 for (size_t c = 0; c < channels(); c++) {
363 ASSERT_LE(int32_t(output[c]), int32_t(qmax() - 0x80))
364 << "at channel " << c << " / " << channels() << ", rows = " << rows();
365 ASSERT_GE(int32_t(output[c]), int32_t(qmin() - 0x80))
366 << "at channel " << c << " / " << channels() << ", rows = " << rows();
367 ASSERT_NEAR(float(int32_t(output[c])), output_fp[c], 0.5f)
368 << "at channel " << c << " / " << channels() << ", rows = " << rows()
369 << ", accumulator = " << accumulators[c];
370 ASSERT_EQ(int32_t(output_ref[c]), int32_t(output[c]))
371 << "at channel " << c << " / " << channels() << ", rows = " << rows()
372 << ", accumulator = " << accumulators[c];
373 }
374 }
375 }
376
Marat Dukhan159688f2020-08-06 10:34:29 -0700377 void Test(xnn_qs8_gavgpool_minmax_multipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700378 std::random_device random_device;
379 auto rng = std::mt19937(random_device());
Marat Dukhan159688f2020-08-06 10:34:29 -0700380 auto i8rng = std::bind(
381 std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()), rng);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700382
Marat Dukhan159688f2020-08-06 10:34:29 -0700383 std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) +
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700384 (rows() - 1) * input_stride() + channels());
Marat Dukhan159688f2020-08-06 10:34:29 -0700385 std::vector<int32_t, AlignedAllocator<int32_t, 64>> buffer(channels() + XNN_EXTRA_BYTES / sizeof(int8_t));
386 std::vector<int8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(int8_t));
387 std::vector<int8_t> output(channels());
388 std::vector<int8_t> output_ref(channels());
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700389 std::vector<float> output_fp(channels());
390 std::vector<int32_t> accumulators(channels());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700391 for (size_t iteration = 0; iteration < iterations(); iteration++) {
Marat Dukhan159688f2020-08-06 10:34:29 -0700392 std::generate(input.begin(), input.end(), std::ref(i8rng));
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700393 std::fill(output.begin(), output.end(), 0xA5);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700394
Frank Barchard9f3a8432020-06-02 13:59:35 -0700395 // Prepare parameters.
Marat Dukhan159688f2020-08-06 10:34:29 -0700396 union xnn_qs8_avgpool_params quantization_params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700397 switch (variant) {
398 case Variant::Native:
Marat Dukhan159688f2020-08-06 10:34:29 -0700399 quantization_params = xnn_init_qs8_avgpool_params(
400 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700401 input_scale() / (output_scale() * float(rows())),
Marat Dukhan159688f2020-08-06 10:34:29 -0700402 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700403 break;
404 case Variant::Scalar:
Marat Dukhan159688f2020-08-06 10:34:29 -0700405 quantization_params = xnn_init_scalar_qs8_avgpool_params(
406 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700407 input_scale() / (output_scale() * float(rows())),
Marat Dukhan159688f2020-08-06 10:34:29 -0700408 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700409 break;
410 }
Marat Dukhan159688f2020-08-06 10:34:29 -0700411 const union xnn_qs8_avgpool_params scalar_quantization_params =
412 xnn_init_scalar_qs8_avgpool_params(
413 -int32_t(input_zero_point() - 0x80) * int32_t(rows()),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700414 input_scale() / (output_scale() * float(rows())),
Marat Dukhan159688f2020-08-06 10:34:29 -0700415 int8_t(output_zero_point() - 0x80), int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700416
417 // Compute reference results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700418 for (size_t c = 0; c < channels(); c++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700419 int32_t acc = scalar_quantization_params.scalar.bias;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700420 for (size_t n = 0; n < rows(); n++) {
421 acc += input[n * input_stride() + c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700422 }
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700423 accumulators[c] = acc;
Marat Dukhan159688f2020-08-06 10:34:29 -0700424 output_ref[c] = xnn_qs8_quantize_avgpool(acc, scalar_quantization_params);
425 output_fp[c] = float(acc) * (input_scale() / (output_scale() * float(rows()))) + float(output_zero_point() - 0x80);
426 output_fp[c] = std::min<float>(output_fp[c], float(qmax() - 0x80));
427 output_fp[c] = std::max<float>(output_fp[c], float(qmin() - 0x80));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700428 }
429
430 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700431 gavgpool_minmax(rows(), channels(),
Marat Dukhan159688f2020-08-06 10:34:29 -0700432 input.data(), input_stride() * sizeof(int8_t),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700433 zero.data(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700434 buffer.data(),
435 output.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700436 &quantization_params);
437
438 // Verify results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700439 for (size_t c = 0; c < channels(); c++) {
Marat Dukhan159688f2020-08-06 10:34:29 -0700440 ASSERT_LE(int32_t(output[c]), int32_t(qmax() - 0x80))
441 << "at channel " << c << " / " << channels() << ", rows = " << rows();
442 ASSERT_GE(int32_t(output[c]), int32_t(qmin() - 0x80))
443 << "at channel " << c << " / " << channels() << ", rows = " << rows();
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700444 ASSERT_NEAR(float(int32_t(output[c])), output_fp[c], 0.5f)
Marat Dukhan159688f2020-08-06 10:34:29 -0700445 << "at channel " << c << " / " << channels() << ", rows = " << rows()
446 << ", accumulator = " << accumulators[c];
447 ASSERT_EQ(int32_t(output_ref[c]), int32_t(output[c]))
448 << "at channel " << c << " / " << channels() << ", rows = " << rows()
449 << ", accumulator = " << accumulators[c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700450 }
451 }
452 }
453
Frank Barchard0bb49a72020-06-04 11:35:11 -0700454 void Test(xnn_f16_gavgpool_minmax_unipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
455 std::random_device random_device;
456 auto rng = std::mt19937(random_device());
457 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
458 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
459
460 std::vector<uint16_t> input((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
461 std::vector<uint16_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
462 std::vector<uint16_t> output(channels());
463 std::vector<float> output_ref(channels());
464
465 std::fill(zero.begin(), zero.end(), 0);
466 for (size_t iteration = 0; iteration < iterations(); iteration++) {
467 std::generate(input.begin(), input.end(), std::ref(f16rng));
468 std::fill(output.begin(), output.end(), UINT16_C(0x7E00) /* NaN */);
469
470 // Compute reference results, without clamping.
471 for (size_t c = 0; c < channels(); c++) {
472 float acc = 0.0f;
473 for (size_t n = 0; n < rows(); n++) {
474 acc += fp16_ieee_to_fp32_value(input[n * input_stride() + c]);
475 }
476 output_ref[c] = acc / float(rows());
477 }
478
479 // Compute clamping parameters.
480 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
481 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
482 const float accumulated_range = accumulated_max - accumulated_min;
483 const float output_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + float(qmin()) / 255.0f * accumulated_range));
484 const float output_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range));
485
486 // Clamp reference results.
487 for (float& output_values : output_ref) {
488 output_values = std::max(std::min(output_values, output_max), output_min);
489 }
490
491 // Prepare parameters.
492 xnn_f16_scaleminmax_params params = xnn_init_f16_scaleminmax_params(
493 fp16_ieee_from_fp32_value(1.0f / float(rows())),
494 fp16_ieee_from_fp32_value(output_min),
495 fp16_ieee_from_fp32_value(output_max));
496
497 // Call optimized micro-kernel.
498 gavgpool_minmax(rows(), channels(),
499 input.data(), input_stride() * sizeof(uint16_t),
500 zero.data(),
501 output.data(),
502 &params);
503
504 // Verify results.
505 for (size_t c = 0; c < channels(); c++) {
506 ASSERT_LE(fp16_ieee_to_fp32_value(output[c]), output_max)
507 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
508 ASSERT_GE(fp16_ieee_to_fp32_value(output[c]), output_min)
509 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
Frank Barchard2b9d29b2020-09-17 12:03:39 -0700510 ASSERT_NEAR(fp16_ieee_to_fp32_value(output[c]), output_ref[c], std::max(1.0e-4f, std::abs(output_ref[c]) * 1.0e-2f))
Frank Barchard0bb49a72020-06-04 11:35:11 -0700511 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
512 }
513 }
514 }
515
516 void Test(xnn_f16_gavgpool_minmax_multipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
517 std::random_device random_device;
518 auto rng = std::mt19937(random_device());
519 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
520 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
521
522 std::vector<uint16_t> input((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
523 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> buffer(channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
524 std::vector<uint16_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
525 std::vector<uint16_t> output(channels());
526 std::vector<float> output_ref(channels());
527 for (size_t iteration = 0; iteration < iterations(); iteration++) {
528 std::generate(input.begin(), input.end(), std::ref(f16rng));
529 std::fill(output.begin(), output.end(), UINT16_C(0x7E00) /* NaN */);
530
531 // Compute reference results, without clamping.
532 for (size_t c = 0; c < channels(); c++) {
533 float acc = 0.0f;
534 for (size_t n = 0; n < rows(); n++) {
535 acc += fp16_ieee_to_fp32_value(input[n * input_stride() + c]);
536 }
537 output_ref[c] = acc / float(rows());
538 }
539
540 // Compute clamping parameters.
541 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
542 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
543 const float accumulated_range = accumulated_max - accumulated_min;
544 const float output_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + float(qmin()) / 255.0f * accumulated_range));
545 const float output_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range));
546
547 // Prepare parameters.
548 xnn_f16_scaleminmax_params params = xnn_init_f16_scaleminmax_params(
549 fp16_ieee_from_fp32_value(1.0f / float(rows())),
550 fp16_ieee_from_fp32_value(output_min),
551 fp16_ieee_from_fp32_value(output_max));
552
553 // Clamp reference results.
554 for (float& output_values : output_ref) {
555 output_values = std::max(std::min(output_values, output_max), output_min);
556 }
557
558 // Call optimized micro-kernel.
559 gavgpool_minmax(rows(), channels(),
560 input.data(), input_stride() * sizeof(uint16_t),
561 zero.data(),
562 buffer.data(),
563 output.data(),
564 &params);
565
566 // Verify results.
567 for (size_t c = 0; c < channels(); c++) {
568 ASSERT_LE(fp16_ieee_to_fp32_value(output[c]), output_max)
569 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
570 ASSERT_GE(fp16_ieee_to_fp32_value(output[c]), output_min)
571 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
572 ASSERT_NEAR(fp16_ieee_to_fp32_value(output[c]), output_ref[c], std::abs(output_ref[c]) * 1.0e-0f)
573 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
574 }
575 }
576 }
577
Marat Dukhan99936602020-04-11 16:47:01 -0700578 void Test(xnn_f32_gavgpool_minmax_unipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700579 std::random_device random_device;
580 auto rng = std::mt19937(random_device());
581 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
582
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700583 std::vector<float> input((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float));
584 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
585 std::vector<float> output(channels());
586 std::vector<float> output_ref(channels());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700587
588 std::fill(zero.begin(), zero.end(), 0.0f);
589 for (size_t iteration = 0; iteration < iterations(); iteration++) {
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700590 std::generate(input.begin(), input.end(), std::ref(f32rng));
591 std::fill(output.begin(), output.end(), std::nanf(""));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700592
593 // Compute reference results, without clamping.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700594 for (size_t c = 0; c < channels(); c++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700595 float acc = 0.0f;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700596 for (size_t n = 0; n < rows(); n++) {
597 acc += input[n * input_stride() + c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700598 }
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700599 output_ref[c] = acc / float(rows());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700600 }
601
602 // Compute clamping parameters.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700603 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
604 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700605 const float accumulated_range = accumulated_max - accumulated_min;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700606 const float output_min = accumulated_min + float(qmin()) / 255.0f * accumulated_range;
607 const float output_max = accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700608
609 // Clamp reference results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700610 for (float& output_values : output_ref) {
611 output_values = std::max(std::min(output_values, output_max), output_min);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700612 }
613
Frank Barchard9f3a8432020-06-02 13:59:35 -0700614 // Prepare parameters.
Marat Dukhan8452ff52020-04-08 20:44:58 -0700615 union xnn_f32_scaleminmax_params params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700616 switch (variant) {
617 case Variant::Native:
Marat Dukhan8452ff52020-04-08 20:44:58 -0700618 params = xnn_init_f32_scaleminmax_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700619 1.0f / float(rows()), output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700620 break;
621 case Variant::Scalar:
Marat Dukhan8452ff52020-04-08 20:44:58 -0700622 params = xnn_init_scalar_f32_scaleminmax_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700623 1.0f / float(rows()), output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700624 break;
625 }
626
627 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700628 gavgpool_minmax(rows(), channels(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700629 input.data(), input_stride() * sizeof(float),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700630 zero.data(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700631 output.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700632 &params);
633
634 // Verify results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700635 for (size_t c = 0; c < channels(); c++) {
636 ASSERT_LE(output[c], output_max)
637 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
638 ASSERT_GE(output[c], output_min)
639 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
640 ASSERT_NEAR(output[c], output_ref[c], std::abs(output_ref[c]) * 1.0e-6f)
641 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700642 }
643 }
644 }
645
Marat Dukhan99936602020-04-11 16:47:01 -0700646 void Test(xnn_f32_gavgpool_minmax_multipass_ukernel_function gavgpool_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700647 std::random_device random_device;
648 auto rng = std::mt19937(random_device());
649 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
650
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700651 std::vector<float> input((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float));
652 std::vector<float, AlignedAllocator<float, 64>> buffer(channels() + XNN_EXTRA_BYTES / sizeof(float));
653 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
654 std::vector<float> output(channels());
655 std::vector<float> output_ref(channels());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700656 for (size_t iteration = 0; iteration < iterations(); iteration++) {
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700657 std::generate(input.begin(), input.end(), std::ref(f32rng));
658 std::fill(output.begin(), output.end(), std::nanf(""));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700659
660 // Compute reference results, without clamping.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700661 for (size_t c = 0; c < channels(); c++) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700662 float acc = 0.0f;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700663 for (size_t n = 0; n < rows(); n++) {
664 acc += input[n * input_stride() + c];
XNNPACK Teamb455b122019-09-27 18:10:33 -0700665 }
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700666 output_ref[c] = acc / float(rows());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700667 }
668
669 // Compute clamping parameters.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700670 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
671 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700672 const float accumulated_range = accumulated_max - accumulated_min;
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700673 const float output_min = accumulated_min + float(qmin()) / 255.0f * accumulated_range;
674 const float output_max = accumulated_max - float(255 - qmax()) / 255.0f * accumulated_range;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700675
Frank Barchard9f3a8432020-06-02 13:59:35 -0700676 // Prepare parameters.
Marat Dukhan8452ff52020-04-08 20:44:58 -0700677 union xnn_f32_scaleminmax_params params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700678 switch (variant) {
679 case Variant::Native:
Marat Dukhan8452ff52020-04-08 20:44:58 -0700680 params = xnn_init_f32_scaleminmax_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700681 1.0f / float(rows()), output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700682 break;
683 case Variant::Scalar:
Marat Dukhan8452ff52020-04-08 20:44:58 -0700684 params = xnn_init_scalar_f32_scaleminmax_params(
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700685 1.0f / float(rows()), output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700686 break;
687 }
688
689 // Clamp reference results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700690 for (float& output_values : output_ref) {
691 output_values = std::max(std::min(output_values, output_max), output_min);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700692 }
693
694 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700695 gavgpool_minmax(rows(), channels(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700696 input.data(), input_stride() * sizeof(float),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700697 zero.data(),
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700698 buffer.data(),
699 output.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700700 &params);
701
702 // Verify results.
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700703 for (size_t c = 0; c < channels(); c++) {
704 ASSERT_LE(output[c], output_max)
705 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
706 ASSERT_GE(output[c], output_min)
707 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
708 ASSERT_NEAR(output[c], output_ref[c], std::abs(output_ref[c]) * 1.0e-6f)
709 << "at position " << c << ", rows = " << rows() << ", channels = " << channels();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700710 }
711 }
712 }
713
714 private:
Marat Dukhana63a6fc2020-03-10 06:12:48 -0700715 size_t rows_{1};
716 size_t channels_{1};
717 size_t channel_tile_{1};
718 size_t input_stride_{0};
719 float input_scale_{1.25f};
720 float output_scale_{0.75f};
721 uint8_t input_zero_point_{121};
722 uint8_t output_zero_point_{133};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700723 uint8_t qmin_{0};
724 uint8_t qmax_{255};
725 size_t iterations_{15};
726};