Compute Library
 22.11
PoolingLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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"
28 #include "tests/NEON/Accessor.h"
30 #include "tests/datasets/PoolingLayerDataset.h"
31 #include "tests/datasets/PoolingTypesDataset.h"
32 #include "tests/datasets/ShapeDatasets.h"
34 #include "tests/framework/Macros.h"
37 #include "tests/validation/fixtures/PoolingLayerFixture.h"
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 /** Input data sets for float data types */
47 
48 const auto PoolingLayerDatasetFP = combine(combine(combine(datasets::PoolingTypes(), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3), Size2D(7, 7), Size2D(3, 7), Size2D(7, 8) })),
49  framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(1, 2, 1, 1), PadStrideInfo(2, 2, 1, 0) })),
50  framework::dataset::make("ExcludePadding", { true, false }));
51 const auto PoolingLayerDatasetFPSmall = combine(combine(combine(datasets::PoolingTypes(), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3) })),
52  framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 1, 0, 0) })),
53  framework::dataset::make("ExcludePadding", { true, false }));
54 
55 /** Input data sets for asymmetric data type */
56 
57 const auto PoolingLayerDatasetQASYMM8Small = combine(combine(combine(framework::dataset::make("PoolingType", { PoolingType::MAX, PoolingType::AVG }), framework::dataset::make("PoolingSize", { Size2D(2, 2), Size2D(3, 3), Size2D(3, 7), Size2D(7, 7) })),
58  framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(1, 2, 1, 1) })),
59  framework::dataset::make("ExcludePadding", { true }));
60 
61 constexpr AbsoluteTolerance<float> tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for float types */
62 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
63 constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for float types */
64 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
65 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1); /**< Tolerance value for comparing reference's output against implementation's output for unsigned 8-bit asymmetric type */
66 constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_s(1); /**< Tolerance value for comparing reference's output against implementation's output for signed 8-bit asymmetric type */
67 const auto pool_data_layout_dataset = framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC });
68 
69 const auto qasymm8_in_qinfo_dataset = framework::dataset::make("InputQuantInfo", { QuantizationInfo(.2f, 10) });
70 const auto qasymm8_out_qinfo_dataset = framework::dataset::make("OutputQuantInfo",
71 {
72  QuantizationInfo(.2f, 10), // Same qinfo
73  QuantizationInfo(.1f, 5), // Multiplier <= 1
74  QuantizationInfo(2.f, 3) // Multiplier > 1
75 });
76 
77 const auto qasymm8_signed_in_qinfo_dataset = framework::dataset::make("InputQuantInfo", { QuantizationInfo(.2f, -10) });
78 const auto qasymm8_signed_out_qinfo_dataset = framework::dataset::make("OutputQuantInfo",
79 {
80  QuantizationInfo(.2f, -10), // Same qinfo
81  QuantizationInfo(.1f, -5), // Multiplier <= 1
82  QuantizationInfo(2.f, -3) // Multiplier > 1
83 });
84 
85 // Cases where pooling region is completely outside the input tensor (excluding global pooling)
86 const auto pool_outside_input_dataset = zip(zip(zip(zip(
87  framework::dataset::make("Shape", { TensorShape{ 2U, 2U, 1U }, TensorShape{ 2U, 2U, 4U }, TensorShape{ 3U, 5U, 2U }, TensorShape{ 10U, 20U, 3U } }),
89  framework::dataset::make("PoolingSize", { Size2D{ 2, 2 }, Size2D{ 3, 3 }, Size2D{ 2, 2 }, Size2D{ 3, 6 } })),
90  framework::dataset::make("PadStride", { PadStrideInfo{ 1, 1, 2, 2 }, PadStrideInfo{ 1, 1, 4, 4 }, PadStrideInfo{ 1, 1, 3, 3 }, PadStrideInfo{ 1, 1, 2, 5 } })),
91  framework::dataset::make("ExcludePadding", { false, false, false, false }));
92 } // namespace
93 
94 TEST_SUITE(NEON)
95 TEST_SUITE(PoolingLayer)
96 
97 // *INDENT-OFF*
98 // clang-format off
99 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
100  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data type
101  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
102  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid pad/size combination
103  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Invalid pad/size combination
104  TensorInfo(TensorShape(15U, 13U, 5U), 1, DataType::F32), // Non-rectangular Global Pooling
105  TensorInfo(TensorShape(13U, 13U, 5U), 1, DataType::F32), // Invalid output Global Pooling
106  TensorInfo(TensorShape(13U, 13U, 5U), 1, DataType::QASYMM8), // Invalid exclude_padding = false with quantized type, no actual padding and NHWC
107  TensorInfo(TensorShape(13U, 13U, 5U), 1, DataType::F32),
108  TensorInfo(TensorShape(1U, 16U, 1U), 1, DataType::F32),
109  }),
110  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F16),
111  TensorInfo(TensorShape(25U, 10U, 2U), 1, DataType::F32),
112  TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
113  TensorInfo(TensorShape(25U, 16U, 2U), 1, DataType::F32),
117  TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F32),
118  TensorInfo(TensorShape(1U, 15U, 1U), 1, DataType::F32),
119  })),
129  })),
130  framework::dataset::make("Expected", { false, false, false, false, true, false, true, false, false})),
131  input_info, output_info, pool_info, expected)
132 {
133  bool is_valid = bool(NEPoolingLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), pool_info));
135 }
136 // clang-format on
137 // *INDENT-ON*
138 
139 template <typename T>
140 using NEPoolingLayerIndicesFixture = PoolingLayerIndicesValidationFixture<Tensor, Accessor, NEPoolingLayer, T>;
141 
142 template <typename T>
143 using NEPoolingLayerFixture = PoolingLayerValidationFixture<Tensor, Accessor, NEPoolingLayer, T>;
144 template <typename T>
145 using NEPoolingLayerMixedDataLayoutFixture = PoolingLayerValidationFixture<Tensor, Accessor, NEPoolingLayer, T, true>;
146 
147 template <typename T>
148 using NESpecialPoolingLayerFixture = SpecialPoolingLayerValidationFixture<Tensor, Accessor, NEPoolingLayer, T>;
149 
151  framework::dataset::make("PadStride", { PadStrideInfo(1, 1, 0, 0), PadStrideInfo(2, 1, 0, 0) })),
152  framework::dataset::make("ExcludePadding", { true, false }));
153 
154 TEST_SUITE(Float)
155 TEST_SUITE(FP32)
156 FIXTURE_DATA_TEST_CASE(RunIndices, NEPoolingLayerIndicesFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallNoneUnitShapes(), combine(PoolingLayerIndicesDatasetFPSmall,
157  framework::dataset::make("DataType",
158  DataType::F32))),
159  framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })))
160 {
161  // Validate output
162  validate(Accessor(_target), _reference, tolerance_f32);
163  validate(Accessor(_target_indices), _ref_indices);
164 }
166 {
167  // Validate output
168  validate(Accessor(_target), _reference, tolerance_f32);
169 }
170 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallNoneUnitShapes(), combine(PoolingLayerDatasetFPSmall,
171  framework::dataset::make("DataType",
172  DataType::F32))),
173  pool_data_layout_dataset))
174 {
175  // Validate output
176  validate(Accessor(_target), _reference, tolerance_f32);
177 }
179  combine(combine(combine(combine(datasets::PoolingTypes(),
180  framework::dataset::make("PoolingSize", { Size2D(2, 2) })),
181  framework::dataset::make("PadStride", { PadStrideInfo(2, 1, 0, 0) })),
182  framework::dataset::make("ExcludePadding", { false })),
184  pool_data_layout_dataset))
185 {
186  // Validate output
187  validate(Accessor(_target), _reference, tolerance_f32);
188 }
190  framework::dataset::make("DataType",
191  DataType::F32))),
192  pool_data_layout_dataset))
193 {
194  // Validate output
195  validate(Accessor(_target), _reference, tolerance_f32);
196 }
197 TEST_SUITE(CornerCases)
198 FIXTURE_DATA_TEST_CASE(PoolRegionCompletelyOutsideInput, NEPoolingLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(pool_outside_input_dataset,
199  framework::dataset::make("DataType",
200  DataType::F32)),
201  pool_data_layout_dataset))
202 {
203  // Validate output
204  validate(Accessor(_target), _reference, tolerance_f32);
205 }
206 TEST_SUITE_END() // CornerCases
207 TEST_SUITE_END() // FP32
208 
209 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
210 TEST_SUITE(FP16)
212  framework::dataset::make("DataType",
213  DataType::F16))),
215 {
216  // Validate output
217  validate(Accessor(_target), _reference, tolerance_f16);
218  validate(Accessor(_target_indices), _ref_indices);
219 }
220 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallNoneUnitShapes(), combine(PoolingLayerDatasetFPSmall,
222  pool_data_layout_dataset))
223 {
224  // Validate output
225  validate(Accessor(_target), _reference, tolerance_f16);
226 }
227 FIXTURE_DATA_TEST_CASE(RunLarge, NEPoolingLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), combine(PoolingLayerDatasetFP,
229  pool_data_layout_dataset))
230 {
231  // Validate output
232  validate(Accessor(_target), _reference, tolerance_f16);
233 }
234 TEST_SUITE(CornerCases)
235 FIXTURE_DATA_TEST_CASE(PoolRegionCompletelyOutsideInput, NEPoolingLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(pool_outside_input_dataset,
236  framework::dataset::make("DataType",
237  DataType::F16)),
238  pool_data_layout_dataset))
239 {
240  // Validate output
241  validate(Accessor(_target), _reference, tolerance_f16);
242 }
243 TEST_SUITE_END() // CornerCases
244 TEST_SUITE_END() // FP16
245 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
246 TEST_SUITE_END() // Float
247 
248 TEST_SUITE(Quantized)
249 
250 template <typename T>
251 using NEPoolingLayerQuantizedFixture = PoolingLayerValidationQuantizedFixture<Tensor, Accessor, NEPoolingLayer, T>;
252 template <typename T>
253 using NEPoolingLayerQuantizedMixedDataLayoutFixture = PoolingLayerValidationQuantizedFixture<Tensor, Accessor, NEPoolingLayer, T, true>;
254 
255 TEST_SUITE(QASYMM8)
256 FIXTURE_DATA_TEST_CASE(RunSmallNCHW, NEPoolingLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallNoneUnitShapes(),
257  combine(PoolingLayerDatasetQASYMM8Small,
258  framework::dataset::make("DataType", DataType::QASYMM8))),
259  framework::dataset::make("DataLayout", { DataLayout::NCHW })),
260  qasymm8_in_qinfo_dataset),
261  qasymm8_in_qinfo_dataset))
262 {
263  // Validate output
264  validate(Accessor(_target), _reference, tolerance_qasymm8);
265 }
266 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallNoneUnitShapes(),
267  combine(PoolingLayerDatasetQASYMM8Small,
269  framework::dataset::make("DataLayout", { DataLayout::NHWC })),
270  qasymm8_in_qinfo_dataset),
271  qasymm8_out_qinfo_dataset))
272 {
273  // Validate output
274  validate(Accessor(_target), _reference, tolerance_qasymm8);
275 }
278  framework::dataset::make("PoolingSize", { Size2D(2, 2) })),
279  framework::dataset::make("PadStride", { PadStrideInfo(1, 2, 1, 1) })),
280  framework::dataset::make("ExcludePadding", { true })),
283  framework::dataset::make("InputQuantInfo", { QuantizationInfo(1.f / 255.f, 10) })),
284  framework::dataset::make("OutputQuantInfo", { QuantizationInfo(1.f / 255.f, 5) })))
285 {
286  // Validate output
287  validate(Accessor(_target), _reference, tolerance_qasymm8);
288 }
289 TEST_SUITE_END() // QASYMM8
291 FIXTURE_DATA_TEST_CASE(RunSmall, NEPoolingLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallNoneUnitShapes(),
292  combine(PoolingLayerDatasetQASYMM8Small,
293  framework::dataset::make("DataType", DataType::QASYMM8_SIGNED))),
294  framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
295  qasymm8_signed_in_qinfo_dataset),
296  qasymm8_signed_in_qinfo_dataset))
297 {
298  // Validate output
299  validate(Accessor(_target), _reference, tolerance_qasymm8_s);
300 }
303  framework::dataset::make("PoolingSize", { Size2D(2, 2) })),
304  framework::dataset::make("PadStride", { PadStrideInfo(1, 2, 1, 1) })),
305  framework::dataset::make("ExcludePadding", { true })),
308  framework::dataset::make("InputQuantInfo", { QuantizationInfo(1.f / 127.f, -10) })),
309  framework::dataset::make("OutputQuantInfo", { QuantizationInfo(1.f / 127.f, -10) })))
310 {
311  // Validate output
312  validate(Accessor(_target), _reference, tolerance_qasymm8_s);
313 }
314 TEST_SUITE_END() // QASYMM8_SIGNED
315 TEST_SUITE_END() // Quantized
316 TEST_SUITE_END() // PoolingLayer
317 TEST_SUITE_END() // Neon
318 } // namespace validation
319 } // namespace test
320 } // namespace arm_compute
PoolingLayerValidationQuantizedFixture< Tensor, Accessor, NEPoolingLayer, T, true > NEPoolingLayerQuantizedMixedDataLayoutFixture
Shape of a tensor.
Definition: TensorShape.h:39
SpecialPoolingLayerValidationFixture< Tensor, Accessor, NEPoolingLayer, T > NESpecialPoolingLayerFixture
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
RelativeTolerance< half_float::half > tolerance_f16(half_float::half(0.1))
Tolerance value for comparing reference&#39;s output against implementation&#39;s output for DataType::F16...
Quantization information.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
Pooling Layer Information struct.
Definition: Types.h:1200
RelativeTolerance< float > tolerance_f32(0.01f)
Tolerance value for comparing reference&#39;s output against implementation&#39;s output for DataType::F32...
Padding and stride information class.
Definition: Types.h:669
validate(CLAccessor(output_state), expected_output)
Num samples, channels, height, width.
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
PoolingLayerIndicesValidationFixture< Tensor, Accessor, NEPoolingLayer, T > NEPoolingLayerIndicesFixture
PoolingLayerValidationFixture< Tensor, Accessor, NEPoolingLayer, T > NEPoolingLayerFixture
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Num samples, height, width, channels.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
PoolingLayerValidationFixture< Tensor, Accessor, NEPoolingLayer, T, true > NEPoolingLayerMixedDataLayoutFixture
quantized, asymmetric fixed-point 8-bit number signed
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
DataType
Available data types.
Definition: Types.h:79
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices=nullptr)
Static function to check if given info will lead to a valid configuration of NEPoolingLayer.