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/AlignedAllocator.h> 22 #include <xnnpack/pack.h> 23 #include <xnnpack/params-init.h> 24 #include <xnnpack/params.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 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); 120 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng); 121 122 if (inplace()) { 123 ASSERT_EQ(input_stride(), output_stride()); 124 } 125 126 std::vector<uint16_t> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(uint16_t)); 127 std::vector<uint16_t> scale(channels()); 128 std::vector<uint16_t> bias(channels()); 129 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> packed_w(packed_channels() * 2); 130 std::vector<uint16_t> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(uint16_t) : 0)); 131 std::vector<float> y_ref(rows() * channels()); 132 133 for (size_t iteration = 0; iteration < iterations(); iteration++) { 134 std::generate(scale.begin(), scale.end(), std::ref(f16rng)); 135 std::generate(bias.begin(), bias.end(), std::ref(f16rng)); 136 std::generate(x.begin(), x.end(), std::ref(f16rng)); 137 if (inplace()) { 138 std::copy(x.cbegin(), x.cend(), y.begin()); 139 } else { 140 std::fill(y.begin(), y.end(), UINT16_C(0x7E00) /* NaN */); 141 } 142 const uint16_t* x_data = inplace() ? y.data() : x.data(); 143 144 std::fill(packed_w.begin(), packed_w.end(), UINT16_C(0x7E00) /* NaN */); 145 xnn_pack_f16_vmulcaddc_w(channels(), channel_tile(), 146 scale.data(), bias.data(), packed_w.data(), nullptr); 147 148 // Compute reference results. 149 for (size_t i = 0; i < rows(); i++) { 150 for (size_t j = 0; j < channels(); j++) { 151 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]); 152 } 153 } 154 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); 155 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); 156 const float accumulated_range = accumulated_max - accumulated_min; 157 const float y_max = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_max - accumulated_range / 255.0f * float(255 - qmax()))); 158 const float y_min = fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(accumulated_min + accumulated_range / 255.0f * float(qmin()))); 159 160 for (float& y_value : y_ref) { 161 y_value = std::max(std::min(y_value, y_max), y_min); 162 } 163 164 // Prepare parameters. 165 xnn_f16_minmax_params params; 166 init_params(¶ms, fp16_ieee_from_fp32_value(y_min), fp16_ieee_from_fp32_value(y_max)); 167 168 // Call optimized micro-kernel. 169 vmulcaddc(rows(), channels() * sizeof(uint16_t), 170 x_data, input_stride() * sizeof(uint16_t), 171 packed_w.data(), 172 y.data(), output_stride() * sizeof(uint16_t), 173 ¶ms); 174 175 // Verify results. 176 for (size_t i = 0; i < rows(); i++) { 177 for (size_t j = 0; j < channels(); j++) { 178 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)) 179 << "at pixel " << i << " / " << rows() 180 << ", channel = " << j << " / " << channels(); 181 } 182 } 183 } 184 } 185 Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc,xnn_init_f32_minmax_params_fn init_params)186 void Test(xnn_f32_vmulcaddc_ukernel_function vmulcaddc, xnn_init_f32_minmax_params_fn init_params) const { 187 std::random_device random_device; 188 auto rng = std::mt19937(random_device()); 189 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); 190 191 if (inplace()) { 192 ASSERT_EQ(input_stride(), output_stride()); 193 } 194 195 std::vector<float> x((rows() - 1) * input_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); 196 std::vector<float> scale(channels()); 197 std::vector<float> bias(channels()); 198 std::vector<float, AlignedAllocator<float, 64>> packed_w(packed_channels() * 2); 199 std::vector<float> y((rows() - 1) * output_stride() + channels() + (inplace() ? XNN_EXTRA_BYTES / sizeof(float) : 0)); 200 std::vector<float> y_ref(rows() * channels()); 201 for (size_t iteration = 0; iteration < iterations(); iteration++) { 202 std::generate(scale.begin(), scale.end(), std::ref(f32rng)); 203 std::generate(bias.begin(), bias.end(), std::ref(f32rng)); 204 std::generate(x.begin(), x.end(), std::ref(f32rng)); 205 if (inplace()) { 206 std::copy(x.cbegin(), x.cend(), y.begin()); 207 } else { 208 std::fill(y.begin(), y.end(), nanf("")); 209 } 210 const float* x_data = inplace() ? y.data() : x.data(); 211 212 std::fill(packed_w.begin(), packed_w.end(), nanf("")); 213 xnn_pack_f32_vmulcaddc_w(channels(), channel_tile(), 214 scale.data(), bias.data(), packed_w.data(), nullptr); 215 216 // Compute reference results. 217 for (size_t i = 0; i < rows(); i++) { 218 for (size_t j = 0; j < channels(); j++) { 219 y_ref[i * channels() + j] = x_data[i * input_stride() + j] * scale[j] + bias[j]; 220 } 221 } 222 const float accumulated_min = *std::min_element(y_ref.cbegin(), y_ref.cend()); 223 const float accumulated_max = *std::max_element(y_ref.cbegin(), y_ref.cend()); 224 const float accumulated_range = accumulated_max - accumulated_min; 225 const float y_max = accumulated_max - accumulated_range / 255.0f * float(255 - qmax()); 226 const float y_min = accumulated_min + accumulated_range / 255.0f * float(qmin()); 227 for (float& y_value : y_ref) { 228 y_value = std::max<float>(std::min<float>(y_value, y_max), y_min); 229 } 230 231 // Prepare parameters. 232 xnn_f32_minmax_params params; 233 init_params(¶ms, y_min, y_max); 234 235 // Call optimized micro-kernel. 236 vmulcaddc(rows(), channels() * sizeof(float), 237 x_data, input_stride() * sizeof(float), 238 packed_w.data(), 239 y.data(), output_stride() * sizeof(float), 240 ¶ms); 241 242 // Verify results. 243 for (size_t i = 0; i < rows(); i++) { 244 for (size_t j = 0; j < channels(); j++) { 245 ASSERT_NEAR(y[i * output_stride() + j], y_ref[i * channels() + j], std::abs(y_ref[i * channels() + j]) * 1.0e-6f) 246 << "at pixel " << i << " / " << rows() 247 << ", channel = " << j << " / " << channels(); 248 } 249 } 250 } 251 } 252 253 private: 254 size_t channel_tile_{1}; 255 size_t channels_{1}; 256 size_t rows_{1}; 257 size_t input_stride_{0}; 258 size_t output_stride_{0}; 259 bool inplace_{false}; 260 uint8_t qmin_{0}; 261 uint8_t qmax_{255}; 262 size_t iterations_{15}; 263 }; 264