ArmNN
 23.08
CommonTestUtils.hpp File Reference
#include "TestUtils.hpp"
#include <Graph.hpp>
#include <ResolveType.hpp>
#include <SubgraphViewSelector.hpp>
#include <armnn/BackendRegistry.hpp>
#include <armnn/Types.hpp>
#include <armnn/backends/SubgraphView.hpp>
#include <armnn/backends/TensorHandle.hpp>
#include <algorithm>
#include <random>
#include <vector>
Include dependency graph for CommonTestUtils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename CollectionType >
bool AreEqual (const CollectionType &lhs, const CollectionType &rhs)
 
template<typename CollectionType >
bool Contains (const CollectionType &collection, const typename CollectionType::value_type &item)
 
template<typename MapType >
bool Contains (const MapType &map, const typename MapType::key_type &key)
 
template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
bool Compare (T a, T b, float tolerance=0.000001f)
 
armnn::SubgraphView::InputSlots CreateInputsFrom (armnn::Layer *layer, std::vector< unsigned int > ignoreSlots={})
 
armnn::SubgraphView::InputSlots CreateInputsFrom (const std::vector< armnn::Layer * > &layers, std::vector< unsigned int > ignoreSlots={})
 
armnn::SubgraphView::OutputSlots CreateOutputsFrom (const std::vector< armnn::Layer * > &layers)
 
armnn::SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom (armnn::SubgraphView::InputSlots &&inputs, armnn::SubgraphView::OutputSlots &&outputs, armnn::SubgraphView::Layers &&layers)
 
armnn::IBackendInternalUniquePtr CreateBackendObject (const armnn::BackendId &backendId)
 
armnn::TensorShape MakeTensorShape (unsigned int batches, unsigned int channels, unsigned int height, unsigned int width, armnn::DataLayout layout)
 

Function Documentation

◆ AreEqual()

bool AreEqual ( const CollectionType &  lhs,
const CollectionType &  rhs 
)

Definition at line 27 of file CommonTestUtils.hpp.

28 {
29  if (lhs.size() != rhs.size())
30  {
31  return false;
32  }
33 
34  auto lhs_it = std::find_if(lhs.begin(), lhs.end(), [&rhs](auto& item)
35  {
36  return std::find(rhs.begin(), rhs.end(), item) == rhs.end();
37  });
38 
39  return lhs_it == lhs.end();
40 }

◆ Compare()

bool Compare ( a,
b,
float  tolerance = 0.000001f 
)
inline

Definition at line 58 of file CommonTestUtils.hpp.

59 {
60  if (ArmnnType == armnn::DataType::Boolean)
61  {
62  // NOTE: Boolean is represented as uint8_t (with zero equals
63  // false and everything else equals true), therefore values
64  // need to be casted to bool before comparing them
65  return static_cast<bool>(a) == static_cast<bool>(b);
66  }
67 
68  // NOTE: All other types can be cast to float and compared with
69  // a certain level of tolerance
70  return std::fabs(static_cast<float>(a) - static_cast<float>(b)) <= tolerance;
71 }

References armnn::Boolean.

◆ Contains() [1/2]

bool Contains ( const CollectionType &  collection,
const typename CollectionType::value_type &  item 
)

Definition at line 44 of file CommonTestUtils.hpp.

45 {
46  return std::find(collection.begin(), collection.end(), item) != collection.end();
47 }

◆ Contains() [2/2]

bool Contains ( const MapType &  map,
const typename MapType::key_type &  key 
)

Definition at line 51 of file CommonTestUtils.hpp.

52 {
53  return map.find(key) != map.end();
54 }

◆ CreateBackendObject()

armnn::IBackendInternalUniquePtr CreateBackendObject ( const armnn::BackendId backendId)

Definition at line 72 of file CommonTestUtils.cpp.

73 {
74  auto& backendRegistry = BackendRegistryInstance();
75  auto backendFactory = backendRegistry.GetFactory(backendId);
76  auto backendObjPtr = backendFactory();
77 
78  return backendObjPtr;
79 }

References armnn::BackendRegistryInstance().

◆ CreateInputsFrom() [1/2]

armnn::SubgraphView::InputSlots CreateInputsFrom ( armnn::Layer layer,
std::vector< unsigned int >  ignoreSlots = {} 
)

Definition at line 12 of file CommonTestUtils.cpp.

14 {
16  for (auto&& it = layer->BeginInputSlots(); it != layer->EndInputSlots(); ++it)
17  {
18  if (std::find(ignoreSlots.begin(), ignoreSlots.end(), it->GetSlotIndex()) != ignoreSlots.end())
19  {
20  continue;
21  }
22  else
23  {
24  result.push_back(&(*it));
25  }
26  }
27  return result;
28 }

References Layer::BeginInputSlots(), and Layer::EndInputSlots().

◆ CreateInputsFrom() [2/2]

armnn::SubgraphView::InputSlots CreateInputsFrom ( const std::vector< armnn::Layer * > &  layers,
std::vector< unsigned int >  ignoreSlots = {} 
)

Definition at line 31 of file CommonTestUtils.cpp.

33 {
35  for (auto&& layer: layers)
36  {
37  for (auto&& it = layer->BeginInputSlots(); it != layer->EndInputSlots(); ++it)
38  {
39  if (std::find(ignoreSlots.begin(), ignoreSlots.end(), it->GetSlotIndex()) != ignoreSlots.end())
40  {
41  continue;
42  }
43  else
44  {
45  result.push_back(&(*it));
46  }
47  }
48  }
49  return result;
50 }

◆ CreateOutputsFrom()

armnn::SubgraphView::OutputSlots CreateOutputsFrom ( const std::vector< armnn::Layer * > &  layers)

Definition at line 52 of file CommonTestUtils.cpp.

53 {
55  for (auto && layer : layers)
56  {
57  for (auto&& it = layer->BeginOutputSlots(); it != layer->EndOutputSlots(); ++it)
58  {
59  result.push_back(&(*it));
60  }
61  }
62  return result;
63 }

◆ CreateSubgraphViewFrom()

Definition at line 65 of file CommonTestUtils.cpp.

68 {
69  return std::make_unique<SubgraphView>(std::move(inputs), std::move(outputs), std::move(layers));
70 }

◆ MakeTensorShape()

armnn::TensorShape MakeTensorShape ( unsigned int  batches,
unsigned int  channels,
unsigned int  height,
unsigned int  width,
armnn::DataLayout  layout 
)

Definition at line 81 of file CommonTestUtils.cpp.

86 {
87  using namespace armnn;
88  switch (layout)
89  {
90  case DataLayout::NCHW:
91  return TensorShape{ batches, channels, height, width };
92  case DataLayout::NHWC:
93  return TensorShape{ batches, height, width, channels };
94  default:
95  throw InvalidArgumentException(std::string("Unsupported data layout: ") + GetDataLayoutName(layout));
96  }
97 }

References armnn::GetDataLayoutName(), armnn::NCHW, and armnn::NHWC.

armnn::DataType::Boolean
@ Boolean
armnn::DataLayout::NHWC
@ NHWC
armnn::GetDataLayoutName
constexpr const char * GetDataLayoutName(DataLayout dataLayout)
Definition: TypesUtils.hpp:243
armnn::SubgraphView::InputSlots
std::vector< InputSlot * > InputSlots
Definition: SubgraphView.hpp:57
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::BackendRegistryInstance
BackendRegistry & BackendRegistryInstance()
Definition: BackendRegistry.cpp:15
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::Layer::EndInputSlots
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:263
armnn::Layer::BeginInputSlots
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:262
armnn::SubgraphView::OutputSlots
std::vector< OutputSlot * > OutputSlots
Definition: SubgraphView.hpp:59
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::DataLayout::NCHW
@ NCHW