blob: 9e6dd4bd28f7a5a43317c7b30e04d57e3722284b [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...>
Usama Arife73686a2019-04-08 17:30:48 +010059 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, QuantizationInfo quantization_info,
Manuel Bottinia788c2f2019-04-08 13:18:00 +010060 DataLayout data_layout, ActivationLayerInfo act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010061 {
Giorgio Arena76572242018-04-04 17:44:26 +010062 _quantization_info = quantization_info;
63 _data_type = data_type;
64 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
65
66 TensorShape weights_shape(kernel_size.width, kernel_size.height);
67
Manuel Bottinia788c2f2019-04-08 13:18:00 +010068 const TensorInfo in_info(in_shape, 1, data_type);
69 const TensorInfo we_info(weights_shape, 1, data_type);
70 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 +010071
72 weights_shape.set(2, out_shape.z());
Pablo Tello941cd702017-12-12 14:35:00 +000073 const TensorShape biases_shape(weights_shape[2]);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070074
Manuel Bottinia788c2f2019-04-08 13:18:00 +010075 _target = compute_target(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier, data_type, bias_data_type, quantization_info, data_layout, act_info);
76 _reference = compute_reference(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier, data_type, bias_data_type, 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,
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100111 const DataType data_type, const DataType bias_data_type, const QuantizationInfo quantization_info, const DataLayout data_layout, ActivationLayerInfo act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100112 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100113 if(data_layout == DataLayout::NHWC)
114 {
115 permute(input_shape, PermutationVector(2U, 0U, 1U));
116 permute(weights_shape, PermutationVector(2U, 0U, 1U));
117 permute(output_shape, PermutationVector(2U, 0U, 1U));
118 }
119
Giorgio Arena93a690e2017-08-01 16:09:33 +0100120 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100121 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, quantization_info, data_layout);
122 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, quantization_info, data_layout);
123 TensorType biases = create_tensor<TensorType>(biases_shape, bias_data_type, 1, quantization_info, data_layout);
124 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, quantization_info, data_layout);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100125
126 // Create Depthwise Convolution configure function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700127 FunctionType dwc;
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100128 dwc.configure(&src, &weights, &biases, &dst, pad_stride_info, depth_multiplier, act_info, dilation);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700129
130 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
131 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
132 ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS);
133 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100134
135 // Allocate tensors
136 src.allocator()->allocate();
137 weights.allocator()->allocate();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100138 biases.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100139 dst.allocator()->allocate();
140
141 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
142 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100143 ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100144 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
145
146 // Fill tensors
147 fill(AccessorType(src), 0);
148 fill(AccessorType(weights), 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100149 fill(AccessorType(biases), 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100150
151 // Compute function
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700152 dwc.run();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100153
154 return dst;
155 }
156
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700157 SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const TensorShape &out_shape, const PadStrideInfo &pad_stride_info,
Usama Arife73686a2019-04-08 17:30:48 +0100158 const Size2D &dilation, unsigned int depth_multiplier,
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100159 const DataType data_type, const DataType bias_data_type, const QuantizationInfo quantization_info, ActivationLayerInfo act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100160 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100161 SimpleTensor<T> src{ in_shape, data_type, 1, quantization_info };
162 SimpleTensor<T> weights{ weights_shape, data_type, 1, quantization_info };
163 SimpleTensor<TBias> biases{ biases_shape, bias_data_type, 1, quantization_info };
Giorgio Arena93a690e2017-08-01 16:09:33 +0100164
165 fill(src, 0);
166 fill(weights, 1);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100167 fill(biases, 2);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100168
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100169 SimpleTensor<T> depth_out = reference::depthwise_convolution(src, weights, biases, out_shape, pad_stride_info, depth_multiplier, dilation);
170 return (act_info.enabled()) ? reference::activation_layer<T>(depth_out, act_info) : depth_out;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100171 }
172
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700173 TensorType _target{};
174 SimpleTensor<T> _reference{};
175 DataType _data_type{};
176 QuantizationInfo _quantization_info{};
177};
178
179template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000180class DepthwiseConvolutionLayerValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700181{
182public:
183 template <typename...>
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100184 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, DataLayout data_layout,
185 ActivationLayerInfo act_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700186 {
Usama Arife73686a2019-04-08 17:30:48 +0100187 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier,
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100188 data_type, QuantizationInfo(), data_layout, act_info);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700189 }
190};
191
192template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000193class DepthwiseConvolutionLayerValidationQuantizedFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700194{
195public:
196 template <typename...>
Usama Arife73686a2019-04-08 17:30:48 +0100197 void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, QuantizationInfo quantization_info,
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100198 DataLayout data_layout, ActivationLayerInfo act_info)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700199 {
Usama Arife73686a2019-04-08 17:30:48 +0100200 DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier,
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100201 data_type, quantization_info, data_layout, act_info);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700202 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100203};
204} // namespace validation
205} // namespace test
206} // namespace arm_compute
207#endif /* ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE */