blob: d63a5bcdba0f6a0682b20f37d155bc8d1de33d4c [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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#include "arm_compute/core/TensorShape.h"
25#include "arm_compute/core/Types.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010026#include "tests/AssetsLibrary.h"
27#include "tests/Globals.h"
28#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/framework/Asserts.h"
30#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/validation/Helpers.h"
32#include "tests/validation/fixtures/ConvolutionLayerFixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000033#include "tests/validation/reference/ConvolutionLayer.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010034
35#include <random>
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +070044class DirectConvolutionValidationGenericFixture : public framework::Fixture
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010045{
46public:
Georgios Pinitas540d0082017-11-17 10:55:00 +000047 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
48
49public:
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010050 template <typename...>
Chunosovd621bca2017-11-03 17:33:15 +070051 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels,
52 DataType data_type, int fractional_bits, QuantizationInfo quantization_info)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010053 {
Chunosovd621bca2017-11-03 17:33:15 +070054 _fractional_bits = fractional_bits;
55 _quantization_info = quantization_info;
56 _data_type = data_type;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010057
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010058 const TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
59 const TensorShape bias_shape(num_kernels);
60 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
Georgios Pinitas540d0082017-11-17 10:55:00 +000061 const TensorShape output_shape = get_output_shape(input_shape, weights_shape, info);
62 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010063
Georgios Pinitas540d0082017-11-17 10:55:00 +000064 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info);
65 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010066 }
67
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000068 template <typename...>
69 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
70 DataType data_type, int fractional_bits, QuantizationInfo quantization_info)
71 {
72 _fractional_bits = fractional_bits;
73 _quantization_info = quantization_info;
74 _data_type = data_type;
75
76 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
77
78 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info);
79 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info);
80 }
81
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010082protected:
83 template <typename U>
84 void fill(U &&tensor, int i)
85 {
86 switch(tensor.data_type())
87 {
Chunosovd621bca2017-11-03 17:33:15 +070088 case DataType::QASYMM8:
89 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +000090 std::uniform_int_distribution<uint8_t> distribution(0, 50);
Chunosovd621bca2017-11-03 17:33:15 +070091 library->fill(tensor, distribution, i);
92 break;
93 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010094 case DataType::F16:
95 case DataType::F32:
96 {
97 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
98 library->fill(tensor, distribution, i);
99 break;
100 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700101 case DataType::S32:
102 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +0000103 std::uniform_int_distribution<int32_t> distribution(-5, 5);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700104 library->fill(tensor, distribution, i);
105 break;
106 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100107 default:
108 library->fill_tensor_uniform(tensor, i);
109 }
110 }
111
112 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Georgios Pinitas540d0082017-11-17 10:55:00 +0000113 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100114 {
115 // Create tensors
Chunosovd621bca2017-11-03 17:33:15 +0700116 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info);
117 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info);
Georgios Pinitas540d0082017-11-17 10:55:00 +0000118 TensorType bias = create_tensor<TensorType>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info);
Chunosovd621bca2017-11-03 17:33:15 +0700119 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position, quantization_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100120
121 // Create and configure function
122 FunctionType conv;
123 conv.configure(&src, &weights, &bias, &dst, info);
124
125 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
126 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
127 ARM_COMPUTE_EXPECT(bias.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 weights.allocator()->allocate();
133 bias.allocator()->allocate();
134 dst.allocator()->allocate();
135
136 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
137 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
138 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
139 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
140
141 // Fill tensors
142 fill(AccessorType(src), 0);
143 fill(AccessorType(weights), 1);
144 fill(AccessorType(bias), 2);
145
146 // Compute NEConvolutionLayer function
147 conv.run();
148
149 return dst;
150 }
151
152 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Georgios Pinitas540d0082017-11-17 10:55:00 +0000153 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100154 {
155 // Create reference
Georgios Pinitas540d0082017-11-17 10:55:00 +0000156 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info };
157 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info };
158 SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100159
160 // Fill reference
161 fill(src, 0);
162 fill(weights, 1);
163 fill(bias, 2);
164
165 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
166 }
167
Chunosovd621bca2017-11-03 17:33:15 +0700168 TensorType _target{};
169 SimpleTensor<T> _reference{};
170 int _fractional_bits{};
171 QuantizationInfo _quantization_info{};
172 DataType _data_type{};
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100173
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100174private:
175 TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info)
176 {
177 TensorShape out_shape(in_shape);
178 const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(),
179 in_shape.y(),
180 kernel_shape.x(),
181 kernel_shape.y(),
182 info);
183 out_shape.set(0, scaled_dims.first);
184 out_shape.set(1, scaled_dims.second);
185 out_shape.set(2, kernel_shape[3]);
186 return out_shape;
187 }
188};
189
190template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700191class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100192{
193public:
194 template <typename...>
195 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type)
196 {
Chunosovd621bca2017-11-03 17:33:15 +0700197 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, QuantizationInfo());
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100198 }
199};
Chunosovd621bca2017-11-03 17:33:15 +0700200
201template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
202class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
203{
204public:
205 template <typename...>
206 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, int fractional_bits)
207 {
208 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits,
209 QuantizationInfo());
210 }
211};
212
213template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
214class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
215{
216public:
217 template <typename...>
218 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, QuantizationInfo quantization_info)
219 {
220 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, quantization_info);
221 }
222};
223
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000224template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
225class DirectConvolutionValidationWithTensorShapesQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
226{
227public:
228 template <typename...>
229 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
230 DataType data_type, QuantizationInfo quantization_info)
231 {
232 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, 0, quantization_info);
233 }
234};
235
236template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
237class DirectConvolutionValidationWithTensorShapesFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
238{
239public:
240 template <typename...>
241 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
242 DataType data_type)
243 {
244 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, 0, QuantizationInfo());
245 }
246};
247
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100248} // namespace validation
249} // namespace test
250} // namespace arm_compute