blob: 5da0228842c7972536f7588ebaa983449db4a98c [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>
narpra01b9546cf2018-11-20 15:21:28 +000011
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010012#include <backendsCommon/CpuTensorHandle.hpp>
13#include <backendsCommon/BackendRegistry.hpp>
narpra01b9546cf2018-11-20 15:21:28 +000014
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010015#include <algorithm>
16
17// Connects two layers
18void Connect(armnn::IConnectableLayer* from, armnn::IConnectableLayer* to, const armnn::TensorInfo& tensorInfo,
19 unsigned int fromIndex = 0, unsigned int toIndex = 0);
20
21// Checks that two collections have the exact same contents (in any order)
22// The given collections do not have to contain duplicates
23// Cannot use std::sort here because std lists have their own std::list::sort method
24template <typename CollectionType>
25bool AreEqual(const CollectionType& lhs, const CollectionType& rhs)
narpra01b9546cf2018-11-20 15:21:28 +000026{
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010027 if (lhs.size() != rhs.size())
28 {
29 return false;
30 }
narpra01b9546cf2018-11-20 15:21:28 +000031
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010032 auto lhs_it = std::find_if(lhs.begin(), lhs.end(), [&rhs](auto& item)
33 {
34 return std::find(rhs.begin(), rhs.end(), item) == rhs.end();
35 });
36
37 return lhs_it == lhs.end();
narpra01b9546cf2018-11-20 15:21:28 +000038}
39
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010040// Checks that the given collection contains the specified item
41template <typename CollectionType>
42bool Contains(const CollectionType& collection, const typename CollectionType::value_type& item)
43{
44 return std::find(collection.begin(), collection.end(), item) != collection.end();
narpra01b9546cf2018-11-20 15:21:28 +000045}
Matteo Martincighf02e6cd2019-05-17 12:15:30 +010046
47// Checks that the given map contains the specified key
48template <typename MapType>
49bool Contains(const MapType& map, const typename MapType::key_type& key)
50{
51 return map.find(key) != map.end();
52}
53
54template <typename ConvolutionLayer>
55void SetWeightAndBias(ConvolutionLayer* layer, const armnn::TensorInfo& weightInfo, const armnn::TensorInfo& biasInfo)
56{
57 layer->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(weightInfo);
58 layer->m_Bias = std::make_unique<armnn::ScopedCpuTensorHandle>(biasInfo);
59
60 layer->m_Weight->Allocate();
61 layer->m_Bias->Allocate();
62}
63
64armnn::SubgraphView::InputSlots CreateInputsFrom(const std::vector<armnn::Layer*>& layers);
65
66armnn::SubgraphView::OutputSlots CreateOutputsFrom(const std::vector<armnn::Layer*>& layers);
67
68armnn::SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom(armnn::SubgraphView::InputSlots&& inputs,
69 armnn::SubgraphView::OutputSlots&& outputs,
70 armnn::SubgraphView::Layers&& layers);
71
72armnn::IBackendInternalUniquePtr CreateBackendObject(const armnn::BackendId& backendId);