Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "Pooling2d.hpp" |
| 7 | |
| 8 | #include <armnn/Exceptions.hpp> |
| 9 | #include <armnn/Types.hpp> |
| 10 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 11 | #include <armnnUtils/DataLayoutIndexed.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 12 | #include <armnn/utility/NumericCast.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 13 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | #include <limits> |
| 15 | #include <algorithm> |
| 16 | #include <functional> |
| 17 | |
| 18 | namespace |
| 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 Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 86 | return [](float & /*accumulated*/, float /*kernelSize*/) {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 110 | bool OnPaddingOnly(int start, int end, int maxRange) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | { |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 112 | if (end <= 0 || start > maxRange) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 138 | using namespace armnnUtils; |
| 139 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 140 | namespace armnn |
| 141 | { |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 142 | void Pooling2d(Decoder<float>& rInputDecoder, |
| 143 | Encoder<float>& rOutputEncoder, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 144 | const TensorInfo& inputInfo, |
| 145 | const TensorInfo& outputInfo, |
| 146 | const Pooling2dDescriptor& params) |
| 147 | { |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 148 | const DataLayoutIndexed dataLayout(params.m_DataLayout); |
James Conroy | 45a9b77 | 2018-10-31 11:47:53 +0000 | [diff] [blame] | 149 | auto channelsIndex = dataLayout.GetChannelsIndex(); |
| 150 | auto heightIndex = dataLayout.GetHeightIndex(); |
| 151 | auto widthIndex = dataLayout.GetWidthIndex(); |
James Conroy | 6948227 | 2018-10-19 10:41:35 +0100 | [diff] [blame] | 152 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 153 | 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); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 167 | |
| 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 |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 174 | // the inner loop. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 175 | if (params.m_PaddingMethod != PaddingMethod::Exclude && |
| 176 | params.m_PaddingMethod != PaddingMethod::IgnoreValue) |
| 177 | { |
| 178 | throw armnn::InvalidArgumentException("Unsupported padding type"); |
| 179 | } |
| 180 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 181 | const std::vector<float> decodedInputVec = rInputDecoder.DecodeTensor(inputInfo.GetShape()); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 182 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | { |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 189 | // 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | for (int xOutput = 0; xOutput < widthOutput; xOutput++) |
| 200 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 201 | int wstart = (xOutput * strideX) - padLeft; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 202 | 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. |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 206 | wend = std::min(wend, widthInput + padRight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 207 | |
| 208 | float result = defaultInitializer; |
Matthew Sloyan | 24ac859 | 2020-09-23 16:57:23 +0100 | [diff] [blame] | 209 | float poolAreaSize = armnn::numeric_cast<float>(height * (wend - wstart)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 210 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 211 | // Special case: when the pooling kernel is over a padding region and the padding |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 212 | // 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 Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 216 | if (OnPaddingOnly(hstart, hend, heightInput) || |
| 217 | OnPaddingOnly(wstart, wend, widthInput)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 218 | { |
| 219 | result = 0.0f; |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 220 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 221 | 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 Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 239 | rOutputEncoder.Set(result); |
| 240 | continue; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 243 | bool clamped = hclamped |= ClampRange(wstart, wend, widthInput); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 244 | |
| 245 | if (clamped && params.m_PaddingMethod == PaddingMethod::Exclude) |
| 246 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 247 | // When we exclude the padding, it means we calculate with a smaller |
| 248 | // kernel size, so I changed the divisor here. |
Matthew Sloyan | 24ac859 | 2020-09-23 16:57:23 +0100 | [diff] [blame] | 249 | poolAreaSize = armnn::numeric_cast<float>((hend - hstart) * (wend - wstart)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | for (auto yInput = hstart; yInput < hend; yInput++) |
| 253 | { |
| 254 | for (auto xInput = wstart; xInput < wend; xInput++) |
| 255 | { |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 256 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 257 | int inputIndex; |
| 258 | if(dataLayout.GetDataLayout() == DataLayout::NHWC) |
| 259 | { |
| 260 | inputIndex = n * heightInput * widthInput * channels + |
| 261 | yInput * widthInput * channels + |
| 262 | xInput * channels + |
| 263 | c; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 264 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 265 | } |
| 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)]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | execute(result, poolAreaSize); |
| 279 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 280 | int outputIndex; |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 281 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 282 | 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 Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 298 | rOutputEncoder.Set(result); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | } //namespace armnn |