Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 1 | /* |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 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 | #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 Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/Tensor.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 31 | #include "tests/AssetsLibrary.h" |
| 32 | #include "tests/Globals.h" |
| 33 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/framework/Asserts.h" |
| 35 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/PoolingLayer.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 37 | #include <random> |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 45 | class PoolingLayerValidationGenericFixture : public framework::Fixture |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 46 | { |
| 47 | public: |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 48 | void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, DataLayout data_layout, bool indices = false, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 49 | QuantizationInfo input_qinfo = QuantizationInfo(), QuantizationInfo output_qinfo = QuantizationInfo(), bool mixed_layout = false) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 50 | { |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 51 | _mixed_layout = mixed_layout; |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 52 | _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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | protected: |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 58 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 73 | template <typename U> |
| 74 | void fill(U &&tensor) |
| 75 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 76 | if(tensor.data_type() == DataType::F32) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 77 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 78 | 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 Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 83 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 84 | library->fill(tensor, distribution, 0); |
| 85 | } |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 86 | else // data type is quantized_asymmetric |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 87 | { |
| 88 | library->fill_tensor_uniform(tensor, 0); |
| 89 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 92 | TensorType compute_target(TensorShape shape, PoolingLayerInfo info, |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 93 | DataType data_type, DataLayout data_layout, |
| 94 | QuantizationInfo input_qinfo, QuantizationInfo output_qinfo, |
| 95 | bool indices) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 96 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 97 | // Change shape in case of NHWC. |
| 98 | if(data_layout == DataLayout::NHWC) |
| 99 | { |
| 100 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 101 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 102 | // Create tensors |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 103 | 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); |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 106 | _target_indices = create_tensor<TensorType>(dst_shape, DataType::U32, 1, output_qinfo, data_layout); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 107 | |
| 108 | // Create and configure function |
| 109 | FunctionType pool_layer; |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 110 | pool_layer.configure(&src, &dst, info, (indices) ? &_target_indices : nullptr); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 111 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 112 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 115 | |
Sang-Hoon Park | fe56edb | 2021-04-13 20:21:11 +0100 | [diff] [blame] | 116 | add_padding_x({ &src, &dst, &_target_indices }, data_layout); |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 117 | |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 118 | // Allocate tensors |
| 119 | src.allocator()->allocate(); |
| 120 | dst.allocator()->allocate(); |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 121 | _target_indices.allocator()->allocate(); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 122 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 123 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 126 | |
| 127 | // Fill tensors |
| 128 | fill(AccessorType(src)); |
| 129 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 130 | if(_mixed_layout) |
| 131 | { |
| 132 | mix_layout(pool_layer, src, dst); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // Compute function |
| 137 | pool_layer.run(); |
| 138 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 139 | return dst; |
| 140 | } |
| 141 | |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 142 | SimpleTensor<T> compute_reference(TensorShape shape, PoolingLayerInfo info, DataType data_type, DataLayout data_layout, |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 143 | QuantizationInfo input_qinfo, QuantizationInfo output_qinfo, bool indices) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 144 | { |
| 145 | // Create reference |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 146 | SimpleTensor<T> src(shape, data_type, 1, input_qinfo); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 147 | // Fill reference |
| 148 | fill(src); |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 149 | return reference::pooling_layer<T>(src, info, output_qinfo, indices ? &_ref_indices : nullptr, data_layout); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 150 | } |
| 151 | |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 152 | TensorType _target{}; |
| 153 | SimpleTensor<T> _reference{}; |
| 154 | PoolingLayerInfo _pool_info{}; |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 155 | bool _mixed_layout{ false }; |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 156 | TensorType _target_indices{}; |
| 157 | SimpleTensor<uint32_t> _ref_indices{}; |
| 158 | }; |
| 159 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 160 | class PoolingLayerIndicesValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 161 | { |
| 162 | public: |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 163 | 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) |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 164 | { |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame] | 165 | 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), |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 167 | data_type, data_layout, true); |
| 168 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 169 | }; |
| 170 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 171 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 172 | class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 173 | { |
| 174 | public: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 175 | void setup(TensorShape shape, PoolingType pool_type, Size2D pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, DataLayout data_layout) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 176 | { |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 177 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding), |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 178 | data_type, data_layout, false, mixed_layout); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 179 | } |
| 180 | }; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 181 | |
| 182 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 183 | class PoolingLayerValidationMixedPrecisionFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 184 | { |
| 185 | public: |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 186 | 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 Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 188 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding, fp_mixed_precision), |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 189 | data_type, data_layout); |
| 190 | } |
| 191 | }; |
| 192 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 193 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 194 | class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 195 | { |
| 196 | public: |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 197 | 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 Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 199 | { |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 200 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, data_layout, pad_stride_info, exclude_padding), |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 201 | data_type, data_layout, false, input_qinfo, output_qinfo, mixed_layout); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 202 | } |
| 203 | }; |
| 204 | |
| 205 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 206 | class SpecialPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 207 | { |
| 208 | public: |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 209 | void setup(TensorShape src_shape, PoolingLayerInfo pool_info, DataType data_type) |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 210 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 211 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, pool_info, data_type, pool_info.data_layout); |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 212 | } |
| 213 | }; |
| 214 | |
| 215 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 216 | class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 217 | { |
| 218 | public: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 219 | void setup(TensorShape shape, PoolingType pool_type, DataType data_type, DataLayout data_layout = DataLayout::NCHW) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 220 | { |
Sang-Hoon Park | 11fedda | 2020-01-15 14:44:04 +0000 | [diff] [blame] | 221 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, data_layout), data_type, data_layout); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 222 | } |
| 223 | }; |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 224 | |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 225 | } // namespace validation |
| 226 | } // namespace test |
| 227 | } // namespace arm_compute |
| 228 | #endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */ |