arm_compute v18.05
[platform/upstream/armcl.git] / tests / validation / NEON / PoolingLayer.cpp
1 /*
2  * Copyright (c) 2017-2018 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 "arm_compute/core/Types.h"
25 #include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h"
26 #include "arm_compute/runtime/Tensor.h"
27 #include "arm_compute/runtime/TensorAllocator.h"
28 #include "tests/NEON/Accessor.h"
29 #include "tests/PaddingCalculator.h"
30 #include "tests/datasets/PoolingLayerDataset.h"
31 #include "tests/datasets/PoolingTypesDataset.h"
32 #include "tests/datasets/ShapeDatasets.h"
33 #include "tests/framework/Asserts.h"
34 #include "tests/framework/Macros.h"
35 #include "tests/framework/datasets/Datasets.h"
36 #include "tests/validation/Validation.h"
37 #include "tests/validation/fixtures/PoolingLayerFixture.h"
38
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 namespace
46 {
47 /** Input data set for float data types */
48
49 const auto PoolingLayerDatasetFP = combine(combine(combine(datasets::PoolingTypes(), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3), Size2D(7, 7), Size2D(9, 9), Size2D(4, 4), Size2D(3, 7), Size2D(7, 8) })),
50                                                    framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 1, 0, 0), PadStrideInfo(1, 2, 1, 1), PadStrideInfo(2, 2, 1, 0) })),
51                                            framework::dataset::make("ExcludePadding", { true, false }));
52
53 /** Input data set for quantized data types */
54 const auto PoolingLayerDatasetQS = combine(combine(combine(framework::dataset::make("PoolingType", { PoolingType::MAX, PoolingType::AVG }), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3) })),
55                                                    framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 1, 0, 0), PadStrideInfo(1, 2, 1, 1), PadStrideInfo(2, 2, 1, 0) })),
56                                            framework::dataset::make("ExcludePadding", { false }));
57
58 /** Input data set for asymmetric data type */
59
60 const auto PoolingLayerDatasetQASYMM8 = combine(combine(combine(framework::dataset::make("PoolingType", { PoolingType::MAX, PoolingType::AVG }), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3), Size2D(4, 4), Size2D(9, 9), Size2D(3, 7), Size2D(7, 8) })),
61                                                         framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 1, 0, 0), PadStrideInfo(1, 2, 1, 1), PadStrideInfo(2, 2, 1, 0) })),
62                                                 framework::dataset::make("ExcludePadding", { true }));
63
64 constexpr AbsoluteTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for float types */
65 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
66 constexpr AbsoluteTolerance<float> tolerance_f16(0.01f);   /**< Tolerance value for comparing reference's output against implementation's output for float types */
67 #endif                                                     /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
68 constexpr AbsoluteTolerance<float>   tolerance_qs8(0);     /**< Tolerance value for comparing reference's output against implementation's output for quantized input */
69 constexpr AbsoluteTolerance<float>   tolerance_qs16(0);    /**< Tolerance value for comparing reference's output against implementation's output for quantized input */
70 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1); /**< Tolerance value for comparing reference's output against implementation's output for 8-bit asymmetric type */
71 } // namespace
72
73 TEST_SUITE(NEON)
74 TEST_SUITE(PoolingLayer)
75
76 // *INDENT-OFF*
77 // clang-format off
78 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
79     framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, 0),     // Mismatching data type
80                                             TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, 0),     // Window shrink
81                                             TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 4),     // Mismatching fixed point position
82                                             TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS16, 11),   // Window shrink
83                                             TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, 0),     // Invalid pad/size combination
84                                             TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, 0),     // Invalid pad/size combination
85                                             TensorInfo(TensorShape(15U, 13U, 5U), 1, DataType::F32, 0),     // Non-rectangular Global Pooling
86                                             TensorInfo(TensorShape(13U, 13U, 5U), 1, DataType::F32, 0),     // Invalid output Global Pooling
87                                             TensorInfo(TensorShape(13U, 13U, 5U), 1, DataType::F32, 0),
88                                           }),
89     framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F16, 0),
90                                             TensorInfo(TensorShape(25U, 10U, 2U), 1, DataType::F32, 0),
91                                             TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::QS8, 5),
92                                             TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::QS16, 11),
93                                             TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32, 0),
94                                             TensorInfo(TensorShape(25U, 16U, 2U), 1, DataType::F32, 0),
95                                             TensorInfo(TensorShape(1U, 1U, 5U), 1, DataType::F32, 0),
96                                             TensorInfo(TensorShape(2U, 2U, 5U), 1, DataType::F32, 0),
97                                             TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F32, 0),
98                                           })),
99     framework::dataset::make("PoolInfo",  { PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(1, 1, 0, 0)),
100                                             PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(1, 1, 0, 0)),
101                                             PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(1, 1, 0, 0)),
102                                             PoolingLayerInfo(PoolingType::AVG, 3, PadStrideInfo(1, 1, 0, 0)),
103                                             PoolingLayerInfo(PoolingType::AVG, 2, PadStrideInfo(1, 1, 2, 0)),
104                                             PoolingLayerInfo(PoolingType::AVG, 2, PadStrideInfo(1, 1, 0, 2)),
105                                             PoolingLayerInfo(PoolingType::AVG),
106                                             PoolingLayerInfo(PoolingType::MAX),
107                                             PoolingLayerInfo(PoolingType::AVG),
108                                            })),
109     framework::dataset::make("Expected", { false, false, false, false, false, false, true, false, false, true })),
110     input_info, output_info, pool_info, expected)
111 {
112     bool is_valid = bool(NEPoolingLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), pool_info));
113     ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
114 }
115 // clang-format on
116 // *INDENT-ON*
117
118 template <typename T>
119 using NEPoolingLayerFixture = PoolingLayerValidationFixture<Tensor, Accessor, NEPoolingLayer, T>;
120
121 template <typename T>
122 using NESpecialPoolingLayerFixture = SpecialPoolingLayerValidationFixture<Tensor, Accessor, NEPoolingLayer, T>;
123
124 TEST_SUITE(Float)
125 TEST_SUITE(FP32)
126 FIXTURE_DATA_TEST_CASE(RunSpecial, NESpecialPoolingLayerFixture<float>, framework::DatasetMode::ALL, datasets::PoolingLayerDatasetSpecial() * framework::dataset::make("DataType", DataType::F32))
127 {
128     // Validate output
129     validate(Accessor(_target), _reference, tolerance_f32);
130 }
131 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), combine(PoolingLayerDatasetFP, framework::dataset::make("DataType",
132                                                                                                             DataType::F32))),
133                                                                                                     framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
134 {
135     // Validate output
136     validate(Accessor(_target), _reference, tolerance_f32);
137 }
138 FIXTURE_DATA_TEST_CASE(RunLarge, NEPoolingLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), combine(PoolingLayerDatasetFP,
139                                                                                                                 framework::dataset::make("DataType",
140                                                                                                                         DataType::F32))),
141                                                                                                         framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
142 {
143     // Validate output
144     validate(Accessor(_target), _reference, tolerance_f32);
145 }
146 TEST_SUITE_END() // FP32
147
148 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
149 TEST_SUITE(FP16)
150 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), combine(PoolingLayerDatasetFP,
151                                                                                                            framework::dataset::make("DataType", DataType::F16))),
152                                                                                                    framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
153 {
154     // Validate output
155     validate(Accessor(_target), _reference, tolerance_f16);
156 }
157 FIXTURE_DATA_TEST_CASE(RunLarge, NEPoolingLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), combine(PoolingLayerDatasetFP,
158                                                                                                                framework::dataset::make("DataType", DataType::F16))),
159                                                                                                        framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
160 {
161     // Validate output
162     validate(Accessor(_target), _reference, tolerance_f16);
163 }
164 TEST_SUITE_END() // FP16
165 #endif           /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
166 TEST_SUITE_END() // Float
167
168 template <typename T>
169 using NEPoolingLayerFixedPointFixture = PoolingLayerValidationFixedPointFixture<Tensor, Accessor, NEPoolingLayer, T>;
170
171 TEST_SUITE(FixedPoint)
172 TEST_SUITE(QS8)
173 FIXTURE_DATA_TEST_CASE(RunTiny, NEPoolingLayerFixedPointFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(datasets::TinyShapes(), combine(PoolingLayerDatasetQS,
174                                                                                                                       framework::dataset::make("DataType", DataType::QS8))),
175                                                                                                               framework::dataset::make("FractionalBits", 1, 5)))
176 {
177     // Validate output
178     validate(Accessor(_target), _reference, tolerance_qs8);
179 }
180 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SmallShapes(), combine(PoolingLayerDatasetQS,
181                                                                                                                    framework::dataset::make("DataType", DataType::QS8))),
182                                                                                                                    framework::dataset::make("FractionalBits", 1, 5)))
183 {
184     // Validate output
185     validate(Accessor(_target), _reference, tolerance_qs8);
186 }
187 TEST_SUITE_END() // QS8
188
189 TEST_SUITE(QS16)
190 FIXTURE_DATA_TEST_CASE(RunTiny, NEPoolingLayerFixedPointFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(datasets::TinyShapes(), combine(PoolingLayerDatasetQS,
191                                                                                                                        framework::dataset::make("DataType", DataType::QS16))),
192                                                                                                                framework::dataset::make("FractionalBits", 1, 13)))
193 {
194     // Validate output
195     validate(Accessor(_target), _reference, tolerance_qs16);
196 }
197 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::SmallShapes(), combine(PoolingLayerDatasetQS,
198                                                                                                                     framework::dataset::make("DataType", DataType::QS16))),
199                                                                                                                     framework::dataset::make("FractionalBits", 1, 13)))
200 {
201     // Validate output
202     validate(Accessor(_target), _reference, tolerance_qs16);
203 }
204 TEST_SUITE_END() // QS16
205 TEST_SUITE_END() // FixedPoint
206
207 TEST_SUITE(Quantized)
208
209 template <typename T>
210 using NEPoolingLayerQuantizedFixture = PoolingLayerValidationQuantizedFixture<Tensor, Accessor, NEPoolingLayer, T>;
211
212 TEST_SUITE(QASYMM8)
213 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), combine(PoolingLayerDatasetQASYMM8,
214                                                                                                                        framework::dataset::make("DataType", DataType::QASYMM8))),
215                                                                                                                        framework::dataset::make("QuantizationInfo", { QuantizationInfo(2.f / 255, 127),
216                                                                                                                                QuantizationInfo(7.f / 255, 123)
217                                                                                                                                                                     })),
218                                                                                                                framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
219 {
220     // Validate output
221     validate(Accessor(_target), _reference, tolerance_qasymm8);
222 }
223 FIXTURE_DATA_TEST_CASE(RunLarge, NEPoolingLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), combine(PoolingLayerDatasetQASYMM8,
224                                                                                                                    framework::dataset::make("DataType", DataType::QASYMM8))),
225                                                                                                                    framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255, 0) })),
226                                                                                                                    framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
227 {
228     // Validate output
229     validate(Accessor(_target), _reference, tolerance_qasymm8);
230 }
231 TEST_SUITE_END() // QASYMM8
232 TEST_SUITE_END() // Quantized
233 TEST_SUITE_END() // PoolingLayer
234 TEST_SUITE_END() // NEON
235 } // namespace validation
236 } // namespace test
237 } // namespace arm_compute