Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 1 | /* |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +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 | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 49 | void setup(TensorShape shape, DataType 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] | 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 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 62 | int16_t conv[width * height]; |
| 63 | create_conv(conv); |
| 64 | |
| 65 | _width = width; |
| 66 | _height = height; |
| 67 | _target = compute_target(shape, data_type, conv, scale, border_mode, constant_border_value); |
| 68 | _reference = compute_reference(shape, data_type, conv, scale, border_mode, constant_border_value); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 71 | void |
| 72 | create_conv(int16_t *conv) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 73 | { |
| 74 | std::mt19937 gen(library->seed()); |
| 75 | std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767); |
| 76 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 77 | for(unsigned int i = 0; i < _width * _height; ++i) |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 78 | { |
| 79 | conv[i] = distribution_int16(gen); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | template <typename U> |
| 84 | void fill(U &&tensor, int i) |
| 85 | { |
| 86 | library->fill_tensor_uniform(tensor, i); |
| 87 | } |
| 88 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 89 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(data_type != DataType::U8); |
| 92 | |
| 93 | // Create reference |
| 94 | SimpleTensor<T> src{ shape, data_type }; |
| 95 | |
| 96 | // Fill reference |
| 97 | fill(src, 0); |
| 98 | |
| 99 | // Compute reference |
| 100 | return reference::convolution<T>(src, conv, scale, border_mode, constant_border_value, _width, _height); |
| 101 | } |
| 102 | |
| 103 | virtual TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) = 0; |
| 104 | |
| 105 | BorderMode _border_mode{}; |
| 106 | TensorType _target{}; |
| 107 | SimpleTensor<T> _reference{}; |
| 108 | unsigned int _width{}; |
| 109 | unsigned int _height{}; |
| 110 | }; |
| 111 | |
| 112 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 113 | class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 114 | { |
| 115 | public: |
| 116 | template <typename...> |
| 117 | void setup(TensorShape shape, DataType data_type, BorderMode border_mode, const unsigned int width) |
| 118 | { |
| 119 | ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, border_mode, width, width); |
| 120 | } |
| 121 | |
| 122 | protected: |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 123 | TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
| 124 | { |
| 125 | // Create tensors |
| 126 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 127 | TensorType dst = create_tensor<TensorType>(shape, data_type); |
| 128 | |
| 129 | // Create and configure function |
| 130 | FunctionType convolution; |
| 131 | convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value); |
| 132 | |
| 133 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 134 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 135 | |
| 136 | // Allocate tensors |
| 137 | src.allocator()->allocate(); |
| 138 | dst.allocator()->allocate(); |
| 139 | |
| 140 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 141 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 142 | |
| 143 | // Fill tensors |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 144 | this->fill(AccessorType(src), 0); |
| 145 | this->fill(AccessorType(dst), 1); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 146 | |
| 147 | // Compute function |
| 148 | convolution.run(); |
| 149 | |
| 150 | return dst; |
| 151 | } |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 152 | }; |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 153 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 154 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 155 | class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 156 | { |
| 157 | public: |
| 158 | template <typename...> |
| 159 | void setup(TensorShape shape, DataType 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] | 160 | { |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 161 | ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, border_mode, width, height); |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 164 | protected: |
| 165 | TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) |
| 166 | { |
| 167 | // Create tensors |
| 168 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 169 | TensorType dst = create_tensor<TensorType>(shape, data_type); |
| 170 | |
| 171 | // Create and configure function |
| 172 | FunctionType convolution; |
| 173 | convolution.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value); |
| 174 | |
| 175 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 176 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 177 | |
| 178 | // Allocate tensors |
| 179 | src.allocator()->allocate(); |
| 180 | dst.allocator()->allocate(); |
| 181 | |
| 182 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 183 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 184 | |
| 185 | // Fill tensors |
| 186 | this->fill(AccessorType(src), 0); |
| 187 | this->fill(AccessorType(dst), 1); |
| 188 | |
| 189 | // Compute function |
| 190 | convolution.run(); |
| 191 | |
| 192 | return dst; |
| 193 | } |
Sanghoon Lee | c8a85ba | 2017-11-29 11:23:14 +0000 | [diff] [blame] | 194 | }; |
| 195 | } // namespace validation |
| 196 | } // namespace test |
| 197 | } // namespace arm_compute |
| 198 | #endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */ |