Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
Michalis Spyrou | aeebe4a | 2019-01-09 14:21:03 +0000 | [diff] [blame] | 55 | // Generate random scale value between 0 and 255. |
| 56 | const uint32_t scale = distribution(gen); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 57 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width); |
| 59 | ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 60 | |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame] | 61 | std::vector<int16_t> conv(width * height); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 62 | |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 63 | _width = width; |
| 64 | _height = height; |
| 65 | |
| 66 | if(is_separable) |
| 67 | { |
John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 68 | init_separable_conv(conv.data(), width, height, library->seed()); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 69 | } |
| 70 | else |
| 71 | { |
John Richardson | 32af1f8 | 2018-06-05 12:47:20 +0100 | [diff] [blame] | 72 | init_conv(conv.data(), width, height, library->seed()); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Michalis Spyrou | 5ae9b69 | 2018-02-05 14:59:54 +0000 | [diff] [blame] | 75 | _target = compute_target(shape, output_data_type, conv.data(), scale, border_mode, constant_border_value); |
| 76 | _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] | 77 | } |
| 78 | |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 79 | template <typename U> |
| 80 | void fill(U &&tensor, int i) |
| 81 | { |
| 82 | library->fill_tensor_uniform(tensor, i); |
| 83 | } |
| 84 | |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 85 | 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] | 86 | { |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 87 | // Create reference |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 88 | SimpleTensor<uint8_t> src{ shape, DataType::U8 }; |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 89 | |
| 90 | // Fill reference |
| 91 | fill(src, 0); |
| 92 | |
| 93 | // Compute reference |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 94 | 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] | 95 | } |
| 96 | |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 97 | 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] | 98 | |
| 99 | BorderMode _border_mode{}; |
| 100 | TensorType _target{}; |
| 101 | SimpleTensor<T> _reference{}; |
| 102 | unsigned int _width{}; |
| 103 | unsigned int _height{}; |
| 104 | }; |
| 105 | |
| 106 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 107 | class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 108 | { |
| 109 | public: |
| 110 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 111 | 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] | 112 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 113 | 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] | 114 | } |
| 115 | |
| 116 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 120 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 121 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 122 | |
| 123 | // Create and configure function |
| 124 | FunctionType convolution; |
| 125 | convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 126 | |
| 127 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 128 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 129 | |
| 130 | // Allocate tensors |
| 131 | src.allocator()->allocate(); |
| 132 | dst.allocator()->allocate(); |
| 133 | |
| 134 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 135 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 136 | |
| 137 | // Fill tensors |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 138 | this->fill(AccessorType(src), 0); |
| 139 | this->fill(AccessorType(dst), 1); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 140 | |
| 141 | // Compute function |
| 142 | convolution.run(); |
| 143 | |
| 144 | return dst; |
| 145 | } |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 146 | }; |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 147 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 148 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 149 | class ConvolutionSeparableValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 150 | { |
| 151 | public: |
| 152 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 153 | 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] | 154 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 155 | 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] | 156 | } |
| 157 | |
| 158 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 159 | 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] | 160 | { |
| 161 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 162 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 163 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | 571b18a | 2017-12-22 16:20:11 +0000 | [diff] [blame] | 164 | |
| 165 | // Create and configure function |
| 166 | FunctionType convolution; |
| 167 | convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 168 | |
| 169 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 170 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 171 | |
| 172 | // Allocate tensors |
| 173 | src.allocator()->allocate(); |
| 174 | dst.allocator()->allocate(); |
| 175 | |
| 176 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 177 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 178 | |
| 179 | // Fill tensors |
| 180 | this->fill(AccessorType(src), 0); |
| 181 | this->fill(AccessorType(dst), 1); |
| 182 | |
| 183 | // Compute function |
| 184 | convolution.run(); |
| 185 | |
| 186 | return dst; |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 191 | class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 192 | { |
| 193 | public: |
| 194 | template <typename...> |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 195 | 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] | 196 | { |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 197 | 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] | 198 | } |
| 199 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 200 | protected: |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 201 | 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] | 202 | { |
| 203 | // Create tensors |
Sanghoon Lee | c8d2316 | 2018-01-12 16:19:10 +0000 | [diff] [blame] | 204 | TensorType src = create_tensor<TensorType>(shape, DataType::U8); |
| 205 | TensorType dst = create_tensor<TensorType>(shape, output_data_type); |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 206 | |
| 207 | // Create and configure function |
| 208 | FunctionType convolution; |
| 209 | convolution.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value); |
| 210 | |
| 211 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 212 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 213 | |
| 214 | // Allocate tensors |
| 215 | src.allocator()->allocate(); |
| 216 | dst.allocator()->allocate(); |
| 217 | |
| 218 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 219 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 220 | |
| 221 | // Fill tensors |
| 222 | this->fill(AccessorType(src), 0); |
| 223 | this->fill(AccessorType(dst), 1); |
| 224 | |
| 225 | // Compute function |
| 226 | convolution.run(); |
| 227 | |
| 228 | return dst; |
| 229 | } |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 230 | }; |
| 231 | } // namespace validation |
| 232 | } // namespace test |
| 233 | } // namespace arm_compute |
| 234 | #endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */ |