blob: f2ca44cb68ee064e210a8d450579468b726f6631 [file] [log] [blame]
David Beckf98d21a2018-10-26 16:03:03 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "Layer.hpp"
Matteo Martincighadddddb2019-01-24 14:06:23 +00009#include "Graph.hpp"
David Beckf98d21a2018-10-26 16:03:03 +010010
11#include <vector>
Matteo Martincigh49124022019-01-11 13:25:59 +000012#include <list>
David Beckf98d21a2018-10-26 16:03:03 +010013
14namespace armnn
15{
16
17///
Derek Lambertiff05cc52019-04-26 13:05:17 +010018/// The SubgraphView class represents a subgraph of a Graph.
David Beckf98d21a2018-10-26 16:03:03 +010019/// The data it holds, points to data held by layers of the Graph, so the
Matteo Martincigh602af092019-05-01 10:31:27 +010020/// the contents of the SubgraphView become invalid when the Layers are destroyed
David Beckf98d21a2018-10-26 16:03:03 +010021/// or changed.
22///
Derek Lambertiff05cc52019-04-26 13:05:17 +010023class SubgraphView final
David Beckf98d21a2018-10-26 16:03:03 +010024{
25public:
David Monahan5200afa2019-05-10 11:52:14 +010026 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
Francis Murtagh56ccf682021-12-13 18:48:12 +000037 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
Derek Lambertiff05cc52019-04-26 13:05:17 +010048 using SubgraphViewPtr = std::unique_ptr<SubgraphView>;
Matteo Martincighadddddb2019-01-24 14:06:23 +000049 using InputSlots = std::vector<InputSlot*>;
Francis Murtagh56ccf682021-12-13 18:48:12 +000050 using IInputSlots = std::vector<IInputSlot*>;
Matteo Martincighadddddb2019-01-24 14:06:23 +000051 using OutputSlots = std::vector<OutputSlot*>;
Francis Murtagh56ccf682021-12-13 18:48:12 +000052 using IOutputSlots = std::vector<IOutputSlot*>;
Matteo Martincigh49124022019-01-11 13:25:59 +000053 using Layers = std::list<Layer*>;
Francis Murtagh56ccf682021-12-13 18:48:12 +000054 using IConnectableLayers = std::list<IConnectableLayer*>;
Matteo Martincighadddddb2019-01-24 14:06:23 +000055 using Iterator = Layers::iterator;
Francis Murtagh56ccf682021-12-13 18:48:12 +000056 using IConnectableLayerIterator = IConnectableLayers::iterator;
Matteo Martincighadddddb2019-01-24 14:06:23 +000057 using ConstIterator = Layers::const_iterator;
Francis Murtagh56ccf682021-12-13 18:48:12 +000058 using ConstIConnectableIterator = IConnectableLayers::const_iterator;
David Beckf98d21a2018-10-26 16:03:03 +010059
Matteo Martincighadddddb2019-01-24 14:06:23 +000060 /// Constructs a sub-graph from the entire given graph.
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010061 explicit SubgraphView(Graph& graph);
Matteo Martincighadddddb2019-01-24 14:06:23 +000062
Matteo Martincigh602af092019-05-01 10:31:27 +010063 /// Constructs a sub-graph with the given arguments.
Francis Murtagh56ccf682021-12-13 18:48:12 +000064 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use constructor with arguments: "
65 "IConnectableLayers, IInputSlots and IOutputSlots", "22.08")
Matteo Martincigh602af092019-05-01 10:31:27 +010066 SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers);
Matteo Martincighadddddb2019-01-24 14:06:23 +000067
Francis Murtagh56ccf682021-12-13 18:48:12 +000068 /// Constructs a sub-graph with the given arguments.
69 SubgraphView(IConnectableLayers&& layers, IInputSlots&& inputs, IOutputSlots&& outputs);
70
Matteo Martincighadddddb2019-01-24 14:06:23 +000071 /// Copy-constructor.
Derek Lambertiff05cc52019-04-26 13:05:17 +010072 SubgraphView(const SubgraphView& subgraph);
Matteo Martincighadddddb2019-01-24 14:06:23 +000073
74 /// Move-constructor.
Derek Lambertiff05cc52019-04-26 13:05:17 +010075 SubgraphView(SubgraphView&& subgraph);
Matteo Martincighadddddb2019-01-24 14:06:23 +000076
Matteo Martincigh602af092019-05-01 10:31:27 +010077 /// Constructs a sub-graph with only the given layer.
78 SubgraphView(IConnectableLayer* layer);
Matteo Martincighadddddb2019-01-24 14:06:23 +000079
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010080 /// Move-assignment operator.
81 SubgraphView& operator=(SubgraphView&& other);
82
Francis Murtagh56ccf682021-12-13 18:48:12 +000083 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlots() returning"
84 " public IInputSlots", "22.08")
Matteo Martincighadddddb2019-01-24 14:06:23 +000085 const InputSlots& GetInputSlots() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +000086 const IInputSlots& GetIInputSlots() const;
87
88 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlots() returning"
89 " public IOutputSlots", "22.08")
Matteo Martincighadddddb2019-01-24 14:06:23 +000090 const OutputSlots& GetOutputSlots() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +000091 const IOutputSlots& GetIOutputSlots() const;
92
93 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIConnectableLayers() "
94 "returning public IConnectableLayers", "22.08")
Matteo Martincighadddddb2019-01-24 14:06:23 +000095 const Layers& GetLayers() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +000096 const IConnectableLayers& GetIConnectableLayers() const;
David Beckf98d21a2018-10-26 16:03:03 +010097
Francis Murtagh56ccf682021-12-13 18:48:12 +000098 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public "
99 "IInputSlot", "22.08")
David Beckf98d21a2018-10-26 16:03:03 +0100100 const InputSlot* GetInputSlot(unsigned int index) const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000101 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.08")
David Beckf98d21a2018-10-26 16:03:03 +0100104 InputSlot* GetInputSlot(unsigned int index);
Francis Murtagh56ccf682021-12-13 18:48:12 +0000105 IInputSlot* GetIInputSlot(unsigned int index);
David Beckf98d21a2018-10-26 16:03:03 +0100106
Francis Murtagh56ccf682021-12-13 18:48:12 +0000107 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning"
108 " public IOutputSlot", "22.08")
David Beckf98d21a2018-10-26 16:03:03 +0100109 const OutputSlot* GetOutputSlot(unsigned int index) const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000110 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.08")
David Beckf98d21a2018-10-26 16:03:03 +0100113 OutputSlot* GetOutputSlot(unsigned int index);
Francis Murtagh56ccf682021-12-13 18:48:12 +0000114 IOutputSlot* GetIOutputSlot(unsigned int index);
David Beckf98d21a2018-10-26 16:03:03 +0100115
116 unsigned int GetNumInputSlots() const;
117 unsigned int GetNumOutputSlots() const;
118
Francis Murtagh56ccf682021-12-13 18:48:12 +0000119 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000122 Iterator begin();
Francis Murtagh56ccf682021-12-13 18:48:12 +0000123 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000127 Iterator end();
Francis Murtagh56ccf682021-12-13 18:48:12 +0000128 IConnectableLayerIterator endIConnectable();
Matteo Martincigh49124022019-01-11 13:25:59 +0000129
Francis Murtagh56ccf682021-12-13 18:48:12 +0000130 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000133 ConstIterator begin() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000134 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000138 ConstIterator end() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000139 ConstIConnectableIterator endIConnectable() const;
Matteo Martincigh49124022019-01-11 13:25:59 +0000140
Francis Murtagh56ccf682021-12-13 18:48:12 +0000141 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000144 ConstIterator cbegin() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000145 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")
Matteo Martincighadddddb2019-01-24 14:06:23 +0000149 ConstIterator cend() const;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000150 ConstIConnectableIterator cendIConnectable() const;
Matteo Martincigh49124022019-01-11 13:25:59 +0000151
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100152 void Clear();
153
David Beckf98d21a2018-10-26 16:03:03 +0100154private:
Derek Lambertiff05cc52019-04-26 13:05:17 +0100155 void CheckSubgraph();
Matteo Martincighadddddb2019-01-24 14:06:23 +0000156
Derek Lamberti161d29c2020-12-07 13:54:12 +0000157 /// Arrange the order of layers topologically so that nodes can be visited in valid order
158 void ArrangeBySortOrder();
159
Matteo Martincigh0c051f92019-01-31 12:09:49 +0000160 /// The list of pointers to the input slots of the parent graph.
David Beckf98d21a2018-10-26 16:03:03 +0100161 InputSlots m_InputSlots;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000162 IInputSlots m_IInputSlots;
Matteo Martincigh0c051f92019-01-31 12:09:49 +0000163
164 /// The list of pointers to the output slots of the parent graph.
David Beckf98d21a2018-10-26 16:03:03 +0100165 OutputSlots m_OutputSlots;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000166 IOutputSlots m_IOutputSlots;
Matteo Martincigh0c051f92019-01-31 12:09:49 +0000167
168 /// The list of pointers to the layers of the parent graph.
David Beckf98d21a2018-10-26 16:03:03 +0100169 Layers m_Layers;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000170 IConnectableLayers m_IConnectableLayers;
David Beckf98d21a2018-10-26 16:03:03 +0100171};
David Beckf98d21a2018-10-26 16:03:03 +0100172} // namespace armnn