1 // Copyright 2020 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 <random> 15 #include <vector> 16 17 #include <fp16.h> 18 19 #include <xnnpack.h> 20 21 22 class NegateOperatorTester { 23 public: channels(size_t channels)24 inline NegateOperatorTester& channels(size_t channels) { 25 assert(channels != 0); 26 this->channels_ = channels; 27 return *this; 28 } 29 channels()30 inline size_t channels() const { 31 return this->channels_; 32 } 33 input_stride(size_t input_stride)34 inline NegateOperatorTester& input_stride(size_t input_stride) { 35 assert(input_stride != 0); 36 this->input_stride_ = input_stride; 37 return *this; 38 } 39 input_stride()40 inline size_t input_stride() const { 41 if (this->input_stride_ == 0) { 42 return this->channels_; 43 } else { 44 assert(this->input_stride_ >= this->channels_); 45 return this->input_stride_; 46 } 47 } 48 output_stride(size_t output_stride)49 inline NegateOperatorTester& output_stride(size_t output_stride) { 50 assert(output_stride != 0); 51 this->output_stride_ = output_stride; 52 return *this; 53 } 54 output_stride()55 inline size_t output_stride() const { 56 if (this->output_stride_ == 0) { 57 return this->channels_; 58 } else { 59 assert(this->output_stride_ >= this->channels_); 60 return this->output_stride_; 61 } 62 } 63 batch_size(size_t batch_size)64 inline NegateOperatorTester& batch_size(size_t batch_size) { 65 assert(batch_size != 0); 66 this->batch_size_ = batch_size; 67 return *this; 68 } 69 batch_size()70 inline size_t batch_size() const { 71 return this->batch_size_; 72 } 73 iterations(size_t iterations)74 inline NegateOperatorTester& iterations(size_t iterations) { 75 this->iterations_ = iterations; 76 return *this; 77 } 78 iterations()79 inline size_t iterations() const { 80 return this->iterations_; 81 } 82 TestF16()83 void TestF16() const { 84 std::random_device random_device; 85 auto rng = std::mt19937(random_device()); 86 std::uniform_real_distribution<float> f32dist(-1.0f, 1.0f); 87 88 std::vector<uint16_t> input(XNN_EXTRA_BYTES / sizeof(uint16_t) + 89 (batch_size() - 1) * input_stride() + channels()); 90 std::vector<uint16_t> output((batch_size() - 1) * output_stride() + channels()); 91 std::vector<uint16_t> output_ref(batch_size() * channels()); 92 for (size_t iteration = 0; iteration < iterations(); iteration++) { 93 std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); }); 94 std::fill(output.begin(), output.end(), UINT16_C(0x7E00) /* NaN */); 95 96 // Compute reference results. 97 for (size_t i = 0; i < batch_size(); i++) { 98 for (size_t c = 0; c < channels(); c++) { 99 output_ref[i * channels() + c] = input[i * input_stride() + c] ^ UINT16_C(0x8000); 100 } 101 } 102 103 // Create, setup, run, and destroy Negate operator. 104 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); 105 xnn_operator_t negate_op = nullptr; 106 107 const xnn_status status = xnn_create_negate_nc_f16( 108 channels(), input_stride(), output_stride(), 109 0, &negate_op); 110 if (status == xnn_status_unsupported_hardware) { 111 GTEST_SKIP(); 112 } 113 ASSERT_EQ(xnn_status_success, status); 114 ASSERT_NE(nullptr, negate_op); 115 116 // Smart pointer to automatically delete negate_op. 117 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_negate_op(negate_op, xnn_delete_operator); 118 119 ASSERT_EQ(xnn_status_success, 120 xnn_setup_negate_nc_f16( 121 negate_op, 122 batch_size(), 123 input.data(), output.data(), 124 nullptr /* thread pool */)); 125 126 ASSERT_EQ(xnn_status_success, 127 xnn_run_operator(negate_op, nullptr /* thread pool */)); 128 129 // Verify results. 130 for (size_t i = 0; i < batch_size(); i++) { 131 for (size_t c = 0; c < channels(); c++) { 132 ASSERT_EQ(output_ref[i * channels() + c], output[i * output_stride() + c]) 133 << "at batch " << i << " / " << batch_size() << ", channel " << c << " / " << channels(); 134 } 135 } 136 } 137 } 138 TestF32()139 void TestF32() const { 140 std::random_device random_device; 141 auto rng = std::mt19937(random_device()); 142 std::uniform_real_distribution<float> f32dist(-1.0f, 1.0f); 143 144 std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + 145 (batch_size() - 1) * input_stride() + channels()); 146 std::vector<float> output((batch_size() - 1) * output_stride() + channels()); 147 std::vector<float> output_ref(batch_size() * channels()); 148 for (size_t iteration = 0; iteration < iterations(); iteration++) { 149 std::generate(input.begin(), input.end(), [&]() { return f32dist(rng); }); 150 std::fill(output.begin(), output.end(), std::nanf("")); 151 152 // Compute reference results. 153 for (size_t i = 0; i < batch_size(); i++) { 154 for (size_t c = 0; c < channels(); c++) { 155 output_ref[i * channels() + c] = -input[i * input_stride() + c]; 156 } 157 } 158 159 // Create, setup, run, and destroy Negate operator. 160 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); 161 xnn_operator_t negate_op = nullptr; 162 163 ASSERT_EQ(xnn_status_success, 164 xnn_create_negate_nc_f32( 165 channels(), input_stride(), output_stride(), 166 0, &negate_op)); 167 ASSERT_NE(nullptr, negate_op); 168 169 // Smart pointer to automatically delete negate_op. 170 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_negate_op(negate_op, xnn_delete_operator); 171 172 ASSERT_EQ(xnn_status_success, 173 xnn_setup_negate_nc_f32( 174 negate_op, 175 batch_size(), 176 input.data(), output.data(), 177 nullptr /* thread pool */)); 178 179 ASSERT_EQ(xnn_status_success, 180 xnn_run_operator(negate_op, nullptr /* thread pool */)); 181 182 // Verify results. 183 for (size_t i = 0; i < batch_size(); i++) { 184 for (size_t c = 0; c < channels(); c++) { 185 ASSERT_EQ(output_ref[i * channels() + c], output[i * output_stride() + c]) 186 << "at batch " << i << " / " << batch_size() << ", channel " << c << " / " << channels(); 187 } 188 } 189 } 190 } 191 192 private: 193 size_t batch_size_{1}; 194 size_t channels_{1}; 195 size_t input_stride_{0}; 196 size_t output_stride_{0}; 197 size_t iterations_{15}; 198 }; 199