ArmNN
 21.02
SubgraphView.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "SubgraphView.hpp"
7 #include "Graph.hpp"
8 
12 
13 #include <utility>
14 
15 namespace armnn
16 {
17 
18 namespace
19 {
20 
21 template <class C>
22 void AssertIfNullsOrDuplicates(const C& container, const std::string& errorMessage)
23 {
24  using T = typename C::value_type;
25  std::unordered_set<T> duplicateSet;
26  std::for_each(container.begin(), container.end(), [&duplicateSet, &errorMessage](const T& i)
27  {
28  // Ignore unused for release builds
29  IgnoreUnused(errorMessage);
30 
31  // Check if the item is valid
32  ARMNN_ASSERT_MSG(i, errorMessage.c_str());
33 
34  // Check if a duplicate has been found
35  ARMNN_ASSERT_MSG(duplicateSet.find(i) == duplicateSet.end(), errorMessage.c_str());
36 
37  duplicateSet.insert(i);
38  });
39 }
40 
41 } // anonymous namespace
42 
44  : m_InputSlots{}
45  , m_OutputSlots{}
46  , m_Layers(graph.begin(), graph.end())
47 {
48  ArrangeBySortOrder();
49  CheckSubgraph();
50 }
51 
53  : m_InputSlots{inputs}
54  , m_OutputSlots{outputs}
55  , m_Layers{layers}
56 {
57  ArrangeBySortOrder();
58  CheckSubgraph();
59 }
60 
62  : m_InputSlots(subgraph.m_InputSlots.begin(), subgraph.m_InputSlots.end())
63  , m_OutputSlots(subgraph.m_OutputSlots.begin(), subgraph.m_OutputSlots.end())
64  , m_Layers(subgraph.m_Layers.begin(), subgraph.m_Layers.end())
65 {
66  ArrangeBySortOrder();
67  CheckSubgraph();
68 }
69 
71  : m_InputSlots(std::move(subgraph.m_InputSlots))
72  , m_OutputSlots(std::move(subgraph.m_OutputSlots))
73  , m_Layers(std::move(subgraph.m_Layers))
74 {
75  ArrangeBySortOrder();
76  CheckSubgraph();
77 }
78 
80  : m_InputSlots{}
81  , m_OutputSlots{}
82  , m_Layers{PolymorphicDowncast<Layer*>(layer)}
83 {
84  unsigned int numInputSlots = layer->GetNumInputSlots();
85  m_InputSlots.resize(numInputSlots);
86  for (unsigned int i = 0; i < numInputSlots; i++)
87  {
88  m_InputSlots.at(i) = PolymorphicDowncast<InputSlot*>(&(layer->GetInputSlot(i)));
89  }
90 
91  unsigned int numOutputSlots = layer->GetNumOutputSlots();
92  m_OutputSlots.resize(numOutputSlots);
93  for (unsigned int i = 0; i < numOutputSlots; i++)
94  {
95  m_OutputSlots.at(i) = PolymorphicDowncast<OutputSlot*>(&(layer->GetOutputSlot(i)));
96  }
97 
98  CheckSubgraph();
99 }
100 
102 {
103  m_InputSlots = std::move(other.m_InputSlots);
104  m_OutputSlots = std::move(other.m_OutputSlots);
105  m_Layers = std::move(other.m_Layers);
106 
107  CheckSubgraph();
108 
109  return *this;
110 }
111 
112 void SubgraphView::CheckSubgraph()
113 {
114  // Check for invalid or duplicate input slots
115  AssertIfNullsOrDuplicates(m_InputSlots, "Sub-graphs cannot contain null or duplicate input slots");
116 
117  // Check for invalid or duplicate output slots
118  AssertIfNullsOrDuplicates(m_OutputSlots, "Sub-graphs cannot contain null or duplicate output slots");
119 
120  // Check for invalid or duplicate layers
121  AssertIfNullsOrDuplicates(m_Layers, "Sub-graphs cannot contain null or duplicate layers");
122 }
123 
125 {
126  return m_InputSlots;
127 }
128 
130 {
131  return m_OutputSlots;
132 }
133 
134 const InputSlot* SubgraphView::GetInputSlot(unsigned int index) const
135 {
136  return m_InputSlots.at(index);
137 }
138 
140 {
141  return m_InputSlots.at(index);
142 }
143 
144 const OutputSlot* SubgraphView::GetOutputSlot(unsigned int index) const
145 {
146  return m_OutputSlots.at(index);
147 }
148 
150 {
151  return m_OutputSlots.at(index);
152 }
153 
154 unsigned int SubgraphView::GetNumInputSlots() const
155 {
156  return armnn::numeric_cast<unsigned int>(m_InputSlots.size());
157 }
158 
160 {
161  return armnn::numeric_cast<unsigned int>(m_OutputSlots.size());
162 }
163 
165 {
166  return m_Layers;
167 }
168 
170 {
171  return m_Layers.begin();
172 }
173 
175 {
176  return m_Layers.end();
177 }
178 
180 {
181  return m_Layers.begin();
182 }
183 
185 {
186  return m_Layers.end();
187 }
188 
190 {
191  return begin();
192 }
193 
195 {
196  return end();
197 }
198 
200 {
201  m_InputSlots.clear();
202  m_OutputSlots.clear();
203  m_Layers.clear();
204 }
205 
206 void SubgraphView::ArrangeBySortOrder()
207 {
208  using LayerList = std::list<Layer*>;
209  auto compareLayerPriority = [](const LayerList::value_type& layerA, const LayerList::value_type& layerB)
210  {
211  return layerA->GetPriority() < layerB->GetPriority();
212  };
213 
214  m_Layers.sort(compareLayerPriority);
215 }
216 
217 } // namespace armnn
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
Layers::iterator Iterator
SubgraphView & operator=(SubgraphView &&other)
Move-assignment operator.
std::vector< OutputSlot * > OutputSlots
unsigned int GetNumInputSlots() const
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
The SubgraphView class represents a subgraph of a Graph.
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
const OutputSlot * GetOutputSlot(unsigned int index) const
std::vector< InputSlot * > InputSlots
SubgraphView(Graph &graph)
Constructs a sub-graph from the entire given graph.
const InputSlots & GetInputSlots() const
ConstIterator cend() const
const InputSlot * GetInputSlot(unsigned int index) const
const OutputSlots & GetOutputSlots() const
const Layers & GetLayers() const
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
ConstIterator cbegin() const
std::list< Layer * > Layers
Layers::const_iterator ConstIterator
unsigned int GetNumOutputSlots() const