blob: 9a228d803bc49f9fdf3a689fccc08b51b4f48107 [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:
XNNPACK Teamb455b122019-09-27 18:10:33 -070035 inline DWConvMicrokernelTester& width(uint32_t width) {
36 assert(width >= 1);
37 this->width_ = width;
38 return *this;
39 }
40
41 inline uint32_t width() const {
42 return this->width_;
43 }
44
45 inline DWConvMicrokernelTester& step(uint32_t step) {
46 assert(step >= 1);
47 this->step_ = step;
48 return *this;
49 }
50
51 inline uint32_t step() const {
52 return this->step_;
53 }
54
55 inline DWConvMicrokernelTester& channels(uint32_t channels) {
56 assert(channels >= 1);
57 this->channels_ = channels;
58 return *this;
59 }
60
61 inline uint32_t channels() const {
62 return this->channels_;
63 }
64
65 inline DWConvMicrokernelTester& cr(uint32_t cr) {
66 assert(cr != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -070067 this->cr_ = cr;
68 return *this;
69 }
70
71 inline uint32_t cr() const {
72 return this->cr_;
73 }
74
75 inline DWConvMicrokernelTester& kr(uint32_t kr) {
76 assert(kr != 0);
77 this->kr_ = kr;
78 return *this;
79 }
80
81 inline uint32_t kr() const {
82 return this->kr_;
83 }
84
85 inline uint32_t packed_channels() const {
86 return (channels() / cr() + !!(channels() % cr())) * cr();
87 }
88
89 inline DWConvMicrokernelTester& output_stride(uint32_t output_stride) {
90 assert(output_stride != 0);
91 this->output_stride_ = output_stride;
92 return *this;
93 }
94
95 inline uint32_t output_stride() const {
96 if (this->output_stride_ == 0) {
97 return channels();
98 } else {
99 assert(this->output_stride_ >= channels());
100 return this->output_stride_;
101 }
102 }
103
104 inline DWConvMicrokernelTester& input_zero_point(uint8_t input_zero_point) {
105 this->input_zero_point_ = input_zero_point;
106 return *this;
107 }
108
109 inline uint8_t input_zero_point() const {
110 return this->input_zero_point_;
111 }
112
113 inline DWConvMicrokernelTester& kernel_zero_point(uint8_t kernel_zero_point) {
114 this->kernel_zero_point_ = kernel_zero_point;
115 return *this;
116 }
117
118 inline uint8_t kernel_zero_point() const {
119 return this->kernel_zero_point_;
120 }
121
122 inline DWConvMicrokernelTester& qmin(uint8_t qmin) {
123 this->qmin_ = qmin;
124 return *this;
125 }
126
127 inline uint8_t qmin() const {
128 return this->qmin_;
129 }
130
131 inline DWConvMicrokernelTester& qmax(uint8_t qmax) {
132 this->qmax_ = qmax;
133 return *this;
134 }
135
136 inline uint8_t qmax() const {
137 return this->qmax_;
138 }
139
Frank Barchardd5360722020-05-17 16:10:36 -0700140 inline DWConvMicrokernelTester& input_offset(size_t input_offset) {
141 this->input_offset_ = input_offset;
142 return *this;
143 }
144
145 inline size_t input_offset() const {
146 return this->input_offset_;
147 }
148
149 inline DWConvMicrokernelTester& zero_index(size_t zero_index) {
150 this->zero_index_ = zero_index;
151 return *this;
152 }
153
154 inline size_t zero_index() const {
155 return this->zero_index_;
156 }
157
XNNPACK Teamb455b122019-09-27 18:10:33 -0700158 inline DWConvMicrokernelTester& iterations(size_t iterations) {
159 this->iterations_ = iterations;
160 return *this;
161 }
162
163 inline size_t iterations() const {
164 return this->iterations_;
165 }
166
Marat Dukhanc2e8f662021-07-01 17:06:34 -0700167 void Test(
168 xnn_qu8_dwconv_minmax_unipass_ukernel_function dwconv_minmax,
169 xnn_init_qu8_conv_minmax_params_fn init_params,
Marat Dukhanc2e8f662021-07-01 17:06:34 -0700170 xnn_qu8_requantize_fn requantize) const
171 {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700172 std::random_device random_device;
173 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700174 auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng));
175 auto u8rng = std::bind(
176 std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), std::ref(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 Dukhan82286892021-06-04 17:27:27 -0700203 kernel.data(), bias.data(), packed_weights.data(),
204 0 /* extra bytes */, &packing_params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700205 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700206 indirection[i] = input.data() + i * channels() - input_offset();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700207 }
208 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700209 if (zero_index() != SIZE_MAX) {
210 for (size_t i = 0; i < indirection.size(); i += kr()) {
211 indirection[i + zero_index()] = zero.data();
212 }
213 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700214
215 // Compute reference results, without renormalization.
216 for (size_t x = 0; x < width(); x++) {
217 for (size_t c = 0; c < channels(); c++) {
218 float acc = bias[c];
219 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700220 if (indirection[x * step() + k] != zero.data()) {
221 acc +=
222 (int32_t(indirection[x * step() + k][c + input_offset()]) - int32_t(input_zero_point())) *
223 (int32_t(kernel[c * kr() + k]) - int32_t(kernel_zero_point()));
224 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700225 }
226 accumulators[x * channels() + c] = acc;
227 }
228 }
229
230 // Compute renormalization parameters.
231 const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend());
232 const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend());
233 const uint32_t accumulated_range = uint32_t(accumulated_max) - uint32_t(accumulated_min);
234 const double output_scale = accumulated_range >= 256 ? double(accumulated_range) / 255.0 : 1.00001;
235 const uint8_t output_zero_point = uint8_t(std::max(std::min(
236 lrint(127.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale),
237 long(std::numeric_limits<uint8_t>::max())), long(std::numeric_limits<uint8_t>::min())));
238
Frank Barchard9f3a8432020-06-02 13:59:35 -0700239 // Prepare parameters.
XNNPACK Teamb455b122019-09-27 18:10:33 -0700240 const float requantization_scale = 1.0f / float(output_scale);
Marat Dukhane3d17bf2021-05-24 22:22:43 -0700241 union xnn_qu8_conv_minmax_params quantization_params;
Marat Dukhand5694df2021-05-20 17:10:40 -0700242 init_params(&quantization_params,
243 kernel_zero_point(), requantization_scale, output_zero_point, qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700244
245 // Renormalize reference results.
246 for (size_t x = 0; x < width(); x++) {
247 for (size_t c = 0; c < channels(); c++) {
Marat Dukhan50323b82022-01-11 00:12:01 -0800248 output_ref[x * channels() + c] = requantize(
249 accumulators[x * channels() + c], requantization_scale, output_zero_point, qmin(), qmax());
XNNPACK Teamb455b122019-09-27 18:10:33 -0700250 }
251 }
252
253 // Call optimized micro-kernel.
Marat Dukhan99936602020-04-11 16:47:01 -0700254 dwconv_minmax(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700255 channels(), width(),
256 indirection.data(), packed_weights.data(), output.data(),
257 step() * sizeof(void*),
258 (output_stride() - channels()) * sizeof(uint8_t),
Frank Barchardd5360722020-05-17 16:10:36 -0700259 input_offset() * sizeof(uint8_t), zero.data(),
XNNPACK Teamb455b122019-09-27 18:10:33 -0700260 &quantization_params);
261
262 // Verify results.
263 for (size_t x = 0; x < width(); x++) {
264 for (size_t c = 0; c < channels(); c++) {
265 ASSERT_GE(uint32_t(output[x * output_stride() + c]), uint32_t(qmin()))
266 << "x = " << x << ", channel = " << c;
267 ASSERT_LE(uint32_t(output[x * output_stride() + c]), uint32_t(qmax()))
268 << "x = " << x << ", channel = " << c;
269 ASSERT_EQ(uint32_t(output[x * output_stride() + c]), uint32_t(output_ref[x * channels() + c]))
270 << "x = " << x << ", channel = " << c << ", accumulator = " << accumulators[x * channels() + c];
271 }
272 }
273 }
274 }
275
Marat Dukhana5d12612021-05-25 01:12:26 -0700276 void Test(
Marat Dukhan82286892021-06-04 17:27:27 -0700277 xnn_qc8_dwconv_minmax_unipass_ukernel_function dwconv_minmax,
278 xnn_init_qs8_minmax_params_fn init_params,
Marat Dukhan82286892021-06-04 17:27:27 -0700279 xnn_qs8_requantize_fn requantize) const
280 {
281 std::random_device random_device;
282 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700283 auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng));
Marat Dukhan82286892021-06-04 17:27:27 -0700284 auto i8rng = std::bind(
Marat Dukhanb3faed32021-08-10 22:00:44 -0700285 std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()),
286 std::ref(rng));
287 auto w8rng = std::bind(
288 std::uniform_int_distribution<int32_t>(-std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max()),
289 std::ref(rng));
Marat Dukhan82286892021-06-04 17:27:27 -0700290
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(float)) / 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<float> scale(channels());
300 std::vector<int8_t> output_ref(width() * channels());
301
302 for (size_t iteration = 0; iteration < iterations(); iteration++) {
303 do {
304 std::generate(input.begin(), input.end(), std::ref(i8rng));
305 } while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
306 do {
Marat Dukhanb3faed32021-08-10 22:00:44 -0700307 std::generate(kernel.begin(), kernel.end(), std::ref(w8rng));
Marat Dukhan82286892021-06-04 17:27:27 -0700308 } while (kernel.size() > 1 && *std::max_element(kernel.cbegin(), kernel.cend()) == *std::min_element(kernel.cbegin(), kernel.cend()));
309 std::generate(bias.begin(), bias.end(), std::ref(i32rng));
310 std::fill(zero.begin(), zero.end(), int8_t(input_zero_point() - 0x80));
311 std::fill(output.begin(), output.end(), 0xA5);
312
313 std::fill(packed_weights.begin(), packed_weights.end(), 0);
314 const xnn_qs8_packing_params packing_params = { int8_t(input_zero_point() - 0x80) };
315 xnn_pack_qs8_dwconv_ghw_w(
316 kr(), 1, channels(), cr(),
317 kernel.data(), bias.data(), packed_weights.data(), cr() * sizeof(float),
318 &packing_params);
319 for (size_t i = 0; i < indirection.size(); i++) {
320 indirection[i] = input.data() + i * channels() - input_offset();
321 }
322 std::shuffle(indirection.begin(), indirection.end(), rng);
323 if (zero_index() != SIZE_MAX) {
324 for (size_t i = 0; i < indirection.size(); i += kr()) {
325 indirection[i + zero_index()] = zero.data();
326 }
327 }
328
329 // Compute reference results, without renormalization.
330 for (size_t x = 0; x < width(); x++) {
331 for (size_t c = 0; c < channels(); c++) {
332 float acc = bias[c];
333 for (size_t k = 0; k < kr(); k++) {
334 if (indirection[x * step() + k] != zero.data()) {
335 acc +=
336 (int32_t(indirection[x * step() + k][c + input_offset()]) - int32_t(input_zero_point() - 0x80)) *
337 int32_t(kernel[c * kr() + k]);
338 }
339 }
340 accumulators[x * channels() + c] = acc;
341 }
342 }
343
344 // Compute renormalization parameters.
345 const int8_t output_zero_point = -1;
346 for (size_t c = 0; c < channels(); c++) {
347 int32_t accumulated_min = accumulators[c];
348 int32_t accumulated_max = accumulators[c];
349 for (size_t x = 0; x < width(); x++) {
Alan Kellycefc3762021-11-04 01:49:42 -0700350 accumulated_min = std::min(accumulated_min, accumulators[x * channels() + c]);
351 accumulated_max = std::max(accumulated_max, accumulators[x * channels() + c]);
Marat Dukhan82286892021-06-04 17:27:27 -0700352 }
353 const uint32_t accumulated_range = uint32_t(accumulated_max - accumulated_min);
354 const float output_scale = accumulated_range >= 256 ? double(accumulated_range) / 255.0 : 1.00001;
355 scale[c] = 1.0f / output_scale;
356 }
357 xnn_init_qc8_scale_fp32_params(
358 channels(), cr(),
359 cr() * (kr() * sizeof(int8_t) + sizeof(int32_t) + sizeof(float)), scale.data(),
360 (void*) ((uintptr_t) packed_weights.data() + cr() * (kr() * sizeof(int8_t) + sizeof(int32_t))));
361
362 // Prepare parameters.
363 union xnn_qs8_minmax_params minmax_params;
364 init_params(&minmax_params,
365 output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
Marat Dukhan82286892021-06-04 17:27:27 -0700366
367 // Renormalize reference results.
368 for (size_t x = 0; x < width(); x++) {
369 for (size_t c = 0; c < channels(); c++) {
Marat Dukhan50323b82022-01-11 00:12:01 -0800370 output_ref[x * channels() + c] = requantize(
371 accumulators[x * channels() + c], scale[c], output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
Marat Dukhan82286892021-06-04 17:27:27 -0700372 }
373 }
374
375 // Call optimized micro-kernel.
376 dwconv_minmax(
377 channels(), width(),
378 indirection.data(), packed_weights.data(), output.data(),
379 step() * sizeof(void*),
380 (output_stride() - channels()) * sizeof(int8_t),
381 input_offset() * sizeof(int8_t), zero.data(),
382 &minmax_params);
383
384 // Verify results.
385 for (size_t x = 0; x < width(); x++) {
386 for (size_t c = 0; c < channels(); c++) {
387 ASSERT_GE(int32_t(output[x * output_stride() + c]), int32_t(qmin()) - 0x80)
388 << "x = " << x << ", channel = " << c;
389 ASSERT_LE(int32_t(output[x * output_stride() + c]), int32_t(qmax()) - 0x80)
390 << "x = " << x << ", channel = " << c;
391 ASSERT_EQ(int32_t(output[x * output_stride() + c]), int32_t(output_ref[x * channels() + c]))
392 << "x = " << x << ", channel = " << c << ", accumulator = " << accumulators[x * channels() + c];
393 }
394 }
395 }
396 }
397
398 void Test(
Marat Dukhana5d12612021-05-25 01:12:26 -0700399 xnn_qs8_dwconv_minmax_unipass_ukernel_function dwconv_minmax,
400 xnn_init_qs8_conv_minmax_params_fn init_params,
Marat Dukhana5d12612021-05-25 01:12:26 -0700401 xnn_qs8_requantize_fn requantize) const
402 {
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700403 std::random_device random_device;
404 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700405 auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng));
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700406 auto i8rng = std::bind(
Marat Dukhanb3faed32021-08-10 22:00:44 -0700407 std::uniform_int_distribution<int32_t>(std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()),
408 std::ref(rng));
409 auto w8rng = std::bind(
410 std::uniform_int_distribution<int32_t>(-std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max()),
411 std::ref(rng));
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700412
413 std::vector<const int8_t*> indirection((width() - 1) * step() + kr());
414 std::vector<int8_t> input(XNN_EXTRA_BYTES / sizeof(int8_t) + indirection.size() * channels());
415 std::vector<int8_t> kernel(channels() * kr());
416 std::vector<int32_t> bias(channels());
417 std::vector<int8_t, AlignedAllocator<int8_t, 64>> packed_weights((kr() + sizeof(int32_t) / sizeof(int8_t)) * packed_channels());
418 std::vector<int8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(int8_t));
419 std::vector<int8_t> output((width() - 1) * output_stride() + channels());
420 std::vector<int32_t> accumulators(width() * channels());
421 std::vector<int8_t> output_ref(width() * channels());
422
423 for (size_t iteration = 0; iteration < iterations(); iteration++) {
424 do {
425 std::generate(input.begin(), input.end(), std::ref(i8rng));
426 } while (input.size() > 1 && *std::max_element(input.cbegin(), input.cend()) == *std::min_element(input.cbegin(), input.cend()));
427 do {
Marat Dukhanb3faed32021-08-10 22:00:44 -0700428 std::generate(kernel.begin(), kernel.end(), std::ref(w8rng));
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700429 } while (kernel.size() > 1 && *std::max_element(kernel.cbegin(), kernel.cend()) == *std::min_element(kernel.cbegin(), kernel.cend()));
430 std::generate(bias.begin(), bias.end(), std::ref(i32rng));
431 std::fill(zero.begin(), zero.end(), int8_t(input_zero_point() - 0x80));
432 std::fill(output.begin(), output.end(), 0xA5);
433
434 std::fill(packed_weights.begin(), packed_weights.end(), 0);
435 const xnn_qs8_packing_params packing_params = { int8_t(input_zero_point() - 0x80) };
436 xnn_pack_qs8_dwconv_ghw_w(
437 kr(), 1, channels(), cr(),
Marat Dukhan82286892021-06-04 17:27:27 -0700438 kernel.data(), bias.data(), packed_weights.data(),
439 0 /* extra bytes */, &packing_params);
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700440 for (size_t i = 0; i < indirection.size(); i++) {
441 indirection[i] = input.data() + i * channels() - input_offset();
442 }
443 std::shuffle(indirection.begin(), indirection.end(), rng);
444 if (zero_index() != SIZE_MAX) {
445 for (size_t i = 0; i < indirection.size(); i += kr()) {
446 indirection[i + zero_index()] = zero.data();
447 }
448 }
449
450 // Compute reference results, without renormalization.
451 for (size_t x = 0; x < width(); x++) {
452 for (size_t c = 0; c < channels(); c++) {
453 float acc = bias[c];
454 for (size_t k = 0; k < kr(); k++) {
455 if (indirection[x * step() + k] != zero.data()) {
456 acc +=
457 (int32_t(indirection[x * step() + k][c + input_offset()]) - int32_t(input_zero_point() - 0x80)) *
458 int32_t(kernel[c * kr() + k]);
459 }
460 }
461 accumulators[x * channels() + c] = acc;
462 }
463 }
464
465 // Compute renormalization parameters.
466 const int32_t accumulated_min = *std::min_element(accumulators.cbegin(), accumulators.cend());
467 const int32_t accumulated_max = *std::max_element(accumulators.cbegin(), accumulators.cend());
468 const uint32_t accumulated_range = uint32_t(accumulated_max) - uint32_t(accumulated_min);
469 const double output_scale = accumulated_range >= 256 ? double(accumulated_range) / 255.0 : 1.00001;
470 const int8_t output_zero_point = int8_t(std::max(std::min(
471 lrint(-0.5 - 0.5 * double(accumulated_min + accumulated_max) / output_scale),
472 long(std::numeric_limits<int8_t>::max())), long(std::numeric_limits<int8_t>::min())));
473
474 // Prepare parameters.
475 const float requantization_scale = 1.0f / float(output_scale);
Marat Dukhane3d17bf2021-05-24 22:22:43 -0700476 union xnn_qs8_conv_minmax_params quantization_params;
Marat Dukhand5694df2021-05-20 17:10:40 -0700477 init_params(&quantization_params,
478 requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700479
480 // Renormalize reference results.
481 for (size_t x = 0; x < width(); x++) {
482 for (size_t c = 0; c < channels(); c++) {
Marat Dukhan50323b82022-01-11 00:12:01 -0800483 output_ref[x * channels() + c] = requantize(
484 accumulators[x * channels() + c], requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80));
Marat Dukhanf62bbdc2020-08-04 13:59:04 -0700485 }
486 }
487
488 // Call optimized micro-kernel.
489 dwconv_minmax(
490 channels(), width(),
491 indirection.data(), packed_weights.data(), output.data(),
492 step() * sizeof(void*),
493 (output_stride() - channels()) * sizeof(int8_t),
494 input_offset() * sizeof(int8_t), zero.data(),
495 &quantization_params);
496
497 // Verify results.
498 for (size_t x = 0; x < width(); x++) {
499 for (size_t c = 0; c < channels(); c++) {
500 ASSERT_GE(int32_t(output[x * output_stride() + c]), int32_t(qmin()) - 0x80)
501 << "x = " << x << ", channel = " << c;
502 ASSERT_LE(int32_t(output[x * output_stride() + c]), int32_t(qmax()) - 0x80)
503 << "x = " << x << ", channel = " << c;
504 ASSERT_EQ(int32_t(output[x * output_stride() + c]), int32_t(output_ref[x * channels() + c]))
505 << "x = " << x << ", channel = " << c << ", accumulator = " << accumulators[x * channels() + c];
506 }
507 }
508 }
509 }
510
Marat Dukhand5694df2021-05-20 17:10:40 -0700511 void Test(xnn_f16_dwconv_minmax_unipass_ukernel_function dwconv_minmax, xnn_init_f16_minmax_params_fn init_params) const {
Frank Barchard5a599a62020-06-04 20:12:44 -0700512 std::random_device random_device;
513 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700514 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
Frank Barchard5a599a62020-06-04 20:12:44 -0700515 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
516
517 std::vector<const uint16_t*> indirection((width() - 1) * step() + kr());
518 std::vector<uint16_t> input(XNN_EXTRA_BYTES / sizeof(uint16_t) + indirection.size() * channels());
519 std::vector<uint16_t> kernel(channels() * kr());
520 std::vector<uint16_t> bias(channels());
521 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_weights((kr() + 1) * packed_channels());
522 std::vector<uint16_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint16_t));
523 std::vector<uint16_t> output((width() - 1) * output_stride() + channels());
524 std::vector<float> output_ref(width() * channels());
525
526 for (size_t iteration = 0; iteration < iterations(); iteration++) {
527 std::generate(input.begin(), input.end(), std::ref(f16rng));
528 std::generate(kernel.begin(), kernel.end(), std::ref(f16rng));
529 std::generate(bias.begin(), bias.end(), std::ref(f16rng));
530 std::fill(zero.begin(), zero.end(), 0);
531 std::fill(output_ref.begin(), output_ref.end(), 0.0f);
532 std::fill(output.begin(), output.end(), UINT16_C(0x7E00) /* NaN */);
533
534 std::fill(packed_weights.begin(), packed_weights.end(), 0);
535 xnn_pack_f16_dwconv_ghw_w(
536 kr(), 1, channels(), cr(),
Marat Dukhan82286892021-06-04 17:27:27 -0700537 kernel.data(), bias.data(), packed_weights.data(),
538 0 /* extra bytes */, nullptr);
Frank Barchard5a599a62020-06-04 20:12:44 -0700539 for (size_t i = 0; i < indirection.size(); i++) {
540 indirection[i] = input.data() + i * channels() - input_offset();
541 }
542 std::shuffle(indirection.begin(), indirection.end(), rng);
543 if (zero_index() != SIZE_MAX) {
544 for (size_t i = 0; i < indirection.size(); i += kr()) {
545 indirection[i + zero_index()] = zero.data();
546 }
547 }
548
549 // Compute reference results, without clamping.
550 for (size_t x = 0; x < width(); x++) {
551 for (size_t c = 0; c < channels(); c++) {
552 float acc = fp16_ieee_to_fp32_value(bias[c]);
553 for (size_t k = 0; k < kr(); k++) {
554 if (indirection[x * step() + k] != zero.data()) {
555 acc += fp16_ieee_to_fp32_value(indirection[x * step() + k][c + input_offset()]) * fp16_ieee_to_fp32_value(kernel[c * kr() + k]);
556 }
557 }
558 output_ref[x * channels() + c] = acc;
559 }
560 }
561
562 // Compute clamping parameters.
563 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
564 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
565 const float accumulated_range = accumulated_max - accumulated_min;
566 const float output_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin())));
567 const float output_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax())));
568
569 // Prepare parameters.
Marat Dukhanf56f4c42021-05-17 01:47:20 -0700570 xnn_f16_minmax_params params;
Marat Dukhand5694df2021-05-20 17:10:40 -0700571 init_params(&params,
Frank Barchard5a599a62020-06-04 20:12:44 -0700572 fp16_ieee_from_fp32_value(output_min),
573 fp16_ieee_from_fp32_value(output_max));
574
575 // Clamp reference results.
576 for (float& output_val : output_ref) {
577 output_val = std::max(std::min(output_val, output_max), output_min);
578 }
579
580 // Call optimized micro-kernel.
581 dwconv_minmax(
582 channels(), width(),
583 reinterpret_cast<const void**>(indirection.data()), packed_weights.data(), output.data(),
584 step() * sizeof(void*),
585 (output_stride() - channels()) * sizeof(uint16_t),
586 input_offset() * sizeof(uint16_t), zero.data(),
587 &params);
588
589 // Verify results.
590 for (size_t x = 0; x < width(); x++) {
591 for (size_t c = 0; c < channels(); c++) {
592 ASSERT_GE(fp16_ieee_to_fp32_value(output[x * output_stride() + c]), output_min)
593 << "x = " << x << ", channel = " << c;
594 ASSERT_LE(fp16_ieee_to_fp32_value(output[x * output_stride() + c]), output_max)
595 << "x = " << x << ", channel = " << c;
Frank Barchard2b9d29b2020-09-17 12:03:39 -0700596 ASSERT_NEAR(output_ref[x * channels() + c], fp16_ieee_to_fp32_value(output[x * output_stride() + c]), std::max(1.0e-4f, std::abs(output_ref[x * channels() + c]) * 1.0e-2f))
Frank Barchard5a599a62020-06-04 20:12:44 -0700597 << "x = " << x << ", channel = " << c;
598 }
599 }
600 }
601 }
602
Marat Dukhan163a7e62020-04-09 04:19:26 -0700603 void Test(xnn_f32_dwconv_unipass_ukernel_function dwconv) const {
604 std::random_device random_device;
605 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700606 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
Marat Dukhan163a7e62020-04-09 04:19:26 -0700607
608 std::vector<const float*> indirection((width() - 1) * step() + kr());
609 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + indirection.size() * channels());
610 std::vector<float> kernel(channels() * kr());
611 std::vector<float> bias(channels());
612 std::vector<float, AlignedAllocator<float, 64>> packed_weights((kr() + 1) * packed_channels());
Frank Barchardd5360722020-05-17 16:10:36 -0700613 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
Marat Dukhan163a7e62020-04-09 04:19:26 -0700614 std::vector<float> output((width() - 1) * output_stride() + channels());
615 std::vector<float> output_ref(width() * channels());
616
617 for (size_t iteration = 0; iteration < iterations(); iteration++) {
618 std::generate(input.begin(), input.end(), std::ref(f32rng));
619 std::generate(kernel.begin(), kernel.end(), std::ref(f32rng));
620 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
Frank Barchardd5360722020-05-17 16:10:36 -0700621 std::fill(zero.begin(), zero.end(), 0.0f);
Marat Dukhan163a7e62020-04-09 04:19:26 -0700622 std::fill(output_ref.begin(), output_ref.end(), nanf(""));
623 std::fill(output.begin(), output.end(), nanf(""));
624
625 std::fill(packed_weights.begin(), packed_weights.end(), 0.0f);
626 xnn_pack_f32_dwconv_ghw_w(
627 kr(), 1, channels(), cr(),
Marat Dukhan82286892021-06-04 17:27:27 -0700628 kernel.data(), bias.data(), packed_weights.data(),
629 0 /* extra bytes */, nullptr);
Marat Dukhan163a7e62020-04-09 04:19:26 -0700630 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700631 indirection[i] = input.data() + i * channels() - input_offset();
Marat Dukhan163a7e62020-04-09 04:19:26 -0700632 }
633 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700634 if (zero_index() != SIZE_MAX) {
635 for (size_t i = 0; i < indirection.size(); i += kr()) {
636 indirection[i + zero_index()] = zero.data();
637 }
638 }
Marat Dukhan163a7e62020-04-09 04:19:26 -0700639
640 // Compute reference results, without clamping.
641 for (size_t x = 0; x < width(); x++) {
642 for (size_t c = 0; c < channels(); c++) {
643 float acc = bias[c];
644 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700645 if (indirection[x * step() + k] != zero.data()) {
646 acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k];
647 }
Marat Dukhan163a7e62020-04-09 04:19:26 -0700648 }
649 output_ref[x * channels() + c] = acc;
650 }
651 }
652
653 // Call optimized micro-kernel.
654 dwconv(
655 channels(), width(),
656 indirection.data(), packed_weights.data(), output.data(),
657 step() * sizeof(void*),
658 (output_stride() - channels()) * sizeof(float),
Frank Barchardd5360722020-05-17 16:10:36 -0700659 input_offset() * sizeof(float), zero.data(),
Marat Dukhan163a7e62020-04-09 04:19:26 -0700660 nullptr);
661
662 // Verify results.
663 for (size_t x = 0; x < width(); x++) {
664 for (size_t c = 0; c < channels(); c++) {
665 ASSERT_NEAR(
666 output_ref[x * channels() + c],
667 output[x * output_stride() + c],
668 std::abs(output_ref[x * channels() + c]) * 1.0e-5)
669 << "x = " << x << ", channel = " << c;
670 }
671 }
672 }
673 }
674
Marat Dukhand5694df2021-05-20 17:10:40 -0700675 void Test(xnn_f32_dwconv_minmax_unipass_ukernel_function dwconv_minmax, xnn_init_f32_minmax_params_fn init_params) const {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700676 std::random_device random_device;
677 auto rng = std::mt19937(random_device());
Marat Dukhanb3faed32021-08-10 22:00:44 -0700678 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700679
680 std::vector<const float*> indirection((width() - 1) * step() + kr());
681 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + indirection.size() * channels());
682 std::vector<float> kernel(channels() * kr());
683 std::vector<float> bias(channels());
Marat Dukhan9594db02019-12-05 14:32:37 -0800684 std::vector<float, AlignedAllocator<float, 64>> packed_weights((kr() + 1) * packed_channels());
Frank Barchardd5360722020-05-17 16:10:36 -0700685 std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700686 std::vector<float> output((width() - 1) * output_stride() + channels());
687 std::vector<float> output_ref(width() * channels());
688
689 for (size_t iteration = 0; iteration < iterations(); iteration++) {
690 std::generate(input.begin(), input.end(), std::ref(f32rng));
691 std::generate(kernel.begin(), kernel.end(), std::ref(f32rng));
692 std::generate(bias.begin(), bias.end(), std::ref(f32rng));
Frank Barchardd5360722020-05-17 16:10:36 -0700693 std::fill(zero.begin(), zero.end(), 0.0f);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700694 std::fill(output_ref.begin(), output_ref.end(), nanf(""));
695 std::fill(output.begin(), output.end(), nanf(""));
696
697 std::fill(packed_weights.begin(), packed_weights.end(), 0.0f);
698 xnn_pack_f32_dwconv_ghw_w(
699 kr(), 1, channels(), cr(),
Marat Dukhan82286892021-06-04 17:27:27 -0700700 kernel.data(), bias.data(), packed_weights.data(),
701 0 /* extra bytes */, nullptr);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700702 for (size_t i = 0; i < indirection.size(); i++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700703 indirection[i] = input.data() + i * channels() - input_offset();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700704 }
705 std::shuffle(indirection.begin(), indirection.end(), rng);
Frank Barchardd5360722020-05-17 16:10:36 -0700706 if (zero_index() != SIZE_MAX) {
707 for (size_t i = 0; i < indirection.size(); i += kr()) {
708 indirection[i + zero_index()] = zero.data();
709 }
710 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700711
712 // Compute reference results, without clamping.
713 for (size_t x = 0; x < width(); x++) {
714 for (size_t c = 0; c < channels(); c++) {
715 float acc = bias[c];
716 for (size_t k = 0; k < kr(); k++) {
Frank Barchardd5360722020-05-17 16:10:36 -0700717 if (indirection[x * step() + k] != zero.data()) {
718 acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k];
719 }
XNNPACK Teamb455b122019-09-27 18:10:33 -0700720 }
721 output_ref[x * channels() + c] = acc;
722 }
723 }
724
725 // Compute clamping parameters.
726 const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
727 const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
728 const float accumulated_range = accumulated_max - accumulated_min;
729 const float output_min = accumulated_min + accumulated_range / 255.0f * float(qmin());
730 const float output_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax());
731
Frank Barchard9f3a8432020-06-02 13:59:35 -0700732 // Prepare parameters.
Marat Dukhanf56f4c42021-05-17 01:47:20 -0700733 xnn_f32_minmax_params params;
Marat Dukhand5694df2021-05-20 17:10:40 -0700734 init_params(&params, output_min, output_max);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700735
736 // Clamp reference results.
737 for (float& output_val : output_ref) {
738 output_val = std::max(std::min(output_val, output_max), output_min);
739 }
740
741 // Call optimized micro-kernel.
Marat Dukhan163a7e62020-04-09 04:19:26 -0700742 dwconv_minmax(
XNNPACK Teamb455b122019-09-27 18:10:33 -0700743 channels(), width(),
744 indirection.data(), packed_weights.data(), output.data(),
745 step() * sizeof(void*),
746 (output_stride() - channels()) * sizeof(float),
Frank Barchardd5360722020-05-17 16:10:36 -0700747 input_offset() * sizeof(float), zero.data(),
Frank Barcharde70dbeb2020-05-01 15:46:41 -0700748 &params);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700749
750 // Verify results.
751 for (size_t x = 0; x < width(); x++) {
752 for (size_t c = 0; c < channels(); c++) {
753 ASSERT_GE(output[x * output_stride() + c], output_min)
754 << "x = " << x << ", channel = " << c;
755 ASSERT_LE(output[x * output_stride() + c], output_max)
756 << "x = " << x << ", channel = " << c;
757 ASSERT_NEAR(
758 output_ref[x * channels() + c],
759 output[x * output_stride() + c],
760 std::abs(output_ref[x * channels() + c]) * 1.0e-5)
761 << "x = " << x << ", channel = " << c;
762 }
763 }
764 }
765 }
766
767 private:
768 uint32_t channels_{1};
769 uint32_t cr_{1};
770 uint32_t kr_{1};
771 uint32_t width_{1};
772 uint32_t step_{1};
773 uint32_t output_stride_{0};
774 uint8_t input_zero_point_{127};
775 uint8_t kernel_zero_point_{127};
776 uint8_t qmin_{0};
777 uint8_t qmax_{255};
Frank Barchardd5360722020-05-17 16:10:36 -0700778 size_t input_offset_{0};
779 size_t zero_index_{SIZE_MAX};
XNNPACK Teamb455b122019-09-27 18:10:33 -0700780 size_t iterations_{3};
781};