ArmNN
 20.02
Pad.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Pad.hpp"
9 #include "Encoders.hpp"
10 
11 #include <boost/numeric/conversion/cast.hpp>
12 #include <cmath>
13 #include <cstddef>
14 #include <functional>
15 #include <limits>
16 #include <cassert>
17 
18 namespace armnn
19 {
20 
21 template <typename T>
22 void Pad(const TensorInfo& inputInfo,
23  const TensorInfo& outputInfo,
24  std::vector<std::pair<unsigned int, unsigned int>> m_padList,
25  const T* inputData,
26  T* outData,
27  const float padValue)
28 {
29  unsigned int numOutputElements = outputInfo.GetNumElements();
30 
31  TensorShape outputShape = outputInfo.GetShape();
32  TensorShape inputShape = inputInfo.GetShape();
33 
34  unsigned int numInputDimensions = inputShape.GetNumDimensions();
35 
36  #ifndef NDEBUG
37 
38  unsigned int numOutputDimensions = outputShape.GetNumDimensions();
39  assert(numInputDimensions == numOutputDimensions);
40 
41  #endif
42 
43  unsigned int inputBatches = 0;
44  unsigned int inputChannels = 0;
45  unsigned int inputHeight = 0;
46  unsigned int inputWidth = 0;
47 
48  unsigned int outputChannels = 0;
49  unsigned int outputHeight = 0;
50  unsigned int outputWidth = 0;
51 
52  T convertedPadValue = static_cast<T>(padValue);
53 
54  for (unsigned int i = 0; i < numOutputElements; ++i)
55  {
56  outData[i] = convertedPadValue;
57  }
58 
59  switch(numInputDimensions) {
60 
61  case 1:
62 
63  inputWidth = inputShape[0];
64 
65  for (unsigned int w = 0; w < inputWidth ; w++)
66  {
67  outData[w+std::get<0>(m_padList[0])] = inputData[w];
68  }
69 
70  break;
71 
72  case 2 :
73 
74  inputHeight = inputShape[0];
75  inputWidth = inputShape[1];
76  outputHeight = outputShape[0];
77  outputWidth = outputShape[1];
78 
79  for (unsigned int h = 0; h < inputHeight; h++)
80  {
81  for (unsigned int w = 0; w < inputWidth ; w++)
82  {
83  outData[(h+std::get<0>(m_padList[0]))*outputWidth
84  + (w+std::get<0>(m_padList[1]))] = inputData[h * inputWidth + w];
85  }
86  }
87 
88  break;
89 
90  case 3 :
91 
92  inputChannels = inputShape[0];
93  inputHeight = inputShape[1];
94  inputWidth = inputShape[2];
95  outputChannels = outputShape[0];
96  outputHeight = outputShape[1];
97  outputWidth = outputShape[2];
98 
99  for (unsigned int c = 0; c < inputChannels; c++)
100  {
101  for (unsigned int h = 0; h < inputHeight; h++)
102  {
103  for (unsigned int w = 0; w < inputWidth ; w++)
104  {
105  outData[(c+std::get<0>(m_padList[0]))*outputHeight*outputWidth
106  + (h+std::get<0>(m_padList[1]))*outputWidth
107  + (w+std::get<0>(m_padList[2]))] = inputData[c * inputHeight * inputWidth
108  + h * inputWidth
109  + w];
110  }
111  }
112  }
113 
114  break;
115 
116  case 4 :
117 
118  inputBatches = inputShape[0];
119  inputChannels = inputShape[1];
120  inputHeight = inputShape[2];
121  inputWidth = inputShape[3];
122  outputChannels = outputShape[1];
123  outputHeight = outputShape[2];
124  outputWidth = outputShape[3];
125 
126  for (unsigned int b = 0; b < inputBatches; b++)
127  {
128  for (unsigned int c = 0; c < inputChannels; c++)
129  {
130  for (unsigned int h = 0; h < inputHeight; h++)
131  {
132  for (unsigned int w = 0; w < inputWidth ; w++)
133  {
134  outData[(b+std::get<0>(m_padList[0])) * outputChannels * outputHeight * outputWidth
135  + (c+std::get<0>(m_padList[1])) * outputHeight * outputWidth
136  + (h+std::get<0>(m_padList[2])) * outputWidth
137  + (w+std::get<0>(m_padList[3]))] = inputData[b * inputChannels * inputHeight
138  * inputWidth
139  + c * inputHeight * inputWidth
140  + h * inputWidth
141  + w];
142  }
143  }
144  }
145  }
146 
147  break;
148 
149  default :
150 
151  break;
152  }
153 }
154 
155 template void Pad<BFloat16>(const TensorInfo& inputInfo,
156  const TensorInfo& outputInfo,
157  std::vector<std::pair<unsigned int, unsigned int>> m_PadList,
158  const BFloat16* inputData,
159  BFloat16* outData,
160  const float padValue);
161 
162 template void Pad<float>(const TensorInfo& inputInfo,
163  const TensorInfo& outputInfo,
164  std::vector<std::pair<unsigned int, unsigned int>> m_PadList,
165  const float* inputData,
166  float* outData,
167  const float padValue);
168 template void Pad<Half>(const TensorInfo& inputInfo,
169  const TensorInfo& outputInfo,
170  std::vector<std::pair<unsigned int, unsigned int>> m_PadList,
171  const Half* inputData,
172  Half* outData,
173  const float padValue);
174 template void Pad<uint8_t>(const TensorInfo& inputInfo,
175  const TensorInfo& outputInfo,
176  std::vector<std::pair<unsigned int, unsigned int>> m_PadList,
177  const uint8_t* inputData,
178  uint8_t* outData,
179  const float padValue);
180 template void Pad<int16_t>(const TensorInfo& inputInfo,
181  const TensorInfo& outputInfo,
182  std::vector<std::pair<unsigned int, unsigned int>> m_PadList,
183  const int16_t* inputData,
184  int16_t* outData,
185  const float padValue);
186 
187 } //namespace armnn
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
Copyright (c) 2020 ARM Limited.
void Pad(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_padList, const T *inputData, T *outData, const float padValue)
Definition: Pad.cpp:22
template void Pad< BFloat16 >(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_PadList, const BFloat16 *inputData, BFloat16 *outData, const float padValue)
template void Pad< int16_t >(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_PadList, const int16_t *inputData, int16_t *outData, const float padValue)
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
template void Pad< Half >(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_PadList, const Half *inputData, Half *outData, const float padValue)
template void Pad< float >(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_PadList, const float *inputData, float *outData, const float padValue)
half_float::half Half
Definition: Half.hpp:16
template void Pad< uint8_t >(const TensorInfo &inputInfo, const TensorInfo &outputInfo, std::vector< std::pair< unsigned int, unsigned int >> m_PadList, const uint8_t *inputData, uint8_t *outData, const float padValue)
unsigned int GetNumElements() const
Definition: Tensor.hpp:93