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 <cmath> 12 #include <cassert> 13 #include <cstddef> 14 #include <cstdlib> 15 #include <functional> 16 #include <random> 17 #include <vector> 18 19 #include <xnnpack.h> 20 21 22 class ResizeBilinearOperatorTester { 23 public: input_size(size_t input_height,size_t input_width)24 inline ResizeBilinearOperatorTester& input_size(size_t input_height, size_t input_width) { 25 assert(input_height >= 1); 26 assert(input_width >= 1); 27 this->input_height_ = input_height; 28 this->input_width_ = input_width; 29 return *this; 30 } 31 input_height(size_t input_height)32 inline ResizeBilinearOperatorTester& input_height(size_t input_height) { 33 assert(input_height >= 1); 34 this->input_height_ = input_height; 35 return *this; 36 } 37 input_height()38 inline size_t input_height() const { 39 return this->input_height_; 40 } 41 input_width(size_t input_width)42 inline ResizeBilinearOperatorTester& input_width(size_t input_width) { 43 assert(input_width >= 1); 44 this->input_width_ = input_width; 45 return *this; 46 } 47 input_width()48 inline size_t input_width() const { 49 return this->input_width_; 50 } 51 output_size(size_t output_height,size_t output_width)52 inline ResizeBilinearOperatorTester& output_size(size_t output_height, size_t output_width) { 53 assert(output_height >= 1); 54 assert(output_width >= 1); 55 this->output_height_ = output_height; 56 this->output_width_ = output_width; 57 return *this; 58 } 59 output_height(size_t output_height)60 inline ResizeBilinearOperatorTester& output_height(size_t output_height) { 61 assert(output_height >= 1); 62 this->output_height_ = output_height; 63 return *this; 64 } 65 output_height()66 inline size_t output_height() const { 67 return this->output_height_; 68 } 69 output_width(size_t output_width)70 inline ResizeBilinearOperatorTester& output_width(size_t output_width) { 71 assert(output_width >= 1); 72 this->output_width_ = output_width; 73 return *this; 74 } 75 output_width()76 inline size_t output_width() const { 77 return this->output_width_; 78 } 79 height_scale()80 inline float height_scale() const { 81 if (align_corners() && output_height() > 1) { 82 return float(input_height() - 1) / float(output_height() - 1); 83 } else { 84 return float(input_height()) / float(output_height()); 85 } 86 } 87 width_scale()88 inline float width_scale() const { 89 if (align_corners() && output_width() > 1) { 90 return float(input_width() - 1) / float(output_width() - 1); 91 } else { 92 return float(input_width()) / float(output_width()); 93 } 94 } 95 channels(size_t channels)96 inline ResizeBilinearOperatorTester& channels(size_t channels) { 97 assert(channels != 0); 98 this->channels_ = channels; 99 return *this; 100 } 101 channels()102 inline size_t channels() const { 103 return this->channels_; 104 } 105 batch_size(size_t batch_size)106 inline ResizeBilinearOperatorTester& batch_size(size_t batch_size) { 107 assert(batch_size != 0); 108 this->batch_size_ = batch_size; 109 return *this; 110 } 111 batch_size()112 inline size_t batch_size() const { 113 return this->batch_size_; 114 } 115 input_pixel_stride(size_t input_pixel_stride)116 inline ResizeBilinearOperatorTester& input_pixel_stride(size_t input_pixel_stride) { 117 assert(input_pixel_stride != 0); 118 this->input_pixel_stride_ = input_pixel_stride; 119 return *this; 120 } 121 input_pixel_stride()122 inline size_t input_pixel_stride() const { 123 if (this->input_pixel_stride_ == 0) { 124 return channels(); 125 } else { 126 assert(this->input_pixel_stride_ >= channels()); 127 return this->input_pixel_stride_; 128 } 129 } 130 output_pixel_stride(size_t output_pixel_stride)131 inline ResizeBilinearOperatorTester& output_pixel_stride(size_t output_pixel_stride) { 132 assert(output_pixel_stride != 0); 133 this->output_pixel_stride_ = output_pixel_stride; 134 return *this; 135 } 136 output_pixel_stride()137 inline size_t output_pixel_stride() const { 138 if (this->output_pixel_stride_ == 0) { 139 return channels(); 140 } else { 141 assert(this->output_pixel_stride_ >= channels()); 142 return this->output_pixel_stride_; 143 } 144 } 145 next_input_size(uint32_t next_input_height,uint32_t next_input_width)146 inline ResizeBilinearOperatorTester& next_input_size(uint32_t next_input_height, uint32_t next_input_width) { 147 assert(next_input_height >= 1); 148 assert(next_input_width >= 1); 149 this->next_input_height_ = next_input_height; 150 this->next_input_width_ = next_input_width; 151 return *this; 152 } 153 next_input_height(uint32_t next_input_height)154 inline ResizeBilinearOperatorTester& next_input_height(uint32_t next_input_height) { 155 assert(next_input_height >= 1); 156 this->next_input_height_ = next_input_height; 157 return *this; 158 } 159 next_input_height()160 inline uint32_t next_input_height() const { 161 if (this->next_input_height_ == 0) { 162 return input_height(); 163 } else { 164 return this->next_input_height_; 165 } 166 } 167 next_input_width(uint32_t next_input_width)168 inline ResizeBilinearOperatorTester& next_input_width(uint32_t next_input_width) { 169 assert(next_input_width >= 1); 170 this->next_input_width_ = next_input_width; 171 return *this; 172 } 173 next_input_width()174 inline uint32_t next_input_width() const { 175 if (this->next_input_width_ == 0) { 176 return input_width(); 177 } else { 178 return this->next_input_width_; 179 } 180 } 181 next_batch_size(size_t next_batch_size)182 inline ResizeBilinearOperatorTester& next_batch_size(size_t next_batch_size) { 183 assert(next_batch_size >= 1); 184 this->next_batch_size_ = next_batch_size; 185 return *this; 186 } 187 next_batch_size()188 inline size_t next_batch_size() const { 189 if (this->next_batch_size_ == 0) { 190 return batch_size(); 191 } else { 192 return this->next_batch_size_; 193 } 194 } 195 align_corners(bool align_corners)196 inline ResizeBilinearOperatorTester& align_corners(bool align_corners) { 197 this->align_corners_ = align_corners; 198 return *this; 199 } 200 align_corners()201 inline bool align_corners() const { 202 return this->align_corners_; 203 } 204 tf_legacy_mode(bool tf_legacy_mode)205 inline ResizeBilinearOperatorTester& tf_legacy_mode(bool tf_legacy_mode) { 206 this->tf_legacy_mode_ = tf_legacy_mode; 207 return *this; 208 } 209 tf_legacy_mode()210 inline bool tf_legacy_mode() const { 211 return this->tf_legacy_mode_; 212 } 213 iterations(size_t iterations)214 inline ResizeBilinearOperatorTester& iterations(size_t iterations) { 215 this->iterations_ = iterations; 216 return *this; 217 } 218 iterations()219 inline size_t iterations() const { 220 return this->iterations_; 221 } 222 TestF32()223 void TestF32() const { 224 std::random_device random_device; 225 auto rng = std::mt19937(random_device()); 226 auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); 227 228 std::vector<float> input((batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); 229 std::vector<float> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels()); 230 std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels()); 231 for (size_t iteration = 0; iteration < iterations(); iteration++) { 232 std::generate(input.begin(), input.end(), std::ref(f32rng)); 233 std::fill(output.begin(), output.end(), std::nanf("")); 234 235 // Compute reference results. 236 const float offset = tf_legacy_mode() ? 0.0f : 0.5f; 237 for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { 238 for (size_t output_y = 0; output_y < output_height(); output_y++) { 239 const float input_y = (float(output_y) + offset) * height_scale() - offset; 240 const int64_t input_y_top = std::max<int64_t>(int64_t(std::floor(input_y)), 0); 241 const int64_t input_y_bottom = std::min<int64_t>(int64_t(std::ceil(input_y)), input_height() - 1); 242 const float y_alpha = input_y - std::floor(input_y); 243 for (size_t output_x = 0; output_x < output_width(); output_x++) { 244 const float input_x = (float(output_x) + offset) * width_scale() - offset; 245 const int64_t input_x_left = std::max<int64_t>(int64_t(std::floor(input_x)), 0); 246 const int64_t input_x_right = std::min<int64_t>(int64_t(std::ceil(input_x)), input_width() - 1); 247 const float x_alpha = input_x - std::floor(input_x); 248 for (size_t c = 0; c < channels(); c++) { 249 output_ref[((batch_index * output_height() + output_y) * output_width() + output_x) * channels() + c] = 250 input[((batch_index * input_height() + input_y_top) * input_width() + input_x_left) * input_pixel_stride() + c] * (1.0f - y_alpha) * (1.0f - x_alpha) + 251 input[((batch_index * input_height() + input_y_top) * input_width() + input_x_right) * input_pixel_stride() + c] * (1.0f - y_alpha) * x_alpha + 252 input[((batch_index * input_height() + input_y_bottom) * input_width() + input_x_left) * input_pixel_stride() + c] * y_alpha * (1.0f - x_alpha) + 253 input[((batch_index * input_height() + input_y_bottom) * input_width() + input_x_right) * input_pixel_stride() + c] * y_alpha * x_alpha; 254 } 255 } 256 } 257 } 258 259 // Create, setup, run, and destroy Resize Bilinear operator. 260 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); 261 xnn_operator_t resize_bilinear_op = nullptr; 262 263 ASSERT_EQ(xnn_status_success, 264 xnn_create_resize_bilinear2d_nhwc_f32( 265 channels(), input_pixel_stride(), output_pixel_stride(), 266 (align_corners() ? XNN_FLAG_ALIGN_CORNERS : 0) | (tf_legacy_mode() ? XNN_FLAG_TENSORFLOW_LEGACY_MODE : 0), 267 &resize_bilinear_op)); 268 ASSERT_NE(nullptr, resize_bilinear_op); 269 270 // Smart pointer to automatically delete resize_bilinear_op. 271 std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_resize_bilinear_op(resize_bilinear_op, xnn_delete_operator); 272 273 ASSERT_EQ(xnn_status_success, 274 xnn_setup_resize_bilinear2d_nhwc_f32( 275 resize_bilinear_op, 276 batch_size(), input_height(), input_width(), 277 output_height(), output_width(), 278 input.data(), output.data(), 279 nullptr /* thread pool */)); 280 281 ASSERT_EQ(xnn_status_success, 282 xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); 283 284 // Verify results. 285 for (size_t i = 0; i < batch_size(); i++) { 286 for (size_t y = 0; y < output_height(); y++) { 287 for (size_t x = 0; x < output_width(); x++) { 288 for (size_t c = 0; c < channels(); c++) { 289 ASSERT_NEAR(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + c], 290 output_ref[((i * output_height() + y) * output_width() + x) * channels() + c], 291 std::abs(output_ref[((i * output_height() + y) * output_width() + x) * channels() + c]) * 1.0e-5f) << 292 "in batch index " << i << ", pixel (" << y << ", " << x << "), channel " << c; 293 } 294 } 295 } 296 } 297 } 298 } 299 300 // void TestSetupF32() const { 301 // std::random_device random_device; 302 // auto rng = std::mt19937(random_device()); 303 // auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); 304 305 // std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + std::max( 306 // (batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels(), 307 // (next_batch_size() * next_input_height() * next_input_width() - 1) * input_pixel_stride() + channels())); 308 // std::vector<float> output(std::max( 309 // (batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels(), 310 // (next_batch_size() * next_output_height() * next_output_width() - 1) * output_pixel_stride() + channels())); 311 // std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels()); 312 // std::vector<float> next_output_ref(next_batch_size() * next_output_height() * next_output_width() * channels()); 313 // for (size_t iteration = 0; iteration < iterations(); iteration++) { 314 // std::generate(input.begin(), input.end(), std::ref(f32rng)); 315 // std::fill(output.begin(), output.end(), std::nanf("")); 316 317 // // Compute reference results, without clamping. 318 // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { 319 // for (size_t output_y = 0; output_y < output_height(); output_y++) { 320 // for (size_t output_x = 0; output_x < output_width(); output_x++) { 321 // for (size_t c = 0; c < channels(); c++) { 322 // float acc = 0.0f; 323 // size_t n = 0; 324 // for (size_t py = 0; py < pooling_height(); py++) { 325 // const size_t iy = output_y * stride_height() + py - padding_top(); 326 // for (size_t px = 0; px < pooling_width(); px++) { 327 // const size_t input_x = output_x * stride_width() + px - padding_left(); 328 // if (input_x < input_width() && iy < input_height()) { 329 // acc += input[((batch_index * input_height() + iy) * input_width() + input_x) * input_pixel_stride() + c]; 330 // n += 1; 331 // } 332 // } 333 // } 334 // output_ref[((batch_index * output_height() + output_y) * output_width() + output_x) * channels() + c] = acc / float(n); 335 // } 336 // } 337 // } 338 // } 339 340 // // Compute clamping parameters. 341 // const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend()); 342 // const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend()); 343 // const float accumulated_range = accumulated_max - accumulated_min; 344 // const float output_min = accumulated_range == 0.0f ? 345 // -std::numeric_limits<float>::infinity() : 346 // accumulated_min + accumulated_range / 255.0f * float(qmin()); 347 // const float output_max = accumulated_range == 0.0f ? 348 // +std::numeric_limits<float>::infinity() : 349 // accumulated_max - accumulated_range / 255.0f * float(255 - qmax()); 350 351 // // Clamp reference results. 352 // for (float& value : output_ref) { 353 // value = std::max(std::min(value, output_max), output_min); 354 // } 355 356 // // Create, setup, and run Average Pooling operator once. 357 // ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); 358 // xnn_operator_t resize_bilinear_op = nullptr; 359 360 // ASSERT_EQ(xnn_status_success, 361 // xnn_create_average_pooling2d_nhwc_f32( 362 // padding_top(), padding_right(), padding_bottom(), padding_left(), 363 // pooling_height(), pooling_width(), 364 // stride_height(), stride_width(), 365 // channels(), input_pixel_stride(), output_pixel_stride(), 366 // output_min, output_max, 367 // 0, &resize_bilinear_op)); 368 // ASSERT_NE(nullptr, resize_bilinear_op); 369 370 // ASSERT_EQ(xnn_status_success, 371 // xnn_setup_average_pooling2d_nhwc_f32( 372 // resize_bilinear_op, 373 // batch_size(), input_height(), input_width(), 374 // input.data(), output.data(), 375 // nullptr /* thread pool */)); 376 377 // ASSERT_EQ(xnn_status_success, 378 // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); 379 380 // // Verify results of the first run. 381 // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { 382 // for (size_t y = 0; y < output_height(); y++) { 383 // for (size_t x = 0; x < output_width(); x++) { 384 // for (size_t c = 0; c < channels(); c++) { 385 // ASSERT_LE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_max); 386 // ASSERT_GE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_min); 387 // ASSERT_NEAR(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], 388 // output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c], 389 // std::abs(output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c]) * 1.0e-6f) << 390 // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c; 391 // } 392 // } 393 // } 394 // } 395 396 // // Re-generate data for the second run. 397 // std::generate(input.begin(), input.end(), std::ref(f32rng)); 398 // std::fill(output.begin(), output.end(), std::nanf("")); 399 400 // // Compute reference results for the second run. 401 // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) { 402 // for (size_t output_y = 0; output_y < next_output_height(); output_y++) { 403 // for (size_t output_x = 0; output_x < next_output_width(); output_x++) { 404 // for (size_t c = 0; c < channels(); c++) { 405 // float acc = 0.0f; 406 // int32_t n = 0; 407 // for (size_t py = 0; py < pooling_height(); py++) { 408 // const size_t iy = output_y * stride_height() + py - padding_top(); 409 // for (size_t px = 0; px < pooling_width(); px++) { 410 // const size_t input_x = output_x * stride_width() + px - padding_left(); 411 // if (input_x < next_input_width() && iy < next_input_height()) { 412 // acc += input[((batch_index * next_input_height() + iy) * next_input_width() + input_x) * input_pixel_stride() + c]; 413 // n += 1; 414 // } 415 // } 416 // } 417 // next_output_ref[((batch_index * next_output_height() + output_y) * next_output_width() + output_x) * channels() + c] = 418 // std::max(std::min(acc / float(n), output_max), output_min); 419 // } 420 // } 421 // } 422 // } 423 424 // // Setup and run Average Pooling operator the second time, and destroutput_y the operator. 425 // ASSERT_EQ(xnn_status_success, 426 // xnn_setup_average_pooling2d_nhwc_f32( 427 // resize_bilinear_op, 428 // next_batch_size(), next_input_height(), next_input_width(), 429 // input.data(), output.data(), 430 // nullptr /* thread pool */)); 431 432 // ASSERT_EQ(xnn_status_success, 433 // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); 434 435 // ASSERT_EQ(xnn_status_success, 436 // xnn_delete_operator(resize_bilinear_op)); 437 // resize_bilinear_op = nullptr; 438 439 // // Verify results of the second run. 440 // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) { 441 // for (size_t y = 0; y < next_output_height(); y++) { 442 // for (size_t x = 0; x < next_output_width(); x++) { 443 // for (size_t c = 0; c < channels(); c++) { 444 // ASSERT_LE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_max); 445 // ASSERT_GE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_min); 446 // ASSERT_NEAR(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], 447 // next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c], 448 // std::abs(next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c]) * 1.0e-6f) << 449 // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c; 450 // } 451 // } 452 // } 453 // } 454 // } 455 // } 456 457 private: 458 size_t input_height_{1}; 459 size_t input_width_{1}; 460 size_t output_height_{1}; 461 size_t output_width_{1}; 462 size_t channels_{1}; 463 size_t batch_size_{1}; 464 size_t input_pixel_stride_{0}; 465 size_t output_pixel_stride_{0}; 466 size_t next_input_height_{0}; 467 size_t next_input_width_{0}; 468 size_t next_batch_size_{0}; 469 bool align_corners_{false}; 470 bool tf_legacy_mode_{false}; 471 size_t iterations_{1}; 472 }; 473