Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 1 | /* |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | c795196 | 2017-12-12 17:58:11 +0000 | [diff] [blame] | 34 | #include "tests/validation/reference/Convolution.h" |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 35 | |
| 36 | #include <random> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 45 | class ConvolutionValidationFixture : public framework::Fixture |
| 46 | { |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 47 | protected: |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 48 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 49 | void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width, const unsigned int height, const bool is_separable = false) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 50 | { |
| 51 | std::mt19937 gen(library->seed()); |
| 52 | std::uniform_int_distribution<uint8_t> distribution(0, 255); |
| 53 | const uint8_t constant_border_value = distribution(gen); |
| 54 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 55 | // Generate random scale value between 1 and 255. |
| 56 | std::uniform_int_distribution<uint8_t> distribution_scale(1, 255); |
| 57 | const uint32_t scale = distribution_scale(gen); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 58 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width); |
| 60 | ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 61 | |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame^] | 62 | std::vector<int16_t> conv(width * height); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 63 | |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 64 | _width = width; |
| 65 | _height = height; |
| 66 | |
| 67 | if(is_separable) |
| 68 | { |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame^] | 69 | create_separable_conv(conv.data()); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 70 | } |
| 71 | else |
| 72 | { |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame^] | 73 | create_conv(conv.data()); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame^] | 76 | _target = compute_target(shape, output_data_type, conv.data(), scale, border_mode, constant_border_value); |
| 77 | _reference = compute_reference(shape, output_data_type, conv.data(), scale, border_mode, constant_border_value); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 80 | void |
| 81 | create_conv(int16_t *conv) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 82 | { |
| 83 | std::mt19937 gen(library->seed()); |
| 84 | std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767); |
| 85 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 86 | for(unsigned int i = 0; i < _width * _height; ++i) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 87 | { |
| 88 | conv[i] = distribution_int16(gen); |
| 89 | } |
| 90 | } |
| 91 | |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 92 | void |
| 93 | create_separable_conv(int16_t *conv) |
| 94 | { |
| 95 | std::mt19937 gen(library->seed()); |
| 96 | // Set it between -128 and 127 to ensure the matrix does not overflow |
| 97 | std::uniform_int_distribution<int16_t> distribution_int16(-128, 127); |
| 98 | |
| 99 | int16_t conv_row[_width]; |
| 100 | int16_t conv_col[_height]; |
| 101 | |
| 102 | conv_row[0] = conv_col[0] = 1; |
| 103 | for(unsigned int i = 1; i < _width; ++i) |
| 104 | { |
| 105 | conv_row[i] = distribution_int16(gen); |
| 106 | conv_col[i] = distribution_int16(gen); |
| 107 | } |
| 108 | |
| 109 | // Multiply two matrices |
| 110 | for(unsigned int i = 0; i < _width; ++i) |
| 111 | { |
| 112 | for(unsigned int j = 0; j < _height; ++j) |
| 113 | { |
| 114 | conv[i * _width + j] = conv_col[i] * conv_row[j]; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 119 | template <typename U> |
| 120 | void fill(U &&tensor, int i) |
| 121 | { |
| 122 | library->fill_tensor_uniform(tensor, i); |
| 123 | } |
| 124 | |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 125 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType output_data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 126 | { |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 127 | // Create reference |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 128 | SimpleTensor<uint8_t> src{ shape, DataType::U8 }; |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 129 | |
| 130 | // Fill reference |
| 131 | fill(src, 0); |
| 132 | |
| 133 | // Compute reference |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 134 | return reference::convolution<T>(src, output_data_type, conv, scale, border_mode, constant_border_value, _width, _height); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 137 | virtual TensorType compute_target(const TensorShape &shape, DataType output_data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) = 0; |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 138 | |
| 139 | BorderMode _border_mode{}; |
| 140 | TensorType _target{}; |
| 141 | SimpleTensor<T> _reference{}; |
| 142 | unsigned int _width{}; |
| 143 | unsigned int _height{}; |
| 144 | }; |
| 145 | |
| 146 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 147 | class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 148 | { |
| 149 | public: |
| 150 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 151 | void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width) |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 152 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 153 | ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 157 | TensorType compute_target(const TensorShape &shape, DataType output_data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 158 | { |
| 159 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 160 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 161 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 162 | |
| 163 | // Create and configure function |
| 164 | FunctionType convolution; |
| 165 | convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 166 | |
| 167 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 168 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 169 | |
| 170 | // Allocate tensors |
| 171 | src.allocator()->allocate(); |
| 172 | dst.allocator()->allocate(); |
| 173 | |
| 174 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 175 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 176 | |
| 177 | // Fill tensors |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 178 | this->fill(AccessorType(src), 0); |
| 179 | this->fill(AccessorType(dst), 1); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 180 | |
| 181 | // Compute function |
| 182 | convolution.run(); |
| 183 | |
| 184 | return dst; |
| 185 | } |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 186 | }; |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 187 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 188 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 189 | class ConvolutionSeparableValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 190 | { |
| 191 | public: |
| 192 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 193 | void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width) |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 194 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 195 | ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width, true); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 199 | TensorType compute_target(const TensorShape &shape, DataType output_data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 200 | { |
| 201 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 202 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 203 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 204 | |
| 205 | // Create and configure function |
| 206 | FunctionType convolution; |
| 207 | convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 208 | |
| 209 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 210 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 211 | |
| 212 | // Allocate tensors |
| 213 | src.allocator()->allocate(); |
| 214 | dst.allocator()->allocate(); |
| 215 | |
| 216 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 217 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 218 | |
| 219 | // Fill tensors |
| 220 | this->fill(AccessorType(src), 0); |
| 221 | this->fill(AccessorType(dst), 1); |
| 222 | |
| 223 | // Compute function |
| 224 | convolution.run(); |
| 225 | |
| 226 | return dst; |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 231 | class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 232 | { |
| 233 | public: |
| 234 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 235 | void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width, const unsigned int height) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 236 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 237 | ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, height); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 240 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 241 | TensorType compute_target(const TensorShape &shape, DataType output_data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 242 | { |
| 243 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 244 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 245 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 246 | |
| 247 | // Create and configure function |
| 248 | FunctionType convolution; |
| 249 | convolution.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value); |
| 250 | |
| 251 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 252 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 253 | |
| 254 | // Allocate tensors |
| 255 | src.allocator()->allocate(); |
| 256 | dst.allocator()->allocate(); |
| 257 | |
| 258 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 259 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 260 | |
| 261 | // Fill tensors |
| 262 | this->fill(AccessorType(src), 0); |
| 263 | this->fill(AccessorType(dst), 1); |
| 264 | |
| 265 | // Compute function |
| 266 | convolution.run(); |
| 267 | |
| 268 | return dst; |
| 269 | } |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 270 | }; |
| 271 | } // namespace validation |
| 272 | } // namespace test |
| 273 | } // namespace arm_compute |
| 274 | #endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */ |