blob: d2fd0da42ca2ef4806f4fc441e8d1d2e46f3d1df [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// 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"
James Conroy45a9b772018-10-31 11:47:53 +00007#include "TensorBufferArrayView.hpp"
telsoa014fcda012018-03-09 14:13:49 +00008
9#include <armnn/Exceptions.hpp>
10#include <armnn/Types.hpp>
11
12#include <boost/numeric/conversion/cast.hpp>
13
14#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 {
86 return [](float & accumulated, float kernelSize) {};
87 }
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
110 bool OnPaddingOnly(int start, int end, int maxRange, int padding)
111 {
112 if (end <= 0 || start > (maxRange - padding))
113 {
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
138namespace armnn
139{
140
141void Pooling2d(const float* in,
142 float* out,
143 const TensorInfo& inputInfo,
144 const TensorInfo& outputInfo,
145 const Pooling2dDescriptor& params)
146{
James Conroy45a9b772018-10-31 11:47:53 +0000147 const armnn::DataLayoutIndexed dataLayout = params.m_DataLayout;
148 auto channelsIndex = dataLayout.GetChannelsIndex();
149 auto heightIndex = dataLayout.GetHeightIndex();
150 auto widthIndex = dataLayout.GetWidthIndex();
James Conroy69482272018-10-19 10:41:35 +0100151
telsoa014fcda012018-03-09 14:13:49 +0000152 const int batchSize = boost::numeric_cast<int>(outputInfo.GetShape()[0]);
James Conroy69482272018-10-19 10:41:35 +0100153 const int channels = boost::numeric_cast<int>(outputInfo.GetShape()[channelsIndex]);
154 const int heightOutput = boost::numeric_cast<int>(outputInfo.GetShape()[heightIndex]);
155 const int widthOutput = boost::numeric_cast<int>(outputInfo.GetShape()[widthIndex]);
156 const int heightInput = boost::numeric_cast<int>(inputInfo.GetShape()[heightIndex]);
157 const int widthInput = boost::numeric_cast<int>(inputInfo.GetShape()[widthIndex]);
telsoa014fcda012018-03-09 14:13:49 +0000158 const int padLeft = boost::numeric_cast<int>(params.m_PadLeft);
159 const int padRight = boost::numeric_cast<int>(params.m_PadRight);
160 const int padTop = boost::numeric_cast<int>(params.m_PadTop);
161 const int padBottom = boost::numeric_cast<int>(params.m_PadBottom);
162 const int strideX = boost::numeric_cast<int>(params.m_StrideX);
163 const int strideY = boost::numeric_cast<int>(params.m_StrideY);
164 const int poolHeight = boost::numeric_cast<int>(params.m_PoolHeight);
165 const int poolWidth = boost::numeric_cast<int>(params.m_PoolWidth);
166
167 float defaultInitializer = DefaultInitializer(params.m_PoolType);
168
169 Accumulator accumulate = GetAccumulator(params.m_PoolType);
170 Executor execute = GetExecutor(params.m_PoolType);
171
James Conroy45a9b772018-10-31 11:47:53 +0000172 TensorBufferArrayView<const float> input(inputInfo.GetShape(), in, dataLayout);
173 TensorBufferArrayView<float> output(outputInfo.GetShape(), out, dataLayout);
174
telsoa014fcda012018-03-09 14:13:49 +0000175 // Check supported padding methods outside the loop to simplify
telsoa01c577f2c2018-08-31 09:22:23 +0100176 // the inner loop.
telsoa014fcda012018-03-09 14:13:49 +0000177 if (params.m_PaddingMethod != PaddingMethod::Exclude &&
178 params.m_PaddingMethod != PaddingMethod::IgnoreValue)
179 {
180 throw armnn::InvalidArgumentException("Unsupported padding type");
181 }
182
183 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 {
189 for (int xOutput = 0; xOutput < widthOutput; xOutput++)
190 {
191 int hstart = (yOutput * strideY) - padTop;
192 int wstart = (xOutput * strideX) - padLeft;
193 int hend = hstart + poolHeight;
194 int wend = wstart + poolWidth;
195
196 // Clamp the pooling region inside the valid input area (which includes the padding).
197 // This is necessary because the final pooling in a row may overlap beyond the padding.
surmeh01bceff2f2018-03-29 16:29:27 +0100198 hend = std::min(hend, heightInput + padBottom);
199 wend = std::min(wend, widthInput + padRight);
telsoa014fcda012018-03-09 14:13:49 +0000200
201 float result = defaultInitializer;
202 float poolAreaSize = boost::numeric_cast<float>((hend - hstart) * (wend - wstart));
203
telsoa01c577f2c2018-08-31 09:22:23 +0100204 // Special case: when the pooling kernel is over a padding region and the padding
telsoa014fcda012018-03-09 14:13:49 +0000205 // size is larger or equal to the kernel and the kernel only covers
206 // padding and no real values, then we initialize the result as zero
207 // by convention. This is because we need to choose a value here and
208 // all values we have are padding, which we ignore.
209 if (OnPaddingOnly(hstart, hend, heightInput, padBottom) ||
210 OnPaddingOnly(wstart, wend, widthInput, padRight))
211 {
212 result = 0.0f;
213 }
214
215 bool clamped = ClampRange(wstart, wend, widthInput);
216 clamped |= ClampRange(hstart, hend, heightInput);
217
218 if (clamped && params.m_PaddingMethod == PaddingMethod::Exclude)
219 {
telsoa01c577f2c2018-08-31 09:22:23 +0100220 // When we exclude the padding, it means we calculate with a smaller
221 // kernel size, so I changed the divisor here.
telsoa014fcda012018-03-09 14:13:49 +0000222 poolAreaSize = boost::numeric_cast<float>((hend - hstart) * (wend - wstart));
223 }
224
225 for (auto yInput = hstart; yInput < hend; yInput++)
226 {
227 for (auto xInput = wstart; xInput < wend; xInput++)
228 {
James Conroy45a9b772018-10-31 11:47:53 +0000229 float inval = input.Get(boost::numeric_cast<unsigned int>(n),
230 boost::numeric_cast<unsigned int>(c),
231 boost::numeric_cast<unsigned int>(yInput),
232 boost::numeric_cast<unsigned int>(xInput));
telsoa014fcda012018-03-09 14:13:49 +0000233
234 accumulate(result, inval);
235 }
236 }
237
238 execute(result, poolAreaSize);
239
James Conroy45a9b772018-10-31 11:47:53 +0000240 output.Get(boost::numeric_cast<unsigned int>(n),
241 boost::numeric_cast<unsigned int>(c),
242 boost::numeric_cast<unsigned int>(yOutput),
243 boost::numeric_cast<unsigned int>(xOutput)) = result;
telsoa014fcda012018-03-09 14:13:49 +0000244 }
245 }
246 }
247 }
248}
249
250} //namespace armnn