blob: b9a478b1a9e018bc7a85a16eb2f78fc11b1d04f2 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Sheri Zhanga14817a2020-02-26 10:30:15 +00002 * 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:
Sheri Zhanga14817a2020-02-26 10:30:15 +000049 using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, 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:
93 case DataType::F32:
94 {
95 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
96 library->fill(tensor, distribution, i);
97 break;
98 }
99 default:
100 library->fill_tensor_uniform(tensor, i);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100101 }
102 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000103
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100104 template <typename U>
105 void fill_zeros(U &&tensor)
106 {
107 switch(tensor.data_type())
108 {
109 case DataType::S32:
110 {
111 const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset);
112 library->fill_tensor_value(tensor, value);
113 break;
114 }
115 case DataType::F16:
116 library->fill_tensor_value(tensor, static_cast<half>(0.0f));
117 break;
118 case DataType::F32:
119 library->fill_tensor_value(tensor, static_cast<float>(0.0f));
120 break;
121 default:
122 ARM_COMPUTE_ERROR("Not supported");
123 }
124 }
125
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100126 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100127 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100128 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +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
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100136 // Create tensors
Manuel Bottini279814b2019-10-25 10:28:28 +0100137 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _input_quantization_info, _data_layout);
138 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _input_quantization_info, _data_layout);
139 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _input_quantization_info, _data_layout);
140 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _output_quantization_info, _data_layout);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100141
142 // Create and configure function
143 FunctionType conv;
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100144 conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100145
146 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
147 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100148 if(add_bias)
149 {
150 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
151 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100152 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
153
154 // Allocate tensors
155 src.allocator()->allocate();
156 weights.allocator()->allocate();
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100157 if(add_bias)
158 {
159 bias.allocator()->allocate();
160 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100161 dst.allocator()->allocate();
162
163 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
164 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100165 if(add_bias)
166 {
167 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
168 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100169 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
170
171 // Fill tensors
172 fill(AccessorType(src), 0);
173 fill(AccessorType(weights), 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100174 if(add_bias)
175 {
176 fill(AccessorType(bias), 2);
177 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100178
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100179 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100180 conv.run();
181
182 return dst;
183 }
184
185 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 +0100186 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100187 {
188 // Create reference
Manuel Bottini279814b2019-10-25 10:28:28 +0100189 SimpleTensor<T> src{ input_shape, _data_type, 1, _input_quantization_info };
190 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _input_quantization_info };
191 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _input_quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100192
193 // Fill reference
194 fill(src, 0);
195 fill(weights, 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100196
197 if(add_bias)
198 {
199 fill(bias, 2);
200 }
201 else
202 {
203 fill_zeros(bias);
204 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100205
Manuel Bottini279814b2019-10-25 10:28:28 +0100206 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, _output_quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100207 }
208
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100209 TensorType _target{};
210 SimpleTensor<T> _reference{};
211 DataType _data_type{};
212 DataType _bias_data_type{};
213 DataLayout _data_layout{};
Manuel Bottini279814b2019-10-25 10:28:28 +0100214 QuantizationInfo _input_quantization_info{};
215 QuantizationInfo _output_quantization_info{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100216};
217
218template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
219class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
220{
221public:
222 template <typename...>
223 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100224 unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100225 {
226 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
227 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
228 const TensorShape bias_shape(num_kernels);
229 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100230 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
231 TensorInfo input_info(input_shape, 1, data_type);
232 TensorInfo weights_info(weights_shape, 1, data_type);
233 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100234 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
235 QuantizationInfo(), add_bias);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100236 }
237};
238
239template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
240class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
241{
242public:
243 template <typename...>
244 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top,
245 unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
246 {
247 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
248 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
249 const TensorShape bias_shape(num_kernels);
250 const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL);
251 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 +0000252 TensorInfo input_info(input_shape, 1, data_type);
253 TensorInfo weights_info(weights_shape, 1, data_type);
254 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100255 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
256 QuantizationInfo(), add_bias);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100257 }
258};
259
260template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
261class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
262{
263public:
264 template <typename...>
265 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini279814b2019-10-25 10:28:28 +0100266 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 +0100267 {
268 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
269 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
270 const TensorShape bias_shape(num_kernels);
271 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100272 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 +0100273 TensorInfo input_info(input_shape, 1, data_type, input_quantization_info);
274 TensorInfo weights_info(weights_shape, 1, data_type, input_quantization_info);
giuros01a69a88b2019-01-31 16:29:19 +0000275 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100276 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, input_quantization_info,
277 output_quantization_info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100278 }
279};
280
281} // namespace validation
282} // namespace test
283} // namespace arm_compute