blob: 59c920868b13ccf00eeaa4727e1515cff474ff1d [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00002 * Copyright (c) 2017-2021, 2023 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:
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +000048 void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout, bool indices = false,
Manuel Bottinica62c6f2021-03-23 11:50:34 +000049 QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo(), bool mixed_layout = false)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010050 {
Manuel Bottinica62c6f2021-03-23 11:50:34 +000051 _mixed_layout = mixed_layout;
Giorgio Arena63825e82021-03-25 14:54:50 +000052 _pool_info = pool_info;
53 _target = compute_target(shape, pool_info, data_type, data_layout, input_qinfo, output_qinfo, indices);
54 _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:
Manuel Bottinica62c6f2021-03-23 11:50:34 +000058 void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst)
59 {
60 const DataLayout data_layout = src.info()->data_layout();
61 // Test Multi DataLayout graph cases, when the data layout changes after configure
62 src.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
63 dst.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
64
65 // Compute Convolution function
66 layer.run();
67
68 // Reinstating original data layout for the test suite to properly check the values
69 src.info()->set_data_layout(data_layout);
70 dst.info()->set_data_layout(data_layout);
71 }
72
Georgios Pinitasdc460f12017-08-24 19:02:44 +010073 template <typename U>
74 void fill(U &&tensor)
75 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000076 if(tensor.data_type() == DataType::F32)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010077 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000078 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
79 library->fill(tensor, distribution, 0);
80 }
81 else if(tensor.data_type() == DataType::F16)
82 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000083 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f };
Georgios Pinitasdc460f12017-08-24 19:02:44 +010084 library->fill(tensor, distribution, 0);
85 }
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010086 else // data type is quantized_asymmetric
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000087 {
88 library->fill_tensor_uniform(tensor, 0);
89 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +010090 }
91
Giorgio Arena563494c2018-04-30 17:29:41 +010092 TensorType compute_target(TensorShape shape, PoolingLayerInfo info,
morgolockcc1f6c92020-03-24 09:26:48 +000093 DataType data_type, DataLayout data_layout,
94 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo,
95 bool indices)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010096 {
Giorgio Arena563494c2018-04-30 17:29:41 +010097 // Change shape in case of NHWC.
98 if(data_layout == DataLayout::NHWC)
99 {
100 permute(shape, PermutationVector(2U, 0U, 1U));
101 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100102 // Create tensors
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100103 TensorType src = create_tensor<TensorType>(shape, data_type, 1, input_qinfo, data_layout);
104 const TensorShape dst_shape = misc::shape_calculator::compute_pool_shape(*(src.info()), info);
105 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
morgolocke383c352020-04-03 16:57:46 +0100106 _target_indices = create_tensor<TensorType>(dst_shape, DataType::U32, 1, output_qinfo, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100107
108 // Create and configure function
109 FunctionType pool_layer;
morgolockcc1f6c92020-03-24 09:26:48 +0000110 pool_layer.configure(&src, &dst, info, (indices) ? &_target_indices : nullptr);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100111
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100112 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
113 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
114 ARM_COMPUTE_ASSERT(_target_indices.info()->is_resizable());
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100115
Sang-Hoon Parkfe56edb2021-04-13 20:21:11 +0100116 add_padding_x({ &src, &dst, &_target_indices }, data_layout);
Giorgio Arena63825e82021-03-25 14:54:50 +0000117
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100118 // Allocate tensors
119 src.allocator()->allocate();
120 dst.allocator()->allocate();
morgolockcc1f6c92020-03-24 09:26:48 +0000121 _target_indices.allocator()->allocate();
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100122
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100123 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
124 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
125 ARM_COMPUTE_ASSERT(!_target_indices.info()->is_resizable());
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100126
127 // Fill tensors
128 fill(AccessorType(src));
129
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000130 if(_mixed_layout)
131 {
132 mix_layout(pool_layer, src, dst);
133 }
134 else
135 {
136 // Compute function
137 pool_layer.run();
138 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100139 return dst;
140 }
141
morgolocke383c352020-04-03 16:57:46 +0100142 SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo info, DataType data_type, DataLayout data_layout,
morgolockcc1f6c92020-03-24 09:26:48 +0000143 QuantizationInfo input_qinfo, QuantizationInfo output_qinfo, bool indices)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100144 {
145 // Create reference
morgolocke383c352020-04-03 16:57:46 +0100146 SimpleTensor<T> src(shape, data_type, 1, input_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100147 // Fill reference
148 fill(src);
morgolocke383c352020-04-03 16:57:46 +0100149 return reference::pooling_layer<T>(src, info, output_qinfo, indices ? &_ref_indices : nullptr, data_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100150 }
151
morgolockcc1f6c92020-03-24 09:26:48 +0000152 TensorType _target{};
153 SimpleTensor<T> _reference{};
154 PoolingLayerInfo _pool_info{};
Giorgio Arena63825e82021-03-25 14:54:50 +0000155 bool _mixed_layout{ false };
morgolockcc1f6c92020-03-24 09:26:48 +0000156 TensorType _target_indices{};
157 SimpleTensor<uint32_t> _ref_indices{};
158};
159template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
160class PoolingLayerIndicesValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
161{
162public:
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000163 void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout, bool use_kernel_indices)
morgolockcc1f6c92020-03-24 09:26:48 +0000164 {
Adnan AlSinanbbf2e742023-02-22 12:15:14 +0000165 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding, false,
166 true, use_kernel_indices),
morgolockcc1f6c92020-03-24 09:26:48 +0000167 data_type, data_layout, true);
168 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100169};
170
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000171template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000172class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100173{
174public:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000175 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 +0100176 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000177 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000178 data_type, data_layout, false, mixed_layout);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100179 }
180};
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100181
182template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100183class PoolingLayerValidationMixedPrecisionFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
184{
185public:
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100186 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)
187 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000188 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 +0100189 data_type, data_layout);
190 }
191};
192
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000193template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000194class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
195{
196public:
Michele Di Giorgiod556d7b2020-10-27 10:56:31 +0000197 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,
198 QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo())
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000199 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000200 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding),
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000201 data_type, data_layout, false, input_qinfo, output_qinfo, mixed_layout);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000202 }
203};
204
205template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas15997872018-02-19 13:58:22 +0000206class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
207{
208public:
Giorgio Arena3c520c52018-05-01 11:47:24 +0100209 void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type)
Georgios Pinitas15997872018-02-19 13:58:22 +0000210 {
SiCongLicb869562021-12-22 15:37:20 +0000211 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, pool_info.data_layout);
Georgios Pinitas15997872018-02-19 13:58:22 +0000212 }
213};
214
215template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000216class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100217{
218public:
Michalis Spyrou57dac842018-03-01 16:03:50 +0000219 void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW)
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100220 {
Sang-Hoon Park11fedda2020-01-15 14:44:04 +0000221 PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, data_layout), data_type, data_layout);
Gian Marco Iodicebf179552017-09-05 13:51:21 +0100222 }
223};
Georgios Pinitas15997872018-02-19 13:58:22 +0000224
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100225} // namespace validation
226} // namespace test
227} // namespace arm_compute
228#endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */