XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [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 <cstddef> |
| 13 | #include <cstdlib> |
| 14 | #include <functional> |
| 15 | #include <random> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <xnnpack.h> |
| 19 | |
| 20 | |
| 21 | class ChannelPadOperatorTester { |
| 22 | public: |
| 23 | inline ChannelPadOperatorTester& batch_size(size_t batch_size) { |
| 24 | assert(batch_size != 0); |
| 25 | this->batch_size_ = batch_size; |
| 26 | return *this; |
| 27 | } |
| 28 | |
| 29 | inline size_t batch_size() const { |
| 30 | return this->batch_size_; |
| 31 | } |
| 32 | |
| 33 | inline ChannelPadOperatorTester& input_channels(size_t input_channels) { |
| 34 | assert(input_channels != 0); |
| 35 | this->input_channels_ = input_channels; |
| 36 | return *this; |
| 37 | } |
| 38 | |
| 39 | inline size_t input_channels() const { |
| 40 | return this->input_channels_; |
| 41 | } |
| 42 | |
| 43 | inline ChannelPadOperatorTester& pad_before(size_t pad_before) { |
| 44 | this->pad_before_ = pad_before; |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | inline size_t pad_before() const { |
| 49 | return this->pad_before_; |
| 50 | } |
| 51 | |
| 52 | inline ChannelPadOperatorTester& pad_after(size_t pad_after) { |
| 53 | this->pad_after_ = pad_after; |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | inline size_t pad_after() const { |
| 58 | return this->pad_after_; |
| 59 | } |
| 60 | |
| 61 | inline size_t output_channels() const { |
| 62 | return pad_before() + input_channels() + pad_after(); |
| 63 | } |
| 64 | |
| 65 | inline ChannelPadOperatorTester& input_stride(size_t input_stride) { |
| 66 | assert(input_stride != 0); |
| 67 | this->input_stride_ = input_stride; |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | inline size_t input_stride() const { |
| 72 | if (this->input_stride_ == 0) { |
| 73 | return this->input_channels_; |
| 74 | } else { |
| 75 | assert(this->input_stride_ >= this->input_channels_); |
| 76 | return this->input_stride_; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | inline ChannelPadOperatorTester& output_stride(size_t output_stride) { |
| 81 | assert(output_stride != 0); |
| 82 | this->output_stride_ = output_stride; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | inline size_t output_stride() const { |
| 87 | if (this->output_stride_ == 0) { |
| 88 | return output_channels(); |
| 89 | } else { |
| 90 | assert(this->output_stride_ >= output_channels()); |
| 91 | return this->output_stride_; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | inline ChannelPadOperatorTester& iterations(size_t iterations) { |
| 96 | this->iterations_ = iterations; |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | inline size_t iterations() const { |
| 101 | return this->iterations_; |
| 102 | } |
| 103 | |
| 104 | void TestX32() const { |
| 105 | std::random_device random_device; |
| 106 | auto rng = std::mt19937(random_device()); |
| 107 | auto u32rng = std::bind(std::uniform_int_distribution<uint32_t>(), rng); |
| 108 | |
| 109 | const uint32_t pad_value = u32rng(); |
| 110 | std::vector<uint32_t> input(input_channels() + (batch_size() - 1) * input_stride() + XNN_EXTRA_BYTES / sizeof(uint32_t)); |
| 111 | std::vector<uint32_t> output(output_channels() + (batch_size() - 1) * output_stride()); |
| 112 | for (size_t iteration = 0; iteration < iterations(); iteration++) { |
| 113 | std::generate(input.begin(), input.end(), std::ref(u32rng)); |
| 114 | std::generate(output.begin(), output.end(), std::ref(u32rng)); |
| 115 | |
| 116 | // Create, setup, run, and destroy Channel Pad operator. |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 117 | ASSERT_EQ(xnn_status_success, xnn_initialize(nullptr /* allocator */)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 118 | xnn_operator_t channel_pad_op = nullptr; |
| 119 | |
| 120 | ASSERT_EQ(xnn_status_success, |
| 121 | xnn_create_channel_pad_nc_x32( |
| 122 | input_channels(), pad_before(), pad_after(), |
| 123 | input_stride(), output_stride(), |
| 124 | &pad_value, |
| 125 | 0 /* flags */, |
| 126 | &channel_pad_op)); |
| 127 | ASSERT_NE(nullptr, channel_pad_op); |
| 128 | |
| 129 | // Smart pointer to automatically delete channel_pad_op. |
| 130 | std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)> auto_channel_pad_op(channel_pad_op, xnn_delete_operator); |
| 131 | |
| 132 | ASSERT_EQ(xnn_status_success, |
| 133 | xnn_setup_channel_pad_nc_x32( |
| 134 | channel_pad_op, |
| 135 | batch_size(), |
| 136 | input.data(), output.data(), |
| 137 | nullptr /* thread pool */)); |
| 138 | |
| 139 | ASSERT_EQ(xnn_status_success, |
| 140 | xnn_run_operator(channel_pad_op, nullptr /* thread pool */)); |
| 141 | |
| 142 | // Verify results. |
| 143 | for (size_t i = 0; i < batch_size(); i++) { |
| 144 | for (size_t k = 0; k < pad_before(); k++) { |
| 145 | ASSERT_EQ(pad_value, |
| 146 | output[i * output_stride() + k]); |
| 147 | } |
| 148 | for (size_t k = 0; k < input_channels(); k++) { |
| 149 | ASSERT_EQ(input[i * input_stride() + k], |
| 150 | output[i * output_stride() + pad_before() + k]); |
| 151 | } |
| 152 | for (size_t k = 0; k < pad_after(); k++) { |
| 153 | ASSERT_EQ(pad_value, |
| 154 | output[i * output_stride() + pad_before() + input_channels() + k]); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | size_t batch_size_{1}; |
| 162 | size_t input_channels_{1}; |
| 163 | size_t pad_before_{0}; |
| 164 | size_t pad_after_{0}; |
| 165 | size_t input_stride_{0}; |
| 166 | size_t output_stride_{0}; |
| 167 | size_t iterations_{15}; |
| 168 | }; |