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 18 #include <fp16.h> 19 20 #include <xnnpack.h> 21 #include <xnnpack/aligned-allocator.h> 22 #include <xnnpack/pack.h> 23 #include <xnnpack/microfnptr.h> 24 #include <xnnpack/microparams-init.h> 25 26 27 class VMulCAddCMicrokernelTester { 28 public: channel_tile(size_t channel_tile)29 inline VMulCAddCMicrokernelTester& channel_tile(size_t channel_tile) { 30 this->channel_tile_ = channel_tile; 31 return *this; 32 } 33 channel_tile()34 inline size_t channel_tile() const { 35 return this->channel_tile_; 36 } 37 channels(size_t channels)38 inline VMulCAddCMicrokernelTester& channels(size_t channels) { 39 assert(channels != 0); 40 this->channels_ = channels; 41 return *this; 42 } 43 channels()44 inline size_t channels() const { 45 return this->channels_; 46 } 47 packed_channels()48 inline size_t packed_channels() const { 49 return channels() % channel_tile() == 0 ? channels() : (channels() / channel_tile() + 1) * channel_tile(); 50 } 51 rows(size_t rows)52 inline VMulCAddCMicrokernelTester& rows(size_t rows) { 53 assert(rows != 0); 54 this->rows_ = rows; 55 return *this; 56 } 57 rows()58 inline size_t rows() const { 59 return this->rows_; 60 } 61 input_stride(size_t input_stride)62 inline VMulCAddCMicrokernelTester& input_stride(size_t input_stride) { 63 this->input_stride_ = input_stride; 64 return *this; 65 } 66 input_stride()67 inline size_t input_stride() const { 68 return this->input_stride_ == 0 ? channels() : this->input_stride_; 69 } 70 output_stride(size_t output_stride)71 inline VMulCAddCMicrokernelTester& output_stride(size_t output_stride) { 72 this->output_stride_ = output_stride; 73 return *this; 74 } 75 output_stride()76 inline size_t output_stride() const { 77 return this->output_stride_ == 0 ? channels() : this->output_stride_; 78 } 79 inplace(bool inplace)80 inline VMulCAddCMicrokernelTester& inplace(bool inplace) { 81 this->inplace_ = inplace; 82 return *this; 83 } 84 inplace()85 inline bool inplace() const { 86 return this->inplace_; 87 } 88 qmin(uint8_t qmin)89 inline VMulCAddCMicrokernelTester& qmin(uint8_t qmin) { 90 this->qmin_ = qmin; 91 return *this; 92 } 93 qmin()94 inline uint8_t qmin() const { 95 return this->qmin_; 96 } 97 qmax(uint8_t qmax)98 inline VMulCAddCMicrokernelTester& qmax(uint8_t qmax) { 99 this->qmax_ = qmax; 100 return *this; 101 } 102 qmax()103 inline uint8_t qmax() const { 104 return this->qmax_; 105 } 106 iterations(size_t iterations)107 inline VMulCAddCMicrokernelTester& iterations(size_t iterations) { 108 this->iterations_ = iterations; 109 return *this; 110 } 111 iterations()112 inline size_t iterations() const { 113 return this->iterations_; 114 } 115 Test(xnn_f16_vmulcaddc_ukernel_function vmulcaddc,xnn_init_f16_minmax_params_fn init_params)116 void Test(xnn_f16_vmulcaddc_ukernel_function vmulcaddc, xnn_init_f16_minmax_params_fn init_params) const { 117 std::random_device random_device; 118 auto rng = std::mt19937(random_device()); 119 std::uniform_real_distribution<float> f32dist; 120 121 if (inplace()) { 122 ASSERT_EQ(input_stride(), output_stride()); 123 } 124 125 std::vector<uint16_t> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t)); 126 std::vector<uint16_t> scale(channels()); 127 std::vector<uint16_t> bias(channels()); 128 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_w(packed_channels() * 2); 129 std::vector<uint16_t> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0)); 130 std::vector<float> y_ref(rows() * channels()); 131 132 for (size_t iteration = 0; iteration < iterations(); iteration++) { 133 std::generate(scale.begin(), scale.end(), [&]() { return fp16_ieee_from_fp32_value(f32dist(rng)); }); 134 std::generate(bias.begin(), bias.end(), [&]() { return fp16_ieee_from_fp32_value(f32dist(rng)); }); 135 std::generate(x.begin(), x.end(), [&]() { return fp16_ieee_from_fp32_value(f32dist(rng)); }); 136 if (inplace()) { 137 std::copy(x.cbegin(), x.cend(), y.begin()); 138 } else { 139 std::fill(y.begin(), y.end(), UINT16_C(0x7E00) /* NaN */); 140 } 141 const uint16_t* x_data = inplace() ? y.data() : x.data(); 142 143 std::fill(packed_w.begin(), packed_w.end(), UINT16_C(0x7E00) /* NaN */); 144 xnn_pack_f16_vmulcaddc_w(channels(), channel_tile(), 145 scale.data(), bias.data(), packed_w.data(), nullptr); 146 147 // Compute reference results. 148 for (size_t i = 0; i < rows(); i++) { 149 for (size_t j = 0; j < channels(); j++) { 150 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]); 151 } 152 } 153 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); 154 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); 155 const float accumulated_range = accumulated_max - accumulated_min; 156 const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax()))); 157 const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin()))); 158 159 for (float& y_value : y_ref) { 160 y_value = std::max(std::min(y_value, y_max), y_min); 161 } 162 163 // Prepare parameters. 164 xnn_f16_minmax_params params; 165 init_params(¶ms, fp16_ieee_from_fp32_value(y_min), fp16_ieee_from_fp32_value(y_max)); 166 167 // Call optimized micro-kernel. 168 vmulcaddc(rows(), channels() * sizeof(uint16_t), 169 x_data, input_stride() * sizeof(uint16_t), 170 packed_w.data(), 171 y.data(), output_stride() * sizeof(uint16_t), 172 ¶ms); 173 174 // Verify results. 175 for (size_t i = 0; i < rows(); i++) { 176 for (size_t j = 0; j < channels(); j++) { 177 ASSERT_NEAR(fp16_ieee_to_fp32_value(y[i * output_stride() + j]), y_ref[i * channels() + j], std::max(1.0e-4f, std::abs(y_ref[i * channels() + j]) * 1.0e-2f)) 178 << "at pixel " << i << " / " << rows() 179 << ", channel = " << j << " / " << channels(); 180 } 181 } 182 } 183 } 184 Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc,xnn_init_f32_minmax_params_fn init_params)185 void Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc, xnn_init_f32_minmax_params_fn init_params) const { 186 std::random_device random_device; 187 auto rng = std::mt19937(random_device()); 188 std::uniform_real_distribution<float> f32dist; 189 190 if (inplace()) { 191 ASSERT_EQ(input_stride(), output_stride()); 192 } 193 194 std::vector<float> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); 195 std::vector<float> scale(channels()); 196 std::vector<float> bias(channels()); 197 std::vector<float, AlignedAllocator<float, 64>> packed_w(packed_channels() * 2); 198 std::vector<float> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); 199 std::vector<float> y_ref(rows() * channels()); 200 for (size_t iteration = 0; iteration < iterations(); iteration++) { 201 std::generate(scale.begin(), scale.end(), [&]() { return f32dist(rng); }); 202 std::generate(bias.begin(), bias.end(), [&]() { return f32dist(rng); }); 203 std::generate(x.begin(), x.end(), [&]() { return f32dist(rng); }); 204 if (inplace()) { 205 std::copy(x.cbegin(), x.cend(), y.begin()); 206 } else { 207 std::fill(y.begin(), y.end(), nanf("")); 208 } 209 const float* x_data = inplace() ? y.data() : x.data(); 210 211 std::fill(packed_w.begin(), packed_w.end(), nanf("")); 212 xnn_pack_f32_vmulcaddc_w(channels(), channel_tile(), 213 scale.data(), bias.data(), packed_w.data(), nullptr); 214 215 // Compute reference results. 216 for (size_t i = 0; i < rows(); i++) { 217 for (size_t j = 0; j < channels(); j++) { 218 y_ref[i * channels() + j] = x_data[i * input_stride() + j] * scale[j] + bias[j]; 219 } 220 } 221 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); 222 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); 223 const float accumulated_range = accumulated_max - accumulated_min; 224 const float y_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax()); 225 const float y_min = accumulated_min + accumulated_range / 255.0f * float(qmin()); 226 for (float& y_value : y_ref) { 227 y_value = std::max<float>(std::min<float>(y_value, y_max), y_min); 228 } 229 230 // Prepare parameters. 231 xnn_f32_minmax_params params; 232 init_params(¶ms, y_min, y_max); 233 234 // Call optimized micro-kernel. 235 vmulcaddc(rows(), channels() * sizeof(float), 236 x_data, input_stride() * sizeof(float), 237 packed_w.data(), 238 y.data(), output_stride() * sizeof(float), 239 ¶ms); 240 241 // Verify results. 242 for (size_t i = 0; i < rows(); i++) { 243 for (size_t j = 0; j < channels(); j++) { 244 ASSERT_NEAR(y[i * output_stride() + j], y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-6f) 245 << "at pixel " << i << " / " << rows() 246 << ", channel = " << j << " / " << channels(); 247 } 248 } 249 } 250 } 251 252 private: 253 size_t channel_tile_{1}; 254 size_t channels_{1}; 255 size_t rows_{1}; 256 size_t input_stride_{0}; 257 size_t output_stride_{0}; 258 bool inplace_{false}; 259 uint8_t qmin_{0}; 260 uint8_t qmax_{255}; 261 size_t iterations_{15}; 262 }; 263