telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 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" |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 7 | #include "DataLayoutIndexed.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | |
| 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 | |
| 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 | { |
| 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 | |
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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | const int batchSize = boost::numeric_cast<int>(outputInfo.GetShape()[0]); |
James Conroy | 6948227 | 2018-10-19 10:41:35 +0100 | [diff] [blame] | 154 | const int channels = boost::numeric_cast<int>(outputInfo.GetShape()[channelsIndex]); |
| 155 | const int heightOutput = boost::numeric_cast<int>(outputInfo.GetShape()[heightIndex]); |
| 156 | const int widthOutput = boost::numeric_cast<int>(outputInfo.GetShape()[widthIndex]); |
| 157 | const int heightInput = boost::numeric_cast<int>(inputInfo.GetShape()[heightIndex]); |
| 158 | const int widthInput = boost::numeric_cast<int>(inputInfo.GetShape()[widthIndex]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 159 | const int padLeft = boost::numeric_cast<int>(params.m_PadLeft); |
| 160 | const int padRight = boost::numeric_cast<int>(params.m_PadRight); |
| 161 | const int padTop = boost::numeric_cast<int>(params.m_PadTop); |
| 162 | const int padBottom = boost::numeric_cast<int>(params.m_PadBottom); |
| 163 | const int strideX = boost::numeric_cast<int>(params.m_StrideX); |
| 164 | const int strideY = boost::numeric_cast<int>(params.m_StrideY); |
| 165 | const int poolHeight = boost::numeric_cast<int>(params.m_PoolHeight); |
| 166 | const int poolWidth = boost::numeric_cast<int>(params.m_PoolWidth); |
| 167 | |
| 168 | float defaultInitializer = DefaultInitializer(params.m_PoolType); |
| 169 | |
| 170 | Accumulator accumulate = GetAccumulator(params.m_PoolType); |
| 171 | Executor execute = GetExecutor(params.m_PoolType); |
| 172 | |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 173 | TensorShape outputShape = outputInfo.GetShape(); |
| 174 | TensorShape inputShape = inputInfo.GetShape(); |
James Conroy | 45a9b77 | 2018-10-31 11:47:53 +0000 | [diff] [blame] | 175 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 176 | // Check supported padding methods outside the loop to simplify |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 177 | // the inner loop. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 178 | if (params.m_PaddingMethod != PaddingMethod::Exclude && |
| 179 | params.m_PaddingMethod != PaddingMethod::IgnoreValue) |
| 180 | { |
| 181 | throw armnn::InvalidArgumentException("Unsupported padding type"); |
| 182 | } |
| 183 | |
| 184 | for (int n = 0; n < batchSize; n++) |
| 185 | { |
| 186 | for (int c = 0; c < channels; c++) |
| 187 | { |
| 188 | for (int yOutput = 0; yOutput < heightOutput; yOutput++) |
| 189 | { |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 190 | // Calculate values independent of the x axis |
| 191 | int hstart = (yOutput * strideY) - padTop; |
| 192 | int hend = hstart + poolHeight; |
| 193 | // Clamp the pooling region inside the valid input area (which includes the padding). |
| 194 | // This is necessary because the final pooling in a row may overlap beyond the padding. |
| 195 | hend = std::min(hend, heightInput + padBottom); |
| 196 | |
| 197 | int height = hend - hstart; |
| 198 | bool hclamped = ClampRange(hstart, hend, heightInput); |
| 199 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 200 | for (int xOutput = 0; xOutput < widthOutput; xOutput++) |
| 201 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 202 | int wstart = (xOutput * strideX) - padLeft; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 203 | int wend = wstart + poolWidth; |
| 204 | |
| 205 | // Clamp the pooling region inside the valid input area (which includes the padding). |
| 206 | // 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] | 207 | wend = std::min(wend, widthInput + padRight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 208 | |
| 209 | float result = defaultInitializer; |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 210 | float poolAreaSize = boost::numeric_cast<float>(height * (wend - wstart)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 211 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 212 | // 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] | 213 | // size is larger or equal to the kernel and the kernel only covers |
| 214 | // padding and no real values, then we initialize the result as zero |
| 215 | // by convention. This is because we need to choose a value here and |
| 216 | // all values we have are padding, which we ignore. |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 217 | if (OnPaddingOnly(hstart, hend, heightInput) || |
| 218 | OnPaddingOnly(wstart, wend, widthInput)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 219 | { |
| 220 | result = 0.0f; |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 221 | |
| 222 | unsigned int outputIndex = dataLayout.GetIndex(outputShape, |
| 223 | boost::numeric_cast<unsigned int>(n), |
| 224 | boost::numeric_cast<unsigned int>(c), |
| 225 | boost::numeric_cast<unsigned int>(yOutput), |
| 226 | boost::numeric_cast<unsigned int>(xOutput)); |
| 227 | rOutputEncoder[outputIndex]; |
| 228 | rOutputEncoder.Set(result); |
| 229 | continue; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Finn Williams | 70f609b | 2019-11-06 16:54:53 +0000 | [diff] [blame] | 232 | bool clamped = hclamped |= ClampRange(wstart, wend, widthInput); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 233 | |
| 234 | if (clamped && params.m_PaddingMethod == PaddingMethod::Exclude) |
| 235 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 236 | // When we exclude the padding, it means we calculate with a smaller |
| 237 | // kernel size, so I changed the divisor here. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 238 | poolAreaSize = boost::numeric_cast<float>((hend - hstart) * (wend - wstart)); |
| 239 | } |
| 240 | |
| 241 | for (auto yInput = hstart; yInput < hend; yInput++) |
| 242 | { |
| 243 | for (auto xInput = wstart; xInput < wend; xInput++) |
| 244 | { |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 245 | unsigned int inputIndex = dataLayout.GetIndex(inputShape, |
| 246 | boost::numeric_cast<unsigned int>(n), |
| 247 | boost::numeric_cast<unsigned int>(c), |
| 248 | boost::numeric_cast<unsigned int>(yInput), |
| 249 | boost::numeric_cast<unsigned int>(xInput)); |
| 250 | |
| 251 | rInputDecoder[inputIndex]; |
| 252 | float inval = rInputDecoder.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 253 | |
| 254 | accumulate(result, inval); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | execute(result, poolAreaSize); |
| 259 | |
Teresa Charlin | a3b2047 | 2019-06-06 11:12:32 +0100 | [diff] [blame] | 260 | unsigned int outputIndex = dataLayout.GetIndex(outputShape, |
| 261 | boost::numeric_cast<unsigned int>(n), |
| 262 | boost::numeric_cast<unsigned int>(c), |
| 263 | boost::numeric_cast<unsigned int>(yOutput), |
| 264 | boost::numeric_cast<unsigned int>(xOutput)); |
| 265 | |
| 266 | rOutputEncoder[outputIndex]; |
| 267 | rOutputEncoder.Set(result); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | } //namespace armnn |