blob: 38ddf33e240f87c5b1ee27f84f36aa39efe0eedd [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +01001/*
Alex Gilday7da29b62018-03-23 14:16:00 +00002 * Copyright (c) 2017-2018 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:
Georgios Pinitas540d0082017-11-17 10:55:00 +000052 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
53
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,
Giorgio Arenac0f54432018-03-16 14:02:34 +000057 DataType data_type, int fractional_bits, 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 _fractional_bits = fractional_bits;
62 _quantization_info = quantization_info;
63 _data_type = data_type;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010064
Giorgio Arenac0f54432018-03-16 14:02:34 +000065 TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010066 const TensorShape bias_shape(num_kernels);
67 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
Georgios Pinitas540d0082017-11-17 10:55:00 +000068 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010069
Giorgio Arenac0f54432018-03-16 14:02:34 +000070 TensorInfo input_info = TensorInfo(input_shape, 1, data_type, _fractional_bits);
71 TensorInfo weights_info = TensorInfo(weights_shape, 1, data_type, _fractional_bits);
72
Giorgio Arenac0f54432018-03-16 14:02:34 +000073 const TensorShape output_shape = compute_deep_convolution_shape(input_info, weights_info, info);
74
75 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info, act_info, data_layout);
Giorgio Arena563494c2018-04-30 17:29:41 +010076 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info, act_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010077 }
78
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000079 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +000080 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation,
Giorgio Arenac0f54432018-03-16 14:02:34 +000081 DataType data_type, int fractional_bits, QuantizationInfo quantization_info, ActivationLayerInfo act_info, DataLayout data_layout)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000082 {
Giorgio Arenac0f54432018-03-16 14:02:34 +000083 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Alex Gilday7da29b62018-03-23 14:16:00 +000084 ARM_COMPUTE_UNUSED(dilation);
85
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000086 _fractional_bits = fractional_bits;
87 _quantization_info = quantization_info;
88 _data_type = data_type;
89
90 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
91
Giorgio Arenac0f54432018-03-16 14:02:34 +000092 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info, act_info, data_layout);
Giorgio Arena563494c2018-04-30 17:29:41 +010093 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, bias_data_type, fractional_bits, quantization_info, act_info);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +000094 }
95
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010096protected:
97 template <typename U>
98 void fill(U &&tensor, int i)
99 {
100 switch(tensor.data_type())
101 {
Chunosovd621bca2017-11-03 17:33:15 +0700102 case DataType::QASYMM8:
103 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +0000104 std::uniform_int_distribution<uint8_t> distribution(0, 50);
Chunosovd621bca2017-11-03 17:33:15 +0700105 library->fill(tensor, distribution, i);
106 break;
107 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100108 case DataType::F16:
109 case DataType::F32:
110 {
111 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
112 library->fill(tensor, distribution, i);
113 break;
114 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700115 case DataType::S32:
116 {
Georgios Pinitas6fdfaa82017-11-29 14:27:24 +0000117 std::uniform_int_distribution<int32_t> distribution(-5, 5);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700118 library->fill(tensor, distribution, i);
119 break;
120 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100121 default:
122 library->fill_tensor_uniform(tensor, i);
123 }
124 }
125
Giorgio Arena563494c2018-04-30 17:29:41 +0100126 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info,
Giorgio Arenac0f54432018-03-16 14:02:34 +0000127 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info, ActivationLayerInfo act_info, const DataLayout &data_layout)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100128 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100129 if(data_layout == DataLayout::NHWC)
130 {
131 permute(input_shape, PermutationVector(2U, 0U, 1U));
132 permute(weights_shape, PermutationVector(2U, 0U, 1U));
133 permute(output_shape, PermutationVector(2U, 0U, 1U));
134 }
135
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100136 // Create tensors
Giorgio Arenac0f54432018-03-16 14:02:34 +0000137 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info, data_layout);
138 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info, data_layout);
Georgios Pinitas540d0082017-11-17 10:55:00 +0000139 TensorType bias = create_tensor<TensorType>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000140 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position, quantization_info, data_layout);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100141
142 // Create and configure function
143 FunctionType conv;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000144 conv.configure(&src, &weights, &bias, &dst, info, act_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100145
146 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
147 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
148 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
149 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
150
151 // Allocate tensors
152 src.allocator()->allocate();
153 weights.allocator()->allocate();
154 bias.allocator()->allocate();
155 dst.allocator()->allocate();
156
157 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
158 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
159 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
160 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
161
162 // Fill tensors
163 fill(AccessorType(src), 0);
164 fill(AccessorType(weights), 1);
165 fill(AccessorType(bias), 2);
166
167 // Compute NEConvolutionLayer function
168 conv.run();
169
170 return dst;
171 }
172
173 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Giorgio Arena563494c2018-04-30 17:29:41 +0100174 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info, ActivationLayerInfo act_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100175 {
176 // Create reference
Giorgio Arena563494c2018-04-30 17:29:41 +0100177 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info };
178 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info };
Georgios Pinitas540d0082017-11-17 10:55:00 +0000179 SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100180
181 // Fill reference
182 fill(src, 0);
183 fill(weights, 1);
184 fill(bias, 2);
185
Giorgio Arena563494c2018-04-30 17:29:41 +0100186 SimpleTensor<T> dst = reference::convolution_layer<T>(src, weights, bias, output_shape, info);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000187
188 return (act_info.enabled()) ? reference::activation_layer<T>(dst, act_info) : dst;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100189 }
190
Chunosovd621bca2017-11-03 17:33:15 +0700191 TensorType _target{};
192 SimpleTensor<T> _reference{};
193 int _fractional_bits{};
194 QuantizationInfo _quantization_info{};
195 DataType _data_type{};
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100196};
197
198template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700199class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100200{
201public:
202 template <typename...>
Giorgio Arenac0f54432018-03-16 14:02:34 +0000203 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,
204 DataLayout data_layout)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100205 {
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000206 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, QuantizationInfo(),
Giorgio Arenac0f54432018-03-16 14:02:34 +0000207 act_info, data_layout);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100208 }
209};
Chunosovd621bca2017-11-03 17:33:15 +0700210
211template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
212class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
213{
214public:
215 template <typename...>
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000216 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,
217 ActivationLayerInfo act_info)
Chunosovd621bca2017-11-03 17:33:15 +0700218 {
219 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits,
Giorgio Arenac0f54432018-03-16 14:02:34 +0000220 QuantizationInfo(), act_info, DataLayout::NCHW);
Chunosovd621bca2017-11-03 17:33:15 +0700221 }
222};
223
224template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
225class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
226{
227public:
228 template <typename...>
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000229 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,
230 ActivationLayerInfo act_info)
Chunosovd621bca2017-11-03 17:33:15 +0700231 {
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000232 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,
Giorgio Arenac0f54432018-03-16 14:02:34 +0000233 act_info, DataLayout::NCHW);
Chunosovd621bca2017-11-03 17:33:15 +0700234 }
235};
236
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000237template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
238class DirectConvolutionValidationWithTensorShapesQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
239{
240public:
241 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +0000242 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 +0000243 DataType data_type, QuantizationInfo quantization_info, ActivationLayerInfo act_info)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000244 {
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000245 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, data_type, 0, quantization_info,
Giorgio Arenac0f54432018-03-16 14:02:34 +0000246 act_info, DataLayout::NCHW);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000247 }
248};
249
250template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
251class DirectConvolutionValidationWithTensorShapesFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
252{
253public:
254 template <typename...>
Alex Gilday7da29b62018-03-23 14:16:00 +0000255 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 +0000256 DataType data_type, ActivationLayerInfo act_info)
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000257 {
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000258 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, data_type, 0, QuantizationInfo(),
Giorgio Arenac0f54432018-03-16 14:02:34 +0000259 act_info, DataLayout::NCHW);
Jaroslaw Rzepecki2ecbada2017-11-29 13:51:34 +0000260 }
261};
262
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100263} // namespace validation
264} // namespace test
265} // namespace arm_compute