blob: 4692e2faf84c79318acc9ccd2c1a954016223771 [file] [log] [blame]
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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);
Georgios Pinitas4bce1152020-02-05 16:22:14 +000053 std::uniform_int_distribution<uint8_t> scale_distribution(1, 255);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000054 const uint8_t constant_border_value = distribution(gen);
55
Georgios Pinitas4bce1152020-02-05 16:22:14 +000056 // Generate random scale value between 1 and 255.
57 const uint32_t scale = scale_distribution(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 {
John Richardson32af1f82018-06-05 12:47:20 +010069 init_separable_conv(conv.data(), width, height, library->seed());
Sanghoon Lee571b18a2017-12-22 16:20:11 +000070 }
71 else
72 {
John Richardson32af1f82018-06-05 12:47:20 +010073 init_conv(conv.data(), width, height, library->seed());
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 Leec8a85ba2017-11-29 11:23:14 +000080 template <typename U>
81 void fill(U &&tensor, int i)
82 {
83 library->fill_tensor_uniform(tensor, i);
84 }
85
Sanghoon Leec8d23162018-01-12 16:19:10 +000086 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 +000087 {
Sanghoon Leed7ba5392017-12-13 11:28:50 +000088 // Create reference
Sanghoon Leec8d23162018-01-12 16:19:10 +000089 SimpleTensor<uint8_t> src{ shape, DataType::U8 };
Sanghoon Leed7ba5392017-12-13 11:28:50 +000090
91 // Fill reference
92 fill(src, 0);
93
94 // Compute reference
Sanghoon Leec8d23162018-01-12 16:19:10 +000095 return reference::convolution<T>(src, output_data_type, conv, scale, border_mode, constant_border_value, _width, _height);
Sanghoon Leed7ba5392017-12-13 11:28:50 +000096 }
97
Sanghoon Leec8d23162018-01-12 16:19:10 +000098 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 +000099
100 BorderMode _border_mode{};
101 TensorType _target{};
102 SimpleTensor<T> _reference{};
103 unsigned int _width{};
104 unsigned int _height{};
105};
106
107template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
108class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
109{
110public:
111 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000112 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000113 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000114 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000115 }
116
117protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000118 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 +0000119 {
120 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000121 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
122 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000123
124 // Create and configure function
125 FunctionType convolution;
126 convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value);
127
128 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
129 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
130
131 // Allocate tensors
132 src.allocator()->allocate();
133 dst.allocator()->allocate();
134
135 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
136 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
137
138 // Fill tensors
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000139 this->fill(AccessorType(src), 0);
140 this->fill(AccessorType(dst), 1);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000141
142 // Compute function
143 convolution.run();
144
145 return dst;
146 }
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000147};
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000148
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000149template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000150class ConvolutionSeparableValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
151{
152public:
153 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000154 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000155 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000156 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width, true);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000157 }
158
159protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000160 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 +0000161 {
162 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000163 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
164 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000165
166 // Create and configure function
167 FunctionType convolution;
168 convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value);
169
170 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
171 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
172
173 // Allocate tensors
174 src.allocator()->allocate();
175 dst.allocator()->allocate();
176
177 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
178 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
179
180 // Fill tensors
181 this->fill(AccessorType(src), 0);
182 this->fill(AccessorType(dst), 1);
183
184 // Compute function
185 convolution.run();
186
187 return dst;
188 }
189};
190
191template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000192class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
193{
194public:
195 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000196 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 +0000197 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000198 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, height);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000199 }
200
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000201protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000202 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 +0000203 {
204 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000205 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
206 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000207
208 // Create and configure function
209 FunctionType convolution;
210 convolution.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value);
211
212 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
213 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
214
215 // Allocate tensors
216 src.allocator()->allocate();
217 dst.allocator()->allocate();
218
219 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
220 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
221
222 // Fill tensors
223 this->fill(AccessorType(src), 0);
224 this->fill(AccessorType(dst), 1);
225
226 // Compute function
227 convolution.run();
228
229 return dst;
230 }
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000231};
232} // namespace validation
233} // namespace test
234} // namespace arm_compute
235#endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */