ArmNN
 25.11
Loading...
Searching...
No Matches
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()

template<typename CollectionType>
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()

template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
bool Compare ( T a,
T 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]

template<typename CollectionType>
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]

template<typename MapType>
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}
BackendRegistry & BackendRegistryInstance()

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}
std::vector< InputSlot >::iterator EndInputSlots()
Definition Layer.hpp:263
std::vector< InputSlot >::iterator BeginInputSlots()
Definition Layer.hpp:262
std::vector< InputSlot * > InputSlots

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}
std::vector< OutputSlot * > OutputSlots

◆ 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 {
91 return TensorShape{ batches, channels, height, width };
93 return TensorShape{ batches, height, width, channels };
94 default:
95 throw InvalidArgumentException(std::string("Unsupported data layout: ") + GetDataLayoutName(layout));
96 }
97}
Copyright (c) 2021 ARM Limited and Contributors.
constexpr const char * GetDataLayoutName(DataLayout dataLayout)

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