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> ForEachLayer(Func func) const35 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> ForEachIConnectableLayer(Func func) const46 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") 74 SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers); 75 76 /// Constructs a sub-graph with the given arguments. 77 SubgraphView(IConnectableLayers&& layers, IInputSlots&& inputs, IOutputSlots&& outputs); 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. 86 SubgraphView(IConnectableLayer* layer); 87 88 /// Move-assignment operator. 89 SubgraphView& operator=(SubgraphView&& other); 90 91 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlots() returning" 92 " public IInputSlots", "23.08") 93 const InputSlots& GetInputSlots() const; 94 const IInputSlots& GetIInputSlots() const; 95 96 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlots() returning" 97 " public IOutputSlots", "23.08") 98 const OutputSlots& GetOutputSlots() const; 99 const IOutputSlots& GetIOutputSlots() const; 100 101 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIConnectableLayers() " 102 "returning public IConnectableLayers", "23.08") 103 const Layers& GetLayers() const; 104 const IConnectableLayers& GetIConnectableLayers() const; 105 106 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public " 107 "IInputSlot", "23.08") 108 const InputSlot* GetInputSlot(unsigned int index) const; 109 const IInputSlot* GetIInputSlot(unsigned int index) const; 110 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIInputSlot() returning public " 111 "IInputSlot", "23.08") 112 InputSlot* GetInputSlot(unsigned int index); 113 IInputSlot* GetIInputSlot(unsigned int index); 114 115 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning" 116 " public IOutputSlot", "23.08") 117 const OutputSlot* GetOutputSlot(unsigned int index) const; 118 const IOutputSlot* GetIOutputSlot(unsigned int index) const; 119 ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use GetIOutputSlot() returning" 120 " public IOutputSlot", "23.08") 121 OutputSlot* GetOutputSlot(unsigned int index); 122 IOutputSlot* GetIOutputSlot(unsigned int index); 123 124 unsigned int GetNumInputSlots() const; 125 unsigned int GetNumOutputSlots() const; 126 127 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 128 "IConnectableLayerIterator, until that occurs in 23.08; please use " 129 "beginIConnectable() returning public IConnectableLayerIterator", "23.08") 130 Iterator begin(); 131 IConnectableLayerIterator beginIConnectable(); 132 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 133 "IConnectableLayerIterator, until that occurs in 23.08; please use " 134 "endIConnectable() returning public IConnectableLayerIterator", "23.08") 135 Iterator end(); 136 IConnectableLayerIterator endIConnectable(); 137 138 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 139 "ConstIConnectableIterator, until that occurs in 23.08; please use " 140 "beginIConnectable() returning public ConstIConnectableIterator", "23.08") 141 ConstIterator begin() const; 142 ConstIConnectableIterator beginIConnectable() const; 143 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 144 "ConstIConnectableIterator, until that occurs in 23.08; please use " 145 "endIConnectable() returning public ConstIConnectableIterator", "23.08") 146 ConstIterator end() const; 147 ConstIConnectableIterator endIConnectable() const; 148 149 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 150 "ConstIConnectableIterator, until that occurs in 23.08; please use " 151 "cbeginIConnectable() returning public ConstIConnectableIterator", "23.08") 152 ConstIterator cbegin() const; 153 ConstIConnectableIterator cbeginIConnectable() const; 154 ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be changed to return an " 155 "ConstIConnectableIterator, until that occurs in 23.08; please use " 156 "cendIConnectable() returning public ConstIConnectableIterator", "23.08") 157 ConstIterator cend() const; 158 ConstIConnectableIterator cendIConnectable() const; 159 160 void Clear(); 161 162 /// This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separate 163 /// underlying graph from the main ArmNN graph. 164 /// Backend users should edit this working copy and then add it as a SubstitutionPair, along with original 165 /// SubgraphView, to the OptimizationViews returned by OptimizeSubgraphView. 166 /// ArmNN will then decide on whether or not to carry out Substitution of the two SubgraphViews. 167 SubgraphView GetWorkingCopy() const; 168 169 /// These methods should be called on a working copy subgraph created from GetWorkingCopy. 170 /// They take a SubgraphView pattern to replace and the substitute layer or subgraphView to substitute in. 171 void SubstituteSubgraph(SubgraphView&, IConnectableLayer*); 172 void SubstituteSubgraph(SubgraphView&, const SubgraphView&); 173 174 /// These methods should be called on a working copy subgraph created from GetWorkingCopy. 175 /// They return pointers to the input and output Slots belonging to the original SubgraphView 176 /// that the working copy was created from. 177 /// This may be used to find the original TensorInfo of connected boundary OutputSlots. 178 const IInputSlots& GetOriginalInputSlots() const; 179 const IOutputSlots& GetOriginalOutputSlots() const; 180 181 private: 182 struct SubgraphViewWorkingCopy; 183 184 /// Constructs a sub-graph with the given arguments. 185 SubgraphView(IConnectableLayers&& layers, 186 IInputSlots&& inputs, 187 IOutputSlots&& outputs, 188 std::shared_ptr<SubgraphViewWorkingCopy> ptr); 189 190 void CheckSubgraph(); 191 192 /// Arrange the order of layers topologically so that nodes can be visited in valid order 193 void ArrangeBySortOrder(); 194 195 /// Updates the IInputSlots and IOutputSlots variables assigned to a SubgraphView 196 void UpdateSubgraphViewSlotPointers(SubgraphView&, const SubgraphView&); 197 198 /// The list of pointers to the input slots of the parent graph. 199 InputSlots m_InputSlots; 200 IInputSlots m_IInputSlots; 201 202 /// The list of pointers to the output slots of the parent graph. 203 OutputSlots m_OutputSlots; 204 IOutputSlots m_IOutputSlots; 205 206 /// The list of pointers to the layers of the parent graph. 207 Layers m_Layers; 208 IConnectableLayers m_IConnectableLayers; 209 210 /// Pointer to internal graph implementation. This stores a working copy of a graph, separate from the main 211 /// ArmNN graph, for use by Backends so that they can edit it and add as a SubstitutionPair to OptimizationViews 212 /// along with the original SubgraphView. 213 /// ArmNN will then decide on whether or not to substitute in the provided SubgraphView working copy. 214 std::shared_ptr<SubgraphViewWorkingCopy> p_WorkingCopyImpl; 215 }; 216 } // namespace armnn 217