blob: 3da5158e974d03a8b78b211d13c7b1389d3111c4 [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Moritz Pflanzerb3d25792017-07-26 11:49:37 +01003 *
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 */
Giorgio Arenac0f54432018-03-16 14:02:34 +000024#include "arm_compute/core/Helpers.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010025#include "arm_compute/core/TensorShape.h"
26#include "arm_compute/core/Types.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010028#include "tests/AssetsLibrary.h"
29#include "tests/Globals.h"
30#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/framework/Asserts.h"
32#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/validation/Helpers.h"
34#include "tests/validation/fixtures/ConvolutionLayerFixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/ConvolutionLayer.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000036#include "tests/validation/reference/Permute.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Giorgio Arenac0f54432018-03-16 14:02:34 +000046using namespace arm_compute::misc::shape_calculator;
47
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010048template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +070049class DirectConvolutionValidationGenericFixture : public framework::Fixture
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010050{
51public:
Sheri Zhang681f2d42020-02-20 11:23:08 +000052 using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int32_t, T >::type;
Georgios Pinitas540d0082017-11-17 10:55:00 +000053
54public:
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010055 template <typename...>
Chunosovd621bca2017-11-03 17:33:15 +070056 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,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010057 DataType data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info, DataLayout data_layout)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010058 {
Giorgio Arenac0f54432018-03-16 14:02:34 +000059 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
60
Chunosovd621bca2017-11-03 17:33:15 +070061 _quantization_info = quantization_info;
62 _data_type = data_type;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010063
Giorgio Arenac0f54432018-03-16 14:02:34 +000064 TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010065 const TensorShape bias_shape(num_kernels);
66 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
Georgios Pinitas540d0082017-11-17 10:55:00 +000067 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010068
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010069 TensorInfo input_info = TensorInfo(input_shape, 1, data_type);
70 TensorInfo weights_info = TensorInfo(weights_shape, 1, data_type);
Giorgio Arenac0f54432018-03-16 14:02:34 +000071
Giorgio Arenac0f54432018-03-16 14:02:34 +000072 const TensorShape output_shape = compute_deep_convolution_shape(input_info, weights_info, info);
73
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010074 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, quantization_info, act_info, data_layout);
75 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, quantization_info, act_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010076 }
77
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000078 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +000079 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010080 DataType data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info, DataLayout data_layout)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000081 {
Giorgio Arenac0f54432018-03-16 14:02:34 +000082 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Alex Gilday7da29b62018-03-23 14:16:00 +000083 ARM_COMPUTE_UNUSED(dilation);
84
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000085 _quantization_info = quantization_info;
86 _data_type = data_type;
87
88 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
89
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010090 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, quantization_info, act_info, data_layout);
91 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, quantization_info, act_info);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000092 }
93
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010094protected:
95 template <typename U>
96 void fill(U &&tensor, int i)
97 {
98 switch(tensor.data_type())
99 {
Chunosovd621bca2017-11-03 17:33:15 +0700100 case DataType::QASYMM8:
101 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +0000102 std::uniform_int_distribution<uint8_t> distribution(0, 50);
Chunosovd621bca2017-11-03 17:33:15 +0700103 library->fill(tensor, distribution, i);
104 break;
105 }
Sheri Zhang681f2d42020-02-20 11:23:08 +0000106 case DataType::QASYMM8_SIGNED:
107 {
Sheri Zhang970353e2020-03-19 14:29:49 +0000108 // Use small input range to avoid all the test results being saturated at the end.
109 std::uniform_int_distribution<int8_t> distribution(-25, 25);
Sheri Zhang681f2d42020-02-20 11:23:08 +0000110 library->fill(tensor, distribution, i);
111 break;
112 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100113 case DataType::F16:
114 case DataType::F32:
115 {
Pablo Tello3d319462018-06-21 15:13:17 +0100116 std::uniform_real_distribution<> distribution(-1.f, 1.f);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100117 library->fill(tensor, distribution, i);
118 break;
119 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700120 case DataType::S32:
121 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +0000122 std::uniform_int_distribution<int32_t> distribution(-5, 5);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700123 library->fill(tensor, distribution, i);
124 break;
125 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100126 default:
127 library->fill_tensor_uniform(tensor, i);
128 }
129 }
130
Giorgio Arena563494c2018-04-30 17:29:41 +0100131 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100132 DataType data_type, DataType bias_data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info, const DataLayout &data_layout)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100133 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100134 if(data_layout == DataLayout::NHWC)
135 {
136 permute(input_shape, PermutationVector(2U, 0U, 1U));
137 permute(weights_shape, PermutationVector(2U, 0U, 1U));
138 permute(output_shape, PermutationVector(2U, 0U, 1U));
139 }
140
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100141 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100142 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, quantization_info, data_layout);
143 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, quantization_info, data_layout);
144 TensorType bias = create_tensor<TensorType>(bias_shape, bias_data_type, 1, quantization_info);
145 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, quantization_info, data_layout);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100146
147 // Create and configure function
148 FunctionType conv;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000149 conv.configure(&src, &weights, &bias, &dst, info, act_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100150
151 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
152 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
153 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
154 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
155
156 // Allocate tensors
157 src.allocator()->allocate();
158 weights.allocator()->allocate();
159 bias.allocator()->allocate();
160 dst.allocator()->allocate();
161
162 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
163 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
164 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
165 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
166
167 // Fill tensors
168 fill(AccessorType(src), 0);
169 fill(AccessorType(weights), 1);
170 fill(AccessorType(bias), 2);
171
172 // Compute NEConvolutionLayer function
173 conv.run();
174
175 return dst;
176 }
177
178 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100179 DataType data_type, DataType bias_data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100180 {
181 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100182 SimpleTensor<T> src{ input_shape, data_type, 1, quantization_info };
183 SimpleTensor<T> weights{ weights_shape, data_type, 1, quantization_info };
184 SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, quantization_info };
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100185
186 // Fill reference
187 fill(src, 0);
188 fill(weights, 1);
189 fill(bias, 2);
190
Giorgio Arena563494c2018-04-30 17:29:41 +0100191 SimpleTensor<T> dst = reference::convolution_layer<T>(src, weights, bias, output_shape, info);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000192 return (act_info.enabled()) ? reference::activation_layer<T>(dst, act_info) : dst;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100193 }
Chunosovd621bca2017-11-03 17:33:15 +0700194 TensorType _target{};
195 SimpleTensor<T> _reference{};
Chunosovd621bca2017-11-03 17:33:15 +0700196 QuantizationInfo _quantization_info{};
197 DataType _data_type{};
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100198};
199
200template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700201class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100202{
203public:
204 template <typename...>
Giorgio Arenac0f54432018-03-16 14:02:34 +0000205 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, ActivationLayerInfo act_info,
206 DataLayout data_layout)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100207 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100208 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, QuantizationInfo(),
Giorgio Arenac0f54432018-03-16 14:02:34 +0000209 act_info, data_layout);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100210 }
211};
Chunosovd621bca2017-11-03 17:33:15 +0700212
213template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700214class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
215{
216public:
217 template <typename...>
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000218 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,
Giorgio Arenae620a832020-02-17 16:33:20 +0000219 ActivationLayerInfo act_info, DataLayout data_layout)
Chunosovd621bca2017-11-03 17:33:15 +0700220 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100221 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, quantization_info,
Giorgio Arenae620a832020-02-17 16:33:20 +0000222 act_info, data_layout);
Chunosovd621bca2017-11-03 17:33:15 +0700223 }
224};
225
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000226template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
227class DirectConvolutionValidationWithTensorShapesQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
228{
229public:
230 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +0000231 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
Giorgio Arenae620a832020-02-17 16:33:20 +0000232 DataType data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info, DataLayout data_layout)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000233 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100234 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, data_type, quantization_info,
Giorgio Arenae620a832020-02-17 16:33:20 +0000235 act_info, data_layout);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000236 }
237};
238
239template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
240class DirectConvolutionValidationWithTensorShapesFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
241{
242public:
243 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +0000244 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000245 DataType data_type, ActivationLayerInfo act_info)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000246 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100247 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, data_type, QuantizationInfo(),
Giorgio Arenac0f54432018-03-16 14:02:34 +0000248 act_info, DataLayout::NCHW);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000249 }
250};
251
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100252} // namespace validation
253} // namespace test
254} // namespace arm_compute