ArmNN
 24.02
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  const IInputSlots& GetIInputSlots() const;
92 
93  const IOutputSlots& GetIOutputSlots() const;
94 
96 
97  const IInputSlot* GetIInputSlot(unsigned int index) const;
98  IInputSlot* GetIInputSlot(unsigned int index);
99 
100  const IOutputSlot* GetIOutputSlot(unsigned int index) const;
101 
102  OutputSlot* GetOutputSlot(unsigned int index);
103  IOutputSlot* GetIOutputSlot(unsigned int index);
104 
105  unsigned int GetNumInputSlots() const;
106  unsigned int GetNumOutputSlots() const;
107 
109  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
110  "begin() returning public IConnectableIterator", "24.05")
111  IConnectableLayerIterator beginIConnectable();
113  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
114  "end() returning public IConnectableLayerIterator", "24.05")
115  IConnectableLayerIterator endIConnectable();
117 
118  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
119  "begin() returning public ConstIConnectableIterator", "24.05")
120  ConstIConnectableIterator beginIConnectable() const;
122  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
123  "end() returning public ConstIConnectableIterator", "24.05")
124  ConstIConnectableIterator endIConnectable() const;
125 
127  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
128  "cbegin() returning public ConstIterator", "24.05")
129  ConstIConnectableIterator cbeginIConnectable() const;
130 
132  ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
133  "cend() returning public ConstIConnectableIterator", "24.05")
134  ConstIConnectableIterator cendIConnectable() const;
135 
136  void Clear();
137 
138  /// This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separate
139  /// underlying graph from the main ArmNN graph.
140  /// Backend users should edit this working copy and then add it as a SubstitutionPair, along with original
141  /// SubgraphView, to the OptimizationViews returned by OptimizeSubgraphView.
142  /// ArmNN will then decide on whether or not to carry out Substitution of the two SubgraphViews.
144 
145  /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
146  /// They take a SubgraphView pattern to replace and the substitute layer or subgraphView to substitute in.
149 
150  /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
151  /// They return pointers to the input and output Slots belonging to the original SubgraphView
152  /// that the working copy was created from.
153  /// This may be used to find the original TensorInfo of connected boundary OutputSlots.
154  const IInputSlots& GetOriginalInputSlots() const;
155  const IOutputSlots& GetOriginalOutputSlots() const;
156 
157 private:
158  struct SubgraphViewWorkingCopy;
159 
160  /// Constructs a sub-graph with the given arguments.
162  IInputSlots&& inputs,
164  std::shared_ptr<SubgraphViewWorkingCopy> ptr);
165 
166  void CheckSubgraph();
167 
168  /// Arrange the order of layers topologically so that nodes can be visited in valid order
169  void ArrangeBySortOrder();
170 
171  /// Updates the IInputSlots and IOutputSlots variables assigned to a SubgraphView
172  void UpdateSubgraphViewSlotPointers(SubgraphView&, const SubgraphView&);
173 
174  /// The list of pointers to the input slots of the parent graph.
175  InputSlots m_InputSlots;
176  IInputSlots m_IInputSlots;
177 
178  /// The list of pointers to the output slots of the parent graph.
179  OutputSlots m_OutputSlots;
180  IOutputSlots m_IOutputSlots;
181 
182  /// The list of pointers to the layers of the parent graph.
183  Layers m_Layers;
184  IConnectableLayers m_IConnectableLayers;
185 
186  /// Pointer to internal graph implementation. This stores a working copy of a graph, separate from the main
187  /// ArmNN graph, for use by Backends so that they can edit it and add as a SubstitutionPair to OptimizationViews
188  /// along with the original SubgraphView.
189  /// ArmNN will then decide on whether or not to substitute in the provided SubgraphView working copy.
190  std::shared_ptr<SubgraphViewWorkingCopy> p_WorkingCopyImpl;
191 };
192 } // namespace armnn
armnn::SubgraphView::IOutputSlots
std::vector< IOutputSlot * > IOutputSlots
Definition: SubgraphView.hpp:60
armnn::SubgraphView::GetIInputSlot
const IInputSlot * GetIInputSlot(unsigned int index) const
Definition: SubgraphView.cpp:243
armnn::SubgraphView::ConstIterator
Layers::const_iterator ConstIterator
Definition: SubgraphView.hpp:65
armnn::SubgraphView::GetOutputSlot
OutputSlot * GetOutputSlot(unsigned int index)
Definition: SubgraphView.cpp:258
armnn::OutputSlot
Definition: Layer.hpp:100
armnn::SubgraphView::Layers
std::list< Layer * > Layers
Definition: SubgraphView.hpp:61
Deprecated.hpp
armnn::SubgraphView::cbegin
ConstIConnectableIterator cbegin() const
Definition: SubgraphView.cpp:325
armnn::SubgraphView::GetIOutputSlot
const IOutputSlot * GetIOutputSlot(unsigned int index) const
Definition: SubgraphView.cpp:253
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::GetNumInputSlots
unsigned int GetNumInputSlots() const
Definition: SubgraphView.cpp:268
armnn::SubgraphView::IConnectableLayerIterator
IConnectableLayers::iterator IConnectableLayerIterator
Definition: SubgraphView.hpp:64
armnn::SubgraphView::Iterator
Layers::iterator Iterator
Definition: SubgraphView.hpp:63
armnn::SubgraphView::InputSlots
std::vector< InputSlot * > InputSlots
Definition: SubgraphView.hpp:57
armnn::SubgraphView::cend
ConstIConnectableIterator cend() const
Definition: SubgraphView.cpp:330
armnn::SubgraphView::Clear
void Clear()
Definition: SubgraphView.cpp:346
armnn::SubgraphView::IConnectableLayers
std::list< IConnectableLayer * > IConnectableLayers
Definition: SubgraphView.hpp:62
armnn::SubgraphView::ConstIConnectableIterator
IConnectableLayers::const_iterator ConstIConnectableIterator
Definition: SubgraphView.hpp:66
armnn::IOutputSlot
An output connection slot for a layer.
Definition: INetwork.hpp:53
armnn::SubgraphView::GetIConnectableLayers
const IConnectableLayers & GetIConnectableLayers() const
Definition: SubgraphView.cpp:278
armnn::SubgraphView::begin
IConnectableLayerIterator begin()
Definition: SubgraphView.cpp:283
armnn::SubgraphView::ARMNN_DEPRECATED_MSG_CHANGE_DATE
ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use " "begin() returning public IConnectableIterator", "24.05") IConnectableLayerIterator beginIConnectable()
armnn::SubgraphView::SubgraphViewPtr
std::shared_ptr< SubgraphView > SubgraphViewPtr
Definition: SubgraphView.hpp:56
armnn::SubgraphView::IInputSlots
std::vector< IInputSlot * > IInputSlots
Definition: SubgraphView.hpp:58
armnn::SubgraphView
The SubgraphView class represents a subgraph of a Graph.
Definition: SubgraphView.hpp:31
armnn::SubgraphView::GetWorkingCopy
SubgraphView GetWorkingCopy() const
This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separ...
Definition: SubgraphView.cpp:393
armnn::SubgraphView::layers
OutputSlots Layers && layers
Definition: SubgraphView.hpp:74
armnn::SubgraphView::ForEachLayer
void ForEachLayer(Func func) const
Definition: SubgraphView.hpp:35
armnn::SubgraphView::GetNumOutputSlots
unsigned int GetNumOutputSlots() const
Definition: SubgraphView.cpp:273
std
Definition: BackendId.hpp:149
armnn::SubgraphView::GetIInputSlots
const IInputSlots & GetIInputSlots() const
Definition: SubgraphView.cpp:233
armnn::SubgraphView::end
IConnectableLayerIterator end()
Definition: SubgraphView.cpp:288
armnn::SubgraphView::OutputSlots
std::vector< OutputSlot * > OutputSlots
Definition: SubgraphView.hpp:59
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::SubgraphView::ForEachIConnectableLayer
void ForEachIConnectableLayer(Func func) const
Definition: SubgraphView.hpp:46
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:80
armnn::IInputSlot
An input connection slot for a layer.
Definition: INetwork.hpp:25
armnn::Graph
Definition: Graph.hpp:30
armnn::SubgraphView::outputs
OutputSlots && outputs
Definition: SubgraphView.hpp:74
armnn::SubgraphView::GetIOutputSlots
const IOutputSlots & GetIOutputSlots() const
Definition: SubgraphView.cpp:238
armnn::SubgraphView::SubstituteSubgraph
void SubstituteSubgraph(SubgraphView &, IConnectableLayer *)
These methods should be called on a working copy subgraph created from GetWorkingCopy.
Definition: SubgraphView.cpp:494
armnn::SubgraphView::GetOriginalOutputSlots
const IOutputSlots & GetOriginalOutputSlots() const
Definition: SubgraphView.cpp:615
armnn::SubgraphView::SubgraphView
SubgraphView(Graph &graph)
Constructs a sub-graph from the entire given graph.
Definition: SubgraphView.cpp:45
armnn::SubgraphView::GetOriginalInputSlots
const IInputSlots & GetOriginalInputSlots() const
These methods should be called on a working copy subgraph created from GetWorkingCopy.
Definition: SubgraphView.cpp:602