blob: 7741557f48bfeb45badb0a5fae6a1aaae385e598 [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"
26#include "tests/AssetsLibrary.h"
27#include "tests/Globals.h"
28#include "tests/IAccessor.h"
29#include "tests/framework/Asserts.h"
30#include "tests/framework/Fixture.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010031#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000032#include "tests/validation/reference/DeconvolutionLayer.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010033
34#include <random>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
43class DeconvolutionLayerFixtureBase : public framework::Fixture
44{
45public:
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010046 using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type;
47
48public:
Pablo Tellof5f34bb2017-08-22 13:34:13 +010049 template <typename...>
50 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010051 const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, QuantizationInfo quantization_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010052 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010053 _data_type = data_type;
Pablo Tellof5f34bb2017-08-22 13:34:13 +010054
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010055 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, quantization_info);
56 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010057 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor, int i)
62 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010063 switch(tensor.data_type())
Pablo Tellof5f34bb2017-08-22 13:34:13 +010064 {
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010065 case DataType::QASYMM8:
66 {
67 std::uniform_int_distribution<uint8_t> distribution(0, 255);
68 library->fill(tensor, distribution, i);
69 break;
70 }
71 case DataType::S32:
72 {
73 std::uniform_int_distribution<int32_t> distribution(-100, 100);
74 library->fill(tensor, distribution, i);
75 break;
76 }
77 case DataType::F16:
78 case DataType::F32:
79 {
80 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
81 library->fill(tensor, distribution, i);
82 break;
83 }
84 default:
85 library->fill_tensor_uniform(tensor, i);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010086 }
87 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +000088
Pablo Tellof5f34bb2017-08-22 13:34:13 +010089 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010090 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, QuantizationInfo quantization_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010091 {
92 // Create tensors
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +010093 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, quantization_info);
94 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, quantization_info);
95 TensorType bias = create_tensor<TensorType>(bias_shape, is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type, 1, quantization_info);
96 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010097
98 // Create and configure function
99 FunctionType conv;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000100 conv.configure(&src, &weights, &bias, &dst, info, inner_border.first, inner_border.second);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100101
102 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
106
107 // Allocate tensors
108 src.allocator()->allocate();
109 weights.allocator()->allocate();
110 bias.allocator()->allocate();
111 dst.allocator()->allocate();
112
113 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
114 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
116 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
117
118 // Fill tensors
119 fill(AccessorType(src), 0);
120 fill(AccessorType(weights), 1);
121 fill(AccessorType(bias), 2);
122
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100123 // Compute DeconvolutionLayer function
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100124 conv.run();
125
126 return dst;
127 }
128
129 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100130 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> inner_border, DataType data_type, QuantizationInfo quantization_info)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100131 {
132 // Create reference
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100133 SimpleTensor<T> src{ input_shape, data_type, 1, quantization_info };
134 SimpleTensor<T> weights{ weights_shape, data_type, 1, quantization_info };
135 SimpleTensor<TBias> bias{ bias_shape, data_type, 1, quantization_info };
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100136
137 // Fill reference
138 fill(src, 0);
139 fill(weights, 1);
140 fill(bias, 2);
141
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000142 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, inner_border);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100143 }
144
145 TensorType _target{};
146 SimpleTensor<T> _reference{};
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100147 DataType _data_type{};
148};
149
150template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
151class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
152{
153public:
154 template <typename...>
155 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000156 unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100157 {
158 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
159 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
160 const TensorShape bias_shape(num_kernels);
161 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000162 const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top);
163 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, inner_border.first, inner_border.second, sx, sy);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100164 TensorShape output_shape = deconvolution_output_shape(out_dim, input_shape, weights_shape);
Michele Di Giorgio9fef38a2018-07-06 18:06:58 +0100165 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, QuantizationInfo());
166 }
167};
168
169template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
170class DeconvolutionValidationQuantizedFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
171{
172public:
173 template <typename...>
174 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
175 unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type, QuantizationInfo quantization_info)
176 {
177 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
178 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
179 const TensorShape bias_shape(num_kernels);
180 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
181 const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top);
182 auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, inner_border.first, inner_border.second, sx, sy);
183 TensorShape output_shape = deconvolution_output_shape(out_dim, input_shape, weights_shape);
184 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, quantization_info);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100185 }
186};
187
188} // namespace validation
189} // namespace test
190} // namespace arm_compute