blob: 4e36e331bd5b96e95728e17c2fc253fc2345292d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 "Globals.h"
25#include "NEON/Helper.h"
26#include "NEON/NEAccessor.h"
27#include "TensorLibrary.h"
28#include "TypePrinter.h"
29#include "Utils.h"
30#include "validation/Datasets.h"
31#include "validation/Reference.h"
32#include "validation/Validation.h"
33
34#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
37#include "arm_compute/runtime/Tensor.h"
38#include "arm_compute/runtime/TensorAllocator.h"
39
40#include "boost_wrapper.h"
41
42#include <random>
43#include <string>
44#include <tuple>
45
46using namespace arm_compute;
47using namespace arm_compute::test;
48using namespace arm_compute::test::neon;
49using namespace arm_compute::test::validation;
50
51namespace
52{
53const float tolerance_fp = 1e-3f; /**< Tolerance for floating point tests */
54const float tolerance_qs8 = 1; /**< Tolerance for fixed point tests */
55
56/** Compute NEON direct convolution layer function.
57 *
58 * @param[in] src_shape Shape of the input tensor.
59 * @param[in] weights_shape Shape of the weights.
60 * @param[in] bias_shape Shape of the bias tensor.
61 * @param[in] dst_shape Shape of the output tensor.
62 * @param[in] dt Data type of input, convolution matrix and output tensors.
63 * @param[in] conv_info Padding and stride information.
64 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
65 *
66 * @return Computed output tensor.
67*/
68Tensor compute_convolution_layer(const TensorShape &src_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &dst_shape,
69 DataType dt, PadStrideInfo conv_info, int fixed_point_position = 0)
70{
71 // Create tensors
72 Tensor src = create_tensor(src_shape, dt, 1, fixed_point_position);
73 Tensor weights = create_tensor(weights_shape, dt, 1, fixed_point_position);
74 Tensor bias = create_tensor(bias_shape, dt, 1, fixed_point_position);
75 Tensor dst = create_tensor(dst_shape, dt, 1, fixed_point_position);
76
77 // Create and configure function
78 NEDirectConvolutionLayer conv_layer;
79 conv_layer.configure(&src, &weights, &bias, &dst, conv_info);
80
81 // Allocate tensors
82 src.allocator()->allocate();
83 weights.allocator()->allocate();
84 bias.allocator()->allocate();
85 dst.allocator()->allocate();
86
87 BOOST_TEST(!src.info()->is_resizable());
88 BOOST_TEST(!weights.info()->is_resizable());
89 BOOST_TEST(!bias.info()->is_resizable());
90 BOOST_TEST(!dst.info()->is_resizable());
91
92 // Fill tensors
93 if(dt == DataType::F32)
94 {
95 std::uniform_real_distribution<> distribution(-1.f, 1.f);
96 library->fill(NEAccessor(src), distribution, 0);
97 library->fill(NEAccessor(weights), distribution, 1);
98 library->fill(NEAccessor(bias), distribution, 2);
99 }
100 else
101 {
102 library->fill_tensor_uniform(NEAccessor(src), 0);
103 library->fill_tensor_uniform(NEAccessor(weights), 1);
104 library->fill_tensor_uniform(NEAccessor(bias), 2);
105 }
106
107 // Compute function
108 conv_layer.run();
109
110 return dst;
111}
112
113TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &conv_info)
114{
115 TensorShape out_shape(in_shape);
116 const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(in_shape.x(),
117 in_shape.y(),
118 kernel_shape.x(),
119 conv_info.stride().first, conv_info.stride().second,
120 conv_info.pad().first, conv_info.pad().second,
121 conv_info.round());
122 out_shape.set(0, scaled_dims.first);
123 out_shape.set(1, scaled_dims.second);
124 out_shape.set(2, kernel_shape[3]);
125 return out_shape;
126}
127
128} // namespace
129
130#ifndef DOXYGEN_SKIP_THIS
131BOOST_AUTO_TEST_SUITE(NEON)
132BOOST_AUTO_TEST_SUITE(ConvolutionLayer)
133BOOST_AUTO_TEST_SUITE(Direct)
134
135BOOST_AUTO_TEST_SUITE(Float)
136BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
137BOOST_DATA_TEST_CASE(W1x1,
138 DirectConvolutionShapes() * CNNFloatDataTypes() * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::make({ 1, 4, 8, 16 }),
139 input_shape, dt, sx, sy, num_kernels)
140{
141 const unsigned int kernel_size = 1;
142 const PadStrideInfo conv_info(sx, sy, 0, 0, DimensionRoundingType::FLOOR);
143 const TensorShape w_shape(kernel_size, kernel_size, input_shape.z(), static_cast<unsigned int>(num_kernels));
144 const TensorShape b_shape(static_cast<unsigned int>(num_kernels));
145 const TensorShape d_shape(get_output_shape(input_shape, w_shape, conv_info));
146
147 Tensor dst = compute_convolution_layer(input_shape, w_shape, b_shape, d_shape, dt, conv_info);
148
149 RawTensor ref = Reference::compute_reference_convolution_layer(input_shape, w_shape, b_shape, d_shape, dt, conv_info, 0);
150
151 // Validate output
152 validate(NEAccessor(dst), ref);
153}
154
155BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
156BOOST_DATA_TEST_CASE(W3x3, DirectConvolutionShapes() * CNNFloatDataTypes() * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(0, 2,
157 1)
158 * boost::unit_test::data::xrange(0, 2, 1) * boost::unit_test::data::make({ 1, 4, 8, 16 }),
159 input_shape, dt, sx, sy, px, py, num_kernels)
160{
161 const unsigned int kernel_size = 3;
162 const PadStrideInfo conv_info(sx, sy, px, py, DimensionRoundingType::FLOOR);
163 const TensorShape w_shape(kernel_size, kernel_size, input_shape.z(), static_cast<unsigned int>(num_kernels));
164 const TensorShape b_shape(static_cast<unsigned int>(num_kernels));
165 const TensorShape d_shape(get_output_shape(input_shape, w_shape, conv_info));
166
167 Tensor dst = compute_convolution_layer(input_shape, w_shape, b_shape, d_shape, dt, conv_info);
168
169 RawTensor ref = Reference::compute_reference_convolution_layer(input_shape, w_shape, b_shape, d_shape, dt, conv_info, 0);
170
171 // Validate output
172 validate(NEAccessor(dst), ref, tolerance_fp);
173}
174BOOST_AUTO_TEST_SUITE_END()
175
176BOOST_AUTO_TEST_SUITE(Quantized)
177BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
178BOOST_DATA_TEST_CASE(W1x1,
179 DirectConvolutionShapes() * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::make({ 1, 4, 8, 16 }) * boost::unit_test::data::make({ 4, 5 }),
180 input_shape, sx, sy, num_kernels, fixed_point_position)
181{
182 const unsigned int kernel_size = 1;
183 const PadStrideInfo conv_info(sx, sy, 0, 0, DimensionRoundingType::FLOOR);
184 const TensorShape w_shape(kernel_size, kernel_size, input_shape.z(), static_cast<unsigned int>(num_kernels));
185 const TensorShape b_shape(static_cast<unsigned int>(num_kernels));
186 const TensorShape d_shape(get_output_shape(input_shape, w_shape, conv_info));
187
188 Tensor dst = compute_convolution_layer(input_shape, w_shape, b_shape, d_shape, DataType::QS8, conv_info, fixed_point_position);
189
190 RawTensor ref = Reference::compute_reference_convolution_layer(input_shape, w_shape, b_shape, d_shape, DataType::QS8, conv_info, fixed_point_position);
191
192 // Validate output
193 validate(NEAccessor(dst), ref);
194}
195
196BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
197BOOST_DATA_TEST_CASE(W3x3, DirectConvolutionShapes() * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(1, 3, 1) * boost::unit_test::data::xrange(0, 2, 1)
198 * boost::unit_test::data::xrange(0, 2, 1) * boost::unit_test::data::make({ 1, 4, 8, 16 }) * boost::unit_test::data::make({ 4, 5 }),
199 input_shape, sx, sy, px, py, num_kernels, fixed_point_position)
200{
201 const unsigned int kernel_size = 3;
202 const PadStrideInfo conv_info(sx, sy, px, py, DimensionRoundingType::FLOOR);
203 const TensorShape w_shape(kernel_size, kernel_size, input_shape.z(), static_cast<unsigned int>(num_kernels));
204 const TensorShape b_shape(static_cast<unsigned int>(num_kernels));
205 const TensorShape d_shape(get_output_shape(input_shape, w_shape, conv_info));
206
207 Tensor dst = compute_convolution_layer(input_shape, w_shape, b_shape, d_shape, DataType::QS8, conv_info, fixed_point_position);
208
209 RawTensor ref = Reference::compute_reference_convolution_layer(input_shape, w_shape, b_shape, d_shape, DataType::QS8, conv_info, fixed_point_position);
210
211 // Validate output
212 validate(NEAccessor(dst), ref, tolerance_qs8);
213}
214BOOST_AUTO_TEST_SUITE_END()
215
216BOOST_AUTO_TEST_SUITE_END()
217BOOST_AUTO_TEST_SUITE_END()
218BOOST_AUTO_TEST_SUITE_END()
219#endif