ArmNN
 20.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 
10 
11 #include <boost/numeric/conversion/cast.hpp>
12 #include <utility>
13 
14 namespace armnn
15 {
16 
17 namespace
18 {
19 
20 template <class C>
21 void AssertIfNullsOrDuplicates(const C& container, const std::string& errorMessage)
22 {
23  using T = typename C::value_type;
24  std::unordered_set<T> duplicateSet;
25  std::for_each(container.begin(), container.end(), [&duplicateSet, &errorMessage](const T& i)
26  {
27  // Ignore unused for release builds
28  IgnoreUnused(errorMessage);
29 
30  // Check if the item is valid
31  BOOST_ASSERT_MSG(i, errorMessage.c_str());
32 
33  // Check if a duplicate has been found
34  BOOST_ASSERT_MSG(duplicateSet.find(i) == duplicateSet.end(), errorMessage.c_str());
35 
36  duplicateSet.insert(i);
37  });
38 }
39 
40 } // anonymous namespace
41 
43  : m_InputSlots{}
44  , m_OutputSlots{}
45  , m_Layers(graph.begin(), graph.end())
46 {
47  CheckSubgraph();
48 }
49 
51  : m_InputSlots{inputs}
52  , m_OutputSlots{outputs}
53  , m_Layers{layers}
54 {
55  CheckSubgraph();
56 }
57 
59  : m_InputSlots(subgraph.m_InputSlots.begin(), subgraph.m_InputSlots.end())
60  , m_OutputSlots(subgraph.m_OutputSlots.begin(), subgraph.m_OutputSlots.end())
61  , m_Layers(subgraph.m_Layers.begin(), subgraph.m_Layers.end())
62 {
63  CheckSubgraph();
64 }
65 
67  : m_InputSlots(std::move(subgraph.m_InputSlots))
68  , m_OutputSlots(std::move(subgraph.m_OutputSlots))
69  , m_Layers(std::move(subgraph.m_Layers))
70 {
71  CheckSubgraph();
72 }
73 
75  : m_InputSlots{}
76  , m_OutputSlots{}
77  , m_Layers{boost::polymorphic_downcast<Layer*>(layer)}
78 {
79  unsigned int numInputSlots = layer->GetNumInputSlots();
80  m_InputSlots.resize(numInputSlots);
81  for (unsigned int i = 0; i < numInputSlots; i++)
82  {
83  m_InputSlots.at(i) = boost::polymorphic_downcast<InputSlot*>(&(layer->GetInputSlot(i)));
84  }
85 
86  unsigned int numOutputSlots = layer->GetNumOutputSlots();
87  m_OutputSlots.resize(numOutputSlots);
88  for (unsigned int i = 0; i < numOutputSlots; i++)
89  {
90  m_OutputSlots.at(i) = boost::polymorphic_downcast<OutputSlot*>(&(layer->GetOutputSlot(i)));
91  }
92 
93  CheckSubgraph();
94 }
95 
97 {
98  m_InputSlots = std::move(other.m_InputSlots);
99  m_OutputSlots = std::move(other.m_OutputSlots);
100  m_Layers = std::move(other.m_Layers);
101 
102  CheckSubgraph();
103 
104  return *this;
105 }
106 
107 void SubgraphView::CheckSubgraph()
108 {
109  // Check for invalid or duplicate input slots
110  AssertIfNullsOrDuplicates(m_InputSlots, "Sub-graphs cannot contain null or duplicate input slots");
111 
112  // Check for invalid or duplicate output slots
113  AssertIfNullsOrDuplicates(m_OutputSlots, "Sub-graphs cannot contain null or duplicate output slots");
114 
115  // Check for invalid or duplicate layers
116  AssertIfNullsOrDuplicates(m_Layers, "Sub-graphs cannot contain null or duplicate layers");
117 }
118 
120 {
121  return m_InputSlots;
122 }
123 
125 {
126  return m_OutputSlots;
127 }
128 
129 const InputSlot* SubgraphView::GetInputSlot(unsigned int index) const
130 {
131  return m_InputSlots.at(index);
132 }
133 
135 {
136  return m_InputSlots.at(index);
137 }
138 
139 const OutputSlot* SubgraphView::GetOutputSlot(unsigned int index) const
140 {
141  return m_OutputSlots.at(index);
142 }
143 
145 {
146  return m_OutputSlots.at(index);
147 }
148 
149 unsigned int SubgraphView::GetNumInputSlots() const
150 {
151  return boost::numeric_cast<unsigned int>(m_InputSlots.size());
152 }
153 
155 {
156  return boost::numeric_cast<unsigned int>(m_OutputSlots.size());
157 }
158 
160 {
161  return m_Layers;
162 }
163 
165 {
166  return m_Layers.begin();
167 }
168 
170 {
171  return m_Layers.end();
172 }
173 
175 {
176  return m_Layers.begin();
177 }
178 
180 {
181  return m_Layers.end();
182 }
183 
185 {
186  return begin();
187 }
188 
190 {
191  return end();
192 }
193 
195 {
196  m_InputSlots.clear();
197  m_OutputSlots.clear();
198  m_Layers.clear();
199 }
200 
201 } // namespace armnn
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
Layers::iterator Iterator
SubgraphView & operator=(SubgraphView &&other)
Move-assignment operator.
std::vector< OutputSlot * > OutputSlots
unsigned int GetNumInputSlots() const
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
The SubgraphView class represents a subgraph of a Graph.
const OutputSlot * GetOutputSlot(unsigned int index) const
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
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
ConstIterator cbegin() const
std::list< Layer * > Layers
Layers::const_iterator ConstIterator
unsigned int GetNumOutputSlots() const