David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "Layer.hpp" |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 9 | #include "Graph.hpp" |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 10 | |
| 11 | #include <vector> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 12 | #include <list> |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | /// |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 18 | /// The SubgraphView class represents a subgraph of a Graph. |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 19 | /// The data it holds, points to data held by layers of the Graph, so the |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 20 | /// the contents of the SubgraphView become invalid when the Layers are destroyed |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 21 | /// or changed. |
| 22 | /// |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 23 | class SubgraphView final |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 24 | { |
| 25 | public: |
David Monahan | 5200afa | 2019-05-10 11:52:14 +0100 | [diff] [blame] | 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 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 48 | using SubgraphViewPtr = std::unique_ptr<SubgraphView>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 49 | using InputSlots = std::vector<InputSlot*>; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 50 | using IInputSlots = std::vector<IInputSlot*>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 51 | using OutputSlots = std::vector<OutputSlot*>; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 52 | using IOutputSlots = std::vector<IOutputSlot*>; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 53 | using Layers = std::list<Layer*>; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 54 | using IConnectableLayers = std::list<IConnectableLayer*>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 55 | using Iterator = Layers::iterator; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 56 | using IConnectableLayerIterator = IConnectableLayers::iterator; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 57 | using ConstIterator = Layers::const_iterator; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 58 | using ConstIConnectableIterator = IConnectableLayers::const_iterator; |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 59 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 60 | /// Constructs a sub-graph from the entire given graph. |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 61 | explicit SubgraphView(Graph& graph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 62 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 63 | /// Constructs a sub-graph with the given arguments. |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 64 | ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use constructor with arguments: " |
| 65 | "IConnectableLayers, IInputSlots and IOutputSlots", "22.08") |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 66 | SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 67 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 68 | /// Constructs a sub-graph with the given arguments. |
| 69 | SubgraphView(IConnectableLayers&& layers, IInputSlots&& inputs, IOutputSlots&& outputs); |
| 70 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 71 | /// Copy-constructor. |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 72 | SubgraphView(const SubgraphView& subgraph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 73 | |
| 74 | /// Move-constructor. |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 75 | SubgraphView(SubgraphView&& subgraph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 76 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 77 | /// Constructs a sub-graph with only the given layer. |
| 78 | SubgraphView(IConnectableLayer* layer); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 79 | |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 80 | /// Move-assignment operator. |
| 81 | SubgraphView& operator=(SubgraphView&& other); |
| 82 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 83 | ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlots() returning" |
| 84 | " public IInputSlots", "22.08") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 85 | const InputSlots& GetInputSlots() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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.08") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 90 | const OutputSlots& GetOutputSlots() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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.08") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 95 | const Layers& GetLayers() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 96 | const IConnectableLayers& GetIConnectableLayers() const; |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 97 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 98 | ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public " |
| 99 | "IInputSlot", "22.08") |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 100 | const InputSlot* GetInputSlot(unsigned int index) const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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.08") |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 104 | InputSlot* GetInputSlot(unsigned int index); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 105 | IInputSlot* GetIInputSlot(unsigned int index); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 106 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 107 | ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning" |
| 108 | " public IOutputSlot", "22.08") |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 109 | const OutputSlot* GetOutputSlot(unsigned int index) const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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.08") |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 113 | OutputSlot* GetOutputSlot(unsigned int index); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 114 | IOutputSlot* GetIOutputSlot(unsigned int index); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 115 | |
| 116 | unsigned int GetNumInputSlots() const; |
| 117 | unsigned int GetNumOutputSlots() const; |
| 118 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 122 | Iterator begin(); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 127 | Iterator end(); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 128 | IConnectableLayerIterator endIConnectable(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 129 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 133 | ConstIterator begin() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 138 | ConstIterator end() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 139 | ConstIConnectableIterator endIConnectable() const; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 140 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 144 | ConstIterator cbegin() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 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") |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 149 | ConstIterator cend() const; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 150 | ConstIConnectableIterator cendIConnectable() const; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 151 | |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 152 | void Clear(); |
| 153 | |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 154 | private: |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 155 | void CheckSubgraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 156 | |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame] | 157 | /// Arrange the order of layers topologically so that nodes can be visited in valid order |
| 158 | void ArrangeBySortOrder(); |
| 159 | |
Matteo Martincigh | 0c051f9 | 2019-01-31 12:09:49 +0000 | [diff] [blame] | 160 | /// The list of pointers to the input slots of the parent graph. |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 161 | InputSlots m_InputSlots; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 162 | IInputSlots m_IInputSlots; |
Matteo Martincigh | 0c051f9 | 2019-01-31 12:09:49 +0000 | [diff] [blame] | 163 | |
| 164 | /// The list of pointers to the output slots of the parent graph. |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 165 | OutputSlots m_OutputSlots; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 166 | IOutputSlots m_IOutputSlots; |
Matteo Martincigh | 0c051f9 | 2019-01-31 12:09:49 +0000 | [diff] [blame] | 167 | |
| 168 | /// The list of pointers to the layers of the parent graph. |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 169 | Layers m_Layers; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 170 | IConnectableLayers m_IConnectableLayers; |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 171 | }; |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 172 | } // namespace armnn |