blob: a25a65f9971b09866b9359478dbbb90f2ef39e47 [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 Bottini9f0d5ec2019-08-19 13:31:38 +010054 DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010055 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +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 _quantization_info = quantization_info;
Pablo Tellof5f34bb2017-08-22 13:34:13 +010060
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +010061 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, add_bias);
62 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010063 }
64
65protected:
66 template <typename U>
67 void fill(U &&tensor, int i)
68 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010069 switch(tensor.data_type())
Pablo Tellof5f34bb2017-08-22 13:34:13 +010070 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010071 case DataType::QASYMM8:
72 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010073 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f);
74 std::uniform_int_distribution<uint8_t> distribution(bounds.first, bounds.second);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010075 library->fill(tensor, distribution, i);
76 break;
77 }
78 case DataType::S32:
79 {
80 std::uniform_int_distribution<int32_t> distribution(-100, 100);
81 library->fill(tensor, distribution, i);
82 break;
83 }
84 case DataType::F16:
85 case DataType::F32:
86 {
87 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
88 library->fill(tensor, distribution, i);
89 break;
90 }
91 default:
92 library->fill_tensor_uniform(tensor, i);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010093 }
94 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +000095
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +010096 template <typename U>
97 void fill_zeros(U &&tensor)
98 {
99 switch(tensor.data_type())
100 {
101 case DataType::S32:
102 {
103 const int32_t value = static_cast<int32_t>(tensor.quantization_info().uniform().offset);
104 library->fill_tensor_value(tensor, value);
105 break;
106 }
107 case DataType::F16:
108 library->fill_tensor_value(tensor, static_cast<half>(0.0f));
109 break;
110 case DataType::F32:
111 library->fill_tensor_value(tensor, static_cast<float>(0.0f));
112 break;
113 default:
114 ARM_COMPUTE_ERROR("Not supported");
115 }
116 }
117
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100118 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100119 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100120 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100121 if(_data_layout == DataLayout::NHWC)
122 {
123 permute(input_shape, PermutationVector(2U, 0U, 1U));
124 permute(weights_shape, PermutationVector(2U, 0U, 1U));
125 permute(output_shape, PermutationVector(2U, 0U, 1U));
126 }
127
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100128 // Create tensors
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100129 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info, _data_layout);
130 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _quantization_info, _data_layout);
131 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info, _data_layout);
132 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _quantization_info, _data_layout);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100133
134 // Create and configure function
135 FunctionType conv;
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100136 conv.configure(&src, &weights, add_bias ? &bias : nullptr, &dst, info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100137
138 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
139 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100140 if(add_bias)
141 {
142 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
143 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100144 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
145
146 // Allocate tensors
147 src.allocator()->allocate();
148 weights.allocator()->allocate();
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100149 if(add_bias)
150 {
151 bias.allocator()->allocate();
152 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100153 dst.allocator()->allocate();
154
155 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
156 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100157 if(add_bias)
158 {
159 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
160 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100161 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
162
163 // Fill tensors
164 fill(AccessorType(src), 0);
165 fill(AccessorType(weights), 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100166 if(add_bias)
167 {
168 fill(AccessorType(bias), 2);
169 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100170
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100171 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100172 conv.run();
173
174 return dst;
175 }
176
177 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 +0100178 const PadStrideInfo &info, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100179 {
180 // Create reference
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100181 SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info };
182 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info };
183 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100184
185 // Fill reference
186 fill(src, 0);
187 fill(weights, 1);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100188
189 if(add_bias)
190 {
191 fill(bias, 2);
192 }
193 else
194 {
195 fill_zeros(bias);
196 }
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100197
giuros01a69a88b2019-01-31 16:29:19 +0000198 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100199 }
200
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100201 TensorType _target{};
202 SimpleTensor<T> _reference{};
203 DataType _data_type{};
204 DataType _bias_data_type{};
205 DataLayout _data_layout{};
206 QuantizationInfo _quantization_info{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100207};
208
209template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
210class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
211{
212public:
213 template <typename...>
214 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100215 unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100216 {
217 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
218 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
219 const TensorShape bias_shape(num_kernels);
220 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100221 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, info);
222 TensorInfo input_info(input_shape, 1, data_type);
223 TensorInfo weights_info(weights_shape, 1, data_type);
224 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
225 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), add_bias);
226 }
227};
228
229template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
230class DeconvolutionValidationAsymmFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
231{
232public:
233 template <typename...>
234 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int pad_left, unsigned int pad_right, unsigned int pad_top,
235 unsigned int pad_bottom, unsigned int num_kernels, DataType data_type, DataLayout data_layout, bool add_bias)
236 {
237 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
238 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
239 const TensorShape bias_shape(num_kernels);
240 const PadStrideInfo info(sx, sy, pad_left, pad_right, pad_top, pad_bottom, DimensionRoundingType::CEIL);
241 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 +0000242 TensorInfo input_info(input_shape, 1, data_type);
243 TensorInfo weights_info(weights_shape, 1, data_type);
244 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100245 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, QuantizationInfo(), add_bias);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100246 }
247};
248
249template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
250class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
251{
252public:
253 template <typename...>
254 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100255 unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info, bool add_bias)
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100256 {
257 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
258 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
259 const TensorShape bias_shape(num_kernels);
260 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100261 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 +0000262 TensorInfo input_info(input_shape, 1, data_type, quantization_info);
263 TensorInfo weights_info(weights_shape, 1, data_type, quantization_info);
264 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
Manuel Bottini9f0d5ec2019-08-19 13:31:38 +0100265 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, data_type, data_layout, quantization_info, add_bias);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100266 }
267};
268
269} // namespace validation
270} // namespace test
271} // namespace arm_compute