blob: f2455f31ac51787bd513de86211a69190c174d4e [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Michalis Spyrou780db4e2017-11-23 09:49:51 +00002 * 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:
Pablo Tellof5f34bb2017-08-22 13:34:13 +010046 template <typename...>
47 void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info,
Michalis Spyrou780db4e2017-11-23 09:49:51 +000048 const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, int fractional_bits)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010049 {
50 _fractional_bits = fractional_bits;
51 _data_type = data_type;
52
Michalis Spyrou780db4e2017-11-23 09:49:51 +000053 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, fractional_bits);
54 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, fractional_bits);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010055 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor, int i)
60 {
61 switch(tensor.data_type())
62 {
63 case DataType::F32:
64 {
65 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
66 library->fill(tensor, distribution, i);
67 break;
68 }
69 default:
70 library->fill_tensor_uniform(tensor, i);
71 }
72 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +000073
Pablo Tellof5f34bb2017-08-22 13:34:13 +010074 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Michalis Spyrou780db4e2017-11-23 09:49:51 +000075 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &inner_border, DataType data_type, int fixed_point_position)
Pablo Tellof5f34bb2017-08-22 13:34:13 +010076 {
77 // Create tensors
78 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position);
79 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position);
80 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position);
81 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position);
82
83 // Create and configure function
84 FunctionType conv;
Michalis Spyrou780db4e2017-11-23 09:49:51 +000085 conv.configure(&src, &weights, &bias, &dst, info, inner_border.first, inner_border.second);
Pablo Tellof5f34bb2017-08-22 13:34:13 +010086
87 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
88 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
90 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
91
92 // Allocate tensors
93 src.allocator()->allocate();
94 weights.allocator()->allocate();
95 bias.allocator()->allocate();
96 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
103 // Fill tensors
104 fill(AccessorType(src), 0);
105 fill(AccessorType(weights), 1);
106 fill(AccessorType(bias), 2);
107
108 // Compute NEConvolutionLayer function
109 conv.run();
110
111 return dst;
112 }
113
114 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape,
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000115 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> inner_border, DataType data_type, int fixed_point_position)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100116 {
117 // Create reference
118 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position };
119 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position };
120 SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position };
121
122 // Fill reference
123 fill(src, 0);
124 fill(weights, 1);
125 fill(bias, 2);
126
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000127 return reference::deconvolution_layer<T>(src, weights, bias, output_shape, info, inner_border);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100128 }
129
130 TensorType _target{};
131 SimpleTensor<T> _reference{};
132 int _fractional_bits{};
133 DataType _data_type{};
134};
135
136template <typename TensorType, typename AccessorType, typename FunctionType, typename T, unsigned int kernel_size_x, unsigned int kernel_size_y>
137class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>
138{
139public:
140 template <typename...>
141 void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady,
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000142 unsigned int inner_border_right, unsigned int inner_border_top, unsigned int num_kernels, DataType data_type)
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100143 {
144 ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported");
145 const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels);
146 const TensorShape bias_shape(num_kernels);
147 const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000148 const std::pair<unsigned int, unsigned int> inner_border(inner_border_right, inner_border_top);
149 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 +0100150 TensorShape output_shape = deconvolution_output_shape(out_dim, input_shape, weights_shape);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000151 DeconvolutionLayerFixtureBase<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, inner_border, data_type, 0);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100152 }
153};
154
155} // namespace validation
156} // namespace test
157} // namespace arm_compute