blob: b819e651ff910c057ea7e7b251848d08cb61e1cb [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
giuros01a69a88b2019-01-31 16:29:19 +00002 * Copyright (c) 2017-2019 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:
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010049 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
50
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 }
79 case DataType::S32:
80 {
81 std::uniform_int_distribution<int32_t> distribution(-100, 100);
82 library->fill(tensor, distribution, i);
83 break;
84 }
85 case DataType::F16:
86 case DataType::F32:
87 {
88 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
89 library->fill(tensor, distribution, i);
90 break;
91 }
92 default:
93 library->fill_tensor_uniform(tensor, i);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010094 }
95 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +000096
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +010097 template <typename U>
98 void fill_zeros(U &&tensor)
99 {
100 switch(tensor.data_type())
101 {
102 case DataType::S32:
103 {
104 const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset);
105 library->fill_tensor_value(tensor, value);
106 break;
107 }
108 case DataType::F16:
109 library->fill_tensor_value(tensor, static_cast<half>(0.0f));
110 break;
111 case DataType::F32:
112 library->fill_tensor_value(tensor, static_cast<float>(0.0f));
113 break;
114 default:
115 ARM_COMPUTE_ERROR("Not supported");
116 }
117 }
118
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100119 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100120 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100121 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100122 if(_data_layout == DataLayout::NHWC)
123 {
124 permute(input_shape, PermutationVector(2U, 0U, 1U));
125 permute(weights_shape, PermutationVector(2U, 0U, 1U));
126 permute(output_shape, PermutationVector(2U, 0U, 1U));
127 }
128
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100129 // Create tensors
Manuel Bottini279814b2019-10-25 10:28:28 +0100130 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _input_quantization_info, _data_layout);
131 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _input_quantization_info, _data_layout);
132 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _input_quantization_info, _data_layout);
133 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _output_quantization_info, _data_layout);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100134
135 // Create and configure function
136 FunctionType conv;
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100137 conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100138
139 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
140 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100141 if(add_bias)
142 {
143 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
144 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100145 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
146
147 // Allocate tensors
148 src.allocator()->allocate();
149 weights.allocator()->allocate();
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100150 if(add_bias)
151 {
152 bias.allocator()->allocate();
153 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100154 dst.allocator()->allocate();
155
156 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
157 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100158 if(add_bias)
159 {
160 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
161 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100162 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
163
164 // Fill tensors
165 fill(AccessorType(src), 0);
166 fill(AccessorType(weights), 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100167 if(add_bias)
168 {
169 fill(AccessorType(bias), 2);
170 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100171
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100172 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100173 conv.run();
174
175 return dst;
176 }
177
178 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 +0100179 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100180 {
181 // Create reference
Manuel Bottini279814b2019-10-25 10:28:28 +0100182 SimpleTensor<T> src{ input_shape, _data_type, 1, _input_quantization_info };
183 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _input_quantization_info };
184 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _input_quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100185
186 // Fill reference
187 fill(src, 0);
188 fill(weights, 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100189
190 if(add_bias)
191 {
192 fill(bias, 2);
193 }
194 else
195 {
196 fill_zeros(bias);
197 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100198
Manuel Bottini279814b2019-10-25 10:28:28 +0100199 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, _output_quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100200 }
201
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100202 TensorType _target{};
203 SimpleTensor<T> _reference{};
204 DataType _data_type{};
205 DataType _bias_data_type{};
206 DataLayout _data_layout{};
Manuel Bottini279814b2019-10-25 10:28:28 +0100207 QuantizationInfo _input_quantization_info{};
208 QuantizationInfo _output_quantization_info{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100209};
210
211template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
212class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
213{
214public:
215 template <typename...>
216 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100217 unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100218 {
219 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
220 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
221 const TensorShape bias_shape(num_kernels);
222 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100223 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
224 TensorInfo input_info(input_shape, 1, data_type);
225 TensorInfo weights_info(weights_shape, 1, data_type);
226 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100227 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
228 QuantizationInfo(), add_bias);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100229 }
230};
231
232template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
233class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
234{
235public:
236 template <typename...>
237 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top,
238 unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
239 {
240 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
241 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
242 const TensorShape bias_shape(num_kernels);
243 const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL);
244 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 +0000245 TensorInfo input_info(input_shape, 1, data_type);
246 TensorInfo weights_info(weights_shape, 1, data_type);
247 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100248 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(),
249 QuantizationInfo(), add_bias);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100250 }
251};
252
253template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
254class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
255{
256public:
257 template <typename...>
258 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini279814b2019-10-25 10:28:28 +0100259 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 +0100260 {
261 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
262 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
263 const TensorShape bias_shape(num_kernels);
264 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100265 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 +0100266 TensorInfo input_info(input_shape, 1, data_type, input_quantization_info);
267 TensorInfo weights_info(weights_shape, 1, data_type, input_quantization_info);
giuros01a69a88b2019-01-31 16:29:19 +0000268 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini279814b2019-10-25 10:28:28 +0100269 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, input_quantization_info,
270 output_quantization_info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100271 }
272};
273
274} // namespace validation
275} // namespace test
276} // namespace arm_compute