blob: 8877bd145a0f96be40e4693c9eb7bef7c5890a2e [file] [log] [blame]
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00001/*
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +00002 * Copyright (c) 2017-2019 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
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +000055 // Generate random scale value between 0 and 255.
56 const uint32_t scale = distribution(gen);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000057
Sanghoon Leed7ba5392017-12-13 11:28:50 +000058 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 Leec8a85ba2017-11-29 11:23:14 +000060
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000061 std::vector<int16_t> conv(width * height);
Sanghoon Leed7ba5392017-12-13 11:28:50 +000062
Sanghoon Lee571b18a2017-12-22 16:20:11 +000063 _width = width;
64 _height = height;
65
66 if(is_separable)
67 {
John Richardson32af1f82018-06-05 12:47:20 +010068 init_separable_conv(conv.data(), width, height, library->seed());
Sanghoon Lee571b18a2017-12-22 16:20:11 +000069 }
70 else
71 {
John Richardson32af1f82018-06-05 12:47:20 +010072 init_conv(conv.data(), width, height, library->seed());
Sanghoon Lee571b18a2017-12-22 16:20:11 +000073 }
74
Michalis Spyrou5ae9b692018-02-05 14:59:54 +000075 _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 Leec8a85ba2017-11-29 11:23:14 +000077 }
78
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000079 template <typename U>
80 void fill(U &&tensor, int i)
81 {
82 library->fill_tensor_uniform(tensor, i);
83 }
84
Sanghoon Leec8d23162018-01-12 16:19:10 +000085 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 +000086 {
Sanghoon Leed7ba5392017-12-13 11:28:50 +000087 // Create reference
Sanghoon Leec8d23162018-01-12 16:19:10 +000088 SimpleTensor<uint8_t> src{ shape, DataType::U8 };
Sanghoon Leed7ba5392017-12-13 11:28:50 +000089
90 // Fill reference
91 fill(src, 0);
92
93 // Compute reference
Sanghoon Leec8d23162018-01-12 16:19:10 +000094 return reference::convolution<T>(src, output_data_type, conv, scale, border_mode, constant_border_value, _width, _height);
Sanghoon Leed7ba5392017-12-13 11:28:50 +000095 }
96
Sanghoon Leec8d23162018-01-12 16:19:10 +000097 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 +000098
99 BorderMode _border_mode{};
100 TensorType _target{};
101 SimpleTensor<T> _reference{};
102 unsigned int _width{};
103 unsigned int _height{};
104};
105
106template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
107class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
108{
109public:
110 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000111 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000112 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000113 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000114 }
115
116protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000117 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 +0000118 {
119 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000120 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
121 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000122
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 Leed7ba5392017-12-13 11:28:50 +0000138 this->fill(AccessorType(src), 0);
139 this->fill(AccessorType(dst), 1);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000140
141 // Compute function
142 convolution.run();
143
144 return dst;
145 }
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000146};
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000147
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000148template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000149class ConvolutionSeparableValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
150{
151public:
152 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000153 void setup(TensorShape shape, DataType output_data_type, BorderMode border_mode, const unsigned int width)
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000154 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000155 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, width, true);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000156 }
157
158protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000159 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 +0000160 {
161 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000162 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
163 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Lee571b18a2017-12-22 16:20:11 +0000164
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
190template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000191class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
192{
193public:
194 template <typename...>
Sanghoon Leec8d23162018-01-12 16:19:10 +0000195 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 +0000196 {
Sanghoon Leec8d23162018-01-12 16:19:10 +0000197 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, output_data_type, border_mode, width, height);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000198 }
199
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000200protected:
Sanghoon Leec8d23162018-01-12 16:19:10 +0000201 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 +0000202 {
203 // Create tensors
Sanghoon Leec8d23162018-01-12 16:19:10 +0000204 TensorType src = create_tensor<TensorType>(shape, DataType::U8);
205 TensorType dst = create_tensor<TensorType>(shape, output_data_type);
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000206
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 Leec8a85ba2017-11-29 11:23:14 +0000230};
231} // namespace validation
232} // namespace test
233} // namespace arm_compute
234#endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */