blob: 9991437a2f08dff606e270b3b867786702b5607d [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 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010024#include "NEON/NEAccessor.h"
25#include "TypePrinter.h"
26#include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h"
Moritz Pflanzer94450f12017-06-30 12:48:43 +010027#include "tests/Globals.h"
28#include "tests/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "tests/dataset/PoolingLayerDataset.h"
30#include "validation/Datasets.h"
31#include "validation/Reference.h"
32#include "validation/Validation.h"
33
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include <random>
35
36using namespace arm_compute;
37using namespace arm_compute::test;
38using namespace arm_compute::test::neon;
39using namespace arm_compute::test::validation;
40
41namespace
42{
43const float tolerance_q = 0; /**< Tolerance value for comparing reference's output against implementation's output for quantized input */
44const float tolerance_f = 1e-05; /**< Tolerance value for comparing reference's output against implementation's output for float input */
45
46/** Compute Neon pooling layer function.
47 *
48 * @param[in] shape Shape of the input and output tensors.
49 * @param[in] dt Data type of input and output tensors.
50 * @param[in] pool_info Pooling Layer information.
51 *
52 * @return Computed output tensor.
53 */
54Tensor compute_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position = 0)
55{
56 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010057 Tensor src = create_tensor<Tensor>(shape_in, dt, 1, fixed_point_position);
58 Tensor dst = create_tensor<Tensor>(shape_out, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059
60 // Create and configure function
61 NEPoolingLayer pool;
62 pool.configure(&src, &dst, pool_info);
63
64 // Allocate tensors
65 src.allocator()->allocate();
66 dst.allocator()->allocate();
67
68 BOOST_TEST(!src.info()->is_resizable());
69 BOOST_TEST(!dst.info()->is_resizable());
70
71 // Fill tensors
72 int min = 0;
73 int max = 0;
74 switch(dt)
75 {
76 case DataType::F32:
77 min = -1;
78 max = 1;
79 break;
80 case DataType::QS8:
81 min = -(1 << fixed_point_position);
82 max = (1 << fixed_point_position);
83 break;
84 default:
85 ARM_COMPUTE_ERROR("DataType not supported.");
86 }
87 std::uniform_real_distribution<> distribution(min, max);
88 library->fill(NEAccessor(src), distribution, 0);
89
90 // Compute function
91 pool.run();
92
93 return dst;
94}
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +010095
96TensorShape get_output_shape(TensorShape in_shape, const PoolingLayerInfo &pool_info)
97{
98 TensorShape out_shape(in_shape);
99 const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(in_shape.x(),
100 in_shape.y(),
101 pool_info.pool_size(),
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100102 pool_info.pool_size(),
103 pool_info.pad_stride_info());
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100104 out_shape.set(0, scaled_dims.first);
105 out_shape.set(1, scaled_dims.second);
106 return out_shape;
107}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108} // namespace
109
110#ifndef DOXYGEN_SKIP_THIS
111BOOST_AUTO_TEST_SUITE(NEON)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112BOOST_AUTO_TEST_SUITE(PoolingLayer)
113
114BOOST_AUTO_TEST_SUITE(Float)
115BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
116BOOST_DATA_TEST_CASE(RandomDataset,
117 RandomPoolingLayerDataset() * boost::unit_test::data::make(DataType::F32),
118 obj, dt)
119{
120 // Compute function
121 Tensor dst = compute_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info);
122
123 // Compute reference
124 RawTensor ref_dst = Reference::compute_reference_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info);
125
126 // Validate output
127 validate(NEAccessor(dst), ref_dst, tolerance_f, 0);
128}
Michele Di Giorgio8af2dd62017-06-19 15:19:29 +0100129
130BOOST_DATA_TEST_CASE(RunSmall7x7,
131 SmallShapes() * CNNFloatDataTypes() * PoolingTypes() * boost::unit_test::data::make({ 2, 3, 7 }) * boost::unit_test::data::make({ 1, 2 }) * boost::unit_test::data::make({ 0, 1 }),
132 src_shape, dt, pool_type, pool_size, pool_stride, pool_pad)
133{
134 PoolingLayerInfo pool_info(pool_type, pool_size, PadStrideInfo(pool_stride, pool_stride, pool_pad, pool_pad, DimensionRoundingType::CEIL));
135 TensorShape dst_shape = get_output_shape(src_shape, pool_info);
136
137 // Compute function
138 Tensor dst = compute_pooling_layer(src_shape, dst_shape, dt, pool_info);
139
140 // Compute reference
141 RawTensor ref_dst = Reference::compute_reference_pooling_layer(src_shape, dst_shape, dt, pool_info);
142
143 // Validate output
144 validate(NEAccessor(dst), ref_dst, tolerance_f, 0);
145}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146BOOST_AUTO_TEST_SUITE_END()
147
148BOOST_AUTO_TEST_SUITE(Quantized)
149BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
150BOOST_DATA_TEST_CASE(RandomDataset,
151 RandomPoolingLayerDataset() * boost::unit_test::data::make(DataType::QS8) * boost::unit_test::data::xrange(1, 5),
152 obj, dt, fixed_point_position)
153{
154 // Compute function
155 Tensor dst = compute_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info, fixed_point_position);
156
157 // Compute reference
158 RawTensor ref_dst = Reference::compute_reference_pooling_layer(obj.src_shape, obj.dst_shape, dt, obj.info, fixed_point_position);
159
160 // Validate output
161 validate(NEAccessor(dst), ref_dst, tolerance_q, 0);
162}
163BOOST_AUTO_TEST_SUITE_END()
164
165BOOST_AUTO_TEST_SUITE_END()
166BOOST_AUTO_TEST_SUITE_END()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167#endif