blob: d56a9337e9f8d127c7365365297b322b23221b3a [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Pablo Tellof5f34bb2017-08-22 13:34:13 +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#include "arm_compute/core/TensorShape.h"
25#include "arm_compute/core/Types.h"
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010027#include "tests/AssetsLibrary.h"
28#include "tests/Globals.h"
29#include "tests/IAccessor.h"
30#include "tests/framework/Asserts.h"
31#include "tests/framework/Fixture.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010032#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000033#include "tests/validation/reference/DeconvolutionLayer.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010034
35#include <random>
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010043using namespace arm_compute::misc::shape_calculator;
44
Pablo Tellof5f34bb2017-08-22 13:34:13 +010045template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class DeconvolutionLayerFixtureBase : public framework::Fixture
47{
48public:
Luca Foschianifedefc32020-02-17 17:02:49 +000049 using TBias = typename std::conditional < std::is_same<typename std::decay<T>::type, uint8_t>::value || std::is_same<typename std::decay<T>::type, int8_t>::value, int32_t, T >::type;
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010050
51public:
Pablo Tellof5f34bb2017-08-22 13:34:13 +010052 template <typename...>
53 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
Manuel Bottini279814b2019-10-25 10:28:28 +010054 DataType data_type, DataLayout data_layout, QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010055 {
Manuel Bottini279814b2019-10-25 10:28:28 +010056 _data_type = data_type;
57 _bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
58 _data_layout = data_layout;
59 _input_quantization_info = input_quantization_info;
60 _output_quantization_info = output_quantization_info;
Pablo Tellof5f34bb2017-08-22 13:34:13 +010061
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +010062 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, add_bias);
63 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010064 }
65
66protected:
67 template <typename U>
68 void fill(U &&tensor, int i)
69 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010070 switch(tensor.data_type())
Pablo Tellof5f34bb2017-08-22 13:34:13 +010071 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010072 case DataType::QASYMM8:
73 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010074 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
75 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010076 library->fill(tensor, distribution, i);
77 break;
78 }
Sheri Zhanga14817a2020-02-26 10:30:15 +000079 case DataType::QASYMM8_SIGNED:
80 {
81 std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f);
82 std::uniform_int_distribution<int8_t> distribution(bounds.first, bounds.second);
83 library->fill(tensor, distribution, i);
84 break;
85 }
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010086 case DataType::S32:
87 {
88 std::uniform_int_distribution<int32_t> distribution(-100, 100);
89 library->fill(tensor, distribution, i);
90 break;
91 }
92 case DataType::F16:
Giorgio Arena6aeb2172020-12-15 15:45:43 +000093 {
94 arm_compute::utils::uniform_real_distribution_fp16 distribution{ half(-1.0f), half(1.0f) };
95 library->fill(tensor, distribution, i);
96 break;
97 }
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010098 case DataType::F32:
99 {
Giorgio Arena6aeb2172020-12-15 15:45:43 +0000100 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100101 library->fill(tensor, distribution, i);
102 break;
103 }
104 default:
105 library->fill_tensor_uniform(tensor, i);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100106 }
107 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000108
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100109 template <typename U>
110 void fill_zeros(U &&tensor)
111 {
112 switch(tensor.data_type())
113 {
114 case DataType::S32:
115 {
116 const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset);
117 library->fill_tensor_value(tensor, value);
118 break;
119 }
120 case DataType::F16:
121 library->fill_tensor_value(tensor, static_cast<half>(0.0f));
122 break;
123 case DataType::F32:
124 library->fill_tensor_value(tensor, static_cast<float>(0.0f));
125 break;
126 default:
127 ARM_COMPUTE_ERROR("Not supported");
128 }
129 }
130
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100131 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100132 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100133 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +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
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100141 // Create tensors
Manuel Bottini279814b2019-10-25 10:28:28 +0100142 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _input_quantization_info, _data_layout);
143 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _input_quantization_info, _data_layout);
144 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _input_quantization_info, _data_layout);
145 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _output_quantization_info, _data_layout);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100146
147 // Create and configure function
148 FunctionType conv;
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100149 conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100150
151 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
152 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100153 if(add_bias)
154 {
155 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
156 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100157 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
158
159 // Allocate tensors
160 src.allocator()->allocate();
161 weights.allocator()->allocate();
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100162 if(add_bias)
163 {
164 bias.allocator()->allocate();
165 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100166 dst.allocator()->allocate();
167
168 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
169 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100170 if(add_bias)
171 {
172 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
173 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100174 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
175
176 // Fill tensors
177 fill(AccessorType(src), 0);
178 fill(AccessorType(weights), 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100179 if(add_bias)
180 {
181 fill(AccessorType(bias), 2);
182 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100183
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100184 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100185 conv.run();
186
187 return dst;
188 }
189
190 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100191 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100192 {
193 // Create reference
Manuel Bottini279814b2019-10-25 10:28:28 +0100194 SimpleTensor<T> src{ input_shape, _data_type, 1, _input_quantization_info };
195 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _input_quantization_info };
196 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _input_quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100197
198 // Fill reference
199 fill(src, 0);
200 fill(weights, 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100201
202 if(add_bias)
203 {
204 fill(bias, 2);
205 }
206 else
207 {
208 fill_zeros(bias);
209 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100210
Manuel Bottini279814b2019-10-25 10:28:28 +0100211 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, _output_quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100212 }
213
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100214 TensorType _target{};
215 SimpleTensor<T> _reference{};
216 DataType _data_type{};
217 DataType _bias_data_type{};
218 DataLayout _data_layout{};
Manuel Bottini279814b2019-10-25 10:28:28 +0100219 QuantizationInfo _input_quantization_info{};
220 QuantizationInfo _output_quantization_info{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100221};
222
223template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
224class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
225{
226public:
227 template <typename...>
228 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100229 unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100230 {
231 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
232 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
233 const TensorShape bias_shape(num_kernels);
234 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100235 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
236 TensorInfo input_info(input_shape, 1, data_type);
237 TensorInfo weights_info(weights_shape, 1, data_type);
238 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100239 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
240 QuantizationInfo(), add_bias);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100241 }
242};
243
244template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
245class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
246{
247public:
248 template <typename...>
249 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top,
250 unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
251 {
252 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
253 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
254 const TensorShape bias_shape(num_kernels);
255 const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL);
256 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
giuros01a69a88b2019-01-31 16:29:19 +0000257 TensorInfo input_info(input_shape, 1, data_type);
258 TensorInfo weights_info(weights_shape, 1, data_type);
259 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100260 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
261 QuantizationInfo(), add_bias);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100262 }
263};
264
265template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
266class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
267{
268public:
269 template <typename...>
270 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini279814b2019-10-25 10:28:28 +0100271 unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, bool add_bias)
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100272 {
273 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
274 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
275 const TensorShape bias_shape(num_kernels);
276 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100277 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100278 TensorInfo input_info(input_shape, 1, data_type, input_quantization_info);
279 TensorInfo weights_info(weights_shape, 1, data_type, input_quantization_info);
giuros01a69a88b2019-01-31 16:29:19 +0000280 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100281 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, input_quantization_info,
282 output_quantization_info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100283 }
284};
285
286} // namespace validation
287} // namespace test
288} // namespace arm_compute