blob: 85c7ed56047cc14759d458566726d5c50a7dd201 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Georgios Pinitas793f87d2018-05-18 20:08:58 +01002 * Copyright (c) 2017-2018 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,
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010054 const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
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
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010061 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, inner_border);
62 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, inner_border);
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
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010096 TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape bias_shape, TensorShape output_shape,
97 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &inner_border)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010098 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010099 if(_data_layout == DataLayout::NHWC)
100 {
101 permute(input_shape, PermutationVector(2U, 0U, 1U));
102 permute(weights_shape, PermutationVector(2U, 0U, 1U));
103 permute(output_shape, PermutationVector(2U, 0U, 1U));
104 }
105
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100106 // Create tensors
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100107 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info, _data_layout);
108 TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, _quantization_info, _data_layout);
109 TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info, _data_layout);
110 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _quantization_info, _data_layout);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100111
112 // Create and configure function
113 FunctionType conv;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000114 conv.configure(&src, &weights, &bias, &dst, info, inner_border.first, inner_border.second);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100115
116 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
117 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
118 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
119 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
120
121 // Allocate tensors
122 src.allocator()->allocate();
123 weights.allocator()->allocate();
124 bias.allocator()->allocate();
125 dst.allocator()->allocate();
126
127 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
128 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
129 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
131
132 // Fill tensors
133 fill(AccessorType(src), 0);
134 fill(AccessorType(weights), 1);
135 fill(AccessorType(bias), 2);
136
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100137 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100138 conv.run();
139
140 return dst;
141 }
142
143 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100144 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> inner_border)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100145 {
146 // Create reference
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100147 SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info };
148 SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info };
149 SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100150
151 // Fill reference
152 fill(src, 0);
153 fill(weights, 1);
154 fill(bias, 2);
155
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000156 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, inner_border);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100157 }
158
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100159 TensorType _target{};
160 SimpleTensor<T> _reference{};
161 DataType _data_type{};
162 DataType _bias_data_type{};
163 DataLayout _data_layout{};
164 QuantizationInfo _quantization_info{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100165};
166
167template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
168class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
169{
170public:
171 template <typename...>
172 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100173 unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type, DataLayout data_layout)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100174 {
175 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
176 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
177 const TensorShape bias_shape(num_kernels);
178 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000179 const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100180 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, sx, sy);
181 TensorInfo input_info(input_shape, 1, data_type);
182 TensorInfo weights_info(weights_shape, 1, data_type);
183 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
184 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, data_layout, QuantizationInfo());
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100185 }
186};
187
188template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
189class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
190{
191public:
192 template <typename...>
193 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100194 unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type, DataLayout data_layout, QuantizationInfo quantization_info)
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100195 {
196 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
197 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
198 const TensorShape bias_shape(num_kernels);
199 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
200 const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100201 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, sx, sy);
202 TensorInfo input_info(input_shape, 1, data_type, quantization_info);
203 TensorInfo weights_info(weights_shape, 1, data_type, quantization_info);
204 TensorShape output_shape = compute_deconvolution_output_shape(out_dim, input_info, weights_info);
205 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, data_layout, quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100206 }
207};
208
209} // namespace validation
210} // namespace test
211} // namespace arm_compute