ArmNN
 25.11
Loading...
Searching...
No Matches
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
9
10#include <vector>
11#include <list>
12#include <iterator>
13#include <memory>
14
15namespace armnn
16{
17class Graph;
19class IInputSlot;
20class IOutputSlot;
21class InputSlot;
22class Layer;
23class 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///
31class SubgraphView final : public std::enable_shared_from_this<SubgraphView>
32{
33public:
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;
156
157private:
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
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition INetwork.hpp:81
An input connection slot for a layer.
Definition INetwork.hpp:26
An output connection slot for a layer.
Definition INetwork.hpp:54
OutputSlot * GetOutputSlot(unsigned int index)
const IOutputSlots & GetOriginalOutputSlots() const
IConnectableLayers::iterator IConnectableLayerIterator
IConnectableLayers::const_iterator ConstIConnectableIterator
IConnectableLayerIterator begin()
OutputSlots && outputs
Layers::const_iterator ConstIterator
std::vector< IOutputSlot * > IOutputSlots
SubgraphView(Graph &graph)
Constructs a sub-graph from the entire given graph.
ConstIConnectableIterator cend() const
std::vector< IInputSlot * > IInputSlots
unsigned int GetNumOutputSlots() const
SubgraphView GetWorkingCopy() const
This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separ...
OutputSlots Layers && layers
std::vector< InputSlot * > InputSlots
unsigned int GetNumInputSlots() const
std::list< Layer * > Layers
ConstIConnectableIterator cbegin() const
std::vector< OutputSlot * > OutputSlots
const IInputSlot * GetIInputSlot(unsigned int index) const
const IConnectableLayers & GetIConnectableLayers() const
void ForEachIConnectableLayer(Func func) const
const IOutputSlot * GetIOutputSlot(unsigned int index) const
const IInputSlots & GetOriginalInputSlots() const
These methods should be called on a working copy subgraph created from GetWorkingCopy.
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_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use " "begin() returning public IConnectableIterator", "24.05") IConnectableLayerIterator beginIConnectable()
std::shared_ptr< SubgraphView > SubgraphViewPtr
void ForEachLayer(Func func) const
std::list< IConnectableLayer * > IConnectableLayers
const IInputSlots & GetIInputSlots() const
const IOutputSlots & GetIOutputSlots() const
void SubstituteSubgraph(SubgraphView &, IConnectableLayer *)
These methods should be called on a working copy subgraph created from GetWorkingCopy.
Layers::iterator Iterator
IConnectableLayerIterator end()
Copyright (c) 2021 ARM Limited and Contributors.