ArmNN
 23.05
SubgraphView.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2022-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/Deprecated.hpp>
9 
10 #include <vector>
11 #include <list>
12 #include <iterator>
13 #include <memory>
14 
15 namespace armnn
16 {
17 class Graph;
18 class IConnectableLayer;
19 class IInputSlot;
20 class IOutputSlot;
21 class InputSlot;
22 class Layer;
23 class OutputSlot;
24 
25 ///
26 /// The SubgraphView class represents a subgraph of a Graph.
27 /// The data it holds, points to data held by layers of the Graph, so the
28 /// the contents of the SubgraphView become invalid when the Layers are destroyed
29 /// or changed.
30 ///
31 class SubgraphView final : public std::enable_shared_from_this<SubgraphView>
32 {
33 public:
34  template <typename Func>
35  void ForEachLayer(Func func) const
36  {
37  for (auto it = m_Layers.begin(); it != m_Layers.end(); )
38  {
39  auto next = std::next(it);
40  func(*it);
41  it = next;
42  }
43  }
44 
45  template <typename Func>
46  void ForEachIConnectableLayer(Func func) const
47  {
48  for (auto it = m_IConnectableLayers.begin(); it != m_IConnectableLayers.end(); )
49  {
50  auto next = std::next(it);
51  func(*it);
52  it = next;
53  }
54  }
55 
56  using SubgraphViewPtr = std::shared_ptr<SubgraphView>;
57  using InputSlots = std::vector<InputSlot*>;
58  using IInputSlots = std::vector<IInputSlot*>;
59  using OutputSlots = std::vector<OutputSlot*>;
60  using IOutputSlots = std::vector<IOutputSlot*>;
61  using Layers = std::list<Layer*>;
62  using IConnectableLayers = std::list<IConnectableLayer*>;
63  using Iterator = Layers::iterator;
64  using IConnectableLayerIterator = IConnectableLayers::iterator;
65  using ConstIterator = Layers::const_iterator;
66  using ConstIConnectableIterator = IConnectableLayers::const_iterator;
67 
68  /// Constructs a sub-graph from the entire given graph.
69  explicit SubgraphView(Graph& graph);
70 
71  /// Constructs a sub-graph with the given arguments.
72  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use constructor with arguments: "
73  "IConnectableLayers, IInputSlots and IOutputSlots", "23.08")
75 
76  /// Constructs a sub-graph with the given arguments.
78 
79  /// Copy-constructor.
80  SubgraphView(const SubgraphView& subgraph);
81 
82  /// Move-constructor.
83  SubgraphView(SubgraphView&& subgraph);
84 
85  /// Constructs a sub-graph with only the given layer.
87 
88  /// Move-assignment operator.
89  SubgraphView& operator=(SubgraphView&& other);
90 
91  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlots() returning"
92  " public IInputSlots", "23.08")
93  const InputSlots& GetInputSlots() const;
94  const IInputSlots& GetIInputSlots() const;
95 
96  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlots() returning"
97  " public IOutputSlots", "23.08")
98  const OutputSlots& GetOutputSlots() const;
99  const IOutputSlots& GetIOutputSlots() const;
100 
101  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIConnectableLayers() "
102  "returning public IConnectableLayers", "23.08")
103  const Layers& GetLayers() const;
105 
106  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public "
107  "IInputSlot", "23.08")
108  const InputSlot* GetInputSlot(unsigned int index) const;
109  const IInputSlot* GetIInputSlot(unsigned int index) const;
110  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public "
111  "IInputSlot", "23.08")
112  InputSlot* GetInputSlot(unsigned int index);
113  IInputSlot* GetIInputSlot(unsigned int index);
114 
115  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning"
116  " public IOutputSlot", "23.08")
117  const OutputSlot* GetOutputSlot(unsigned int index) const;
118  const IOutputSlot* GetIOutputSlot(unsigned int index) const;
119  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning"
120  " public IOutputSlot", "23.08")
121  OutputSlot* GetOutputSlot(unsigned int index);
122  IOutputSlot* GetIOutputSlot(unsigned int index);
123 
124  unsigned int GetNumInputSlots() const;
125  unsigned int GetNumOutputSlots() const;
126 
127  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
128  "IConnectableLayerIterator, until that occurs in 23.08; please use "
129  "beginIConnectable() returning public IConnectableLayerIterator", "23.08")
130  Iterator begin();
132  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
133  "IConnectableLayerIterator, until that occurs in 23.08; please use "
134  "endIConnectable() returning public IConnectableLayerIterator", "23.08")
135  Iterator end();
137 
138  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
139  "ConstIConnectableIterator, until that occurs in 23.08; please use "
140  "beginIConnectable() returning public ConstIConnectableIterator", "23.08")
141  ConstIterator begin() const;
143  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
144  "ConstIConnectableIterator, until that occurs in 23.08; please use "
145  "endIConnectable() returning public ConstIConnectableIterator", "23.08")
146  ConstIterator end() const;
148 
149  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
150  "ConstIConnectableIterator, until that occurs in 23.08; please use "
151  "cbeginIConnectable() returning public ConstIConnectableIterator", "23.08")
152  ConstIterator cbegin() const;
154  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an "
155  "ConstIConnectableIterator, until that occurs in 23.08; please use "
156  "cendIConnectable() returning public ConstIConnectableIterator", "23.08")
157  ConstIterator cend() const;
159 
160  void Clear();
161 
162  /// This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separate
163  /// underlying graph from the main ArmNN graph.
164  /// Backend users should edit this working copy and then add it as a SubstitutionPair, along with original
165  /// SubgraphView, to the OptimizationViews returned by OptimizeSubgraphView.
166  /// ArmNN will then decide on whether or not to carry out Substitution of the two SubgraphViews.
168 
169  /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
170  /// They take a SubgraphView pattern to replace and the substitute layer or subgraphView to substitute in.
173 
174  /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
175  /// They return pointers to the input and output Slots belonging to the original SubgraphView
176  /// that the working copy was created from.
177  /// This may be used to find the original TensorInfo of connected boundary OutputSlots.
178  const IInputSlots& GetOriginalInputSlots() const;
179  const IOutputSlots& GetOriginalOutputSlots() const;
180 
181 private:
182  struct SubgraphViewWorkingCopy;
183 
184  /// Constructs a sub-graph with the given arguments.
186  IInputSlots&& inputs,
188  std::shared_ptr<SubgraphViewWorkingCopy> ptr);
189 
190  void CheckSubgraph();
191 
192  /// Arrange the order of layers topologically so that nodes can be visited in valid order
193  void ArrangeBySortOrder();
194 
195  /// Updates the IInputSlots and IOutputSlots variables assigned to a SubgraphView
196  void UpdateSubgraphViewSlotPointers(SubgraphView&, const SubgraphView&);
197 
198  /// The list of pointers to the input slots of the parent graph.
199  InputSlots m_InputSlots;
200  IInputSlots m_IInputSlots;
201 
202  /// The list of pointers to the output slots of the parent graph.
203  OutputSlots m_OutputSlots;
204  IOutputSlots m_IOutputSlots;
205 
206  /// The list of pointers to the layers of the parent graph.
207  Layers m_Layers;
208  IConnectableLayers m_IConnectableLayers;
209 
210  /// Pointer to internal graph implementation. This stores a working copy of a graph, separate from the main
211  /// ArmNN graph, for use by Backends so that they can edit it and add as a SubstitutionPair to OptimizationViews
212  /// along with the original SubgraphView.
213  /// ArmNN will then decide on whether or not to substitute in the provided SubgraphView working copy.
214  std::shared_ptr<SubgraphViewWorkingCopy> p_WorkingCopyImpl;
215 };
216 } // namespace armnn
armnn::SubgraphView::OutputSlots
std::vector< OutputSlot * > OutputSlots
Definition: SubgraphView.hpp:59
armnn::OutputSlot
Definition: Layer.hpp:87
armnn::SubgraphView::GetNumOutputSlots
unsigned int GetNumOutputSlots() const
Definition: SubgraphView.cpp:298
armnn::SubgraphView::GetOriginalOutputSlots
const IOutputSlots & GetOriginalOutputSlots() const
Definition: SubgraphView.cpp:651
armnn::SubgraphView::GetOriginalInputSlots
const IInputSlots & GetOriginalInputSlots() const
These methods should be called on a working copy subgraph created from GetWorkingCopy.
Definition: SubgraphView.cpp:638
armnn::InputSlot
Definition: Layer.hpp:42
armnn::SubgraphView::GetNumInputSlots
unsigned int GetNumInputSlots() const
Definition: SubgraphView.cpp:293
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:68
armnn::SubgraphView::beginIConnectable
IConnectableLayerIterator beginIConnectable()
Definition: SubgraphView.cpp:324
armnn::SubgraphView::cbeginIConnectable
ConstIConnectableIterator cbeginIConnectable() const
Definition: SubgraphView.cpp:372
armnn::SubgraphView::Iterator
Layers::iterator Iterator
Definition: SubgraphView.hpp:63
armnn::SubgraphView::IConnectableLayerIterator
IConnectableLayers::iterator IConnectableLayerIterator
Definition: SubgraphView.hpp:64
armnn::SubgraphView::SubstituteSubgraph
void SubstituteSubgraph(SubgraphView &, IConnectableLayer *)
These methods should be called on a working copy subgraph created from GetWorkingCopy.
Definition: SubgraphView.cpp:530
armnn::SubgraphView::IConnectableLayers
std::list< IConnectableLayer * > IConnectableLayers
Definition: SubgraphView.hpp:62
armnn::SubgraphView::GetIConnectableLayers
const IConnectableLayers & GetIConnectableLayers() const
Definition: SubgraphView.cpp:308
armnn::SubgraphView::outputs
OutputSlots && outputs
Definition: SubgraphView.hpp:74
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::SubgraphView::IInputSlots
std::vector< IInputSlot * > IInputSlots
Definition: SubgraphView.hpp:58
armnn::IInputSlot
An input connection slot for a layer.
Definition: INetwork.hpp:25
armnn::SubgraphView::endIConnectable
IConnectableLayerIterator endIConnectable()
Definition: SubgraphView.cpp:329
armnn::SubgraphView::ARMNN_DEPRECATED_MSG_CHANGE_DATE
ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " "IConnectableLayerIterator, until that occurs in 23.08; please use " "beginIConnectable() returning public IConnectableLayerIterator", "23.08") Iterator begin()
armnn::SubgraphView::ForEachIConnectableLayer
void ForEachIConnectableLayer(Func func) const
Definition: SubgraphView.hpp:46
armnn::SubgraphView::SubgraphView
SubgraphView(Graph &graph)
Constructs a sub-graph from the entire given graph.
Definition: SubgraphView.cpp:45
armnn::SubgraphView::GetIOutputSlot
const IOutputSlot * GetIOutputSlot(unsigned int index) const
Definition: SubgraphView.cpp:278
armnn::SubgraphView::GetWorkingCopy
SubgraphView GetWorkingCopy() const
This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separ...
Definition: SubgraphView.cpp:429
armnn::SubgraphView
The SubgraphView class represents a subgraph of a Graph.
Definition: SubgraphView.hpp:31
armnn::IOutputSlot
An output connection slot for a layer.
Definition: INetwork.hpp:41
armnn::SubgraphView::cendIConnectable
ConstIConnectableIterator cendIConnectable() const
Definition: SubgraphView.cpp:377
armnn::SubgraphView::ConstIConnectableIterator
IConnectableLayers::const_iterator ConstIConnectableIterator
Definition: SubgraphView.hpp:66
armnn::SubgraphView::ARMNN_DEPRECATED_MSG_REMOVAL_DATE
ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use constructor with arguments: " "IConnectableLayers, IInputSlots and IOutputSlots", "23.08") SubgraphView(InputSlots &&inputs
Constructs a sub-graph with the given arguments.
armnn::SubgraphView::IOutputSlots
std::vector< IOutputSlot * > IOutputSlots
Definition: SubgraphView.hpp:60
armnn::SubgraphView::layers
OutputSlots Layers && layers
Definition: SubgraphView.hpp:74
armnn::Graph
Definition: Graph.hpp:30
armnn::SubgraphView::Layers
std::list< Layer * > Layers
Definition: SubgraphView.hpp:61
armnn::SubgraphView::Clear
void Clear()
Definition: SubgraphView.cpp:382
armnn::SubgraphView::ConstIterator
Layers::const_iterator ConstIterator
Definition: SubgraphView.hpp:65
armnn::SubgraphView::GetIInputSlot
const IInputSlot * GetIInputSlot(unsigned int index) const
Definition: SubgraphView.cpp:258
armnn::SubgraphView::InputSlots
std::vector< InputSlot * > InputSlots
Definition: SubgraphView.hpp:57
armnn::SubgraphView::SubgraphViewPtr
std::shared_ptr< SubgraphView > SubgraphViewPtr
Definition: SubgraphView.hpp:56
armnn::SubgraphView::GetIInputSlots
const IInputSlots & GetIInputSlots() const
Definition: SubgraphView.cpp:238
std
Definition: BackendId.hpp:149
armnn::SubgraphView::GetIOutputSlots
const IOutputSlots & GetIOutputSlots() const
Definition: SubgraphView.cpp:248
armnn::SubgraphView::ForEachLayer
void ForEachLayer(Func func) const
Definition: SubgraphView.hpp:35
Deprecated.hpp