blob: 8ebb924c659938e427c283f79c1ca5022a354ce1 [file] [log] [blame]
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00001/*
Sanghoon Leec8d23162018-01-12 16:19:10 +00002 * Copyright (c) 2017-2018 ARM Limited.
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00003 *
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 Pinitasc7951962017-12-12 17:58:11 +000034#include "tests/validation/reference/Convolution.h"
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000035
36#include <random>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Sanghoon Leed7ba5392017-12-13 11:28:50 +000044template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000045class ConvolutionValidationFixture : public framework::Fixture
46{
Sanghoon Leed7ba5392017-12-13 11:28:50 +000047protected:
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000048 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +000049 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 Leec8a85ba2017-11-29 11:23:14 +000050 {
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 Leed7ba5392017-12-13 11:28:50 +000055 // 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 Leec8a85ba2017-11-29 11:23:14 +000058
Sanghoon Leed7ba5392017-12-13 11:28:50 +000059 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 Leec8a85ba2017-11-29 11:23:14 +000061
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000062 std::vector<int16_t> conv(width * height);
Sanghoon Leed7ba5392017-12-13 11:28:50 +000063
Sanghoon Lee571b18a2017-12-22 16:20:11 +000064 _width = width;
65 _height = height;
66
67 if(is_separable)
68 {
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000069 create_separable_conv(conv.data());
Sanghoon Lee571b18a2017-12-22 16:20:11 +000070 }
71 else
72 {
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000073 create_conv(conv.data());
Sanghoon Lee571b18a2017-12-22 16:20:11 +000074 }
75
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000076 _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 Leec8a85ba2017-11-29 11:23:14 +000078 }
79
Sanghoon Leed7ba5392017-12-13 11:28:50 +000080 void
81 create_conv(int16_t *conv)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000082 {
83 std::mt19937 gen(library->seed());
84 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
85
Sanghoon Leed7ba5392017-12-13 11:28:50 +000086 for(unsigned int i = 0; i < _width * _height; ++i)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000087 {
88 conv[i] = distribution_int16(gen);
89 }
90 }
91
Sanghoon Lee571b18a2017-12-22 16:20:11 +000092 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 Leec8a85ba2017-11-29 11:23:14 +0000119 template <typename U>
120 void fill(U &&tensor, int i)
121 {
122 library->fill_tensor_uniform(tensor, i);
123 }
124
Sanghoon Leec8d23162018-01-12 16:19:10 +0000125 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 Leed7ba5392017-12-13 11:28:50 +0000126 {
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000127 // Create reference
Sanghoon Leec8d23162018-01-12 16:19:10 +0000128 SimpleTensor<uint8_t> src{ shape, DataType::U8 };
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000129
130 // Fill reference
131 fill(src, 0);
132
133 // Compute reference
Sanghoon Leec8d23162018-01-12 16:19:10 +0000134 return reference::convolution<T>(src, output_data_type, conv, scale, border_mode, constant_border_value, _width, _height);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000135 }
136
Sanghoon Leec8d23162018-01-12 16:19:10 +0000137 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 Leed7ba5392017-12-13 11:28:50 +0000138
139 BorderMode _border_mode{};
140 TensorType _target{};
141 SimpleTensor<T> _reference{};
142 unsigned int _width{};
143 unsigned int _height{};
144};
145
146template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
147class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
148{
149public:
150 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000151 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000152 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000153 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000154 }
155
156protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000157 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 Leec8a85ba2017-11-29 11:23:14 +0000158 {
159 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000160 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
161 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000162
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 Leed7ba5392017-12-13 11:28:50 +0000178 this->fill(AccessorType(src), 0);
179 this->fill(AccessorType(dst), 1);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000180
181 // Compute function
182 convolution.run();
183
184 return dst;
185 }
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000186};
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000187
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000188template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000189class ConvolutionSeparableValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
190{
191public:
192 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000193 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000194 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000195 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width, true);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000196 }
197
198protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000199 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 Lee571b18a2017-12-22 16:20:11 +0000200 {
201 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000202 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
203 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000204
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
230template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000231class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
232{
233public:
234 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000235 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width, const unsigned int height)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000236 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000237 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, height);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000238 }
239
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000240protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000241 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 Leed7ba5392017-12-13 11:28:50 +0000242 {
243 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000244 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
245 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000246
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 Leec8a85ba2017-11-29 11:23:14 +0000270};
271} // namespace validation
272} // namespace test
273} // namespace arm_compute
274#endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */