blob: 010412c92b22b38a415a191e6570294108a7dcb7 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Pablo Telloa52e4cf2019-04-01 14:55:18 +01002 * Copyright (c) 2017-2019 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>
41SimpleTensor<T> pooling_layer_internal(const SimpleTensor<T> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010042{
Pablo Telloa52e4cf2019-04-01 14:55:18 +010043 ARM_COMPUTE_UNUSED(output_qinfo); // requantization occurs in pooling_layer<uint8_t>
Georgios Pinitas4c2dd542017-11-13 12:58:41 +000044 ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y()));
45
Giorgio Arena563494c2018-04-30 17:29:41 +010046 // Create reference
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010047 SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info), src.data_type(), 1 };
Giorgio Arena563494c2018-04-30 17:29:41 +010048
Isabella Gottardi6e464c32018-01-26 12:32:45 +000049 const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width;
50 const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000051 PoolingType type = info.pool_type();
52 int pool_stride_x = info.pad_stride_info().stride().first;
53 int pool_stride_y = info.pad_stride_info().stride().second;
Michalis Spyroubd0e6122018-01-23 09:52:16 +000054 int pad_left = info.pad_stride_info().pad_left();
55 int pad_top = info.pad_stride_info().pad_top();
56 int pad_right = info.pad_stride_info().pad_right();
57 int pad_bottom = info.pad_stride_info().pad_bottom();
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000058 bool exclude_padding = info.exclude_padding();
Georgios Pinitasdc460f12017-08-24 19:02:44 +010059
60 const auto w_src = static_cast<int>(src.shape()[0]);
61 const auto h_src = static_cast<int>(src.shape()[1]);
62 const int upper_dims = src.shape().total_size() / (w_src * h_src);
63
Georgios Pinitasdc460f12017-08-24 19:02:44 +010064 const auto w_dst = static_cast<int>(dst.shape()[0]);
65 const auto h_dst = static_cast<int>(dst.shape()[1]);
66
67 if(type == PoolingType::MAX)
68 {
69 for(int r = 0; r < upper_dims; ++r)
70 {
71 for(int h = 0; h < h_dst; ++h)
72 {
73 for(int w = 0; w < w_dst; ++w)
74 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +000075 int wstart = w * pool_stride_x - pad_left;
76 int hstart = h * pool_stride_y - pad_top;
Isabella Gottardi6e464c32018-01-26 12:32:45 +000077 int wend = std::min(wstart + pool_size_x, w_src);
78 int hend = std::min(hstart + pool_size_y, h_src);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010079 wstart = std::max(wstart, 0);
80 hstart = std::max(hstart, 0);
81
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +010082 auto max_val = std::numeric_limits<ACC_T>::lowest();
Georgios Pinitasdc460f12017-08-24 19:02:44 +010083 for(int y = hstart; y < hend; ++y)
84 {
85 for(int x = wstart; x < wend; ++x)
86 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +010087 const auto val = static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010088 if(val > max_val)
89 {
90 max_val = val;
91 }
92 }
93 }
94
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +010095 dst[r * h_dst * w_dst + h * w_dst + w] = static_cast<T>(max_val);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010096 }
97 }
98 }
99 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100100 else // Average or l2 pooling
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100101 {
102 for(int r = 0; r < upper_dims; ++r)
103 {
104 for(int h = 0; h < h_dst; ++h)
105 {
106 for(int w = 0; w < w_dst; ++w)
107 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100108 ACC_T avg_val(0);
109 int wstart = w * pool_stride_x - pad_left;
110 int hstart = h * pool_stride_y - pad_top;
111 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
112 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
113 int pool = (hend - hstart) * (wend - wstart);
114 wstart = std::max(wstart, 0);
115 hstart = std::max(hstart, 0);
116 wend = std::min(wend, w_src);
117 hend = std::min(hend, h_src);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000118 // Exclude padding pixels from the average
119 if(exclude_padding)
120 {
121 pool = (hend - hstart) * (wend - wstart);
122 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100123
Georgios Pinitascdf51452017-08-31 14:21:36 +0100124 if(type == PoolingType::AVG)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100125 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100126 for(int y = hstart; y < hend; ++y)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100127 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100128 for(int x = wstart; x < wend; ++x)
129 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100130 avg_val += static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100131 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100132 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100133 dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool;
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100134 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100135 else
136 {
137 for(int y = hstart; y < hend; ++y)
138 {
139 for(int x = wstart; x < wend; ++x)
140 {
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100141 const auto val = static_cast<ACC_T>(src[r * h_src * w_src + y * w_src + x]);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100142 avg_val += val * val;
143 }
144 }
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100145 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 +0100146 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100147 }
148 }
149 }
150 }
151
152 return dst;
153}
154
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100155template SimpleTensor<float> pooling_layer_internal<float>(const SimpleTensor<float> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo);
156template SimpleTensor<half> pooling_layer_internal<half>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo);
157template SimpleTensor<half> pooling_layer_internal<half, float>(const SimpleTensor<half> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo);
158
159template <typename T>
160SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo)
161{
162 return pooling_layer_internal<T, T>(src, info, output_qinfo);
163}
164
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000165template <>
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100166SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo)
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000167{
168 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100169 SimpleTensor<float> dst_tmp = pooling_layer_internal<float>(src_tmp, info, output_qinfo);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100170 SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000171 return dst;
172}
173
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +0100174template <>
175SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo)
176{
177 if(src.data_type() == DataType::F16 && info.fp_mixed_precision())
178 {
179 return pooling_layer_internal<half, float>(src, info, output_qinfo);
180 }
181
182 return pooling_layer_internal<half>(src, info, output_qinfo);
183}
184
Pablo Telloa52e4cf2019-04-01 14:55:18 +0100185template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info, const QuantizationInfo &output_qinfo);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100186} // namespace reference
187} // namespace validation
188} // namespace test
189} // namespace arm_compute