Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Pad.hpp" |
| 7 | #include "backends/WorkloadData.hpp" |
| 8 | |
| 9 | #include <boost/numeric/conversion/cast.hpp> |
| 10 | #include "TensorBufferArrayView.hpp" |
| 11 | |
| 12 | #include <cmath> |
| 13 | #include <cstddef> |
| 14 | #include <functional> |
| 15 | #include <limits> |
| 16 | #include <cassert> |
| 17 | |
| 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | void Pad(const TensorInfo& inputInfo, |
| 22 | const TensorInfo& outputInfo, |
| 23 | std::vector<std::pair<unsigned int, unsigned int>> m_PadList, |
| 24 | const float* inputData, |
| 25 | float* outData) |
| 26 | { |
| 27 | unsigned int numOutputElements = outputInfo.GetNumElements(); |
| 28 | |
| 29 | TensorShape outputShape = outputInfo.GetShape(); |
| 30 | TensorShape inputShape = inputInfo.GetShape(); |
| 31 | |
| 32 | unsigned int numInputDimensions = inputShape.GetNumDimensions(); |
| 33 | #ifndef NDEBUG |
| 34 | unsigned int numOutputDimensions = outputShape.GetNumDimensions(); |
| 35 | |
| 36 | assert(numInputDimensions == numOutputDimensions); |
| 37 | #endif |
| 38 | |
| 39 | unsigned int inputBatches = 0; |
| 40 | unsigned int inputChannels = 0; |
| 41 | unsigned int inputHeight = 0; |
| 42 | unsigned int inputWidth = 0; |
| 43 | |
| 44 | unsigned int outputChannels = 0; |
| 45 | unsigned int outputHeight = 0; |
| 46 | unsigned int outputWidth = 0; |
| 47 | |
| 48 | for (unsigned int i = 0; i < numOutputElements; ++i) |
| 49 | { |
| 50 | outData[i] = 0; |
| 51 | } |
| 52 | |
| 53 | switch(numInputDimensions) { |
| 54 | case 1: |
| 55 | |
| 56 | inputWidth = inputShape[0]; |
| 57 | |
| 58 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 59 | { |
| 60 | |
| 61 | outData[w+std::get<0>(m_PadList[0])] = inputData[w]; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | break; |
| 66 | case 2 : |
| 67 | |
| 68 | inputHeight = inputShape[0]; |
| 69 | inputWidth = inputShape[1]; |
| 70 | |
| 71 | outputHeight = outputShape[0]; |
| 72 | outputWidth = outputShape[1]; |
| 73 | |
| 74 | for (unsigned int h = 0; h < inputHeight; h++) |
| 75 | { |
| 76 | |
| 77 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 78 | { |
| 79 | outData[(h+std::get<0>(m_PadList[0]))*outputWidth |
| 80 | + (w+std::get<0>(m_PadList[1]))] = inputData[h * inputWidth + w]; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | break; |
| 85 | case 3 : |
| 86 | |
| 87 | inputChannels = inputShape[0]; |
| 88 | inputHeight = inputShape[1]; |
| 89 | inputWidth = inputShape[2]; |
| 90 | |
| 91 | outputChannels = outputShape[0]; |
| 92 | outputHeight = outputShape[1]; |
| 93 | outputWidth = outputShape[2]; |
| 94 | |
| 95 | for (unsigned int c = 0; c < inputChannels; c++) |
| 96 | { |
| 97 | |
| 98 | for (unsigned int h = 0; h < inputHeight; h++) |
| 99 | { |
| 100 | |
| 101 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 102 | { |
| 103 | |
| 104 | outData[(c+std::get<0>(m_PadList[0]))*outputHeight*outputWidth |
| 105 | + (h+std::get<0>(m_PadList[1]))*outputWidth |
| 106 | + (w+std::get<0>(m_PadList[2]))] = inputData[c * inputHeight * inputWidth |
| 107 | + h * inputWidth |
| 108 | + w]; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | break; |
| 114 | case 4 : |
| 115 | |
| 116 | inputBatches = inputShape[0]; |
| 117 | inputChannels = inputShape[1]; |
| 118 | inputHeight = inputShape[2]; |
| 119 | inputWidth = inputShape[3]; |
| 120 | |
| 121 | outputChannels = outputShape[1]; |
| 122 | outputHeight = outputShape[2]; |
| 123 | outputWidth = outputShape[3]; |
| 124 | |
| 125 | for (unsigned int b = 0; b < inputBatches; b++) |
| 126 | { |
| 127 | for (unsigned int c = 0; c < inputChannels; c++) |
| 128 | { |
| 129 | |
| 130 | for (unsigned int h = 0; h < inputHeight; h++) |
| 131 | { |
| 132 | |
| 133 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 134 | { |
| 135 | |
| 136 | outData[(b+std::get<0>(m_PadList[0])) * outputChannels * outputHeight * outputWidth |
| 137 | + (c+std::get<0>(m_PadList[1])) * outputHeight * outputWidth |
| 138 | + (h+std::get<0>(m_PadList[2])) * outputWidth |
| 139 | + (w+std::get<0>(m_PadList[3]))] = inputData[b * inputChannels * inputHeight |
| 140 | * inputWidth |
| 141 | + c * inputHeight * inputWidth |
| 142 | + h * inputWidth |
| 143 | + w]; |
| 144 | |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | break; |
| 151 | |
| 152 | default : |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | } //namespace armnn |