blob: 5f4edfe49c7f56c282162b30f5b1e582c1e4e037 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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#include "PoolingLayer.h"
25
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Giorgio Arena3c520c52018-05-01 11:47:24 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000028#include "tests/validation/Helpers.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010029
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
Giorgio Arena3c520c52018-05-01 11:47:24 +010038using namespace arm_compute::misc::shape_calculator;
Georgios Pinitasdc460f12017-08-24 19:02:44 +010039
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +010040template <typename T, typename ACC_T, typename std::enable_if<is_floating_point<T>::value, int>::type>
morgolocke383c352020-04-03 16:57:46 +010041SimpleTensor<T> pooling_layer_internal(const SimpleTensor<T> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010042{
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +000043 ARM_COMPUTE_ERROR_ON(info.is_global_pooling && (src.shape().x() != src.shape().y()));
Giorgio Arena563494c2018-04-30 17:29:41 +010044 // Create reference
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010045 SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info), src.data_type(), 1 };
morgolock37722d92020-04-09 14:17:48 +010046 auto pooled_shape = compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info);
morgolockcc1f6c92020-03-24 09:26:48 +000047 if(indices)
48 {
morgolock37722d92020-04-09 14:17:48 +010049 *indices = SimpleTensor<uint32_t> { pooled_shape, DataType::U32, 1 };
morgolockcc1f6c92020-03-24 09:26:48 +000050 }
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +000051 const int pool_size_x = info.is_global_pooling ? src.shape().x() : info.pool_size.width;
52 const int pool_size_y = info.is_global_pooling ? src.shape().y() : info.pool_size.height;
53 PoolingType type = info.pool_type;
54 int pool_stride_x = info.pad_stride_info.stride().first;
55 int pool_stride_y = info.pad_stride_info.stride().second;
56 int pad_left = info.pad_stride_info.pad_left();
57 int pad_top = info.pad_stride_info.pad_top();
58 int pad_right = info.pad_stride_info.pad_right();
59 int pad_bottom = info.pad_stride_info.pad_bottom();
60 bool exclude_padding = info.exclude_padding;
Georgios Pinitasdc460f12017-08-24 19:02:44 +010061
morgolock37722d92020-04-09 14:17:48 +010062 const auto w_src = static_cast<int>(src.shape()[0]);
63 const auto h_src = static_cast<int>(src.shape()[1]);
64 const auto z_src = static_cast<int>(src.shape()[2]);
65 const auto b_src = static_cast<int>(src.shape()[3]);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010066
morgolock37722d92020-04-09 14:17:48 +010067 const int upper_dims = src.shape().total_size() / (w_src * h_src);
68
69 const auto w_dst = static_cast<int>(dst.shape()[0]);
70 const auto h_dst = static_cast<int>(dst.shape()[1]);
71 const auto z_dst = static_cast<int>(dst.shape()[2]);
72
morgolocke383c352020-04-03 16:57:46 +010073 TensorShape shape_nhwc(src.shape());
74 permute(shape_nhwc, PermutationVector(2U, 0U, 1U));
Georgios Pinitasdc460f12017-08-24 19:02:44 +010075 if(type == PoolingType::MAX)
76 {
morgolock37722d92020-04-09 14:17:48 +010077 for(int b = 0; b < b_src; ++b)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010078 {
morgolock37722d92020-04-09 14:17:48 +010079 for(int r = 0; r < z_src; ++r)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010080 {
morgolock37722d92020-04-09 14:17:48 +010081 for(int h = 0; h < h_dst; ++h)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010082 {
morgolock37722d92020-04-09 14:17:48 +010083 for(int w = 0; w < w_dst; ++w)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010084 {
morgolock37722d92020-04-09 14:17:48 +010085 int wstart = w * pool_stride_x - pad_left;
86 int hstart = h * pool_stride_y - pad_top;
87 int wend = std::min(wstart + pool_size_x, w_src);
88 int hend = std::min(hstart + pool_size_y, h_src);
89 wstart = std::max(wstart, 0);
90 hstart = std::max(hstart, 0);
91 auto max_val = std::numeric_limits<ACC_T>::lowest();
92 int max_index{ 0 };
93 for(int y = hstart; y < hend; ++y)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010094 {
morgolock37722d92020-04-09 14:17:48 +010095 for(int x = wstart; x < wend; ++x)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010096 {
morgolock37722d92020-04-09 14:17:48 +010097 const auto val = static_cast<ACC_T>(src[b * z_src * h_src * w_src + r * h_src * w_src + y * w_src + x]);
98 if(val > max_val)
morgolocke383c352020-04-03 16:57:46 +010099 {
morgolock37722d92020-04-09 14:17:48 +0100100 max_val = val;
101 if(data_layout == DataLayout::NCHW)
102 {
103 max_index = coord2index(src.shape(), Coordinates(x, y, r, 0));
104 }
105 else
106 {
107 max_index = coord2index(shape_nhwc, Coordinates(r, x, y, 0));
108 }
morgolocke383c352020-04-03 16:57:46 +0100109 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100110 }
111 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100112
morgolock37722d92020-04-09 14:17:48 +0100113 dst[b * z_dst * h_dst * w_dst + r * h_dst * w_dst + h * w_dst + w] = static_cast<T>(max_val);
114 if(indices)
115 {
116 (*indices)[b * z_dst * h_dst * w_dst + r * h_dst * w_dst + h * w_dst + w] = max_index;
117 }
morgolockcc1f6c92020-03-24 09:26:48 +0000118 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100119 }
120 }
121 }
122 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100123 else // Average or l2 pooling
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100124 {
125 for(int r = 0; r < upper_dims; ++r)
126 {
127 for(int h = 0; h < h_dst; ++h)
128 {
129 for(int w = 0; w < w_dst; ++w)
130 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100131 ACC_T avg_val(0);
132 int wstart = w * pool_stride_x - pad_left;
133 int hstart = h * pool_stride_y - pad_top;
134 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
135 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
136 int pool = (hend - hstart) * (wend - wstart);
137 wstart = std::max(wstart, 0);
138 hstart = std::max(hstart, 0);
139 wend = std::min(wend, w_src);
140 hend = std::min(hend, h_src);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000141 // Exclude padding pixels from the average
142 if(exclude_padding)
143 {
144 pool = (hend - hstart) * (wend - wstart);
145 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100146
Georgios Pinitascdf51452017-08-31 14:21:36 +0100147 if(type == PoolingType::AVG)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100148 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100149 for(int y = hstart; y < hend; ++y)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100150 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100151 for(int x = wstart; x < wend; ++x)
152 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100153 avg_val += static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100154 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100155 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100156 dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool;
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100157 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100158 else
159 {
160 for(int y = hstart; y < hend; ++y)
161 {
162 for(int x = wstart; x < wend; ++x)
163 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100164 const auto val = static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100165 avg_val += val * val;
166 }
167 }
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100168 dst[r * h_dst * w_dst + h * w_dst + w] = static_cast<T>(std::sqrt(avg_val / pool));
Georgios Pinitascdf51452017-08-31 14:21:36 +0100169 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100170 }
171 }
172 }
173 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100174 return dst;
175}
176
morgolocke383c352020-04-03 16:57:46 +0100177template SimpleTensor<float> pooling_layer_internal<float>(const SimpleTensor<float> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout);
178
179template SimpleTensor<half> pooling_layer_internal<half>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout);
180
181template SimpleTensor<half> pooling_layer_internal<half, float>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, SimpleTensor<uint32_t> *indices, DataLayout data_layout);
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100182
183template <typename T>
morgolocke383c352020-04-03 16:57:46 +0100184SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout)
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100185{
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000186 ARM_COMPUTE_UNUSED(output_qinfo);
morgolocke383c352020-04-03 16:57:46 +0100187 return pooling_layer_internal<T, T>(src, info, indices, data_layout);
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100188}
189
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000190template <>
morgolocke383c352020-04-03 16:57:46 +0100191SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices,
192 DataLayout data_layout)
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000193{
194 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
morgolocke383c352020-04-03 16:57:46 +0100195 SimpleTensor<float> dst_tmp = pooling_layer_internal<float>(src_tmp, info, indices, data_layout);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100196 SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000197 return dst;
198}
199
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100200template <>
morgolocke383c352020-04-03 16:57:46 +0100201SimpleTensor<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 Giorgiocbbed282019-12-20 13:26:08 +0000202{
203 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
morgolocke383c352020-04-03 16:57:46 +0100204 SimpleTensor<float> dst_tmp = pooling_layer_internal<float>(src_tmp, info, indices, data_layout);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000205 SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, output_qinfo);
206 return dst;
207}
208
209template <>
morgolocke383c352020-04-03 16:57:46 +0100210SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout)
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100211{
Manuel Bottinib4bb8272019-12-18 18:01:27 +0000212 ARM_COMPUTE_UNUSED(output_qinfo);
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +0000213 if(src.data_type() == DataType::F16 && info.fp_mixed_precision)
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100214 {
morgolocke383c352020-04-03 16:57:46 +0100215 return pooling_layer_internal<half, float>(src, info, indices, data_layout);
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100216 }
217
Sheri Zhang801bbcb2020-08-03 20:11:56 +0100218 return pooling_layer_internal<half>(src, info, indices, data_layout);
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100219}
220
morgolocke383c352020-04-03 16:57:46 +0100221template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo, SimpleTensor<uint32_t> *indices, DataLayout data_layout);
222
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100223} // namespace reference
224} // namespace validation
225} // namespace test
226} // namespace arm_compute