blob: c5633e8eba7846c44b922806baee2c16eb332cd4 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#include "Pooling2d.hpp"
7
8#include <armnn/Exceptions.hpp>
9#include <armnn/Types.hpp>
10
Matteo Martincighe011d202019-11-28 11:35:47 +000011#include <armnnUtils/DataLayoutIndexed.hpp>
Matthew Sloyan171214c2020-09-09 09:07:37 +010012#include <armnn/utility/NumericCast.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000013
telsoa014fcda012018-03-09 14:13:49 +000014#include <limits>
15#include <algorithm>
16#include <functional>
17
18namespace
19{
20 using PoolingAlgorithm = armnn::PoolingAlgorithm;
21
22 float DefaultInitializer(PoolingAlgorithm algorithm)
23 {
24 switch (algorithm)
25 {
26 case PoolingAlgorithm::Max:
27 {
28 return std::numeric_limits<float>::lowest();
29 }
30 case PoolingAlgorithm::Average:
31 case PoolingAlgorithm::L2:
32 {
33 return 0.0f;
34 }
35 default:
36 {
37 throw armnn::InvalidArgumentException("Unsupported pooling algorithm");
38 }
39 }
40 }
41
42 using Accumulator = std::function<void(float & accu, float value)>;
43
44 Accumulator GetAccumulator(PoolingAlgorithm algorithm)
45 {
46 switch (algorithm)
47 {
48 case PoolingAlgorithm::Max:
49 {
50 return [](float & accu, float value) {
51 if (value > accu) {
52 accu = value;
53 }
54 };
55 }
56
57 case PoolingAlgorithm::Average:
58 {
59 return [](float & accu, float value) {
60 accu += value;
61 };
62 }
63
64 case PoolingAlgorithm::L2:
65 {
66 return [](float & accu, float value) {
67 accu += (value*value);
68 };
69 }
70
71 default:
72 {
73 throw armnn::InvalidArgumentException("Unsupported pooling algorithm");
74 }
75 }
76 }
77
78 using Executor = std::function<void(float & accumulated, float kernelSize)>;
79
80 Executor GetExecutor(PoolingAlgorithm algorithm)
81 {
82 switch (algorithm)
83 {
84 case PoolingAlgorithm::Max:
85 {
Derek Lamberti901ea112019-12-10 22:07:09 +000086 return [](float & /*accumulated*/, float /*kernelSize*/) {};
telsoa014fcda012018-03-09 14:13:49 +000087 }
88
89 case PoolingAlgorithm::Average:
90 {
91 return [](float & accumulated, float kernelSize) {
92 accumulated /= kernelSize;
93 };
94 }
95
96 case PoolingAlgorithm::L2:
97 {
98 return [](float & accumulated, float kernelSize) {
99 accumulated = sqrtf(accumulated / kernelSize);
100 };
101 }
102
103 default:
104 {
105 throw armnn::InvalidArgumentException("Unsupported pooling algorithm");
106 }
107 }
108 }
109
Finn Williams70f609b2019-11-06 16:54:53 +0000110 bool OnPaddingOnly(int start, int end, int maxRange)
telsoa014fcda012018-03-09 14:13:49 +0000111 {
Finn Williams70f609b2019-11-06 16:54:53 +0000112 if (end <= 0 || start > maxRange)
telsoa014fcda012018-03-09 14:13:49 +0000113 {
114 return true;
115 }
116 else
117 {
118 return false;
119 }
120 }
121
122
123 bool ClampRange(int & start, int & end, int maxRange)
124 {
125 if (start < 0 || end > maxRange)
126 {
127 start = std::min(std::max(start, 0), maxRange);
128 end = std::min(std::max(end, 0), maxRange);
129 return true;
130 }
131 else
132 {
133 return false;
134 }
135 }
136}
137
Matteo Martincigh21350152018-11-28 16:22:22 +0000138using namespace armnnUtils;
139
telsoa014fcda012018-03-09 14:13:49 +0000140namespace armnn
141{
Teresa Charlina3b20472019-06-06 11:12:32 +0100142void Pooling2d(Decoder<float>& rInputDecoder,
143 Encoder<float>& rOutputEncoder,
telsoa014fcda012018-03-09 14:13:49 +0000144 const TensorInfo& inputInfo,
145 const TensorInfo& outputInfo,
146 const Pooling2dDescriptor& params)
147{
Teresa Charlina3b20472019-06-06 11:12:32 +0100148 const DataLayoutIndexed dataLayout(params.m_DataLayout);
James Conroy45a9b772018-10-31 11:47:53 +0000149 auto channelsIndex = dataLayout.GetChannelsIndex();
150 auto heightIndex = dataLayout.GetHeightIndex();
151 auto widthIndex = dataLayout.GetWidthIndex();
James Conroy69482272018-10-19 10:41:35 +0100152
Matthew Sloyan171214c2020-09-09 09:07:37 +0100153 const int batchSize = armnn::numeric_cast<int>(outputInfo.GetShape()[0]);
154 const int channels = armnn::numeric_cast<int>(outputInfo.GetShape()[channelsIndex]);
155 const int heightOutput = armnn::numeric_cast<int>(outputInfo.GetShape()[heightIndex]);
156 const int widthOutput = armnn::numeric_cast<int>(outputInfo.GetShape()[widthIndex]);
157 const int heightInput = armnn::numeric_cast<int>(inputInfo.GetShape()[heightIndex]);
158 const int widthInput = armnn::numeric_cast<int>(inputInfo.GetShape()[widthIndex]);
159 const int padLeft = armnn::numeric_cast<int>(params.m_PadLeft);
160 const int padRight = armnn::numeric_cast<int>(params.m_PadRight);
161 const int padTop = armnn::numeric_cast<int>(params.m_PadTop);
162 const int padBottom = armnn::numeric_cast<int>(params.m_PadBottom);
163 const int strideX = armnn::numeric_cast<int>(params.m_StrideX);
164 const int strideY = armnn::numeric_cast<int>(params.m_StrideY);
165 const int poolHeight = armnn::numeric_cast<int>(params.m_PoolHeight);
166 const int poolWidth = armnn::numeric_cast<int>(params.m_PoolWidth);
telsoa014fcda012018-03-09 14:13:49 +0000167
168 float defaultInitializer = DefaultInitializer(params.m_PoolType);
169
170 Accumulator accumulate = GetAccumulator(params.m_PoolType);
171 Executor execute = GetExecutor(params.m_PoolType);
172
173 // Check supported padding methods outside the loop to simplify
telsoa01c577f2c2018-08-31 09:22:23 +0100174 // the inner loop.
telsoa014fcda012018-03-09 14:13:49 +0000175 if (params.m_PaddingMethod != PaddingMethod::Exclude &&
176 params.m_PaddingMethod != PaddingMethod::IgnoreValue)
177 {
178 throw armnn::InvalidArgumentException("Unsupported padding type");
179 }
180
Finn Williamsea8ce702020-09-29 19:54:00 +0100181 const std::vector<float> decodedInputVec = rInputDecoder.DecodeTensor(inputInfo.GetShape());
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100182
telsoa014fcda012018-03-09 14:13:49 +0000183 for (int n = 0; n < batchSize; n++)
184 {
185 for (int c = 0; c < channels; c++)
186 {
187 for (int yOutput = 0; yOutput < heightOutput; yOutput++)
188 {
Finn Williams70f609b2019-11-06 16:54:53 +0000189 // Calculate values independent of the x axis
190 int hstart = (yOutput * strideY) - padTop;
191 int hend = hstart + poolHeight;
192 // Clamp the pooling region inside the valid input area (which includes the padding).
193 // This is necessary because the final pooling in a row may overlap beyond the padding.
194 hend = std::min(hend, heightInput + padBottom);
195
196 int height = hend - hstart;
197 bool hclamped = ClampRange(hstart, hend, heightInput);
198
telsoa014fcda012018-03-09 14:13:49 +0000199 for (int xOutput = 0; xOutput < widthOutput; xOutput++)
200 {
telsoa014fcda012018-03-09 14:13:49 +0000201 int wstart = (xOutput * strideX) - padLeft;
telsoa014fcda012018-03-09 14:13:49 +0000202 int wend = wstart + poolWidth;
203
204 // Clamp the pooling region inside the valid input area (which includes the padding).
205 // This is necessary because the final pooling in a row may overlap beyond the padding.
surmeh01bceff2f2018-03-29 16:29:27 +0100206 wend = std::min(wend, widthInput + padRight);
telsoa014fcda012018-03-09 14:13:49 +0000207
208 float result = defaultInitializer;
Matthew Sloyan24ac8592020-09-23 16:57:23 +0100209 float poolAreaSize = armnn::numeric_cast<float>(height * (wend - wstart));
telsoa014fcda012018-03-09 14:13:49 +0000210
telsoa01c577f2c2018-08-31 09:22:23 +0100211 // Special case: when the pooling kernel is over a padding region and the padding
telsoa014fcda012018-03-09 14:13:49 +0000212 // size is larger or equal to the kernel and the kernel only covers
213 // padding and no real values, then we initialize the result as zero
214 // by convention. This is because we need to choose a value here and
215 // all values we have are padding, which we ignore.
Finn Williams70f609b2019-11-06 16:54:53 +0000216 if (OnPaddingOnly(hstart, hend, heightInput) ||
217 OnPaddingOnly(wstart, wend, widthInput))
telsoa014fcda012018-03-09 14:13:49 +0000218 {
219 result = 0.0f;
Finn Williams70f609b2019-11-06 16:54:53 +0000220
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100221 int outputIndex;
222
223 if(dataLayout.GetDataLayout() == DataLayout::NHWC)
224 {
225 outputIndex = n * heightOutput * widthOutput * channels +
226 yOutput * widthOutput * channels +
227 xOutput * channels +
228 c;
229 }
230 else
231 {
232 outputIndex = n * heightOutput * widthOutput * channels +
233 c * heightOutput * widthOutput +
234 yOutput * widthOutput +
235 xOutput;
236 }
237
238 rOutputEncoder[static_cast<unsigned int>(outputIndex)];
Finn Williams70f609b2019-11-06 16:54:53 +0000239 rOutputEncoder.Set(result);
240 continue;
telsoa014fcda012018-03-09 14:13:49 +0000241 }
242
Finn Williams70f609b2019-11-06 16:54:53 +0000243 bool clamped = hclamped |= ClampRange(wstart, wend, widthInput);
telsoa014fcda012018-03-09 14:13:49 +0000244
245 if (clamped && params.m_PaddingMethod == PaddingMethod::Exclude)
246 {
telsoa01c577f2c2018-08-31 09:22:23 +0100247 // When we exclude the padding, it means we calculate with a smaller
248 // kernel size, so I changed the divisor here.
Matthew Sloyan24ac8592020-09-23 16:57:23 +0100249 poolAreaSize = armnn::numeric_cast<float>((hend - hstart) * (wend - wstart));
telsoa014fcda012018-03-09 14:13:49 +0000250 }
251
252 for (auto yInput = hstart; yInput < hend; yInput++)
253 {
254 for (auto xInput = wstart; xInput < wend; xInput++)
255 {
Teresa Charlina3b20472019-06-06 11:12:32 +0100256
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100257 int inputIndex;
258 if(dataLayout.GetDataLayout() == DataLayout::NHWC)
259 {
260 inputIndex = n * heightInput * widthInput * channels +
261 yInput * widthInput * channels +
262 xInput * channels +
263 c;
telsoa014fcda012018-03-09 14:13:49 +0000264
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100265 }
266 else
267 {
268 inputIndex = n * heightInput * widthInput * channels +
269 c * heightInput * widthInput +
270 yInput * widthInput +
271 xInput;
272 }
273
274 accumulate(result, decodedInputVec[static_cast<unsigned int>(inputIndex)]);
telsoa014fcda012018-03-09 14:13:49 +0000275 }
276 }
277
278 execute(result, poolAreaSize);
279
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100280 int outputIndex;
Teresa Charlina3b20472019-06-06 11:12:32 +0100281
Finn Williamsb9dcfe62020-09-17 15:58:31 +0100282 if(dataLayout.GetDataLayout() == DataLayout::NHWC)
283 {
284 outputIndex = n * heightOutput * widthOutput * channels +
285 yOutput * widthOutput * channels +
286 xOutput * channels +
287 c;
288 }
289 else
290 {
291 outputIndex = n * heightOutput * widthOutput * channels +
292 c * heightOutput * widthOutput +
293 yOutput * widthOutput +
294 xOutput;
295 }
296
297 rOutputEncoder[static_cast<unsigned int>(outputIndex)];
Teresa Charlina3b20472019-06-06 11:12:32 +0100298 rOutputEncoder.Set(result);
telsoa014fcda012018-03-09 14:13:49 +0000299 }
300 }
301 }
302 }
303}
304
305} //namespace armnn