blob: 8bc8e9e707f83ee0a8d1c25e0b473438162c3907 [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 Barchard5a599a62020-06-04 20:12:44 -070023#include <fp16.h>
24
XNNPACK Teamb455b122019-09-27 18:10:33 -070025#include <xnnpack.h>
26#include <xnnpack/AlignedAllocator.h>
27#include <xnnpack/pack.h>
Marat Dukhaneeaa7bd2019-10-25 17:31:25 -070028#include <xnnpack/params-init.h>
XNNPACK Teamb455b122019-09-27 18:10:33 -070029#include <xnnpack/params.h>
30#include <xnnpack/requantization.h>
31
32
33class DWConvMicrokernelTester {
34 public:
35 enum class Variant {
36 Native,
37 Scalar,
38 };
39
40 inline DWConvMicrokernelTester& width(uint32_t width) {
41 assert(width >= 1);
42 this->width_ = width;
43 return *this;
44 }
45
46 inline uint32_t width() const {
47 return this->width_;
48 }
49
50 inline DWConvMicrokernelTester& step(uint32_t step) {
51 assert(step >= 1);
52 this->step_ = step;
53 return *this;
54 }
55
56 inline uint32_t step() const {
57 return this->step_;
58 }
59
60 inline DWConvMicrokernelTester& channels(uint32_t channels) {
61 assert(channels >= 1);
62 this->channels_ = channels;
63 return *this;
64 }
65
66 inline uint32_t channels() const {
67 return this->channels_;
68 }
69
70 inline DWConvMicrokernelTester& cr(uint32_t cr) {
71 assert(cr != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -070072 this->cr_ = cr;
73 return *this;
74 }
75
76 inline uint32_t cr() const {
77 return this->cr_;
78 }
79
80 inline DWConvMicrokernelTester& kr(uint32_t kr) {
81 assert(kr != 0);
82 this->kr_ = kr;
83 return *this;
84 }
85
86 inline uint32_t kr() const {
87 return this->kr_;
88 }
89
90 inline uint32_t packed_channels() const {
91 return (channels() / cr() + !!(channels() % cr())) * cr();
92 }
93
94 inline DWConvMicrokernelTester& output_stride(uint32_t output_stride) {
95 assert(output_stride != 0);
96 this->output_stride_ = output_stride;
97 return *this;
98 }
99
100 inline uint32_t output_stride() const {
101 if (this->output_stride_ == 0) {
102 return channels();
103 } else {
104 assert(this->output_stride_ >= channels());
105 return this->output_stride_;
106 }
107 }
108
109 inline DWConvMicrokernelTester& input_zero_point(uint8_t input_zero_point) {
110 this->input_zero_point_ = input_zero_point;
111 return *this;
112 }
113
114 inline uint8_t input_zero_point() const {
115 return this->input_zero_point_;
116 }
117
118 inline DWConvMicrokernelTester& kernel_zero_point(uint8_t kernel_zero_point) {
119 this->kernel_zero_point_ = kernel_zero_point;
120 return *this;
121 }
122
123 inline uint8_t kernel_zero_point() const {
124 return this->kernel_zero_point_;
125 }
126
127 inline DWConvMicrokernelTester& qmin(uint8_t qmin) {
128 this->qmin_ = qmin;
129 return *this;
130 }
131
132 inline uint8_t qmin() const {
133 return this->qmin_;
134 }
135
136 inline DWConvMicrokernelTester& qmax(uint8_t qmax) {
137 this->qmax_ = qmax;
138 return *this;
139 }
140
141 inline uint8_t qmax() const {
142 return this->qmax_;
143 }
144
Frank Barchardd5360722020-05-17 16:10:36 -0700145 inline DWConvMicrokernelTester& input_offset(size_t input_offset) {
146 this->input_offset_ = input_offset;
147 return *this;
148 }
149
150 inline size_t input_offset() const {
151 return this->input_offset_;
152 }
153
154 inline DWConvMicrokernelTester& zero_index(size_t zero_index) {
155 this->zero_index_ = zero_index;
156 return *this;
157 }
158
159 inline size_t zero_index() const {
160 return this->zero_index_;
161 }
162
XNNPACK Teamb455b122019-09-27 18:10:33 -0700163 inline DWConvMicrokernelTester& iterations(size_t iterations) {
164 this->iterations_ = iterations;
165 return *this;
166 }
167
168 inline size_t iterations() const {
169 return this->iterations_;
170 }
171
Marat Dukhan08b7a972020-07-14 18:17:29 -0700172 void Test(xnn_qu8_dwconv_minmax_unipass_ukernel_function dwconv_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700173 std::random_device random_device;
174 auto rng = std::mt19937(random_device());
Marat Dukhanecd83112020-08-03 21:50:28 -0700175 auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), rng);
Marat Dukhan5ce30d92020-04-14 03:31:26 -0700176 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 -0700177
178 std::vector<const uint8_t*> indirection((width() - 1) * step() + kr());
179 std::vector<uint8_t> input(XNN_EXTRA_BYTES / sizeof(uint8_t) + indirection.size() * channels());
180 std::vector<uint8_t> kernel(channels() * kr());
181 std::vector<int32_t> bias(channels());
Marat Dukhan9594db02019-12-05 14:32:37 -0800182 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> packed_weights((kr() + sizeof(int32_t) / sizeof(uint8_t)) * packed_channels());
Frank Barchardd5360722020-05-17 16:10:36 -0700183 std::vector<uint8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint8_t));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700184 std::vector<uint8_t> output((width() - 1) * output_stride() + channels());
185 std::vector<int32_t> accumulators(width() * channels());
186 std::vector<uint8_t> output_ref(width() * channels());
187
188 for (size_t iteration = 0; iteration < iterations(); iteration++) {
189 do {
190 std::generate(input.begin(), input.end(), std::ref(u8rng));
191 } while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
192 do {
193 std::generate(kernel.begin(), kernel.end(), std::ref(u8rng));
194 } while (kernel.size() > 1 && *std::max_element(kernel.cbegin(), kernel.cend()) == *std::min_element(kernel.cbegin(), kernel.cend()));
Marat Dukhanecd83112020-08-03 21:50:28 -0700195 std::generate(bias.begin(), bias.end(), std::ref(i32rng));
Frank Barchardd5360722020-05-17 16:10:36 -0700196 std::fill(zero.begin(), zero.end(), input_zero_point());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700197 std::fill(output.begin(), output.end(), 0xA5);
198
199 std::fill(packed_weights.begin(), packed_weights.end(), 0);
Marat Dukhan1827af12020-07-16 10:58:55 -0700200 const xnn_qu8_packing_params packing_params = { input_zero_point(), kernel_zero_point() };
Marat Dukhan08b7a972020-07-14 18:17:29 -0700201 xnn_pack_qu8_dwconv_ghw_w(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700202 kr(), 1, channels(), cr(),
Marat Dukhanb42f8662020-07-06 20:46:13 -0700203 kernel.data(), bias.data(), packed_weights.data(), &packing_params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700204 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700205 indirection[i] = input.data() + i * channels() - input_offset();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700206 }
207 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700208 if (zero_index() != SIZE_MAX) {
209 for (size_t i = 0; i < indirection.size(); i += kr()) {
210 indirection[i + zero_index()] = zero.data();
211 }
212 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700213
214 // Compute reference results, without renormalization.
215 for (size_t x = 0; x < width(); x++) {
216 for (size_t c = 0; c < channels(); c++) {
217 float acc = bias[c];
218 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700219 if (indirection[x * step() + k] != zero.data()) {
220 acc +=
221 (int32_t(indirection[x * step() + k][c + input_offset()]) - int32_t(input_zero_point())) *
222 (int32_t(kernel[c * kr() + k]) - int32_t(kernel_zero_point()));
223 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700224 }
225 accumulators[x * channels() + c] = acc;
226 }
227 }
228
229 // Compute renormalization parameters.
230 const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend());
231 const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend());
232 const uint32_t accumulated_range = uint32_t(accumulated_max) - uint32_t(accumulated_min);
233 const double output_scale = accumulated_range >= 256 ? double(accumulated_range) / 255.0 : 1.00001;
234 const uint8_t output_zero_point = uint8_t(std::max(std::min(
235 lrint(127.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale),
236 long(std::numeric_limits<uint8_t>::max())), long(std::numeric_limits<uint8_t>::min())));
237
Frank Barchard9f3a8432020-06-02 13:59:35 -0700238 // Prepare parameters.
XNNPACK Teamb455b122019-09-27 18:10:33 -0700239 const float requantization_scale = 1.0f / float(output_scale);
Marat Dukhan08b7a972020-07-14 18:17:29 -0700240 union xnn_qu8_gemm_params quantization_params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700241 switch (variant) {
242 case Variant::Native:
Marat Dukhan08b7a972020-07-14 18:17:29 -0700243 quantization_params = xnn_init_qu8_gemm_params(
Marat Dukhan91992462020-07-30 00:06:34 -0700244 kernel_zero_point(), requantization_scale, output_zero_point, qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700245 break;
246 case Variant::Scalar:
Marat Dukhan08b7a972020-07-14 18:17:29 -0700247 quantization_params = xnn_init_scalar_qu8_gemm_params(
Marat Dukhan91992462020-07-30 00:06:34 -0700248 kernel_zero_point(), requantization_scale, output_zero_point, qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700249 break;
250 }
Marat Dukhanec88e272020-07-30 15:02:09 -0700251 const union xnn_qu8_requantization_params scalar_requantization_params =
252 xnn_init_scalar_qu8_requantization_params(requantization_scale, output_zero_point, qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700253
254 // Renormalize reference results.
255 for (size_t x = 0; x < width(); x++) {
256 for (size_t c = 0; c < channels(); c++) {
Marat Dukhan5b69f8b2020-07-24 15:26:48 -0700257 output_ref[x * channels() + c] = xnn_qu8_requantize_q31(accumulators[x * channels() + c], scalar_requantization_params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700258 }
259 }
260
261 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700262 dwconv_minmax(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700263 channels(), width(),
264 indirection.data(), packed_weights.data(), output.data(),
265 step() * sizeof(void*),
266 (output_stride() - channels()) * sizeof(uint8_t),
Frank Barchardd5360722020-05-17 16:10:36 -0700267 input_offset() * sizeof(uint8_t), zero.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700268 &quantization_params);
269
270 // Verify results.
271 for (size_t x = 0; x < width(); x++) {
272 for (size_t c = 0; c < channels(); c++) {
273 ASSERT_GE(uint32_t(output[x * output_stride() + c]), uint32_t(qmin()))
274 << "x = " << x << ", channel = " << c;
275 ASSERT_LE(uint32_t(output[x * output_stride() + c]), uint32_t(qmax()))
276 << "x = " << x << ", channel = " << c;
277 ASSERT_EQ(uint32_t(output[x * output_stride() + c]), uint32_t(output_ref[x * channels() + c]))
278 << "x = " << x << ", channel = " << c << ", accumulator = " << accumulators[x * channels() + c];
279 }
280 }
281 }
282 }
283
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700284 void Test(xnn_qs8_dwconv_minmax_unipass_ukernel_function dwconv_minmax, Variant variant = Variant::Native) const {
285 std::random_device random_device;
286 auto rng = std::mt19937(random_device());
287 auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), rng);
288 auto i8rng = std::bind(
289 std::uniform_int_distribution<uint32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()), rng);
290
291 std::vector<const int8_t*> indirection((width() - 1) * step() + kr());
292 std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) + indirection.size() * channels());
293 std::vector<int8_t> kernel(channels() * kr());
294 std::vector<int32_t> bias(channels());
295 std::vector<int8_t, AlignedAllocator<int8_t, 64>> packed_weights((kr() + sizeof(int32_t) / sizeof(int8_t)) * packed_channels());
296 std::vector<int8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(int8_t));
297 std::vector<int8_t> output((width() - 1) * output_stride() + channels());
298 std::vector<int32_t> accumulators(width() * channels());
299 std::vector<int8_t> output_ref(width() * channels());
300
301 for (size_t iteration = 0; iteration < iterations(); iteration++) {
302 do {
303 std::generate(input.begin(), input.end(), std::ref(i8rng));
304 } while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
305 do {
306 std::generate(kernel.begin(), kernel.end(), std::ref(i8rng));
307 } while (kernel.size() > 1 && *std::max_element(kernel.cbegin(), kernel.cend()) == *std::min_element(kernel.cbegin(), kernel.cend()));
308 std::generate(bias.begin(), bias.end(), std::ref(i32rng));
309 std::fill(zero.begin(), zero.end(), int8_t(input_zero_point() - 0x80));
310 std::fill(output.begin(), output.end(), 0xA5);
311
312 std::fill(packed_weights.begin(), packed_weights.end(), 0);
313 const xnn_qs8_packing_params packing_params = { int8_t(input_zero_point() - 0x80) };
314 xnn_pack_qs8_dwconv_ghw_w(
315 kr(), 1, channels(), cr(),
316 kernel.data(), bias.data(), packed_weights.data(), &packing_params);
317 for (size_t i = 0; i < indirection.size(); i++) {
318 indirection[i] = input.data() + i * channels() - input_offset();
319 }
320 std::shuffle(indirection.begin(), indirection.end(), rng);
321 if (zero_index() != SIZE_MAX) {
322 for (size_t i = 0; i < indirection.size(); i += kr()) {
323 indirection[i + zero_index()] = zero.data();
324 }
325 }
326
327 // Compute reference results, without renormalization.
328 for (size_t x = 0; x < width(); x++) {
329 for (size_t c = 0; c < channels(); c++) {
330 float acc = bias[c];
331 for (size_t k = 0; k < kr(); k++) {
332 if (indirection[x * step() + k] != zero.data()) {
333 acc +=
334 (int32_t(indirection[x * step() + k][c + input_offset()]) - int32_t(input_zero_point() - 0x80)) *
335 int32_t(kernel[c * kr() + k]);
336 }
337 }
338 accumulators[x * channels() + c] = acc;
339 }
340 }
341
342 // Compute renormalization parameters.
343 const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend());
344 const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend());
345 const uint32_t accumulated_range = uint32_t(accumulated_max) - uint32_t(accumulated_min);
346 const double output_scale = accumulated_range >= 256 ? double(accumulated_range) / 255.0 : 1.00001;
347 const int8_t output_zero_point = int8_t(std::max(std::min(
348 lrint(-0.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale),
349 long(std::numeric_limits<int8_t>::max())), long(std::numeric_limits<int8_t>::min())));
350
351 // Prepare parameters.
352 const float requantization_scale = 1.0f / float(output_scale);
353 union xnn_qs8_gemm_params quantization_params = { };
354 switch (variant) {
355 case Variant::Native:
356 quantization_params = xnn_init_qs8_gemm_params(
357 requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
358 break;
359 case Variant::Scalar:
360 quantization_params = xnn_init_scalar_qs8_gemm_params(
361 requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
362 break;
363 }
364 const union xnn_qs8_requantization_params scalar_requantization_params =
365 xnn_init_scalar_qs8_requantization_params(requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
366
367 // Renormalize reference results.
368 for (size_t x = 0; x < width(); x++) {
369 for (size_t c = 0; c < channels(); c++) {
370 output_ref[x * channels() + c] = xnn_qs8_requantize_q31(accumulators[x * channels() + c], scalar_requantization_params);
371 }
372 }
373
374 // Call optimized micro-kernel.
375 dwconv_minmax(
376 channels(), width(),
377 indirection.data(), packed_weights.data(), output.data(),
378 step() * sizeof(void*),
379 (output_stride() - channels()) * sizeof(int8_t),
380 input_offset() * sizeof(int8_t), zero.data(),
381 &quantization_params);
382
383 // Verify results.
384 for (size_t x = 0; x < width(); x++) {
385 for (size_t c = 0; c < channels(); c++) {
386 ASSERT_GE(int32_t(output[x * output_stride() + c]), int32_t(qmin()) - 0x80)
387 << "x = " << x << ", channel = " << c;
388 ASSERT_LE(int32_t(output[x * output_stride() + c]), int32_t(qmax()) - 0x80)
389 << "x = " << x << ", channel = " << c;
390 ASSERT_EQ(int32_t(output[x * output_stride() + c]), int32_t(output_ref[x * channels() + c]))
391 << "x = " << x << ", channel = " << c << ", accumulator = " << accumulators[x * channels() + c];
392 }
393 }
394 }
395 }
396
Frank Barchard5a599a62020-06-04 20:12:44 -0700397 void Test(xnn_f16_dwconv_minmax_unipass_ukernel_function dwconv_minmax, Variant variant = Variant::Native) const {
398 std::random_device random_device;
399 auto rng = std::mt19937(random_device());
400 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.1f, 1.0f), rng);
401 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
402
403 std::vector<const uint16_t*> indirection((width() - 1) * step() + kr());
404 std::vector<uint16_t> input(XNN_EXTRA_BYTES / sizeof(uint16_t) + indirection.size() * channels());
405 std::vector<uint16_t> kernel(channels() * kr());
406 std::vector<uint16_t> bias(channels());
407 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_weights((kr() + 1) * packed_channels());
408 std::vector<uint16_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
409 std::vector<uint16_t> output((width() - 1) * output_stride() + channels());
410 std::vector<float> output_ref(width() * channels());
411
412 for (size_t iteration = 0; iteration < iterations(); iteration++) {
413 std::generate(input.begin(), input.end(), std::ref(f16rng));
414 std::generate(kernel.begin(), kernel.end(), std::ref(f16rng));
415 std::generate(bias.begin(), bias.end(), std::ref(f16rng));
416 std::fill(zero.begin(), zero.end(), 0);
417 std::fill(output_ref.begin(), output_ref.end(), 0.0f);
418 std::fill(output.begin(), output.end(), UINT16_C(0x7E00) /* NaN */);
419
420 std::fill(packed_weights.begin(), packed_weights.end(), 0);
421 xnn_pack_f16_dwconv_ghw_w(
422 kr(), 1, channels(), cr(),
Marat Dukhanb42f8662020-07-06 20:46:13 -0700423 kernel.data(), bias.data(), packed_weights.data(), nullptr);
Frank Barchard5a599a62020-06-04 20:12:44 -0700424 for (size_t i = 0; i < indirection.size(); i++) {
425 indirection[i] = input.data() + i * channels() - input_offset();
426 }
427 std::shuffle(indirection.begin(), indirection.end(), rng);
428 if (zero_index() != SIZE_MAX) {
429 for (size_t i = 0; i < indirection.size(); i += kr()) {
430 indirection[i + zero_index()] = zero.data();
431 }
432 }
433
434 // Compute reference results, without clamping.
435 for (size_t x = 0; x < width(); x++) {
436 for (size_t c = 0; c < channels(); c++) {
437 float acc = fp16_ieee_to_fp32_value(bias[c]);
438 for (size_t k = 0; k < kr(); k++) {
439 if (indirection[x * step() + k] != zero.data()) {
440 acc += fp16_ieee_to_fp32_value(indirection[x * step() + k][c + input_offset()]) * fp16_ieee_to_fp32_value(kernel[c * kr() + k]);
441 }
442 }
443 output_ref[x * channels() + c] = acc;
444 }
445 }
446
447 // Compute clamping parameters.
448 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
449 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
450 const float accumulated_range = accumulated_max - accumulated_min;
451 const float output_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin())));
452 const float output_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax())));
453
454 // Prepare parameters.
455 xnn_f16_minmax_params params = xnn_init_f16_minmax_params(
456 fp16_ieee_from_fp32_value(output_min),
457 fp16_ieee_from_fp32_value(output_max));
458
459 // Clamp reference results.
460 for (float& output_val : output_ref) {
461 output_val = std::max(std::min(output_val, output_max), output_min);
462 }
463
464 // Call optimized micro-kernel.
465 dwconv_minmax(
466 channels(), width(),
467 reinterpret_cast<const void**>(indirection.data()), packed_weights.data(), output.data(),
468 step() * sizeof(void*),
469 (output_stride() - channels()) * sizeof(uint16_t),
470 input_offset() * sizeof(uint16_t), zero.data(),
471 &params);
472
473 // Verify results.
474 for (size_t x = 0; x < width(); x++) {
475 for (size_t c = 0; c < channels(); c++) {
476 ASSERT_GE(fp16_ieee_to_fp32_value(output[x * output_stride() + c]), output_min)
477 << "x = " << x << ", channel = " << c;
478 ASSERT_LE(fp16_ieee_to_fp32_value(output[x * output_stride() + c]), output_max)
479 << "x = " << x << ", channel = " << c;
480 ASSERT_NEAR(
481 output_ref[x * channels() + c],
482 fp16_ieee_to_fp32_value(output[x * output_stride() + c]),
483 std::abs(output_ref[x * channels() + c]) * 1.0e-2)
484 << "x = " << x << ", channel = " << c;
485 }
486 }
487 }
488 }
489
Marat Dukhan163a7e62020-04-09 04:19:26 -0700490 void Test(xnn_f32_dwconv_unipass_ukernel_function dwconv) const {
491 std::random_device random_device;
492 auto rng = std::mt19937(random_device());
493 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
494
495 std::vector<const float*> indirection((width() - 1) * step() + kr());
496 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + indirection.size() * channels());
497 std::vector<float> kernel(channels() * kr());
498 std::vector<float> bias(channels());
499 std::vector<float, AlignedAllocator<float, 64>> packed_weights((kr() + 1) * packed_channels());
Frank Barchardd5360722020-05-17 16:10:36 -0700500 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
Marat Dukhan163a7e62020-04-09 04:19:26 -0700501 std::vector<float> output((width() - 1) * output_stride() + channels());
502 std::vector<float> output_ref(width() * channels());
503
504 for (size_t iteration = 0; iteration < iterations(); iteration++) {
505 std::generate(input.begin(), input.end(), std::ref(f32rng));
506 std::generate(kernel.begin(), kernel.end(), std::ref(f32rng));
507 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
Frank Barchardd5360722020-05-17 16:10:36 -0700508 std::fill(zero.begin(), zero.end(), 0.0f);
Marat Dukhan163a7e62020-04-09 04:19:26 -0700509 std::fill(output_ref.begin(), output_ref.end(), nanf(""));
510 std::fill(output.begin(), output.end(), nanf(""));
511
512 std::fill(packed_weights.begin(), packed_weights.end(), 0.0f);
513 xnn_pack_f32_dwconv_ghw_w(
514 kr(), 1, channels(), cr(),
Marat Dukhanb42f8662020-07-06 20:46:13 -0700515 kernel.data(), bias.data(), packed_weights.data(), nullptr);
Marat Dukhan163a7e62020-04-09 04:19:26 -0700516 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700517 indirection[i] = input.data() + i * channels() - input_offset();
Marat Dukhan163a7e62020-04-09 04:19:26 -0700518 }
519 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700520 if (zero_index() != SIZE_MAX) {
521 for (size_t i = 0; i < indirection.size(); i += kr()) {
522 indirection[i + zero_index()] = zero.data();
523 }
524 }
Marat Dukhan163a7e62020-04-09 04:19:26 -0700525
526 // Compute reference results, without clamping.
527 for (size_t x = 0; x < width(); x++) {
528 for (size_t c = 0; c < channels(); c++) {
529 float acc = bias[c];
530 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700531 if (indirection[x * step() + k] != zero.data()) {
532 acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k];
533 }
Marat Dukhan163a7e62020-04-09 04:19:26 -0700534 }
535 output_ref[x * channels() + c] = acc;
536 }
537 }
538
539 // Call optimized micro-kernel.
540 dwconv(
541 channels(), width(),
542 indirection.data(), packed_weights.data(), output.data(),
543 step() * sizeof(void*),
544 (output_stride() - channels()) * sizeof(float),
Frank Barchardd5360722020-05-17 16:10:36 -0700545 input_offset() * sizeof(float), zero.data(),
Marat Dukhan163a7e62020-04-09 04:19:26 -0700546 nullptr);
547
548 // Verify results.
549 for (size_t x = 0; x < width(); x++) {
550 for (size_t c = 0; c < channels(); c++) {
551 ASSERT_NEAR(
552 output_ref[x * channels() + c],
553 output[x * output_stride() + c],
554 std::abs(output_ref[x * channels() + c]) * 1.0e-5)
555 << "x = " << x << ", channel = " << c;
556 }
557 }
558 }
559 }
560
561 void Test(xnn_f32_dwconv_minmax_unipass_ukernel_function dwconv_minmax, Variant variant = Variant::Native) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700562 std::random_device random_device;
563 auto rng = std::mt19937(random_device());
564 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng);
565
566 std::vector<const float*> indirection((width() - 1) * step() + kr());
567 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + indirection.size() * channels());
568 std::vector<float> kernel(channels() * kr());
569 std::vector<float> bias(channels());
Marat Dukhan9594db02019-12-05 14:32:37 -0800570 std::vector<float, AlignedAllocator<float, 64>> packed_weights((kr() + 1) * packed_channels());
Frank Barchardd5360722020-05-17 16:10:36 -0700571 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700572 std::vector<float> output((width() - 1) * output_stride() + channels());
573 std::vector<float> output_ref(width() * channels());
574
575 for (size_t iteration = 0; iteration < iterations(); iteration++) {
576 std::generate(input.begin(), input.end(), std::ref(f32rng));
577 std::generate(kernel.begin(), kernel.end(), std::ref(f32rng));
578 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
Frank Barchardd5360722020-05-17 16:10:36 -0700579 std::fill(zero.begin(), zero.end(), 0.0f);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700580 std::fill(output_ref.begin(), output_ref.end(), nanf(""));
581 std::fill(output.begin(), output.end(), nanf(""));
582
583 std::fill(packed_weights.begin(), packed_weights.end(), 0.0f);
584 xnn_pack_f32_dwconv_ghw_w(
585 kr(), 1, channels(), cr(),
Marat Dukhanb42f8662020-07-06 20:46:13 -0700586 kernel.data(), bias.data(), packed_weights.data(), nullptr);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700587 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700588 indirection[i] = input.data() + i * channels() - input_offset();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700589 }
590 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700591 if (zero_index() != SIZE_MAX) {
592 for (size_t i = 0; i < indirection.size(); i += kr()) {
593 indirection[i + zero_index()] = zero.data();
594 }
595 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700596
597 // Compute reference results, without clamping.
598 for (size_t x = 0; x < width(); x++) {
599 for (size_t c = 0; c < channels(); c++) {
600 float acc = bias[c];
601 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700602 if (indirection[x * step() + k] != zero.data()) {
603 acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k];
604 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700605 }
606 output_ref[x * channels() + c] = acc;
607 }
608 }
609
610 // Compute clamping parameters.
611 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
612 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
613 const float accumulated_range = accumulated_max - accumulated_min;
614 const float output_min = accumulated_min + accumulated_range / 255.0f * float(qmin());
615 const float output_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax());
616
Frank Barchard9f3a8432020-06-02 13:59:35 -0700617 // Prepare parameters.
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700618 xnn_f32_minmax_params params = { };
XNNPACK Teamb455b122019-09-27 18:10:33 -0700619 switch (variant) {
620 case Variant::Native:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700621 params = xnn_init_f32_minmax_params(output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700622 break;
623 case Variant::Scalar:
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700624 params = xnn_init_scalar_f32_minmax_params(output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700625 break;
626 }
627
628 // Clamp reference results.
629 for (float& output_val : output_ref) {
630 output_val = std::max(std::min(output_val, output_max), output_min);
631 }
632
633 // Call optimized micro-kernel.
Marat Dukhan163a7e62020-04-09 04:19:26 -0700634 dwconv_minmax(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700635 channels(), width(),
636 indirection.data(), packed_weights.data(), output.data(),
637 step() * sizeof(void*),
638 (output_stride() - channels()) * sizeof(float),
Frank Barchardd5360722020-05-17 16:10:36 -0700639 input_offset() * sizeof(float), zero.data(),
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700640 &params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700641
642 // Verify results.
643 for (size_t x = 0; x < width(); x++) {
644 for (size_t c = 0; c < channels(); c++) {
645 ASSERT_GE(output[x * output_stride() + c], output_min)
646 << "x = " << x << ", channel = " << c;
647 ASSERT_LE(output[x * output_stride() + c], output_max)
648 << "x = " << x << ", channel = " << c;
649 ASSERT_NEAR(
650 output_ref[x * channels() + c],
651 output[x * output_stride() + c],
652 std::abs(output_ref[x * channels() + c]) * 1.0e-5)
653 << "x = " << x << ", channel = " << c;
654 }
655 }
656 }
657 }
658
659 private:
660 uint32_t channels_{1};
661 uint32_t cr_{1};
662 uint32_t kr_{1};
663 uint32_t width_{1};
664 uint32_t step_{1};
665 uint32_t output_stride_{0};
666 uint8_t input_zero_point_{127};
667 uint8_t kernel_zero_point_{127};
668 uint8_t qmin_{0};
669 uint8_t qmax_{255};
Frank Barchardd5360722020-05-17 16:10:36 -0700670 size_t input_offset_{0};
671 size_t zero_index_{SIZE_MAX};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700672 size_t iterations_{3};
673};