blob: a1d2b7c6cb03b2b210d49585588492e3af32a301 [file] [log] [blame]
Marat Dukhan69722492019-11-11 19:55:50 -08001// 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
22class 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
223 void TestF32() const {
Marat Dukhanf5c46252020-05-22 10:36:13 -0700224 if (align_corners()) {
225 ASSERT_FALSE(tf_legacy_mode());
226 }
227
Marat Dukhan69722492019-11-11 19:55:50 -0800228 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 Dukhanf5c46252020-05-22 10:36:13 -0700240 const float offset = (tf_legacy_mode() || align_corners()) ? 0.0f : 0.5f;
Marat Dukhan69722492019-11-11 19:55:50 -0800241 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 Dukhan04f03be2019-11-19 12:36:47 -0800264 ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
Marat Dukhan69722492019-11-11 19:55:50 -0800265 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
304 // void TestSetupF32() const {
305 // std::random_device random_device;
306 // auto rng = std::mt19937(random_device());
307 // auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
308
309 // std::vector<float> input(XNN_EXTRA_BYTES / sizeof(float) + std::max(
310 // (batch_size() * input_height() * input_width() - 1) * input_pixel_stride() + channels(),
311 // (next_batch_size() * next_input_height() * next_input_width() - 1) * input_pixel_stride() + channels()));
312 // std::vector<float> output(std::max(
313 // (batch_size() * output_height() * output_width() - 1) * output_pixel_stride() + channels(),
314 // (next_batch_size() * next_output_height() * next_output_width() - 1) * output_pixel_stride() + channels()));
315 // std::vector<float> output_ref(batch_size() * output_height() * output_width() * channels());
316 // std::vector<float> next_output_ref(next_batch_size() * next_output_height() * next_output_width() * channels());
317 // for (size_t iteration = 0; iteration < iterations(); iteration++) {
318 // std::generate(input.begin(), input.end(), std::ref(f32rng));
319 // std::fill(output.begin(), output.end(), std::nanf(""));
320
321 // // Compute reference results, without clamping.
322 // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) {
323 // for (size_t output_y = 0; output_y < output_height(); output_y++) {
324 // for (size_t output_x = 0; output_x < output_width(); output_x++) {
325 // for (size_t c = 0; c < channels(); c++) {
326 // float acc = 0.0f;
327 // size_t n = 0;
328 // for (size_t py = 0; py < pooling_height(); py++) {
329 // const size_t iy = output_y * stride_height() + py - padding_top();
330 // for (size_t px = 0; px < pooling_width(); px++) {
331 // const size_t input_x = output_x * stride_width() + px - padding_left();
332 // if (input_x < input_width() && iy < input_height()) {
333 // acc += input[((batch_index * input_height() + iy) * input_width() + input_x) * input_pixel_stride() + c];
334 // n += 1;
335 // }
336 // }
337 // }
338 // output_ref[((batch_index * output_height() + output_y) * output_width() + output_x) * channels() + c] = acc / float(n);
339 // }
340 // }
341 // }
342 // }
343
344 // // Compute clamping parameters.
345 // const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
346 // const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
347 // const float accumulated_range = accumulated_max - accumulated_min;
348 // const float output_min = accumulated_range == 0.0f ?
349 // -std::numeric_limits<float>::infinity() :
350 // accumulated_min + accumulated_range / 255.0f * float(qmin());
351 // const float output_max = accumulated_range == 0.0f ?
352 // +std::numeric_limits<float>::infinity() :
353 // accumulated_max - accumulated_range / 255.0f * float(255 - qmax());
354
355 // // Clamp reference results.
356 // for (float& value : output_ref) {
357 // value = std::max(std::min(value, output_max), output_min);
358 // }
359
360 // // Create, setup, and run Average Pooling operator once.
Marat Dukhan04f03be2019-11-19 12:36:47 -0800361 // ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */));
Marat Dukhan69722492019-11-11 19:55:50 -0800362 // xnn_operator_t resize_bilinear_op = nullptr;
363
364 // ASSERT_EQ(xnn_status_success,
365 // xnn_create_average_pooling2d_nhwc_f32(
366 // padding_top(), padding_right(), padding_bottom(), padding_left(),
367 // pooling_height(), pooling_width(),
368 // stride_height(), stride_width(),
369 // channels(), input_pixel_stride(), output_pixel_stride(),
370 // output_min, output_max,
371 // 0, &resize_bilinear_op));
372 // ASSERT_NE(nullptr, resize_bilinear_op);
373
374 // ASSERT_EQ(xnn_status_success,
375 // xnn_setup_average_pooling2d_nhwc_f32(
376 // resize_bilinear_op,
377 // batch_size(), input_height(), input_width(),
378 // input.data(), output.data(),
379 // nullptr /* thread pool */));
380
381 // ASSERT_EQ(xnn_status_success,
382 // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */));
383
384 // // Verify results of the first run.
385 // for (size_t batch_index = 0; batch_index < batch_size(); batch_index++) {
386 // for (size_t y = 0; y < output_height(); y++) {
387 // for (size_t x = 0; x < output_width(); x++) {
388 // for (size_t c = 0; c < channels(); c++) {
389 // ASSERT_LE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_max);
390 // ASSERT_GE(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c], output_min);
391 // ASSERT_NEAR(output[((batch_index * output_height() + y) * output_width() + x) * output_pixel_stride() + c],
392 // output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c],
393 // std::abs(output_ref[((batch_index * output_height() + y) * output_width() + x) * channels() + c]) * 1.0e-6f) <<
394 // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c;
395 // }
396 // }
397 // }
398 // }
399
400 // // Re-generate data for the second run.
401 // std::generate(input.begin(), input.end(), std::ref(f32rng));
402 // std::fill(output.begin(), output.end(), std::nanf(""));
403
404 // // Compute reference results for the second run.
405 // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) {
406 // for (size_t output_y = 0; output_y < next_output_height(); output_y++) {
407 // for (size_t output_x = 0; output_x < next_output_width(); output_x++) {
408 // for (size_t c = 0; c < channels(); c++) {
409 // float acc = 0.0f;
410 // int32_t n = 0;
411 // for (size_t py = 0; py < pooling_height(); py++) {
412 // const size_t iy = output_y * stride_height() + py - padding_top();
413 // for (size_t px = 0; px < pooling_width(); px++) {
414 // const size_t input_x = output_x * stride_width() + px - padding_left();
415 // if (input_x < next_input_width() && iy < next_input_height()) {
416 // acc += input[((batch_index * next_input_height() + iy) * next_input_width() + input_x) * input_pixel_stride() + c];
417 // n += 1;
418 // }
419 // }
420 // }
421 // next_output_ref[((batch_index * next_output_height() + output_y) * next_output_width() + output_x) * channels() + c] =
422 // std::max(std::min(acc / float(n), output_max), output_min);
423 // }
424 // }
425 // }
426 // }
427
428 // // Setup and run Average Pooling operator the second time, and destroutput_y the operator.
429 // ASSERT_EQ(xnn_status_success,
430 // xnn_setup_average_pooling2d_nhwc_f32(
431 // resize_bilinear_op,
432 // next_batch_size(), next_input_height(), next_input_width(),
433 // input.data(), output.data(),
434 // nullptr /* thread pool */));
435
436 // ASSERT_EQ(xnn_status_success,
437 // xnn_run_operator(resize_bilinear_op, nullptr /* thread pool */));
438
439 // ASSERT_EQ(xnn_status_success,
440 // xnn_delete_operator(resize_bilinear_op));
441 // resize_bilinear_op = nullptr;
442
443 // // Verify results of the second run.
444 // for (size_t batch_index = 0; batch_index < next_batch_size(); batch_index++) {
445 // for (size_t y = 0; y < next_output_height(); y++) {
446 // for (size_t x = 0; x < next_output_width(); x++) {
447 // for (size_t c = 0; c < channels(); c++) {
448 // ASSERT_LE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_max);
449 // ASSERT_GE(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c], output_min);
450 // ASSERT_NEAR(output[((batch_index * next_output_height() + y) * next_output_width() + x) * output_pixel_stride() + c],
451 // next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c],
452 // std::abs(next_output_ref[((batch_index * next_output_height() + y) * next_output_width() + x) * channels() + c]) * 1.0e-6f) <<
453 // "in batch index " << batch_index << ", pixel (" << y << ", " << x << "), channel " << c;
454 // }
455 // }
456 // }
457 // }
458 // }
459 // }
460
461 private:
462 size_t input_height_{1};
463 size_t input_width_{1};
464 size_t output_height_{1};
465 size_t output_width_{1};
466 size_t channels_{1};
467 size_t batch_size_{1};
468 size_t input_pixel_stride_{0};
469 size_t output_pixel_stride_{0};
470 size_t next_input_height_{0};
471 size_t next_input_width_{0};
472 size_t next_batch_size_{0};
473 bool align_corners_{false};
474 bool tf_legacy_mode_{false};
475 size_t iterations_{1};
476};