Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [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 <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: |
| 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 | |
| 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 | |
| 38 | inline size_t input_height() const { |
| 39 | return this->input_height_; |
| 40 | } |
| 41 | |
| 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 | |
| 48 | inline size_t input_width() const { |
| 49 | return this->input_width_; |
| 50 | } |
| 51 | |
| 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 | |
| 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 | |
| 66 | inline size_t output_height() const { |
| 67 | return this->output_height_; |
| 68 | } |
| 69 | |
| 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 | |
| 76 | inline size_t output_width() const { |
| 77 | return this->output_width_; |
| 78 | } |
| 79 | |
| 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 | |
| 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 | |
| 96 | inline ResizeBilinearOperatorTester& channels(size_t channels) { |
| 97 | assert(channels != 0); |
| 98 | this->channels_ = channels; |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | inline size_t channels() const { |
| 103 | return this->channels_; |
| 104 | } |
| 105 | |
| 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 | |
| 112 | inline size_t batch_size() const { |
| 113 | return this->batch_size_; |
| 114 | } |
| 115 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 196 | inline ResizeBilinearOperatorTester& align_corners(bool align_corners) { |
| 197 | this->align_corners_ = align_corners; |
| 198 | return *this; |
| 199 | } |
| 200 | |
| 201 | inline bool align_corners() const { |
| 202 | return this->align_corners_; |
| 203 | } |
| 204 | |
| 205 | inline ResizeBilinearOperatorTester& tf_legacy_mode(bool tf_legacy_mode) { |
| 206 | this->tf_legacy_mode_ = tf_legacy_mode; |
| 207 | return *this; |
| 208 | } |
| 209 | |
| 210 | inline bool tf_legacy_mode() const { |
| 211 | return this->tf_legacy_mode_; |
| 212 | } |
| 213 | |
| 214 | inline ResizeBilinearOperatorTester& iterations(size_t iterations) { |
| 215 | this->iterations_ = iterations; |
| 216 | return *this; |
| 217 | } |
| 218 | |
| 219 | inline size_t iterations() const { |
| 220 | return this->iterations_; |
| 221 | } |
| 222 | |
XNNPACK Team | a5cb677 | 2020-10-20 18:04:33 -0700 | [diff] [blame] | 223 | void TestNHWCxF32() const { |
Marat Dukhan | f5c4625 | 2020-05-22 10:36:13 -0700 | [diff] [blame] | 224 | if (align_corners()) { |
| 225 | ASSERT_FALSE(tf_legacy_mode()); |
| 226 | } |
| 227 | |
Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [diff] [blame] | 228 | std::random_device random_device; |
| 229 | auto rng = std::mt19937(random_device()); |
| 230 | auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); |
| 231 | |
| 232 | std::vector<float> input((batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); |
| 233 | std::vector<float> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels()); |
| 234 | std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels()); |
| 235 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 236 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 237 | std::fill(output.begin(), output.end(), std::nanf("")); |
| 238 | |
| 239 | // Compute reference results. |
Marat Dukhan | f5c4625 | 2020-05-22 10:36:13 -0700 | [diff] [blame] | 240 | const float offset = (tf_legacy_mode() || align_corners()) ? 0.0f : 0.5f; |
Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [diff] [blame] | 241 | for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { |
| 242 | for (size_t output_y = 0; output_y < output_height(); output_y++) { |
| 243 | const float input_y = (float(output_y) + offset) * height_scale() - offset; |
| 244 | const int64_t input_y_top = std::max<int64_t>(int64_t(std::floor(input_y)), 0); |
| 245 | const int64_t input_y_bottom = std::min<int64_t>(int64_t(std::ceil(input_y)), input_height() - 1); |
| 246 | const float y_alpha = input_y - std::floor(input_y); |
| 247 | for (size_t output_x = 0; output_x < output_width(); output_x++) { |
| 248 | const float input_x = (float(output_x) + offset) * width_scale() - offset; |
| 249 | const int64_t input_x_left = std::max<int64_t>(int64_t(std::floor(input_x)), 0); |
| 250 | const int64_t input_x_right = std::min<int64_t>(int64_t(std::ceil(input_x)), input_width() - 1); |
| 251 | const float x_alpha = input_x - std::floor(input_x); |
| 252 | for (size_t c = 0; c < channels(); c++) { |
| 253 | output_ref[((batch_index * output_height() + output_y) * output_width() + output_x) * channels() + c] = |
| 254 | 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) + |
| 255 | input[((batch_index * input_height() + input_y_top) * input_width() + input_x_right) * input_pixel_stride() + c] * (1.0f - y_alpha) * x_alpha + |
| 256 | input[((batch_index * input_height() + input_y_bottom) * input_width() + input_x_left) * input_pixel_stride() + c] * y_alpha * (1.0f - x_alpha) + |
| 257 | input[((batch_index * input_height() + input_y_bottom) * input_width() + input_x_right) * input_pixel_stride() + c] * y_alpha * x_alpha; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Create, setup, run, and destroy Resize Bilinear operator. |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 264 | ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); |
Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [diff] [blame] | 265 | xnn_operator_t resize_bilinear_op = nullptr; |
| 266 | |
| 267 | ASSERT_EQ(xnn_status_success, |
| 268 | xnn_create_resize_bilinear2d_nhwc_f32( |
| 269 | channels(), input_pixel_stride(), output_pixel_stride(), |
| 270 | (align_corners() ? XNN_FLAG_ALIGN_CORNERS : 0) | (tf_legacy_mode() ? XNN_FLAG_TENSORFLOW_LEGACY_MODE : 0), |
| 271 | &resize_bilinear_op)); |
| 272 | ASSERT_NE(nullptr, resize_bilinear_op); |
| 273 | |
| 274 | // Smart pointer to automatically delete resize_bilinear_op. |
| 275 | std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_resize_bilinear_op(resize_bilinear_op, xnn_delete_operator); |
| 276 | |
| 277 | ASSERT_EQ(xnn_status_success, |
| 278 | xnn_setup_resize_bilinear2d_nhwc_f32( |
| 279 | resize_bilinear_op, |
| 280 | batch_size(), input_height(), input_width(), |
| 281 | output_height(), output_width(), |
| 282 | input.data(), output.data(), |
| 283 | nullptr /* thread pool */)); |
| 284 | |
| 285 | ASSERT_EQ(xnn_status_success, |
| 286 | xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); |
| 287 | |
| 288 | // Verify results. |
| 289 | for (size_t i = 0; i < batch_size(); i++) { |
| 290 | for (size_t y = 0; y < output_height(); y++) { |
| 291 | for (size_t x = 0; x < output_width(); x++) { |
| 292 | for (size_t c = 0; c < channels(); c++) { |
| 293 | ASSERT_NEAR(output[((i * output_height() + y) * output_width() + x) * output_pixel_stride() + c], |
| 294 | output_ref[((i * output_height() + y) * output_width() + x) * channels() + c], |
| 295 | std::abs(output_ref[((i * output_height() + y) * output_width() + x) * channels() + c]) * 1.0e-5f) << |
| 296 | "in batch index " << i << ", pixel (" << y << ", " << x << "), channel " << c; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Artsiom Ablavatski | 9791810 | 2020-10-27 15:52:59 -0700 | [diff] [blame] | 304 | void TestNCHWxF32() const { |
| 305 | if (align_corners()) { |
| 306 | ASSERT_FALSE(tf_legacy_mode()); |
| 307 | } |
| 308 | |
| 309 | std::random_device random_device; |
| 310 | auto rng = std::mt19937(random_device()); |
| 311 | auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); |
| 312 | |
| 313 | std::vector<float> input((batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels() + XNN_EXTRA_BYTES / sizeof(float)); |
| 314 | std::vector<float> output((batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels()); |
| 315 | std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels()); |
| 316 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 317 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 318 | std::fill(output.begin(), output.end(), std::nanf("")); |
| 319 | |
| 320 | // Compute reference results. |
| 321 | const float offset = (tf_legacy_mode() || align_corners()) ? 0.0f : 0.5f; |
| 322 | const int64_t input_num_pixels = input_height() * input_width(); |
| 323 | const int64_t input_num_elements = input_num_pixels * input_pixel_stride(); |
| 324 | const int64_t output_num_pixels = output_height() * output_width(); |
| 325 | const int64_t output_num_elements = output_num_pixels * channels(); |
| 326 | for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { |
| 327 | for (size_t output_y = 0; output_y < output_height(); output_y++) { |
| 328 | const float input_y = (float(output_y) + offset) * height_scale() - offset; |
| 329 | const int64_t input_y_top = std::max<int64_t>(int64_t(std::floor(input_y)), 0); |
| 330 | const int64_t input_y_bottom = std::min<int64_t>(int64_t(std::ceil(input_y)), input_height() - 1); |
| 331 | const float y_alpha = input_y - std::floor(input_y); |
| 332 | for (size_t output_x = 0; output_x < output_width(); output_x++) { |
| 333 | const float input_x = (float(output_x) + offset) * width_scale() - offset; |
| 334 | const int64_t input_x_left = std::max<int64_t>(int64_t(std::floor(input_x)), 0); |
| 335 | const int64_t input_x_right = std::min<int64_t>(int64_t(std::ceil(input_x)), input_width() - 1); |
| 336 | const float x_alpha = input_x - std::floor(input_x); |
| 337 | for (size_t c = 0; c < channels(); c++) { |
| 338 | output_ref[batch_index * output_num_elements + c * output_num_pixels + output_y * output_width() + output_x] = |
| 339 | input[batch_index * input_num_elements + c * input_num_pixels + input_y_top * input_width() + input_x_left] * (1.0f - y_alpha) * (1.0f - x_alpha) + |
| 340 | input[batch_index * input_num_elements + c * input_num_pixels + input_y_top * input_width() + input_x_right] * (1.0f - y_alpha) * x_alpha + |
| 341 | input[batch_index * input_num_elements + c * input_num_pixels + input_y_bottom * input_width() + input_x_left] * y_alpha * (1.0f - x_alpha) + |
| 342 | input[batch_index * input_num_elements + c * input_num_pixels + input_y_bottom * input_width() + input_x_right] * y_alpha * x_alpha; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // Create, setup, run, and destroy Resize Bilinear operator. |
| 349 | ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); |
| 350 | xnn_operator_t resize_bilinear_op = nullptr; |
| 351 | |
| 352 | ASSERT_EQ(xnn_status_success, |
| 353 | xnn_create_resize_bilinear2d_nchw_f32( |
| 354 | channels(), input_pixel_stride(), output_pixel_stride(), |
| 355 | (align_corners() ? XNN_FLAG_ALIGN_CORNERS : 0) | (tf_legacy_mode() ? XNN_FLAG_TENSORFLOW_LEGACY_MODE : 0), |
| 356 | &resize_bilinear_op)); |
| 357 | ASSERT_NE(nullptr, resize_bilinear_op); |
| 358 | |
| 359 | // Smart pointer to automatically delete resize_bilinear_op. |
| 360 | std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_resize_bilinear_op(resize_bilinear_op, xnn_delete_operator); |
| 361 | |
| 362 | ASSERT_EQ(xnn_status_success, |
| 363 | xnn_setup_resize_bilinear2d_nchw_f32( |
| 364 | resize_bilinear_op, |
| 365 | batch_size(), input_height(), input_width(), |
| 366 | output_height(), output_width(), |
| 367 | input.data(), output.data(), |
| 368 | nullptr /* thread pool */)); |
| 369 | |
| 370 | ASSERT_EQ(xnn_status_success, |
| 371 | xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); |
| 372 | |
| 373 | // Verify results. |
| 374 | for (size_t i = 0; i < batch_size(); i++) { |
| 375 | for (size_t y = 0; y < output_height(); y++) { |
| 376 | for (size_t x = 0; x < output_width(); x++) { |
| 377 | for (size_t c = 0; c < channels(); c++) { |
| 378 | ASSERT_NEAR(output[i * output_num_elements + c * output_num_pixels + y * output_width() + x], |
| 379 | output_ref[i * output_num_elements + c * output_num_pixels + y * output_width() + x], |
| 380 | 1.0e-6f) << |
| 381 | "in batch index " << i << ", pixel (" << y << ", " << x << "), channel " << c; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [diff] [blame] | 389 | // void TestSetupF32() const { |
| 390 | // std::random_device random_device; |
| 391 | // auto rng = std::mt19937(random_device()); |
| 392 | // auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng); |
| 393 | |
| 394 | // std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + std::max( |
| 395 | // (batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels(), |
| 396 | // (next_batch_size() * next_input_height() * next_input_width() - 1) * input_pixel_stride() + channels())); |
| 397 | // std::vector<float> output(std::max( |
| 398 | // (batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels(), |
| 399 | // (next_batch_size() * next_output_height() * next_output_width() - 1) * output_pixel_stride() + channels())); |
| 400 | // std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels()); |
| 401 | // std::vector<float> next_output_ref(next_batch_size() * next_output_height() * next_output_width() * channels()); |
| 402 | // for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 403 | // std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 404 | // std::fill(output.begin(), output.end(), std::nanf("")); |
| 405 | |
| 406 | // // Compute reference results, without clamping. |
| 407 | // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { |
| 408 | // for (size_t output_y = 0; output_y < output_height(); output_y++) { |
| 409 | // for (size_t output_x = 0; output_x < output_width(); output_x++) { |
| 410 | // for (size_t c = 0; c < channels(); c++) { |
| 411 | // float acc = 0.0f; |
| 412 | // size_t n = 0; |
| 413 | // for (size_t py = 0; py < pooling_height(); py++) { |
| 414 | // const size_t iy = output_y * stride_height() + py - padding_top(); |
| 415 | // for (size_t px = 0; px < pooling_width(); px++) { |
| 416 | // const size_t input_x = output_x * stride_width() + px - padding_left(); |
| 417 | // if (input_x < input_width() && iy < input_height()) { |
| 418 | // acc += input[((batch_index * input_height() + iy) * input_width() + input_x) * input_pixel_stride() + c]; |
| 419 | // n += 1; |
| 420 | // } |
| 421 | // } |
| 422 | // } |
| 423 | // output_ref[((batch_index * output_height() + output_y) * output_width() + output_x) * channels() + c] = acc / float(n); |
| 424 | // } |
| 425 | // } |
| 426 | // } |
| 427 | // } |
| 428 | |
| 429 | // // Compute clamping parameters. |
| 430 | // const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend()); |
| 431 | // const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend()); |
| 432 | // const float accumulated_range = accumulated_max - accumulated_min; |
| 433 | // const float output_min = accumulated_range == 0.0f ? |
| 434 | // -std::numeric_limits<float>::infinity() : |
| 435 | // accumulated_min + accumulated_range / 255.0f * float(qmin()); |
| 436 | // const float output_max = accumulated_range == 0.0f ? |
| 437 | // +std::numeric_limits<float>::infinity() : |
| 438 | // accumulated_max - accumulated_range / 255.0f * float(255 - qmax()); |
| 439 | |
| 440 | // // Clamp reference results. |
| 441 | // for (float& value : output_ref) { |
| 442 | // value = std::max(std::min(value, output_max), output_min); |
| 443 | // } |
| 444 | |
| 445 | // // Create, setup, and run Average Pooling operator once. |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 446 | // ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); |
Marat Dukhan | 6972249 | 2019-11-11 19:55:50 -0800 | [diff] [blame] | 447 | // xnn_operator_t resize_bilinear_op = nullptr; |
| 448 | |
| 449 | // ASSERT_EQ(xnn_status_success, |
| 450 | // xnn_create_average_pooling2d_nhwc_f32( |
| 451 | // padding_top(), padding_right(), padding_bottom(), padding_left(), |
| 452 | // pooling_height(), pooling_width(), |
| 453 | // stride_height(), stride_width(), |
| 454 | // channels(), input_pixel_stride(), output_pixel_stride(), |
| 455 | // output_min, output_max, |
| 456 | // 0, &resize_bilinear_op)); |
| 457 | // ASSERT_NE(nullptr, resize_bilinear_op); |
| 458 | |
| 459 | // ASSERT_EQ(xnn_status_success, |
| 460 | // xnn_setup_average_pooling2d_nhwc_f32( |
| 461 | // resize_bilinear_op, |
| 462 | // batch_size(), input_height(), input_width(), |
| 463 | // input.data(), output.data(), |
| 464 | // nullptr /* thread pool */)); |
| 465 | |
| 466 | // ASSERT_EQ(xnn_status_success, |
| 467 | // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); |
| 468 | |
| 469 | // // Verify results of the first run. |
| 470 | // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) { |
| 471 | // for (size_t y = 0; y < output_height(); y++) { |
| 472 | // for (size_t x = 0; x < output_width(); x++) { |
| 473 | // for (size_t c = 0; c < channels(); c++) { |
| 474 | // ASSERT_LE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_max); |
| 475 | // ASSERT_GE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_min); |
| 476 | // ASSERT_NEAR(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], |
| 477 | // output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c], |
| 478 | // std::abs(output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c]) * 1.0e-6f) << |
| 479 | // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c; |
| 480 | // } |
| 481 | // } |
| 482 | // } |
| 483 | // } |
| 484 | |
| 485 | // // Re-generate data for the second run. |
| 486 | // std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 487 | // std::fill(output.begin(), output.end(), std::nanf("")); |
| 488 | |
| 489 | // // Compute reference results for the second run. |
| 490 | // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) { |
| 491 | // for (size_t output_y = 0; output_y < next_output_height(); output_y++) { |
| 492 | // for (size_t output_x = 0; output_x < next_output_width(); output_x++) { |
| 493 | // for (size_t c = 0; c < channels(); c++) { |
| 494 | // float acc = 0.0f; |
| 495 | // int32_t n = 0; |
| 496 | // for (size_t py = 0; py < pooling_height(); py++) { |
| 497 | // const size_t iy = output_y * stride_height() + py - padding_top(); |
| 498 | // for (size_t px = 0; px < pooling_width(); px++) { |
| 499 | // const size_t input_x = output_x * stride_width() + px - padding_left(); |
| 500 | // if (input_x < next_input_width() && iy < next_input_height()) { |
| 501 | // acc += input[((batch_index * next_input_height() + iy) * next_input_width() + input_x) * input_pixel_stride() + c]; |
| 502 | // n += 1; |
| 503 | // } |
| 504 | // } |
| 505 | // } |
| 506 | // next_output_ref[((batch_index * next_output_height() + output_y) * next_output_width() + output_x) * channels() + c] = |
| 507 | // std::max(std::min(acc / float(n), output_max), output_min); |
| 508 | // } |
| 509 | // } |
| 510 | // } |
| 511 | // } |
| 512 | |
| 513 | // // Setup and run Average Pooling operator the second time, and destroutput_y the operator. |
| 514 | // ASSERT_EQ(xnn_status_success, |
| 515 | // xnn_setup_average_pooling2d_nhwc_f32( |
| 516 | // resize_bilinear_op, |
| 517 | // next_batch_size(), next_input_height(), next_input_width(), |
| 518 | // input.data(), output.data(), |
| 519 | // nullptr /* thread pool */)); |
| 520 | |
| 521 | // ASSERT_EQ(xnn_status_success, |
| 522 | // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */)); |
| 523 | |
| 524 | // ASSERT_EQ(xnn_status_success, |
| 525 | // xnn_delete_operator(resize_bilinear_op)); |
| 526 | // resize_bilinear_op = nullptr; |
| 527 | |
| 528 | // // Verify results of the second run. |
| 529 | // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) { |
| 530 | // for (size_t y = 0; y < next_output_height(); y++) { |
| 531 | // for (size_t x = 0; x < next_output_width(); x++) { |
| 532 | // for (size_t c = 0; c < channels(); c++) { |
| 533 | // ASSERT_LE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_max); |
| 534 | // ASSERT_GE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_min); |
| 535 | // ASSERT_NEAR(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], |
| 536 | // next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c], |
| 537 | // std::abs(next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c]) * 1.0e-6f) << |
| 538 | // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c; |
| 539 | // } |
| 540 | // } |
| 541 | // } |
| 542 | // } |
| 543 | // } |
| 544 | // } |
| 545 | |
| 546 | private: |
| 547 | size_t input_height_{1}; |
| 548 | size_t input_width_{1}; |
| 549 | size_t output_height_{1}; |
| 550 | size_t output_width_{1}; |
| 551 | size_t channels_{1}; |
| 552 | size_t batch_size_{1}; |
| 553 | size_t input_pixel_stride_{0}; |
| 554 | size_t output_pixel_stride_{0}; |
| 555 | size_t next_input_height_{0}; |
| 556 | size_t next_input_width_{0}; |
| 557 | size_t next_batch_size_{0}; |
| 558 | bool align_corners_{false}; |
| 559 | bool tf_legacy_mode_{false}; |
| 560 | size_t iterations_{1}; |
| 561 | }; |