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