ArmNN
 20.05
ParserHelper.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 "ParserHelper.hpp"
7 
8 #include <armnn/Descriptors.hpp>
9 #include <armnnUtils/Permute.hpp>
10 
11 #include <boost/format.hpp>
12 
13 namespace armnnUtils
14 {
15 
16 const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 };
17 const armnn::PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 };
18 
20  armnn::OriginsDescriptor& concatDescriptor,
21  const unsigned int& concatAxis,
22  unsigned int inputIndex,
23  unsigned int& mergeDimOrigin)
24 {
25  const uint32_t inputRank = concatDescriptor.GetNumDimensions();
26 
27  // double check dimensions of the tensors
28  if (inputTensorInfo.GetNumDimensions() != inputRank)
29  {
31  boost::str(
32  boost::format(
33  "The number of dimensions: %1% for input tensors of the "
34  "concatenation op should be %2% %3%")
35  % inputTensorInfo.GetNumDimensions()
36  % inputRank
37  % CHECK_LOCATION().AsString()));
38  }
39 
40  for (unsigned int j = 0; j < concatAxis; ++j)
41  {
42  concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
43  }
44 
45  concatDescriptor.SetViewOriginCoord(inputIndex, concatAxis, mergeDimOrigin);
46  mergeDimOrigin += inputTensorInfo.GetShape()[concatAxis];
47 
48  for (unsigned int j = concatAxis + 1; j < inputRank; ++j)
49  {
50  concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
51  }
52 }
53 
55  const std::set<unsigned int>& axisSet,
56  bool keepDims,
57  armnn::TensorInfo& outputTensorInfo)
58 {
59  std::vector<unsigned int> outputShapeVector;
60  bool dimensionFound = false;
61  unsigned int size = 1;
62 
63  for (unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); ++i)
64  {
65  dimensionFound = false;
66  for (unsigned int axis: axisSet)
67  {
68  if (axis == i)
69  {
70  dimensionFound = true;
71  break;
72  }
73  }
74 
75  if (!dimensionFound)
76  {
77  size *= inputTensorInfo.GetShape()[i];
78 
79  if (keepDims)
80  {
81  outputShapeVector.push_back(inputTensorInfo.GetShape()[i]);
82  }
83  }
84  else
85  {
86  if (keepDims)
87  {
88  outputShapeVector.push_back(1);
89  }
90  }
91  }
92 
93  if (keepDims)
94  {
95  armnn::TensorShape outputTensorShape(inputTensorInfo.GetNumDimensions(), &outputShapeVector[0]);
96  outputTensorInfo = armnn::TensorInfo(outputTensorShape, inputTensorInfo.GetDataType());
97  }
98  else
99  {
100  outputTensorInfo = armnn::TensorInfo({size}, inputTensorInfo.GetDataType());
101  }
102 }
103 
104 
106  const armnn::StridedSliceDescriptor& desc,
107  armnn::TensorInfo& outputTensorInfo)
108 {
109  const armnn::TensorShape& inputShape = inputTensorInfo.GetShape();
110 
111  std::vector<unsigned int> outputShapeVector;
112  for (unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
113  {
114  if (desc.m_ShrinkAxisMask & (1 << i))
115  {
116  continue;
117  }
118 
119  int stride = desc.m_Stride[i];
120  int start = desc.GetStartForAxis(inputShape, i);
121  int stop = desc.GetStopForAxis(inputShape, i, start);
122 
123  int newSize = stride > 0 ? ((stop - start) + stride - 1) / stride :
124  ((start - stop) - stride - 1) / -stride;
125 
126  newSize = std::max(0, newSize);
127 
128  outputShapeVector.push_back(static_cast<unsigned int>(newSize));
129  }
130 
131  armnn::TensorShape outputTensorShape(inputTensorInfo.GetNumDimensions(), &outputShapeVector[0]);
132  outputTensorInfo = armnn::TensorInfo(armnn::TensorShape(outputTensorShape), inputTensorInfo.GetDataType());
133 }
134 } // namespace armnnUtils
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1...
int GetStartForAxis(const TensorShape &inputShape, unsigned int axis) const
void CalculateReducedOutputTensoInfo(const armnn::TensorInfo &inputTensorInfo, const std::set< unsigned int > &axisSet, bool keepDims, armnn::TensorInfo &outputTensorInfo)
Creates a tensor info after reducing the dimensions mentioned in axisData.
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
const armnn::PermutationVector NHWCToArmNN
DataType GetDataType() const
Definition: Tensor.hpp:95
An OriginsDescriptor for the ConcatLayer.
std::vector< int > m_Stride
Stride values for the input that will be sliced.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
void CalculateStridedSliceOutputTensorInfo(const armnn::TensorInfo &inputTensorInfo, const armnn::StridedSliceDescriptor &desc, armnn::TensorInfo &outputTensorInfo)
Create output tensor info for a StridedSlice operator.
uint32_t GetNumDimensions() const
Get the number of dimensions.
A StridedSliceDescriptor for the StridedSliceLayer.
int GetStopForAxis(const TensorShape &inputShape, unsigned int axis, int startForAxis) const
const armnn::PermutationVector ArmNNToNHWC
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:92
Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value)
Set the view origin coordinates.