Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 1 | /* |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +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 | #include "PoolingLayer.h" |
| 25 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 28 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 38 | using namespace arm_compute::misc::shape_calculator; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 39 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 40 | template <typename T, typename ACC_T, typename std::enable_if<is_floating_point<T>::value, int>::type> |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 41 | SimpleTensor<T> pooling_layer_internal(const SimpleTensor<T> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 42 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 43 | // Create reference |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 44 | SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info), src.data_type(), 1 }; |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 45 | auto pooled_shape = compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info); |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 46 | if(indices) |
| 47 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 48 | *indices = SimpleTensor<uint32_t> { pooled_shape, DataType::U32, 1 }; |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 49 | } |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 50 | const int pool_size_x = info.is_global_pooling ? src.shape().x() : info.pool_size.width; |
| 51 | const int pool_size_y = info.is_global_pooling ? src.shape().y() : info.pool_size.height; |
| 52 | PoolingType type = info.pool_type; |
| 53 | int pool_stride_x = info.pad_stride_info.stride().first; |
| 54 | int pool_stride_y = info.pad_stride_info.stride().second; |
| 55 | int pad_left = info.pad_stride_info.pad_left(); |
| 56 | int pad_top = info.pad_stride_info.pad_top(); |
| 57 | int pad_right = info.pad_stride_info.pad_right(); |
| 58 | int pad_bottom = info.pad_stride_info.pad_bottom(); |
| 59 | bool exclude_padding = info.exclude_padding; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 60 | |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 61 | const auto w_src = static_cast<int>(src.shape()[0]); |
| 62 | const auto h_src = static_cast<int>(src.shape()[1]); |
| 63 | const auto z_src = static_cast<int>(src.shape()[2]); |
| 64 | const auto b_src = static_cast<int>(src.shape()[3]); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 65 | |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 66 | const int upper_dims = src.shape().total_size() / (w_src * h_src); |
| 67 | |
| 68 | const auto w_dst = static_cast<int>(dst.shape()[0]); |
| 69 | const auto h_dst = static_cast<int>(dst.shape()[1]); |
| 70 | const auto z_dst = static_cast<int>(dst.shape()[2]); |
| 71 | |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 72 | TensorShape shape_nhwc(src.shape()); |
| 73 | permute(shape_nhwc, PermutationVector(2U, 0U, 1U)); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 74 | if(type == PoolingType::MAX) |
| 75 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 76 | for(int b = 0; b < b_src; ++b) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 77 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 78 | for(int r = 0; r < z_src; ++r) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 79 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 80 | for(int h = 0; h < h_dst; ++h) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 81 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 82 | for(int w = 0; w < w_dst; ++w) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 83 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 84 | int wstart = w * pool_stride_x - pad_left; |
| 85 | int hstart = h * pool_stride_y - pad_top; |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame^] | 86 | |
| 87 | // Used to calculate kernel indices |
| 88 | int kh_start = std::max(0, -hstart); |
| 89 | int kw_start = std::max(0, -wstart); |
| 90 | int max_ker_index{ 0 }; |
| 91 | |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 92 | int wend = std::min(wstart + pool_size_x, w_src); |
| 93 | int hend = std::min(hstart + pool_size_y, h_src); |
| 94 | wstart = std::max(wstart, 0); |
| 95 | hstart = std::max(hstart, 0); |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 96 | auto max_val = info.use_inf_as_limit ? -std::numeric_limits<ACC_T>::infinity() : std::numeric_limits<ACC_T>::lowest(); |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 97 | int max_index{ 0 }; |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame^] | 98 | |
| 99 | for(int y = hstart, kh = kh_start; y < hend; ++y, ++kh) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 100 | { |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame^] | 101 | for(int x = wstart, kw = kw_start; x < wend; ++x, ++kw) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 102 | { |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 103 | const auto val = static_cast<ACC_T>(src[b * z_src * h_src * w_src + r * h_src * w_src + y * w_src + x]); |
| 104 | if(val > max_val) |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 105 | { |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame^] | 106 | max_val = val; |
| 107 | max_ker_index = pool_size_x * (kh) + (kw); |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 108 | if(data_layout == DataLayout::NCHW) |
| 109 | { |
| 110 | max_index = coord2index(src.shape(), Coordinates(x, y, r, 0)); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | max_index = coord2index(shape_nhwc, Coordinates(r, x, y, 0)); |
| 115 | } |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 116 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 119 | |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 120 | dst[b * z_dst * h_dst * w_dst + r * h_dst * w_dst + h * w_dst + w] = static_cast<T>(max_val); |
| 121 | if(indices) |
| 122 | { |
Adnan AlSinan | bbf2e74 | 2023-02-22 12:15:14 +0000 | [diff] [blame^] | 123 | (*indices)[b * z_dst * h_dst * w_dst + r * h_dst * w_dst + h * w_dst + w] = (info.use_kernel_indices) ? max_ker_index : max_index; |
morgolock | 37722d9 | 2020-04-09 14:17:48 +0100 | [diff] [blame] | 124 | } |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 125 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 130 | else // Average or l2 pooling |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 131 | { |
| 132 | for(int r = 0; r < upper_dims; ++r) |
| 133 | { |
| 134 | for(int h = 0; h < h_dst; ++h) |
| 135 | { |
| 136 | for(int w = 0; w < w_dst; ++w) |
| 137 | { |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 138 | ACC_T avg_val(0); |
| 139 | int wstart = w * pool_stride_x - pad_left; |
| 140 | int hstart = h * pool_stride_y - pad_top; |
| 141 | int wend = std::min(wstart + pool_size_x, w_src + pad_right); |
| 142 | int hend = std::min(hstart + pool_size_y, h_src + pad_bottom); |
| 143 | int pool = (hend - hstart) * (wend - wstart); |
| 144 | wstart = std::max(wstart, 0); |
| 145 | hstart = std::max(hstart, 0); |
| 146 | wend = std::min(wend, w_src); |
| 147 | hend = std::min(hend, h_src); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 148 | // Exclude padding pixels from the average |
| 149 | if(exclude_padding) |
| 150 | { |
| 151 | pool = (hend - hstart) * (wend - wstart); |
| 152 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 153 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 154 | if(type == PoolingType::AVG) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 155 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 156 | for(int y = hstart; y < hend; ++y) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 157 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 158 | for(int x = wstart; x < wend; ++x) |
| 159 | { |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 160 | avg_val += static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 161 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 162 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 163 | dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 164 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 165 | else |
| 166 | { |
| 167 | for(int y = hstart; y < hend; ++y) |
| 168 | { |
| 169 | for(int x = wstart; x < wend; ++x) |
| 170 | { |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 171 | const auto val = static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 172 | avg_val += val * val; |
| 173 | } |
| 174 | } |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 175 | dst[r * h_dst * w_dst + h * w_dst + w] = static_cast<T>(std::sqrt(avg_val / pool)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 176 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 181 | return dst; |
| 182 | } |
| 183 | |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 184 | template SimpleTensor<float> pooling_layer_internal<float>(const SimpleTensor<float> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout); |
| 185 | |
| 186 | template SimpleTensor<half> pooling_layer_internal<half>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout); |
| 187 | |
| 188 | template SimpleTensor<half> pooling_layer_internal<half, float>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 189 | |
| 190 | template <typename T> |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 191 | SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout) |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 192 | { |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 193 | ARM_COMPUTE_UNUSED(output_qinfo); |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 194 | return pooling_layer_internal<T, T>(src, info, indices, data_layout); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 197 | template <> |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 198 | SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, |
| 199 | DataLayout data_layout) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 200 | { |
| 201 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 202 | SimpleTensor<float> dst_tmp = pooling_layer_internal<float>(src_tmp, info, indices, data_layout); |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 203 | SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 204 | return dst; |
| 205 | } |
| 206 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 207 | template <> |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 208 | SimpleTensor<int8_t> pooling_layer<int8_t>(const SimpleTensor<int8_t> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 209 | { |
| 210 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 211 | SimpleTensor<float> dst_tmp = pooling_layer_internal<float>(src_tmp, info, indices, data_layout); |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 212 | SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, output_qinfo); |
| 213 | return dst; |
| 214 | } |
| 215 | |
| 216 | template <> |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 217 | SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout) |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 218 | { |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 219 | ARM_COMPUTE_UNUSED(output_qinfo); |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 220 | if(src.data_type() == DataType::F16 && info.fp_mixed_precision) |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 221 | { |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 222 | return pooling_layer_internal<half, float>(src, info, indices, data_layout); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 225 | return pooling_layer_internal<half>(src, info, indices, data_layout); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 226 | } |
| 227 | |
morgolock | e383c35 | 2020-04-03 16:57:46 +0100 | [diff] [blame] | 228 | template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout); |
| 229 | |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 230 | } // namespace reference |
| 231 | } // namespace validation |
| 232 | } // namespace test |
| 233 | } // namespace arm_compute |