blob: 1ec4d31304dbe1797be7288bfd4cd2b0241514e8 [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"
31#include "tests/validation/CPP/ConvolutionLayer.h"
32#include "tests/validation/Helpers.h"
33#include "tests/validation/fixtures/ConvolutionLayerFixture.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
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010068protected:
69 template <typename U>
70 void fill(U &&tensor, int i)
71 {
72 switch(tensor.data_type())
73 {
Chunosovd621bca2017-11-03 17:33:15 +070074 case DataType::QASYMM8:
75 {
76 std::uniform_int_distribution<uint8_t> distribution(0, 10);
77 library->fill(tensor, distribution, i);
78 break;
79 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010080 case DataType::F16:
81 case DataType::F32:
82 {
83 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
84 library->fill(tensor, distribution, i);
85 break;
86 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +070087 case DataType::S32:
88 {
89 std::uniform_int_distribution<int32_t> distribution(-1000, 1000);
90 library->fill(tensor, distribution, i);
91 break;
92 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010093 default:
94 library->fill_tensor_uniform(tensor, i);
95 }
96 }
97
98 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 +000099 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100100 {
101 // Create tensors
Chunosovd621bca2017-11-03 17:33:15 +0700102 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info);
103 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info);
Georgios Pinitas540d0082017-11-17 10:55:00 +0000104 TensorType bias = create_tensor<TensorType>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info);
Chunosovd621bca2017-11-03 17:33:15 +0700105 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position, quantization_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100106
107 // Create and configure function
108 FunctionType conv;
109 conv.configure(&src, &weights, &bias, &dst, info);
110
111 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
112 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
113 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
114 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
115
116 // Allocate tensors
117 src.allocator()->allocate();
118 weights.allocator()->allocate();
119 bias.allocator()->allocate();
120 dst.allocator()->allocate();
121
122 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
123 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
124 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
125 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
126
127 // Fill tensors
128 fill(AccessorType(src), 0);
129 fill(AccessorType(weights), 1);
130 fill(AccessorType(bias), 2);
131
132 // Compute NEConvolutionLayer function
133 conv.run();
134
135 return dst;
136 }
137
138 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 +0000139 DataType data_type, DataType bias_data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100140 {
141 // Create reference
Georgios Pinitas540d0082017-11-17 10:55:00 +0000142 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info };
143 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info };
144 SimpleTensor<TBias> bias{ bias_shape, bias_data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100145
146 // Fill reference
147 fill(src, 0);
148 fill(weights, 1);
149 fill(bias, 2);
150
151 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
152 }
153
Chunosovd621bca2017-11-03 17:33:15 +0700154 TensorType _target{};
155 SimpleTensor<T> _reference{};
156 int _fractional_bits{};
157 QuantizationInfo _quantization_info{};
158 DataType _data_type{};
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100159
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100160private:
161 TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info)
162 {
163 TensorShape out_shape(in_shape);
164 const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(),
165 in_shape.y(),
166 kernel_shape.x(),
167 kernel_shape.y(),
168 info);
169 out_shape.set(0, scaled_dims.first);
170 out_shape.set(1, scaled_dims.second);
171 out_shape.set(2, kernel_shape[3]);
172 return out_shape;
173 }
174};
175
176template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700177class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100178{
179public:
180 template <typename...>
181 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)
182 {
Chunosovd621bca2017-11-03 17:33:15 +0700183 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 +0100184 }
185};
Chunosovd621bca2017-11-03 17:33:15 +0700186
187template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
188class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
189{
190public:
191 template <typename...>
192 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)
193 {
194 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits,
195 QuantizationInfo());
196 }
197};
198
199template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
200class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
201{
202public:
203 template <typename...>
204 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)
205 {
206 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);
207 }
208};
209
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100210} // namespace validation
211} // namespace test
212} // namespace arm_compute