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" |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 7 | #include "backendsCommon/WorkloadData.hpp" |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 8 | #include "TensorBufferArrayView.hpp" |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 9 | #include "Encoders.hpp" |
| 10 | |
| 11 | #include <boost/numeric/conversion/cast.hpp> |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 12 | #include <cmath> |
| 13 | #include <cstddef> |
| 14 | #include <functional> |
| 15 | #include <limits> |
| 16 | #include <cassert> |
| 17 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 20 | |
| 21 | template <typename T> |
| 22 | T ConvertToDataType(const float& value, |
| 23 | const armnn::TensorInfo& tensorInfo) |
| 24 | { |
| 25 | std::vector<T> output(1); |
| 26 | std::unique_ptr<armnn::Encoder<float>> pEncoder = armnn::MakeEncoder<float>(tensorInfo, output.data()); |
| 27 | armnn::Encoder<float>& rEncoder = *pEncoder; |
| 28 | rEncoder.Set(value); |
| 29 | return output[0]; |
| 30 | } |
| 31 | |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 32 | template <typename T> |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 33 | void Pad(const TensorInfo& inputInfo, |
| 34 | const TensorInfo& outputInfo, |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 35 | std::vector<std::pair<unsigned int, unsigned int>> m_padList, |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 36 | const T* inputData, |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 37 | T* outData, |
| 38 | const float padValue) |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 39 | { |
| 40 | unsigned int numOutputElements = outputInfo.GetNumElements(); |
| 41 | |
| 42 | TensorShape outputShape = outputInfo.GetShape(); |
| 43 | TensorShape inputShape = inputInfo.GetShape(); |
| 44 | |
| 45 | unsigned int numInputDimensions = inputShape.GetNumDimensions(); |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 46 | |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 47 | #ifndef NDEBUG |
| 48 | |
| 49 | unsigned int numOutputDimensions = outputShape.GetNumDimensions(); |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 50 | assert(numInputDimensions == numOutputDimensions); |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 51 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | unsigned int inputBatches = 0; |
| 55 | unsigned int inputChannels = 0; |
| 56 | unsigned int inputHeight = 0; |
| 57 | unsigned int inputWidth = 0; |
| 58 | |
| 59 | unsigned int outputChannels = 0; |
| 60 | unsigned int outputHeight = 0; |
| 61 | unsigned int outputWidth = 0; |
| 62 | |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 63 | T convertedPadValue = ConvertToDataType<T>(padValue, inputInfo); |
| 64 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 65 | for (unsigned int i = 0; i < numOutputElements; ++i) |
| 66 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 67 | outData[i] = convertedPadValue; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | switch(numInputDimensions) { |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 71 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 72 | case 1: |
| 73 | |
| 74 | inputWidth = inputShape[0]; |
| 75 | |
| 76 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 77 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 78 | outData[w+std::get<0>(m_padList[0])] = inputData[w]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | break; |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 82 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 83 | case 2 : |
| 84 | |
| 85 | inputHeight = inputShape[0]; |
| 86 | inputWidth = inputShape[1]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 87 | outputHeight = outputShape[0]; |
| 88 | outputWidth = outputShape[1]; |
| 89 | |
| 90 | for (unsigned int h = 0; h < inputHeight; h++) |
| 91 | { |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 92 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 93 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 94 | outData[(h+std::get<0>(m_padList[0]))*outputWidth |
| 95 | + (w+std::get<0>(m_padList[1]))] = inputData[h * inputWidth + w]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | break; |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 100 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 101 | case 3 : |
| 102 | |
| 103 | inputChannels = inputShape[0]; |
| 104 | inputHeight = inputShape[1]; |
| 105 | inputWidth = inputShape[2]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 106 | outputChannels = outputShape[0]; |
| 107 | outputHeight = outputShape[1]; |
| 108 | outputWidth = outputShape[2]; |
| 109 | |
| 110 | for (unsigned int c = 0; c < inputChannels; c++) |
| 111 | { |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 112 | for (unsigned int h = 0; h < inputHeight; h++) |
| 113 | { |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 114 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 115 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 116 | outData[(c+std::get<0>(m_padList[0]))*outputHeight*outputWidth |
| 117 | + (h+std::get<0>(m_padList[1]))*outputWidth |
| 118 | + (w+std::get<0>(m_padList[2]))] = inputData[c * inputHeight * inputWidth |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 119 | + h * inputWidth |
| 120 | + w]; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | break; |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 126 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 127 | case 4 : |
| 128 | |
| 129 | inputBatches = inputShape[0]; |
| 130 | inputChannels = inputShape[1]; |
| 131 | inputHeight = inputShape[2]; |
| 132 | inputWidth = inputShape[3]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 133 | outputChannels = outputShape[1]; |
| 134 | outputHeight = outputShape[2]; |
| 135 | outputWidth = outputShape[3]; |
| 136 | |
| 137 | for (unsigned int b = 0; b < inputBatches; b++) |
| 138 | { |
| 139 | for (unsigned int c = 0; c < inputChannels; c++) |
| 140 | { |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 141 | for (unsigned int h = 0; h < inputHeight; h++) |
| 142 | { |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 143 | for (unsigned int w = 0; w < inputWidth ; w++) |
| 144 | { |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 145 | outData[(b+std::get<0>(m_padList[0])) * outputChannels * outputHeight * outputWidth |
| 146 | + (c+std::get<0>(m_padList[1])) * outputHeight * outputWidth |
| 147 | + (h+std::get<0>(m_padList[2])) * outputWidth |
| 148 | + (w+std::get<0>(m_padList[3]))] = inputData[b * inputChannels * inputHeight |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 149 | * inputWidth |
| 150 | + c * inputHeight * inputWidth |
| 151 | + h * inputWidth |
| 152 | + w]; |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | break; |
| 159 | |
| 160 | default : |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 161 | |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 162 | break; |
| 163 | } |
Mohamed Nour Abouelseoud | 7420e55 | 2018-10-12 12:26:24 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 166 | template void Pad<float>(const TensorInfo& inputInfo, |
| 167 | const TensorInfo& outputInfo, |
| 168 | std::vector<std::pair<unsigned int, unsigned int>> m_PadList, |
| 169 | const float* inputData, |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 170 | float* outData, |
| 171 | const float padValue); |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 172 | template void Pad<uint8_t>(const TensorInfo& inputInfo, |
| 173 | const TensorInfo& outputInfo, |
| 174 | std::vector<std::pair<unsigned int, unsigned int>> m_PadList, |
| 175 | const uint8_t* inputData, |
David Monahan | 3475781 | 2019-06-19 11:47:21 +0100 | [diff] [blame] | 176 | uint8_t* outData, |
| 177 | const float padValue); |
Mohamed Nour Abouelseoud | dd6acea | 2018-10-18 12:26:19 +0100 | [diff] [blame] | 178 | |
| 179 | } //namespace armnn |