blob: af078d4ce38b271d058139f9e39fe1ed98b3b561 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Giorgio Arena33b103b2021-01-08 10:37:15 +00002 * Copyright (c) 2017-2021 Arm Limited.
Georgios Pinitasdc460f12017-08-24 19:02:44 +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#ifndef ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Pablo Telloa52e4cf2019-04-01 14:55:18 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010030#include "arm_compute/runtime/Tensor.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010031#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/PoolingLayer.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010037#include <random>
Georgios Pinitasdc460f12017-08-24 19:02:44 +010038namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000045class PoolingLayerValidationGenericFixture : public framework::Fixture
Georgios Pinitasdc460f12017-08-24 19:02:44 +010046{
47public:
48 template <typename...>
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +000049 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout, bool indices = false,
50 QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo())
Georgios Pinitasdc460f12017-08-24 19:02:44 +010051 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +010052 _pool_info = pool_info;
morgolockcc1f6c92020-03-24 09:26:48 +000053 _target = compute_target(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo, indices);
morgolocke383c352020-04-03 16:57:46 +010054 _reference = compute_reference(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo, indices);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010055 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor)
60 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000061 if(tensor.data_type() == DataType::F32)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010062 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000063 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
64 library->fill(tensor, distribution, 0);
65 }
66 else if(tensor.data_type() == DataType::F16)
67 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000068 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Georgios Pinitasdc460f12017-08-24 19:02:44 +010069 library->fill(tensor, distribution, 0);
70 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010071 else // data type is quantized_asymmetric
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000072 {
73 library->fill_tensor_uniform(tensor, 0);
74 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010075 }
76
Giorgio Arena563494c2018-04-30 17:29:41 +010077 TensorType compute_target(TensorShape shape, PoolingLayerInfo info,
morgolockcc1f6c92020-03-24 09:26:48 +000078 DataType data_type, DataLayout data_layout,
79 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo,
80 bool indices)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010081 {
Giorgio Arena563494c2018-04-30 17:29:41 +010082 // Change shape in case of NHWC.
83 if(data_layout == DataLayout::NHWC)
84 {
85 permute(shape, PermutationVector(2U, 0U, 1U));
86 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010087 // Create tensors
Pablo Telloa52e4cf2019-04-01 14:55:18 +010088 TensorType src = create_tensor<TensorType>(shape, data_type, 1, input_qinfo, data_layout);
89 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), info);
90 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
morgolocke383c352020-04-03 16:57:46 +010091 _target_indices = create_tensor<TensorType>(dst_shape, DataType::U32, 1, output_qinfo, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010092
93 // Create and configure function
94 FunctionType pool_layer;
morgolockcc1f6c92020-03-24 09:26:48 +000095 pool_layer.configure(&src, &dst, info, (indices) ? &_target_indices : nullptr);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010096
97 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
98 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
morgolockcc1f6c92020-03-24 09:26:48 +000099 ARM_COMPUTE_EXPECT(_target_indices.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100100
101 // Allocate tensors
102 src.allocator()->allocate();
103 dst.allocator()->allocate();
morgolockcc1f6c92020-03-24 09:26:48 +0000104 _target_indices.allocator()->allocate();
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100105
106 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
morgolockcc1f6c92020-03-24 09:26:48 +0000108 ARM_COMPUTE_EXPECT(!_target_indices.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100109
110 // Fill tensors
111 fill(AccessorType(src));
112
113 // Compute function
114 pool_layer.run();
115
116 return dst;
117 }
118
morgolocke383c352020-04-03 16:57:46 +0100119 SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo info, DataType data_type, DataLayout data_layout,
morgolockcc1f6c92020-03-24 09:26:48 +0000120 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo, bool indices)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100121 {
122 // Create reference
morgolocke383c352020-04-03 16:57:46 +0100123 SimpleTensor<T> src(shape, data_type, 1, input_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100124 // Fill reference
125 fill(src);
morgolocke383c352020-04-03 16:57:46 +0100126 return reference::pooling_layer<T>(src, info, output_qinfo, indices ? &_ref_indices : nullptr, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100127 }
128
morgolockcc1f6c92020-03-24 09:26:48 +0000129 TensorType _target{};
130 SimpleTensor<T> _reference{};
131 PoolingLayerInfo _pool_info{};
132 TensorType _target_indices{};
133 SimpleTensor<uint32_t> _ref_indices{};
134};
135template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
136class PoolingLayerIndicesValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
137{
138public:
139 template <typename...>
140 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout)
141 {
142 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
143 data_type, data_layout, true);
144 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100145};
146
147template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000148class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100149{
150public:
151 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000152 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100153 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000154 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100155 data_type, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100156 }
157};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100158
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100160class PoolingLayerValidationMixedPrecisionFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
163 template <typename...>
164 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout, bool fp_mixed_precision = false)
165 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000166 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding, fp_mixed_precision),
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100167 data_type, data_layout);
168 }
169};
170
171template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000172class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
173{
174public:
175 template <typename...>
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +0000176 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
177 QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo())
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000178 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000179 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +0000180 data_type, data_layout, false, input_qinfo, output_qinfo);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000181 }
182};
183
184template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas15997872018-02-19 13:58:22 +0000185class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
186{
187public:
188 template <typename...>
Giorgio Arena3c520c52018-05-01 11:47:24 +0100189 void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type)
Georgios Pinitas15997872018-02-19 13:58:22 +0000190 {
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100191 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, DataLayout::NCHW);
Georgios Pinitas15997872018-02-19 13:58:22 +0000192 }
193};
194
195template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000196class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100197{
198public:
199 template <typename...>
Michalis Spyrou57dac842018-03-01 16:03:50 +0000200 void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100201 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000202 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, data_layout), data_type, data_layout);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100203 }
204};
Georgios Pinitas15997872018-02-19 13:58:22 +0000205
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100206} // namespace validation
207} // namespace test
208} // namespace arm_compute
209#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */