blob: c7537f1eed88266f1d48c07af2981950c9e0bdea [file] [log] [blame]
narpra01b9546cf2018-11-20 15:21:28 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
Matteo Martincighf02e6cd2019-05-17 12:15:30 +01005
narpra01b9546cf2018-11-20 15:21:28 +00006#pragma once
7
8#include <Graph.hpp>
Matteo Martincighf02e6cd2019-05-17 12:15:30 +01009#include <SubgraphView.hpp>
10#include <SubgraphViewSelector.hpp>
Keith Davis3674f142020-08-16 23:44:15 +010011#include <ResolveType.hpp>
narpra01b9546cf2018-11-20 15:21:28 +000012
Matteo Martincighc601aa62019-10-29 15:03:22 +000013#include <armnn/BackendRegistry.hpp>
14
Keith Davis3674f142020-08-16 23:44:15 +010015#include <armnn/Types.hpp>
James Conroy1f58f032021-04-27 17:13:27 +010016#include <backendsCommon/TensorHandle.hpp>
narpra01b9546cf2018-11-20 15:21:28 +000017
Matteo Martincighbf0e7222019-06-20 17:17:45 +010018#include <test/TestUtils.hpp>
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010019
Matteo Martincighbf0e7222019-06-20 17:17:45 +010020#include <algorithm>
Mike Kelly0d677db2021-06-27 22:39:21 +010021#include <random>
22#include <vector>
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010023
24// Checks that two collections have the exact same contents (in any order)
25// The given collections do not have to contain duplicates
26// Cannot use std::sort here because std lists have their own std::list::sort method
27template <typename CollectionType>
28bool AreEqual(const CollectionType& lhs, const CollectionType& rhs)
narpra01b9546cf2018-11-20 15:21:28 +000029{
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010030 if (lhs.size() != rhs.size())
31 {
32 return false;
33 }
narpra01b9546cf2018-11-20 15:21:28 +000034
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010035 auto lhs_it = std::find_if(lhs.begin(), lhs.end(), [&rhs](auto& item)
36 {
37 return std::find(rhs.begin(), rhs.end(), item) == rhs.end();
38 });
39
40 return lhs_it == lhs.end();
narpra01b9546cf2018-11-20 15:21:28 +000041}
42
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010043// Checks that the given collection contains the specified item
44template <typename CollectionType>
45bool Contains(const CollectionType& collection, const typename CollectionType::value_type& item)
46{
47 return std::find(collection.begin(), collection.end(), item) != collection.end();
narpra01b9546cf2018-11-20 15:21:28 +000048}
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010049
50// Checks that the given map contains the specified key
51template <typename MapType>
52bool Contains(const MapType& map, const typename MapType::key_type& key)
53{
54 return map.find(key) != map.end();
55}
56
Keith Davis3674f142020-08-16 23:44:15 +010057// Utility template for comparing tensor elements
58template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
59bool Compare(T a, T b, float tolerance = 0.000001f)
60{
61 if (ArmnnType == armnn::DataType::Boolean)
62 {
63 // NOTE: Boolean is represented as uint8_t (with zero equals
64 // false and everything else equals true), therefore values
65 // need to be casted to bool before comparing them
66 return static_cast<bool>(a) == static_cast<bool>(b);
67 }
68
69 // NOTE: All other types can be cast to float and compared with
70 // a certain level of tolerance
71 return std::fabs(static_cast<float>(a) - static_cast<float>(b)) <= tolerance;
72}
73
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010074template <typename ConvolutionLayer>
75void SetWeightAndBias(ConvolutionLayer* layer, const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& biasInfo)
76{
James Conroy1f58f032021-04-27 17:13:27 +010077 layer->m_Weight = std::make_unique<armnn::ScopedTensorHandle>(weightInfo);
78 layer->m_Bias = std::make_unique<armnn::ScopedTensorHandle>(biasInfo);
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010079
80 layer->m_Weight->Allocate();
81 layer->m_Bias->Allocate();
82}
83
84armnn::SubgraphView::InputSlots CreateInputsFrom(const std::vector<armnn::Layer*>& layers);
85
86armnn::SubgraphView::OutputSlots CreateOutputsFrom(const std::vector<armnn::Layer*>& layers);
87
88armnn::SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom(armnn::SubgraphView::InputSlots&& inputs,
89 armnn::SubgraphView::OutputSlots&& outputs,
90 armnn::SubgraphView::Layers&& layers);
91
92armnn::IBackendInternalUniquePtr CreateBackendObject(const armnn::BackendId& backendId);
Aron Virginas-Tar735a4502019-06-26 15:02:47 +010093
94armnn::TensorShape MakeTensorShape(unsigned int batches,
95 unsigned int channels,
96 unsigned int height,
97 unsigned int width,
Matteo Martincighc601aa62019-10-29 15:03:22 +000098 armnn::DataLayout layout);
Mike Kelly0d677db2021-06-27 22:39:21 +010099
100template<typename DataType>
101static std::vector<DataType> GenerateRandomData(size_t size)
102{
103 constexpr bool isIntegerType = std::is_integral<DataType>::value;
104 using Distribution =
105 typename std::conditional<isIntegerType,
106 std::uniform_int_distribution<DataType>,
107 std::uniform_real_distribution<DataType>>::type;
108
109 static constexpr DataType lowerLimit = std::numeric_limits<DataType>::min();
110 static constexpr DataType upperLimit = std::numeric_limits<DataType>::max();
111
112 static Distribution distribution(lowerLimit, upperLimit);
113 static std::default_random_engine generator;
114
115 std::vector<DataType> randomData(size);
116 generate(randomData.begin(), randomData.end(), []() { return distribution(generator); });
117
118 return randomData;
119}