XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // 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 Dukhan | 5ce30d9 | 2020-04-14 03:31:26 -0700 | [diff] [blame] | 19 | #include <limits> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | #include <random> |
| 21 | #include <vector> |
| 22 | |
Frank Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 23 | #include <fp16.h> |
| 24 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | #include <xnnpack.h> |
| 26 | #include <xnnpack/AlignedAllocator.h> |
| 27 | #include <xnnpack/pack.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 28 | #include <xnnpack/params-init.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 29 | #include <xnnpack/params.h> |
| 30 | #include <xnnpack/requantization.h> |
| 31 | |
| 32 | |
| 33 | class DWConvMicrokernelTester { |
| 34 | public: |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 35 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 67 | 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 140 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 158 | 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 Dukhan | c2e8f66 | 2021-07-01 17:06:34 -0700 | [diff] [blame] | 167 | void Test( |
| 168 | xnn_qu8_dwconv_minmax_unipass_ukernel_function dwconv_minmax, |
| 169 | xnn_init_qu8_conv_minmax_params_fn init_params, |
Marat Dukhan | c2e8f66 | 2021-07-01 17:06:34 -0700 | [diff] [blame] | 170 | xnn_qu8_requantize_fn requantize) const |
| 171 | { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 172 | std::random_device random_device; |
| 173 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 174 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 177 | |
| 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 Dukhan | 9594db0 | 2019-12-05 14:32:37 -0800 | [diff] [blame] | 182 | std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> packed_weights((kr() + sizeof(int32_t) / sizeof(uint8_t)) * packed_channels()); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 183 | std::vector<uint8_t> zero(channels() + XNN_EXTRA_BYTES / sizeof(uint8_t)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 184 | 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 Dukhan | ecd8311 | 2020-08-03 21:50:28 -0700 | [diff] [blame] | 195 | std::generate(bias.begin(), bias.end(), std::ref(i32rng)); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 196 | std::fill(zero.begin(), zero.end(), input_zero_point()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 197 | std::fill(output.begin(), output.end(), 0xA5); |
| 198 | |
| 199 | std::fill(packed_weights.begin(), packed_weights.end(), 0); |
Marat Dukhan | 1827af1 | 2020-07-16 10:58:55 -0700 | [diff] [blame] | 200 | const xnn_qu8_packing_params packing_params = { input_zero_point(), kernel_zero_point() }; |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame] | 201 | xnn_pack_qu8_dwconv_ghw_w( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 202 | kr(), 1, channels(), cr(), |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 203 | kernel.data(), bias.data(), packed_weights.data(), |
| 204 | 0 /* extra bytes */, &packing_params); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 205 | for (size_t i = 0; i < indirection.size(); i++) { |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 206 | indirection[i] = input.data() + i * channels() - input_offset(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 207 | } |
| 208 | std::shuffle(indirection.begin(), indirection.end(), rng); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 209 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 214 | |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 220 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 225 | } |
| 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 Barchard | 9f3a843 | 2020-06-02 13:59:35 -0700 | [diff] [blame] | 239 | // Prepare parameters. |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 240 | const float requantization_scale = 1.0f / float(output_scale); |
Marat Dukhan | e3d17bf | 2021-05-24 22:22:43 -0700 | [diff] [blame] | 241 | union xnn_qu8_conv_minmax_params quantization_params; |
Marat Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 242 | init_params(&quantization_params, |
| 243 | kernel_zero_point(), requantization_scale, output_zero_point, qmin(), qmax()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 244 | |
| 245 | // Renormalize reference results. |
| 246 | for (size_t x = 0; x < width(); x++) { |
| 247 | for (size_t c = 0; c < channels(); c++) { |
Marat Dukhan | 50323b8 | 2022-01-11 00:12:01 -0800 | [diff] [blame] | 248 | output_ref[x * channels() + c] = requantize( |
| 249 | accumulators[x * channels() + c], requantization_scale, output_zero_point, qmin(), qmax()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | // Call optimized micro-kernel. |
Marat Dukhan | 9993660 | 2020-04-11 16:47:01 -0700 | [diff] [blame] | 254 | dwconv_minmax( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 255 | channels(), width(), |
| 256 | indirection.data(), packed_weights.data(), output.data(), |
| 257 | step() * sizeof(void*), |
| 258 | (output_stride() - channels()) * sizeof(uint8_t), |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 259 | input_offset() * sizeof(uint8_t), zero.data(), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 260 | &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 Dukhan | a5d1261 | 2021-05-25 01:12:26 -0700 | [diff] [blame] | 276 | void Test( |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 277 | xnn_qc8_dwconv_minmax_unipass_ukernel_function dwconv_minmax, |
| 278 | xnn_init_qs8_minmax_params_fn init_params, |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 279 | xnn_qs8_requantize_fn requantize) const |
| 280 | { |
| 281 | std::random_device random_device; |
| 282 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 283 | auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng)); |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 284 | auto i8rng = std::bind( |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 285 | 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 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(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 Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 307 | std::generate(kernel.begin(), kernel.end(), std::ref(w8rng)); |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 308 | } 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 Kelly | cefc376 | 2021-11-04 01:49:42 -0700 | [diff] [blame] | 350 | accumulated_min = std::min(accumulated_min, accumulators[x * channels() + c]); |
| 351 | accumulated_max = std::max(accumulated_max, accumulators[x * channels() + c]); |
Marat Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 352 | } |
| 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 366 | |
| 367 | // Renormalize reference results. |
| 368 | for (size_t x = 0; x < width(); x++) { |
| 369 | for (size_t c = 0; c < channels(); c++) { |
Marat Dukhan | 50323b8 | 2022-01-11 00:12:01 -0800 | [diff] [blame] | 370 | 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 372 | } |
| 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 Dukhan | a5d1261 | 2021-05-25 01:12:26 -0700 | [diff] [blame] | 399 | xnn_qs8_dwconv_minmax_unipass_ukernel_function dwconv_minmax, |
| 400 | xnn_init_qs8_conv_minmax_params_fn init_params, |
Marat Dukhan | a5d1261 | 2021-05-25 01:12:26 -0700 | [diff] [blame] | 401 | xnn_qs8_requantize_fn requantize) const |
| 402 | { |
Marat Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 403 | std::random_device random_device; |
| 404 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 405 | auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(-10000, 10000), std::ref(rng)); |
Marat Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 406 | auto i8rng = std::bind( |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 407 | 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 Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 412 | |
| 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 Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 428 | std::generate(kernel.begin(), kernel.end(), std::ref(w8rng)); |
Marat Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 429 | } 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 438 | kernel.data(), bias.data(), packed_weights.data(), |
| 439 | 0 /* extra bytes */, &packing_params); |
Marat Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 440 | 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 Dukhan | e3d17bf | 2021-05-24 22:22:43 -0700 | [diff] [blame] | 476 | union xnn_qs8_conv_minmax_params quantization_params; |
Marat Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 477 | init_params(&quantization_params, |
| 478 | requantization_scale, output_zero_point, int8_t(qmin() - 0x80), int8_t(qmax() - 0x80)); |
Marat Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 479 | |
| 480 | // Renormalize reference results. |
| 481 | for (size_t x = 0; x < width(); x++) { |
| 482 | for (size_t c = 0; c < channels(); c++) { |
Marat Dukhan | 50323b8 | 2022-01-11 00:12:01 -0800 | [diff] [blame] | 483 | 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 Dukhan | f62bbdc | 2020-08-04 13:59:04 -0700 | [diff] [blame] | 485 | } |
| 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 Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 511 | void Test(xnn_f16_dwconv_minmax_unipass_ukernel_function dwconv_minmax, xnn_init_f16_minmax_params_fn init_params) const { |
Frank Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 512 | std::random_device random_device; |
| 513 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 514 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng)); |
Frank Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 515 | 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 537 | kernel.data(), bias.data(), packed_weights.data(), |
| 538 | 0 /* extra bytes */, nullptr); |
Frank Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 539 | 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 Dukhan | f56f4c4 | 2021-05-17 01:47:20 -0700 | [diff] [blame] | 570 | xnn_f16_minmax_params params; |
Marat Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 571 | init_params(¶ms, |
Frank Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 572 | 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 | ¶ms); |
| 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 Barchard | 2b9d29b | 2020-09-17 12:03:39 -0700 | [diff] [blame] | 596 | 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 Barchard | 5a599a6 | 2020-06-04 20:12:44 -0700 | [diff] [blame] | 597 | << "x = " << x << ", channel = " << c; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 603 | void Test(xnn_f32_dwconv_unipass_ukernel_function dwconv) const { |
| 604 | std::random_device random_device; |
| 605 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 606 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng)); |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 607 | |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 613 | std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float)); |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 614 | 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 621 | std::fill(zero.begin(), zero.end(), 0.0f); |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 622 | 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 628 | kernel.data(), bias.data(), packed_weights.data(), |
| 629 | 0 /* extra bytes */, nullptr); |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 630 | for (size_t i = 0; i < indirection.size(); i++) { |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 631 | indirection[i] = input.data() + i * channels() - input_offset(); |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 632 | } |
| 633 | std::shuffle(indirection.begin(), indirection.end(), rng); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 634 | 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 Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 639 | |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 645 | if (indirection[x * step() + k] != zero.data()) { |
| 646 | acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k]; |
| 647 | } |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 648 | } |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 659 | input_offset() * sizeof(float), zero.data(), |
Marat Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 660 | 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 Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 675 | void Test(xnn_f32_dwconv_minmax_unipass_ukernel_function dwconv_minmax, xnn_init_f32_minmax_params_fn init_params) const { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 676 | std::random_device random_device; |
| 677 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | b3faed3 | 2021-08-10 22:00:44 -0700 | [diff] [blame] | 678 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 679 | |
| 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 Dukhan | 9594db0 | 2019-12-05 14:32:37 -0800 | [diff] [blame] | 684 | std::vector<float, AlignedAllocator<float, 64>> packed_weights((kr() + 1) * packed_channels()); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 685 | std::vector<float> zero(channels() + XNN_EXTRA_BYTES / sizeof(float)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 686 | 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 693 | std::fill(zero.begin(), zero.end(), 0.0f); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 694 | 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 Dukhan | 8228689 | 2021-06-04 17:27:27 -0700 | [diff] [blame] | 700 | kernel.data(), bias.data(), packed_weights.data(), |
| 701 | 0 /* extra bytes */, nullptr); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 702 | for (size_t i = 0; i < indirection.size(); i++) { |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 703 | indirection[i] = input.data() + i * channels() - input_offset(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 704 | } |
| 705 | std::shuffle(indirection.begin(), indirection.end(), rng); |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 706 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 711 | |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 717 | if (indirection[x * step() + k] != zero.data()) { |
| 718 | acc += indirection[x * step() + k][c + input_offset()] * kernel[c * kr() + k]; |
| 719 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 720 | } |
| 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 Barchard | 9f3a843 | 2020-06-02 13:59:35 -0700 | [diff] [blame] | 732 | // Prepare parameters. |
Marat Dukhan | f56f4c4 | 2021-05-17 01:47:20 -0700 | [diff] [blame] | 733 | xnn_f32_minmax_params params; |
Marat Dukhan | d5694df | 2021-05-20 17:10:40 -0700 | [diff] [blame] | 734 | init_params(¶ms, output_min, output_max); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 735 | |
| 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 Dukhan | 163a7e6 | 2020-04-09 04:19:26 -0700 | [diff] [blame] | 742 | dwconv_minmax( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 743 | channels(), width(), |
| 744 | indirection.data(), packed_weights.data(), output.data(), |
| 745 | step() * sizeof(void*), |
| 746 | (output_stride() - channels()) * sizeof(float), |
Frank Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 747 | input_offset() * sizeof(float), zero.data(), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 748 | ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 749 | |
| 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 Barchard | d536072 | 2020-05-17 16:10:36 -0700 | [diff] [blame] | 778 | size_t input_offset_{0}; |
| 779 | size_t zero_index_{SIZE_MAX}; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 780 | size_t iterations_{3}; |
| 781 | }; |