blob: a3ac49eef1267264b12aea87af78e31d0a2e1a75 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Usama Arife73686a2019-04-08 17:30:48 +01002 * Copyright (c) 2017-2019 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +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 */
24#ifndef ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE
25#define ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Giorgio Arena76572242018-04-04 17:44:26 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010035#include "tests/validation/Helpers.h"
Manuel Bottinia788c2f2019-04-08 13:18:00 +010036#include "tests/validation/reference/ActivationLayer.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000037#include "tests/validation/reference/DepthwiseConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010038
Georgios Pinitasf72f9362018-01-12 16:29:45 +000039#include "utils/Utils.h"
40
Giorgio Arena93a690e2017-08-01 16:09:33 +010041#include <random>
42
43namespace arm_compute
44{
45namespace test
46{
47namespace validation
48{
Giorgio Arena76572242018-04-04 17:44:26 +010049using namespace arm_compute::misc::shape_calculator;
50
Giorgio Arena93a690e2017-08-01 16:09:33 +010051template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000052class DepthwiseConvolutionLayerValidationGenericFixture : public framework::Fixture
Giorgio Arena93a690e2017-08-01 16:09:33 +010053{
54public:
Dmitry Savenkod7295b72017-11-20 22:00:08 +070055 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
56
57public:
Giorgio Arena93a690e2017-08-01 16:09:33 +010058 template <typename...>
Pablo Telloa28aebc2019-06-03 14:59:48 +010059 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type,
60 QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, ActivationLayerInfo act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010061 {
Giorgio Arena76572242018-04-04 17:44:26 +010062 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
63
64 TensorShape weights_shape(kernel_size.width, kernel_size.height);
65
Manuel Bottinia788c2f2019-04-08 13:18:00 +010066 const TensorInfo in_info(in_shape, 1, data_type);
67 const TensorInfo we_info(weights_shape, 1, data_type);
68 const TensorShape out_shape = compute_depthwise_convolution_shape(in_info, we_info, pad_stride_info, depth_multiplier, dilation);
Giorgio Arena76572242018-04-04 17:44:26 +010069
70 weights_shape.set(2, out_shape.z());
Pablo Tello941cd702017-12-12 14:35:00 +000071 const TensorShape biases_shape(weights_shape[2]);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070072
Pablo Telloa28aebc2019-06-03 14:59:48 +010073 _target = compute_target(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier,
74 data_type, bias_data_type, input_quantization_info, output_quantization_info, data_layout, act_info);
75 _reference = compute_reference(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier,
76 data_type, bias_data_type, input_quantization_info, output_quantization_info, act_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +010077 }
78
79protected:
80 template <typename U>
81 void fill(U &&tensor, int i)
82 {
83 switch(tensor.data_type())
84 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +070085 case DataType::QASYMM8:
86 {
87 std::uniform_int_distribution<uint8_t> distribution(0, 10);
88 library->fill(tensor, distribution, i);
89 break;
90 }
Giorgio Arena93a690e2017-08-01 16:09:33 +010091 case DataType::F32:
Frank Lei8cdfdb82018-01-02 16:49:33 +080092 case DataType::F16:
Giorgio Arena93a690e2017-08-01 16:09:33 +010093 {
94 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
95 library->fill(tensor, distribution, i);
96 break;
97 }
Dmitry Savenkod7295b72017-11-20 22:00:08 +070098 case DataType::S32:
99 {
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000100 std::uniform_int_distribution<int32_t> distribution(-100, 100);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700101 library->fill(tensor, distribution, i);
102 break;
103 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100104 default:
105 library->fill_tensor_uniform(tensor, i);
106 }
107 }
108
Usama Arife73686a2019-04-08 17:30:48 +0100109 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, TensorShape output_shape, PadStrideInfo &pad_stride_info, Size2D dilation,
110 unsigned int depth_multiplier,
Pablo Telloa28aebc2019-06-03 14:59:48 +0100111 const DataType data_type, const DataType bias_data_type,
Georgios Pinitasddec4d62019-07-10 19:23:02 +0100112 const QuantizationInfo &input_quantization_info, const QuantizationInfo &output_quantization_info,
113 const DataLayout data_layout, const ActivationLayerInfo &act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100114 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100115 if(data_layout == DataLayout::NHWC)
116 {
117 permute(input_shape, PermutationVector(2U, 0U, 1U));
118 permute(weights_shape, PermutationVector(2U, 0U, 1U));
119 permute(output_shape, PermutationVector(2U, 0U, 1U));
120 }
121
Giorgio Arena93a690e2017-08-01 16:09:33 +0100122 // Create tensors
Pablo Telloa28aebc2019-06-03 14:59:48 +0100123 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, input_quantization_info, data_layout);
124 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, input_quantization_info, data_layout);
125 TensorType biases = create_tensor<TensorType>(biases_shape, bias_data_type, 1, input_quantization_info, data_layout);
126 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, output_quantization_info, data_layout);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100127
128 // Create Depthwise Convolution configure function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700129 FunctionType dwc;
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100130 dwc.configure(&src, &weights, &biases, &dst, pad_stride_info, depth_multiplier, act_info, dilation);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700131
132 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
133 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
134 ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS);
135 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100136
137 // Allocate tensors
138 src.allocator()->allocate();
139 weights.allocator()->allocate();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100140 biases.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100141 dst.allocator()->allocate();
142
143 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
144 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100145 ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100146 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
147
148 // Fill tensors
149 fill(AccessorType(src), 0);
150 fill(AccessorType(weights), 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100151 fill(AccessorType(biases), 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100152
153 // Compute function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700154 dwc.run();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100155
156 return dst;
157 }
158
Pablo Telloa28aebc2019-06-03 14:59:48 +0100159 SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const TensorShape &out_shape,
160 const PadStrideInfo &pad_stride_info,
Usama Arife73686a2019-04-08 17:30:48 +0100161 const Size2D &dilation, unsigned int depth_multiplier,
Pablo Telloa28aebc2019-06-03 14:59:48 +0100162 const DataType data_type, const DataType bias_data_type,
Georgios Pinitasddec4d62019-07-10 19:23:02 +0100163 const QuantizationInfo &input_quantization_info, const QuantizationInfo &output_quantization_info,
164 const ActivationLayerInfo &act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100165 {
Pablo Telloa28aebc2019-06-03 14:59:48 +0100166 SimpleTensor<T> src{ in_shape, data_type, 1, input_quantization_info };
167 SimpleTensor<T> weights{ weights_shape, data_type, 1, input_quantization_info };
168 SimpleTensor<TBias> biases{ biases_shape, bias_data_type, 1, input_quantization_info };
Giorgio Arena93a690e2017-08-01 16:09:33 +0100169
170 fill(src, 0);
171 fill(weights, 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100172 fill(biases, 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100173
Pablo Telloa28aebc2019-06-03 14:59:48 +0100174 SimpleTensor<T> depth_out = reference::depthwise_convolution(src, weights, biases, out_shape, pad_stride_info, depth_multiplier, dilation, output_quantization_info);
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100175 return (act_info.enabled()) ? reference::activation_layer<T>(depth_out, act_info) : depth_out;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100176 }
177
Georgios Pinitasddec4d62019-07-10 19:23:02 +0100178 TensorType _target{};
179 SimpleTensor<T> _reference{};
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700180};
181
182template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000183class DepthwiseConvolutionLayerValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700184{
185public:
186 template <typename...>
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100187 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, DataLayout data_layout,
188 ActivationLayerInfo act_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700189 {
Usama Arife73686a2019-04-08 17:30:48 +0100190 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier,
Pablo Telloa28aebc2019-06-03 14:59:48 +0100191 data_type, QuantizationInfo(), QuantizationInfo(), data_layout, act_info);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700192 }
193};
194
195template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100196class DepthwiseConvolutionLayerNativeValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Giorgio Arena44f55722019-07-12 14:49:49 +0100197{
198public:
199 template <typename...>
200 void setup(size_t width, size_t height, size_t channel, size_t batch, Size2D kernel_size, size_t depth_multiplier, Size2D dilation, Size2D stride, bool padding_valid, DataType data_type,
201 DataLayout data_layout)
202 {
203 const TensorShape src_shape(width, height, channel, batch);
204 const TensorShape weights_shape(kernel_size.width, kernel_size.height, channel * depth_multiplier);
205 const TensorShape biases_shape(weights_shape.z());
206
207 PadStrideInfo conv_info;
208 if(padding_valid)
209 {
210 conv_info = PadStrideInfo();
211 }
212 else
213 {
214 conv_info = calculate_same_pad(src_shape, weights_shape, PadStrideInfo(stride.width, stride.height), DataLayout::NCHW, dilation);
215 }
216
217 _target = compute_target(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type, data_layout);
218 _reference = compute_reference(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type);
219 }
220
221protected:
222 template <typename U>
223 void fill(U &&tensor, int i)
224 {
225 switch(tensor.data_type())
226 {
227 case DataType::F32:
228 {
229 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
230 library->fill(tensor, distribution, i);
231 break;
232 }
233 default:
234 library->fill_tensor_uniform(tensor, i);
235 }
236 }
237
238 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, PadStrideInfo &conv_info, Size2D dilation,
239 unsigned int depth_multiplier, const DataType data_type, const DataLayout data_layout)
240 {
241 if(data_layout == DataLayout::NHWC)
242 {
243 permute(input_shape, PermutationVector(2U, 0U, 1U));
244 permute(weights_shape, PermutationVector(2U, 0U, 1U));
245 }
246
247 // Create tensors
248 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout);
249 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, QuantizationInfo(), data_layout);
250 TensorType biases = create_tensor<TensorType>(biases_shape, data_type, 1, QuantizationInfo(), data_layout);
251 TensorType dst = create_tensor<TensorType>(TensorShape(), data_type, 1, QuantizationInfo(), data_layout);
252
253 // Create Depthwise Convolution configure function
254 FunctionType dwc;
255 dwc.configure(&src, &weights, &biases, &dst, conv_info, depth_multiplier, dilation);
256
257 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
258 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
259 ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS);
260 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
261
262 // Allocate tensors
263 src.allocator()->allocate();
264 weights.allocator()->allocate();
265 biases.allocator()->allocate();
266 dst.allocator()->allocate();
267
268 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
269 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
270 ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS);
271 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
272
273 // Fill tensors
274 fill(AccessorType(src), 0);
275 fill(AccessorType(weights), 1);
276 fill(AccessorType(biases), 2);
277
278 // Compute function
279 dwc.run();
280
281 return dst;
282 }
283
284 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const PadStrideInfo &conv_info,
285 const Size2D &dilation, unsigned int depth_multiplier, const DataType data_type)
286 {
287 SimpleTensor<T> src{ input_shape, data_type };
288 SimpleTensor<T> weights{ weights_shape, data_type };
289 SimpleTensor<T> biases{ biases_shape, data_type };
290
291 fill(src, 0);
292 fill(weights, 1);
293 fill(biases, 2);
294
295 const TensorShape dst_shape = compute_depthwise_convolution_shape(TensorInfo(input_shape, 1, data_type), TensorInfo(weights_shape, 1, data_type), conv_info,
296 depth_multiplier, dilation);
297 return reference::depthwise_convolution(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation);
298 }
299
300 TensorType _target{};
301 SimpleTensor<T> _reference{};
302};
303
304template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000305class DepthwiseConvolutionLayerValidationQuantizedFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700306{
307public:
308 template <typename...>
Pablo Telloa28aebc2019-06-03 14:59:48 +0100309 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type,
310 QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, ActivationLayerInfo act_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700311 {
Usama Arife73686a2019-04-08 17:30:48 +0100312 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier,
Pablo Telloa28aebc2019-06-03 14:59:48 +0100313 data_type, input_quantization_info, output_quantization_info, data_layout, act_info);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700314 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100315};
316} // namespace validation
317} // namespace test
318} // namespace arm_compute
319#endif /* ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE */