XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <cassert> |
| 12 | #include <cstddef> |
| 13 | #include <cstdlib> |
| 14 | #include <functional> |
| 15 | #include <random> |
| 16 | #include <vector> |
| 17 | |
Frank Barchard | 2a1049c | 2020-06-03 02:31:27 -0700 | [diff] [blame] | 18 | #include <fp16.h> |
| 19 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | #include <xnnpack.h> |
| 21 | #include <xnnpack/AlignedAllocator.h> |
| 22 | #include <xnnpack/pack.h> |
Marat Dukhan | eeaa7bd | 2019-10-25 17:31:25 -0700 | [diff] [blame] | 23 | #include <xnnpack/params-init.h> |
Frank Barchard | e0601b5 | 2019-10-25 17:43:34 -0700 | [diff] [blame] | 24 | #include <xnnpack/params.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class VMulCAddCMicrokernelTester { |
| 28 | public: |
| 29 | enum class Variant { |
| 30 | Native, |
| 31 | Scalar, |
| 32 | }; |
| 33 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 34 | inline VMulCAddCMicrokernelTester& channel_tile(size_t channel_tile) { |
| 35 | this->channel_tile_ = channel_tile; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 36 | return *this; |
| 37 | } |
| 38 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 39 | inline size_t channel_tile() const { |
| 40 | return this->channel_tile_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 43 | inline VMulCAddCMicrokernelTester& channels(size_t channels) { |
| 44 | assert(channels != 0); |
| 45 | this->channels_ = channels; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 46 | return *this; |
| 47 | } |
| 48 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 49 | inline size_t channels() const { |
| 50 | return this->channels_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 53 | inline size_t packed_channels() const { |
| 54 | return channels() % channel_tile() == 0 ? channels() : (channels() / channel_tile() + 1) * channel_tile(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 57 | inline VMulCAddCMicrokernelTester& rows(size_t rows) { |
| 58 | assert(rows != 0); |
| 59 | this->rows_ = rows; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 60 | return *this; |
| 61 | } |
| 62 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 63 | inline size_t rows() const { |
| 64 | return this->rows_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 67 | inline VMulCAddCMicrokernelTester& input_stride(size_t input_stride) { |
| 68 | this->input_stride_ = input_stride; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 69 | return *this; |
| 70 | } |
| 71 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 72 | inline size_t input_stride() const { |
| 73 | return this->input_stride_ == 0 ? channels() : this->input_stride_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 76 | inline VMulCAddCMicrokernelTester& output_stride(size_t output_stride) { |
| 77 | this->output_stride_ = output_stride; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 78 | return *this; |
| 79 | } |
| 80 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 81 | inline size_t output_stride() const { |
| 82 | return this->output_stride_ == 0 ? channels() : this->output_stride_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | inline VMulCAddCMicrokernelTester& inplace(bool inplace) { |
| 86 | this->inplace_ = inplace; |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | inline bool inplace() const { |
| 91 | return this->inplace_; |
| 92 | } |
| 93 | |
| 94 | inline VMulCAddCMicrokernelTester& qmin(uint8_t qmin) { |
| 95 | this->qmin_ = qmin; |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | inline uint8_t qmin() const { |
| 100 | return this->qmin_; |
| 101 | } |
| 102 | |
| 103 | inline VMulCAddCMicrokernelTester& qmax(uint8_t qmax) { |
| 104 | this->qmax_ = qmax; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | inline uint8_t qmax() const { |
| 109 | return this->qmax_; |
| 110 | } |
| 111 | |
| 112 | inline VMulCAddCMicrokernelTester& iterations(size_t iterations) { |
| 113 | this->iterations_ = iterations; |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | inline size_t iterations() const { |
| 118 | return this->iterations_; |
| 119 | } |
| 120 | |
Frank Barchard | 2a1049c | 2020-06-03 02:31:27 -0700 | [diff] [blame] | 121 | void Test(xnn_f16_vmulcaddc_ukernel_function vmulcaddc, Variant variant = Variant::Native) const { |
| 122 | std::random_device random_device; |
| 123 | auto rng = std::mt19937(random_device()); |
| 124 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); |
| 125 | auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng); |
| 126 | |
| 127 | if (inplace()) { |
| 128 | ASSERT_EQ(input_stride(), output_stride()); |
| 129 | } |
| 130 | |
| 131 | std::vector<uint16_t> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t)); |
| 132 | std::vector<uint16_t> scale(channels()); |
| 133 | std::vector<uint16_t> bias(channels()); |
| 134 | std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_w(packed_channels() * 2); |
| 135 | std::vector<uint16_t> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0)); |
| 136 | std::vector<float> y_ref(rows() * channels()); |
| 137 | |
| 138 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 139 | std::generate(scale.begin(), scale.end(), std::ref(f16rng)); |
| 140 | std::generate(bias.begin(), bias.end(), std::ref(f16rng)); |
| 141 | std::generate(x.begin(), x.end(), std::ref(f16rng)); |
| 142 | if (inplace()) { |
| 143 | std::copy(x.cbegin(), x.cend(), y.begin()); |
| 144 | } else { |
| 145 | std::fill(y.begin(), y.end(), UINT16_C(0x7E00) /* NaN */); |
| 146 | } |
| 147 | const uint16_t* x_data = inplace() ? y.data() : x.data(); |
| 148 | |
| 149 | std::fill(packed_w.begin(), packed_w.end(), UINT16_C(0x7E00) /* NaN */); |
| 150 | xnn_pack_f16_vmulcaddc_w(channels(), channel_tile(), |
| 151 | scale.data(), bias.data(), packed_w.data()); |
| 152 | |
| 153 | // Compute reference results. |
| 154 | for (size_t i = 0; i < rows(); i++) { |
| 155 | for (size_t j = 0; j < channels(); j++) { |
| 156 | y_ref[i * channels() + j] = fp16_ieee_to_fp32_value(x_data[i * input_stride() + j]) * fp16_ieee_to_fp32_value(scale[j]) + fp16_ieee_to_fp32_value(bias[j]); |
| 157 | } |
| 158 | } |
| 159 | const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); |
| 160 | const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); |
| 161 | const float accumulated_range = accumulated_max - accumulated_min; |
| 162 | const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax()))); |
| 163 | const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin()))); |
| 164 | |
| 165 | for (float& y_value : y_ref) { |
| 166 | y_value = std::max(std::min(y_value, y_max), y_min); |
| 167 | } |
| 168 | |
| 169 | // Prepare parameters. |
| 170 | xnn_f16_minmax_params params = xnn_init_f16_minmax_params( |
| 171 | fp16_ieee_from_fp32_value(y_min), |
| 172 | fp16_ieee_from_fp32_value(y_max)); |
| 173 | |
| 174 | // Call optimized micro-kernel. |
| 175 | vmulcaddc(rows(), channels() * sizeof(uint16_t), |
| 176 | x_data, input_stride() * sizeof(uint16_t), |
| 177 | packed_w.data(), |
| 178 | y.data(), output_stride() * sizeof(uint16_t), |
| 179 | ¶ms); |
| 180 | |
| 181 | // Verify results. |
| 182 | for (size_t i = 0; i < rows(); i++) { |
| 183 | for (size_t j = 0; j < channels(); j++) { |
| 184 | ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i * output_stride() + j]), y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-2f) |
| 185 | << "at pixel " << i << " / " << rows() |
| 186 | << ", channel = " << j << " / " << channels(); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 192 | void Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc, Variant variant = Variant::Native) const { |
| 193 | std::random_device random_device; |
| 194 | auto rng = std::mt19937(random_device()); |
| 195 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); |
| 196 | |
| 197 | if (inplace()) { |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 198 | ASSERT_EQ(input_stride(), output_stride()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 201 | std::vector<float> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); |
| 202 | std::vector<float> scale(channels()); |
| 203 | std::vector<float> bias(channels()); |
Marat Dukhan | 9594db0 | 2019-12-05 14:32:37 -0800 | [diff] [blame] | 204 | std::vector<float, AlignedAllocator<float, 64>> packed_w(packed_channels() * 2); |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 205 | std::vector<float> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); |
| 206 | std::vector<float> y_ref(rows() * channels()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 207 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 208 | std::generate(scale.begin(), scale.end(), std::ref(f32rng)); |
| 209 | std::generate(bias.begin(), bias.end(), std::ref(f32rng)); |
| 210 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 211 | if (inplace()) { |
| 212 | std::copy(x.cbegin(), x.cend(), y.begin()); |
| 213 | } else { |
| 214 | std::fill(y.begin(), y.end(), nanf("")); |
| 215 | } |
| 216 | const float* x_data = inplace() ? y.data() : x.data(); |
| 217 | |
| 218 | std::fill(packed_w.begin(), packed_w.end(), nanf("")); |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 219 | xnn_pack_f32_vmulcaddc_w(channels(), channel_tile(), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 220 | scale.data(), bias.data(), packed_w.data()); |
| 221 | |
| 222 | // Compute reference results. |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 223 | for (size_t i = 0; i < rows(); i++) { |
| 224 | for (size_t j = 0; j < channels(); j++) { |
| 225 | y_ref[i * channels() + j] = x_data[i * input_stride() + j] * scale[j] + bias[j]; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); |
| 229 | const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); |
| 230 | const float accumulated_range = accumulated_max - accumulated_min; |
| 231 | const float y_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax()); |
| 232 | const float y_min = accumulated_min + accumulated_range / 255.0f * float(qmin()); |
| 233 | for (float& y_value : y_ref) { |
| 234 | y_value = std::max<float>(std::min<float>(y_value, y_max), y_min); |
| 235 | } |
| 236 | |
Frank Barchard | 9f3a843 | 2020-06-02 13:59:35 -0700 | [diff] [blame] | 237 | // Prepare parameters. |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 238 | xnn_f32_minmax_params params = { }; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 239 | switch (variant) { |
| 240 | case Variant::Native: |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 241 | params = xnn_init_f32_minmax_params(y_min, y_max); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 242 | break; |
| 243 | case Variant::Scalar: |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 244 | params = xnn_init_scalar_f32_minmax_params(y_min, y_max); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
| 248 | // Call optimized micro-kernel. |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 249 | vmulcaddc(rows(), channels() * sizeof(float), |
| 250 | x_data, input_stride() * sizeof(float), |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 251 | packed_w.data(), |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 252 | y.data(), output_stride() * sizeof(float), |
Frank Barchard | e70dbeb | 2020-05-01 15:46:41 -0700 | [diff] [blame] | 253 | ¶ms); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 254 | |
| 255 | // Verify results. |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 256 | for (size_t i = 0; i < rows(); i++) { |
| 257 | for (size_t j = 0; j < channels(); j++) { |
| 258 | ASSERT_NEAR(y[i * output_stride() + j], y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-6f) |
| 259 | << "at pixel " << i << " / " << rows() |
| 260 | << ", channel = " << j << " / " << channels(); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private: |
Marat Dukhan | 49e6ee9 | 2019-11-06 15:55:29 -0800 | [diff] [blame] | 267 | size_t channel_tile_{1}; |
| 268 | size_t channels_{1}; |
| 269 | size_t rows_{1}; |
| 270 | size_t input_stride_{0}; |
| 271 | size_t output_stride_{0}; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 272 | bool inplace_{false}; |
| 273 | uint8_t qmin_{0}; |
| 274 | uint8_t qmax_{255}; |
| 275 | size_t iterations_{15}; |
| 276 | }; |