ArmNN
 21.05
armnnUtils Namespace Reference

Namespaces

 Filesystem
 
 Processes
 
 Threads
 

Classes

class  DataLayoutIndexed
 Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout. More...
 
class  FloatingPointConverter
 
struct  IsFloatingPointIterator
 
class  ModelAccuracyChecker
 
struct  ParserPrototxtFixture
 
struct  SelectiveQuantizer
 
struct  SelectiveQuantizer< armnn::BFloat16, false >
 
struct  SelectiveQuantizer< armnn::Half, false >
 
struct  SelectiveQuantizer< T, false >
 

Typedefs

using LabelCategoryNames = std::vector< std::string >
 

Functions

bool operator== (const armnn::DataLayout &dataLayout, const DataLayoutIndexed &indexed)
 Equality methods. More...
 
bool operator== (const DataLayoutIndexed &indexed, const armnn::DataLayout &dataLayout)
 
bool within_percentage_tolerance (float a, float b, float tolerancePercent=1.0f)
 Compare two floats and return true if their values are within a specified tolerance of each other. More...
 
armnn::TensorShape Permuted (const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
 
armnn::TensorInfo Permuted (const armnn::TensorInfo &info, const armnn::PermutationVector &mappings)
 
void Permute (const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
 
armnn::TensorShape GetTensorShape (unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout)
 
armnn::TensorInfo GetTensorInfo (unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout, const armnn::DataType dataType)
 
std::pair< float, float > FindMinMax (armnn::ITensorHandle *tensorHandle)
 
armnn::TensorShape ExpandDims (const armnn::TensorShape &tensorShape, int axis)
 
unsigned int GetNumElementsBetween (const armnn::TensorShape &shape, unsigned int firstAxisInclusive, unsigned int lastAxisExclusive)
 
unsigned int GetUnsignedAxis (const unsigned int inputDimension, const int axis)
 
unsigned int GetNumElementsAfter (const armnn::TensorShape &shape, unsigned int axis)
 
std::pair< unsigned int, std::vector< float > > GetPerAxisParams (const armnn::TensorInfo &info)
 
armnn::TensorShape TransposeTensorShape (const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
 
armnn::TensorInfo TransposeTensorShape (const armnn::TensorInfo &info, const armnn::PermutationVector &mappings)
 
void Transpose (const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
 
template<typename TNodeId , typename TTargetNodes >
bool GraphTopologicalSort (const TTargetNodes &targetNodes, std::function< std::vector< TNodeId >(TNodeId)> getIncomingEdges, std::vector< TNodeId > &outSorted)
 
std::vector< std::string > SplitBy (const std::string &originalString, const std::string &delimiter=" ", bool includeEmptyToken=false)
 Split a string into tokens by a delimiter. More...
 
std::string Strip (const std::string &originalString, const std::string &characterSet=" ")
 Remove any preceding and trailing character specified in the characterSet. More...
 
void ProcessConcatInputTensorInfo (armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
 
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. More...
 
void CalculateStridedSliceOutputTensorInfo (const armnn::TensorInfo &inputTensorInfo, const armnn::StridedSliceDescriptor &desc, armnn::TensorInfo &outputTensorInfo)
 Create output tensor info for a StridedSlice operator. More...
 
std::string ConvertInt32ToOctalString (int value)
 Converts an int value into the Prototxt octal representation. More...
 
std::string ConvertTensorShapeToString (const armnn::TensorShape &shape)
 Converts an TensorShape into Prototxt representation. More...
 
template<typename T >
SelectiveQuantize (float value, float scale, int32_t offset)
 
template<typename T >
float SelectiveDequantize (T value, float scale, int32_t offset)
 
template<typename T , typename FloatIt , typename std::enable_if< IsFloatingPointIterator< FloatIt >::value, int >::type = 0>
std::vector< T > QuantizedVector (FloatIt first, FloatIt last, float qScale, int32_t qOffset)
 
template<typename T >
std::vector< T > QuantizedVector (const std::vector< float > &array, float qScale=1.f, int32_t qOffset=0)
 
template<typename T >
std::vector< T > QuantizedVector (std::initializer_list< float > array, float qScale=1.f, int32_t qOffset=0)
 
template<typename TContainer >
armnn::InputTensors MakeInputTensors (const std::vector< armnn::BindingPointInfo > &inputBindings, const std::vector< TContainer > &inputDataContainers)
 
template<typename TContainer >
armnn::OutputTensors MakeOutputTensors (const std::vector< armnn::BindingPointInfo > &outputBindings, std::vector< TContainer > &outputDataContainers)
 
void CheckValidSize (std::initializer_list< size_t > validInputCounts, size_t actualValue, const char *validExpr, const char *actualExpr, const CheckLocation &location)
 
uint32_t NonNegative (const char *expr, int32_t value, const CheckLocation &location)
 
int32_t VerifyInt32 (const char *expr, int64_t value, const armnn::CheckLocation &location)
 

Variables

const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }
 
const armnn::PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 }
 

Typedef Documentation

◆ LabelCategoryNames

using LabelCategoryNames = std::vector<std::string>

Definition at line 25 of file ModelAccuracyChecker.hpp.

Function Documentation

◆ CalculateReducedOutputTensoInfo()

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.

Definition at line 52 of file ParserHelper.cpp.

References TensorInfo::GetDataType(), TensorInfo::GetNumDimensions(), and TensorInfo::GetShape().

Referenced by BOOST_AUTO_TEST_CASE().

56 {
57  std::vector<unsigned int> outputShapeVector;
58  bool dimensionFound = false;
59  unsigned int size = 1;
60 
61  for (unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); ++i)
62  {
63  dimensionFound = false;
64  for (unsigned int axis: axisSet)
65  {
66  if (axis == i)
67  {
68  dimensionFound = true;
69  break;
70  }
71  }
72 
73  if (!dimensionFound)
74  {
75  size *= inputTensorInfo.GetShape()[i];
76 
77  if (keepDims)
78  {
79  outputShapeVector.push_back(inputTensorInfo.GetShape()[i]);
80  }
81  }
82  else
83  {
84  if (keepDims)
85  {
86  outputShapeVector.push_back(1);
87  }
88  }
89  }
90 
91  if (keepDims)
92  {
93  armnn::TensorShape outputTensorShape(inputTensorInfo.GetNumDimensions(), &outputShapeVector[0]);
94  outputTensorInfo = armnn::TensorInfo(outputTensorShape, inputTensorInfo.GetDataType());
95  }
96  else
97  {
98  outputTensorInfo = armnn::TensorInfo({size}, inputTensorInfo.GetDataType());
99  }
100 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
DataType GetDataType() const
Definition: Tensor.hpp:194
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191

◆ CalculateStridedSliceOutputTensorInfo()

void CalculateStridedSliceOutputTensorInfo ( const armnn::TensorInfo inputTensorInfo,
const armnn::StridedSliceDescriptor desc,
armnn::TensorInfo outputTensorInfo 
)

Create output tensor info for a StridedSlice operator.

Definition at line 103 of file ParserHelper.cpp.

References TensorInfo::GetDataType(), TensorInfo::GetNumDimensions(), TensorInfo::GetShape(), StridedSliceDescriptor::GetStartForAxis(), StridedSliceDescriptor::GetStopForAxis(), StridedSliceDescriptor::m_ShrinkAxisMask, and StridedSliceDescriptor::m_Stride.

106 {
107  const armnn::TensorShape& inputShape = inputTensorInfo.GetShape();
108 
109  std::vector<unsigned int> outputShapeVector;
110  for (unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
111  {
112  if (desc.m_ShrinkAxisMask & (1 << i))
113  {
114  continue;
115  }
116 
117  int stride = desc.m_Stride[i];
118  int start = desc.GetStartForAxis(inputShape, i);
119  int stop = desc.GetStopForAxis(inputShape, i, start);
120 
121  int newSize = stride > 0 ? ((stop - start) + stride - 1) / stride :
122  ((start - stop) - stride - 1) / -stride;
123 
124  newSize = std::max(0, newSize);
125 
126  outputShapeVector.push_back(static_cast<unsigned int>(newSize));
127  }
128 
129  armnn::TensorShape outputTensorShape(inputTensorInfo.GetNumDimensions(), &outputShapeVector[0]);
130  outputTensorInfo = armnn::TensorInfo(armnn::TensorShape(outputTensorShape), inputTensorInfo.GetDataType());
131 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
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
DataType GetDataType() const
Definition: Tensor.hpp:194
std::vector< int > m_Stride
Stride values for the input that will be sliced.
int GetStopForAxis(const TensorShape &inputShape, unsigned int axis, int startForAxis) const
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191

◆ CheckValidSize()

void CheckValidSize ( std::initializer_list< size_t >  validInputCounts,
size_t  actualValue,
const char *  validExpr,
const char *  actualExpr,
const CheckLocation location 
)

Definition at line 16 of file VerificationHelpers.cpp.

References CheckLocation::AsString().

21 {
22  bool isValid = std::any_of(validInputCounts.begin(),
23  validInputCounts.end(),
24  [&actualValue](size_t x) { return x == actualValue; } );
25  if (!isValid)
26  {
27  throw ParseException(fmt::format("{} = {} is not valid, not in {{}}. {}",
28  actualExpr,
29  actualValue,
30  validExpr,
31  location.AsString()));
32  }
33 }
std::string AsString() const
Definition: Exceptions.hpp:29

◆ ConvertInt32ToOctalString()

std::string ConvertInt32ToOctalString ( int  value)

Converts an int value into the Prototxt octal representation.

Definition at line 17 of file PrototxtConversions.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

18 {
19  std::stringstream ss;
20  std::string returnString;
21  for (int i = 0; i < 4; ++i)
22  {
23  ss << "\\";
24  ss << std::setw(3) << std::setfill('0') << std::oct << ((value >> (i * 8)) & 0xFF);
25  }
26 
27  ss >> returnString;
28  return returnString;
29 }

◆ ConvertTensorShapeToString()

std::string ConvertTensorShapeToString ( const armnn::TensorShape shape)

Converts an TensorShape into Prototxt representation.

Definition at line 32 of file PrototxtConversions.cpp.

References TensorShape::GetNumDimensions().

Referenced by BOOST_AUTO_TEST_CASE().

33 {
34  std::stringstream ss;
35  for (unsigned int i = 0 ; i < shape.GetNumDimensions() ; i++)
36  {
37  ss << "dim {\n";
38  ss << "size: " << std::to_string(shape[i]) << "\n";
39  ss << "}\n";
40  }
41  return ss.str();
42 
43 }
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174

◆ ExpandDims()

TensorShape ExpandDims ( const armnn::TensorShape tensorShape,
int  axis 
)

Definition at line 85 of file TensorUtils.cpp.

References CHECK_LOCATION, TensorShape::GetNumDimensions(), and armnn::numeric_cast().

Referenced by BOOST_AUTO_TEST_CASE().

86 {
87  unsigned int outputDim = tensorShape.GetNumDimensions() + 1;
88 
89  if (axis < -armnn::numeric_cast<int>(outputDim) || axis > armnn::numeric_cast<int>(tensorShape.GetNumDimensions()))
90  {
91  throw InvalidArgumentException(fmt::format("Invalid expansion axis {} for {}D input tensor. {}",
92  axis,
93  tensorShape.GetNumDimensions(),
94  CHECK_LOCATION().AsString()));
95  }
96 
97  if (axis < 0)
98  {
99  axis = armnn::numeric_cast<int>(outputDim) + axis;
100  }
101 
102  std::vector<unsigned int> outputShape;
103  outputShape.reserve(tensorShape.GetNumDimensions());
104  for (unsigned int i = 0; i < tensorShape.GetNumDimensions(); ++i)
105  {
106  outputShape.push_back(tensorShape[i]);
107  }
108  outputShape.insert(outputShape.begin() + axis, 1);
109 
110  return TensorShape(outputDim, outputShape.data());
111 }
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35

◆ FindMinMax()

std::pair< float, float > FindMinMax ( armnn::ITensorHandle tensorHandle)

Definition at line 58 of file TensorUtils.cpp.

References TensorShape::GetNumElements(), ITensorHandle::GetShape(), ITensorHandle::Map(), and ITensorHandle::Unmap().

59 {
60  auto tensor_data = static_cast<const float *>(tensorHandle->Map(true));
61  auto tensor_size = tensorHandle->GetShape().GetNumElements();
62 
63  // Set min/max initially to first value in tensor
64  float min = tensor_data[0];
65  float max = tensor_data[0];
66 
67  // Loop over rest of tensor and update min/max if necessary
68  for (unsigned int val = 1; val < tensor_size; val++)
69  {
70  if (tensor_data[val] < min)
71  {
72  min = tensor_data[val];
73  }
74  else if (tensor_data[val] > max)
75  {
76  max = tensor_data[val];
77  }
78  }
79 
80  tensorHandle->Unmap();
81 
82  return std::make_pair(min, max);
83 }
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified...
Definition: Tensor.cpp:181
virtual TensorShape GetShape() const =0
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.
virtual void Unmap() const =0
Unmap the tensor data.

◆ GetNumElementsAfter()

unsigned int GetNumElementsAfter ( const armnn::TensorShape shape,
unsigned int  axis 
)

Definition at line 140 of file TensorUtils.cpp.

References ARMNN_ASSERT, and TensorShape::GetNumDimensions().

Referenced by GetPerAxisParams().

141 {
142  unsigned int numDim = shape.GetNumDimensions();
143  ARMNN_ASSERT(axis <= numDim - 1);
144  unsigned int count = 1;
145  for (unsigned int i = axis; i < numDim; i++)
146  {
147  count *= shape[i];
148  }
149  return count;
150 }
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174

◆ GetNumElementsBetween()

unsigned int GetNumElementsBetween ( const armnn::TensorShape shape,
unsigned int  firstAxisInclusive,
unsigned int  lastAxisExclusive 
)

Definition at line 113 of file TensorUtils.cpp.

References ARMNN_ASSERT, and TensorShape::GetNumDimensions().

Referenced by armnn::ArgMinMax(), armnn::LogSoftmax(), and armnn::Softmax().

116 {
117  ARMNN_ASSERT(firstAxisInclusive <= lastAxisExclusive);
118  ARMNN_ASSERT(lastAxisExclusive <= shape.GetNumDimensions());
119  unsigned int count = 1;
120  for (unsigned int i = firstAxisInclusive; i < lastAxisExclusive; i++)
121  {
122  count *= shape[i];
123  }
124  return count;
125 }
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174

◆ GetPerAxisParams()

std::pair< unsigned int, std::vector< float > > GetPerAxisParams ( const armnn::TensorInfo info)

Definition at line 152 of file TensorUtils.cpp.

References CHECK_LOCATION, TensorInfo::GetDataType(), armnn::GetDataTypeName(), GetNumElementsAfter(), TensorInfo::GetQuantizationDim(), TensorInfo::GetQuantizationScales(), TensorInfo::GetShape(), TensorInfo::HasPerAxisQuantization(), and OptionalReferenceSwitch< IsReference, T >::value().

Referenced by armnn::MakeDecoder(), and armnn::MakeEncoder().

153 {
154  const std::vector<float>& scales = info.GetQuantizationScales();
155  armnn::Optional<unsigned int> quantizationDim = info.GetQuantizationDim();
156  if (!info.HasPerAxisQuantization())
157  {
159  std::string("Per-axis quantization params not set for tensor of type ") +
161  }
162  unsigned int axisFactor = GetNumElementsAfter(info.GetShape(), quantizationDim.value());
163 
164  return { axisFactor, scales };
165 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
bool HasPerAxisQuantization() const
Definition: Tensor.cpp:437
Optional< unsigned int > GetQuantizationDim() const
Definition: Tensor.cpp:485
std::vector< float > GetQuantizationScales() const
Definition: Tensor.cpp:442
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:191
DataType GetDataType() const
Definition: Tensor.hpp:194
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
unsigned int GetNumElementsAfter(const armnn::TensorShape &shape, unsigned int axis)

◆ GetTensorInfo()

TensorInfo GetTensorInfo ( unsigned int  numberOfBatches,
unsigned int  numberOfChannels,
unsigned int  height,
unsigned int  width,
const armnn::DataLayout  dataLayout,
const armnn::DataType  dataType 
)

Definition at line 38 of file TensorUtils.cpp.

References CHECK_LOCATION, armnn::NCHW, and armnn::NHWC.

Referenced by DepthwiseConvolution2dAsymmetricTestImpl(), DepthwiseConvolution2dDepthMul1TestImpl(), DepthwiseConvolution2dTestImpl(), and SimpleConvolution2dTestImpl().

44 {
45  switch (dataLayout)
46  {
47  case DataLayout::NCHW:
48  return TensorInfo({numberOfBatches, numberOfChannels, height, width}, dataType);
49  case DataLayout::NHWC:
50  return TensorInfo({numberOfBatches, height, width, numberOfChannels}, dataType);
51  default:
52  throw InvalidArgumentException("Unknown data layout ["
53  + std::to_string(static_cast<int>(dataLayout)) +
54  "]", CHECK_LOCATION());
55  }
56 }
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197

◆ GetTensorShape()

TensorShape GetTensorShape ( unsigned int  numberOfBatches,
unsigned int  numberOfChannels,
unsigned int  height,
unsigned int  width,
const armnn::DataLayout  dataLayout 
)

Definition at line 19 of file TensorUtils.cpp.

References CHECK_LOCATION, armnn::NCHW, and armnn::NHWC.

24 {
25  switch (dataLayout)
26  {
27  case DataLayout::NCHW:
28  return TensorShape({numberOfBatches, numberOfChannels, height, width});
29  case DataLayout::NHWC:
30  return TensorShape({numberOfBatches, height, width, numberOfChannels});
31  default:
32  throw InvalidArgumentException("Unknown data layout ["
33  + std::to_string(static_cast<int>(dataLayout)) +
34  "]", CHECK_LOCATION());
35  }
36 }
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197

◆ GetUnsignedAxis()

unsigned int GetUnsignedAxis ( const unsigned int  inputDimension,
const int  axis 
)

Definition at line 127 of file TensorUtils.cpp.

References ARMNN_ASSERT_MSG, and armnn::numeric_cast().

Referenced by armnn::ArgMinMax(), ClArgMinMaxWorkload::ClArgMinMaxWorkload(), ArgMinMaxLayer::InferOutputShapes(), NeonArgMinMaxWorkload::NeonArgMinMaxWorkload(), TfLiteParserImpl::OutputShapeOfReshape(), and ArgMinMaxQueueDescriptor::Validate().

128 {
129  ARMNN_ASSERT_MSG(axis < armnn::numeric_cast<int>(inputDimension),
130  "Required axis index greater than number of dimensions.");
131  ARMNN_ASSERT_MSG(axis >= -armnn::numeric_cast<int>(inputDimension),
132  "Required axis index lower than negative of the number of dimensions");
133 
134  unsigned int uAxis = axis < 0 ?
135  inputDimension - armnn::numeric_cast<unsigned int>(abs(axis))
136  : armnn::numeric_cast<unsigned int>(axis);
137  return uAxis;
138 }
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35

◆ GraphTopologicalSort()

bool armnnUtils::GraphTopologicalSort ( const TTargetNodes &  targetNodes,
std::function< std::vector< TNodeId >(TNodeId)>  getIncomingEdges,
std::vector< TNodeId > &  outSorted 
)

Definition at line 110 of file GraphTopologicalSort.hpp.

114 {
115  outSorted.clear();
116  std::map<TNodeId, NodeState> nodeStates;
117 
118  for (TNodeId targetNode : targetNodes)
119  {
120  if (!TopologicallySort(targetNode, getIncomingEdges, outSorted, nodeStates))
121  {
122  return false;
123  }
124  }
125 
126  return true;
127 }

◆ MakeInputTensors()

armnn::InputTensors armnnUtils::MakeInputTensors ( const std::vector< armnn::BindingPointInfo > &  inputBindings,
const std::vector< TContainer > &  inputDataContainers 
)
inline

Definition at line 17 of file TensorIOUtils.hpp.

Referenced by InferenceModel< IParser, TDataType >::CreateWorkingMemHandle(), and main().

19 {
20  armnn::InputTensors inputTensors;
21 
22  const size_t numInputs = inputBindings.size();
23  if (numInputs != inputDataContainers.size())
24  {
25  throw armnn::Exception(fmt::format("The number of inputs does not match number of "
26  "tensor data containers: {0} != {1}",
27  numInputs,
28  inputDataContainers.size()));
29  }
30 
31  for (size_t i = 0; i < numInputs; i++)
32  {
33  const armnn::BindingPointInfo& inputBinding = inputBindings[i];
34  const TContainer& inputData = inputDataContainers[i];
35 
36  mapbox::util::apply_visitor([&](auto&& value)
37  {
38  if (value.size() != inputBinding.second.GetNumElements())
39  {
40  throw armnn::Exception(fmt::format("The input tensor has incorrect size (expected {0} got {1})",
41  inputBinding.second.GetNumElements(),
42  value.size()));
43  }
44 
45  armnn::ConstTensor inputTensor(inputBinding.second, value.data());
46  inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor));
47  },
48  inputData);
49  }
50 
51  return inputTensors;
52 }
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:340
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
Definition: Tensor.hpp:261
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46

◆ MakeOutputTensors()

armnn::OutputTensors armnnUtils::MakeOutputTensors ( const std::vector< armnn::BindingPointInfo > &  outputBindings,
std::vector< TContainer > &  outputDataContainers 
)
inline

Definition at line 55 of file TensorIOUtils.hpp.

Referenced by InferenceModel< IParser, TDataType >::CreateWorkingMemHandle(), and main().

57 {
58  armnn::OutputTensors outputTensors;
59 
60  const size_t numOutputs = outputBindings.size();
61  if (numOutputs != outputDataContainers.size())
62  {
63  throw armnn::Exception(fmt::format("Number of outputs does not match number"
64  "of tensor data containers: {0} != {1}",
65  numOutputs,
66  outputDataContainers.size()));
67  }
68 
69  for (size_t i = 0; i < numOutputs; i++)
70  {
71  const armnn::BindingPointInfo& outputBinding = outputBindings[i];
72  TContainer& outputData = outputDataContainers[i];
73 
74  mapbox::util::apply_visitor([&](auto&& value)
75  {
76  if (value.size() != outputBinding.second.GetNumElements())
77  {
78  throw armnn::Exception("Output tensor has incorrect size");
79  }
80 
81  armnn::Tensor outputTensor(outputBinding.second, value.data());
82  outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor));
83  },
84  outputData);
85  }
86 
87  return outputTensors;
88 }
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:306
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:341
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
Definition: Tensor.hpp:261
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46

◆ NonNegative()

uint32_t NonNegative ( const char *  expr,
int32_t  value,
const CheckLocation location 
)

Definition at line 35 of file VerificationHelpers.cpp.

References CheckLocation::AsString().

38 {
39  if (value < 0)
40  {
41  throw ParseException(fmt::format("'{}' must be non-negative, received: {} at {}",
42  expr,
43  value,
44  location.AsString()));
45  }
46  else
47  {
48  return static_cast<uint32_t>(value);
49  }
50 }
std::string AsString() const
Definition: Exceptions.hpp:29

◆ operator==() [1/2]

bool operator== ( const armnn::DataLayout dataLayout,
const DataLayoutIndexed indexed 
)

Equality methods.

Definition at line 34 of file DataLayoutIndexed.cpp.

References DataLayoutIndexed::GetDataLayout().

Referenced by BOOST_AUTO_TEST_CASE(), OutputSlot::Disconnect(), and DataLayoutIndexed::GetIndex().

35 {
36  return dataLayout == indexed.GetDataLayout();
37 }
armnn::DataLayout GetDataLayout() const

◆ operator==() [2/2]

bool operator== ( const DataLayoutIndexed indexed,
const armnn::DataLayout dataLayout 
)

Definition at line 39 of file DataLayoutIndexed.cpp.

References DataLayoutIndexed::GetDataLayout().

40 {
41  return indexed.GetDataLayout() == dataLayout;
42 }
armnn::DataLayout GetDataLayout() const

◆ Permute()

void Permute ( const armnn::TensorShape dstShape,
const armnn::PermutationVector mappings,
const void *  src,
void *  dst,
size_t  dataTypeSize 
)

◆ Permuted() [1/2]

armnn::TensorShape Permuted ( const armnn::TensorShape srcShape,
const armnn::PermutationVector mappings 
)

Definition at line 98 of file Permute.cpp.

References TensorShape::GetNumDimensions(), PermutationVector::GetSize(), and armnn::MaxNumOfTensorDimensions.

Referenced by BOOST_AUTO_TEST_CASE(), armnn::ConvertWeightTensorInfoFromArmnnToAcl(), PermuteLayer::InferOutputShapes(), Permuted(), PermuteInputsForConcat(), armnn::PermuteTensor(), PermuteTensorData(), PermuteTensorNchwToNhwc(), PermuteTensorNhwcToNchw(), and MovePermuteUpImpl::Run().

100 {
101  assert(srcShape.GetNumDimensions() == mappings.GetSize());
102 
103  const unsigned int numDims = mappings.GetSize();
104  unsigned int outDims[armnn::MaxNumOfTensorDimensions];
105 
106  for (unsigned int i = 0U; i < numDims; ++i)
107  {
108  outDims[mappings[i]] = srcShape[i];
109  }
110 
111  armnn::TensorShape permutedShape(numDims, outDims);
112  return permutedShape;
113 }
SizeType GetSize() const
Definition: Types.hpp:274
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:19

◆ Permuted() [2/2]

armnn::TensorInfo Permuted ( const armnn::TensorInfo info,
const armnn::PermutationVector mappings 
)

Definition at line 115 of file Permute.cpp.

References TensorInfo::GetQuantizationDim(), TensorInfo::GetShape(), OptionalBase::has_value(), Permuted(), TensorInfo::SetQuantizationDim(), TensorInfo::SetShape(), and OptionalReferenceSwitch< IsReference, T >::value().

117 {
118  armnn::TensorInfo outInfo(info);
119  outInfo.SetShape(Permuted(info.GetShape(), mappings));
120 
121  // If TensorInfo has Per-Axis Quantization then it also has a QuantizationDim which needs to
122  // be permuted according to the mapping
123  if (info.GetQuantizationDim().has_value())
124  {
125  outInfo.SetQuantizationDim(mappings[info.GetQuantizationDim().value()]);
126  }
127 
128  return outInfo;
129 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
Optional< unsigned int > GetQuantizationDim() const
Definition: Tensor.cpp:485
bool has_value() const noexcept
Definition: Optional.hpp:53
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98

◆ ProcessConcatInputTensorInfo()

void ProcessConcatInputTensorInfo ( armnn::TensorInfo inputTensorInfo,
armnn::OriginsDescriptor concatDescriptor,
const unsigned int &  concatAxis,
unsigned int  inputIndex,
unsigned int &  mergeDimOrigin 
)

Definition at line 19 of file ParserHelper.cpp.

References CHECK_LOCATION, OriginsDescriptor::GetNumDimensions(), TensorInfo::GetNumDimensions(), TensorInfo::GetShape(), and OriginsDescriptor::SetViewOriginCoord().

Referenced by TfLiteParserImpl::OutputShapeOfReshape().

24 {
25  const uint32_t inputRank = concatDescriptor.GetNumDimensions();
26 
27  // double check dimensions of the tensors
28  if (inputTensorInfo.GetNumDimensions() != inputRank)
29  {
30  throw armnn::ParseException(fmt::format(
31  "The number of dimensions: {0} for input tensors of the "
32  "concatenation op should be {1} {2}",
33  inputTensorInfo.GetNumDimensions(),
34  inputRank,
35  CHECK_LOCATION().AsString()));
36  }
37 
38  for (unsigned int j = 0; j < concatAxis; ++j)
39  {
40  concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
41  }
42 
43  concatDescriptor.SetViewOriginCoord(inputIndex, concatAxis, mergeDimOrigin);
44  mergeDimOrigin += inputTensorInfo.GetShape()[concatAxis];
45 
46  for (unsigned int j = concatAxis + 1; j < inputRank; ++j)
47  {
48  concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
49  }
50 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
uint32_t GetNumDimensions() const
Get the number of dimensions.
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191
Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value)
Set the view origin coordinates.

◆ QuantizedVector() [1/3]

std::vector<T> armnnUtils::QuantizedVector ( FloatIt  first,
FloatIt  last,
float  qScale,
int32_t  qOffset 
)

Definition at line 105 of file QuantizeHelper.hpp.

106 {
107  std::vector<T> quantized;
108  quantized.reserve(armnn::numeric_cast<size_t>(std::distance(first, last)));
109 
110  for (auto it = first; it != last; ++it)
111  {
112  auto f = *it;
113  T q = SelectiveQuantize<T>(f, qScale, qOffset);
114  quantized.push_back(q);
115  }
116 
117  return quantized;
118 }

◆ QuantizedVector() [2/3]

std::vector<T> armnnUtils::QuantizedVector ( const std::vector< float > &  array,
float  qScale = 1.f,
int32_t  qOffset = 0 
)

Definition at line 121 of file QuantizeHelper.hpp.

122 {
123  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
124 }

◆ QuantizedVector() [3/3]

std::vector<T> armnnUtils::QuantizedVector ( std::initializer_list< float >  array,
float  qScale = 1.f,
int32_t  qOffset = 0 
)

Definition at line 127 of file QuantizeHelper.hpp.

128 {
129  return QuantizedVector<T>(array.begin(), array.end(), qScale, qOffset);
130 }

◆ SelectiveDequantize()

float armnnUtils::SelectiveDequantize ( value,
float  scale,
int32_t  offset 
)

Definition at line 91 of file QuantizeHelper.hpp.

References SelectiveQuantizer< T, DoQuantize >::Dequantize().

Referenced by ApplyBias().

92 {
93  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Dequantize(value, scale, offset);
94 };
float Dequantize(QuantizedType value, float scale, int32_t offset)
Dequantize an 8-bit data type into a floating point data type.
Definition: TypesUtils.cpp:46

◆ SelectiveQuantize()

T armnnUtils::SelectiveQuantize ( float  value,
float  scale,
int32_t  offset 
)

Definition at line 85 of file QuantizeHelper.hpp.

References SelectiveQuantizer< T, DoQuantize >::Quantize().

86 {
87  return SelectiveQuantizer<T, armnn::IsQuantizedType<T>()>::Quantize(value, scale, offset);
88 };
QuantizedType Quantize(float value, float scale, int32_t offset)
Quantize a floating point data type into an 8-bit data type.
Definition: TypesUtils.cpp:30

◆ SplitBy()

std::vector< std::string > SplitBy ( const std::string &  originalString,
const std::string &  delimiter = " ",
bool  includeEmptyToken = false 
)

Split a string into tokens by a delimiter.

Parameters
[in]originalStringOriginal string to be split
[in]delimiterDelimiter used to split originalString
[in]includeEmptyToeknIf true, include empty tokens in the result
Returns
A vector of tokens split from originalString by

Definition at line 40 of file ModelAccuracyChecker.cpp.

Referenced by LoadModelOutputLabels(), and main().

41 {
42  std::vector<std::string> tokens;
43  size_t cur = 0;
44  size_t next = 0;
45  while ((next = originalString.find(delimiter, cur)) != std::string::npos)
46  {
47  // Skip empty tokens, unless explicitly stated to include them.
48  if (next - cur > 0 || includeEmptyToken)
49  {
50  tokens.push_back(originalString.substr(cur, next - cur));
51  }
52  cur = next + delimiter.size();
53  }
54  // Get the remaining token
55  // Skip empty tokens, unless explicitly stated to include them.
56  if (originalString.size() - cur > 0 || includeEmptyToken)
57  {
58  tokens.push_back(originalString.substr(cur, originalString.size() - cur));
59  }
60  return tokens;
61 }

◆ Strip()

std::string Strip ( const std::string &  originalString,
const std::string &  characterSet = " " 
)

Remove any preceding and trailing character specified in the characterSet.

Parameters
[in]originalStringOriginal string to be stripped
[in]characterSetSet of characters to be stripped from originalString
Returns
A string stripped of all characters specified in characterSet from originalString

Definition at line 64 of file ModelAccuracyChecker.cpp.

References ARMNN_ASSERT.

Referenced by LoadModelOutputLabels().

65 {
66  ARMNN_ASSERT(!characterSet.empty());
67  const std::size_t firstFound = originalString.find_first_not_of(characterSet);
68  const std::size_t lastFound = originalString.find_last_not_of(characterSet);
69  // Return empty if the originalString is empty or the originalString contains only to-be-striped characters
70  if (firstFound == std::string::npos || lastFound == std::string::npos)
71  {
72  return "";
73  }
74  return originalString.substr(firstFound, lastFound + 1 - firstFound);
75 }
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14

◆ Transpose()

void Transpose ( const armnn::TensorShape dstShape,
const armnn::PermutationVector mappings,
const void *  src,
void *  dst,
size_t  dataTypeSize 
)

Definition at line 120 of file Transpose.cpp.

Referenced by TransposeLayer::TransposeLayer().

122 {
123  TransposeLoop(srcShape, mappings).Unroll(src, dst, dataTypeSize);
124 }

◆ TransposeTensorShape() [1/2]

armnn::TensorShape TransposeTensorShape ( const armnn::TensorShape srcShape,
const armnn::PermutationVector mappings 
)

Definition at line 98 of file Transpose.cpp.

References TensorShape::GetNumDimensions(), PermutationVector::GetSize(), and armnn::MaxNumOfTensorDimensions.

Referenced by TransposeLayer::InferOutputShapes(), MoveTransposeUpImpl::Run(), and TransposeTensorShape().

99 {
100  assert(srcShape.GetNumDimensions() == mappings.GetSize());
101 
102  const unsigned int numDims = mappings.GetSize();
103  unsigned int outDims[armnn::MaxNumOfTensorDimensions];
104 
105  for (unsigned int i = 0U; i < numDims; ++i)
106  {
107  outDims[i] = srcShape[mappings[i]];
108  }
109  armnn::TensorShape permutedShape(numDims, outDims);
110  return permutedShape;
111 }
SizeType GetSize() const
Definition: Types.hpp:274
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:19

◆ TransposeTensorShape() [2/2]

armnn::TensorInfo TransposeTensorShape ( const armnn::TensorInfo info,
const armnn::PermutationVector mappings 
)

Definition at line 113 of file Transpose.cpp.

References TensorInfo::GetShape(), TensorInfo::SetShape(), and TransposeTensorShape().

114 {
115  armnn::TensorInfo outInfo(info);
116  outInfo.SetShape(TransposeTensorShape(info.GetShape(), mappings));
117  return outInfo;
118 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
armnn::TensorShape TransposeTensorShape(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Transpose.cpp:98

◆ VerifyInt32()

int32_t VerifyInt32 ( const char *  expr,
int64_t  value,
const armnn::CheckLocation location 
)

Definition at line 52 of file VerificationHelpers.cpp.

References CheckLocation::AsString().

55 {
56  if (value < std::numeric_limits<int>::min() || value > std::numeric_limits<int>::max())
57  {
58  throw ParseException(fmt::format("'{}' must should fit into a int32 (ArmNN don't support int64),"
59  " received: {} at {}",
60  expr,
61  value,
62  location.AsString()));
63  }
64  else
65  {
66  return static_cast<int32_t>(value);
67  }
68 }
std::string AsString() const
Definition: Exceptions.hpp:29

◆ within_percentage_tolerance()

bool armnnUtils::within_percentage_tolerance ( float  a,
float  b,
float  tolerancePercent = 1.0f 
)
inline

Compare two floats and return true if their values are within a specified tolerance of each other.

Parameters
a
b
tolerancePercentageIf not supplied default will be 1% tolerance (1.0f)
Returns
true if the value of float b is within tolerancePercentage of the value for float a.

Definition at line 20 of file FloatingPointComparison.hpp.

Referenced by BOOST_AUTO_TEST_CASE(), SelectiveComparer< T, false >::Compare(), and YoloTestCase< Model >::ProcessResult().

21 {
22  float toleranceValue = std::fabs(a * (tolerancePercent / 100));
23  return std::fabs(a - b) <= toleranceValue;
24 }

Variable Documentation

◆ ArmNNToNHWC

const armnn::PermutationVector ArmNNToNHWC = { 0, 3, 1, 2 }

Definition at line 17 of file ParserHelper.cpp.

◆ NHWCToArmNN

const armnn::PermutationVector NHWCToArmNN = { 0, 2, 3, 1 }

Definition at line 16 of file ParserHelper.cpp.

Referenced by PrepareImageTensor< float >().