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> ForEachLayer(Func func) const27 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 using SubgraphViewPtr = std::unique_ptr<SubgraphView>; 38 using InputSlots = std::vector<InputSlot*>; 39 using OutputSlots = std::vector<OutputSlot*>; 40 using Layers = std::list<Layer*>; 41 using Iterator = Layers::iterator; 42 using ConstIterator = Layers::const_iterator; 43 44 /// Constructs a sub-graph from the entire given graph. 45 explicit SubgraphView(Graph& graph); 46 47 /// Constructs a sub-graph with the given arguments. 48 SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers); 49 50 /// Copy-constructor. 51 SubgraphView(const SubgraphView& subgraph); 52 53 /// Move-constructor. 54 SubgraphView(SubgraphView&& subgraph); 55 56 /// Constructs a sub-graph with only the given layer. 57 SubgraphView(IConnectableLayer* layer); 58 59 /// Move-assignment operator. 60 SubgraphView& operator=(SubgraphView&& other); 61 62 const InputSlots& GetInputSlots() const; 63 const OutputSlots& GetOutputSlots() const; 64 const Layers& GetLayers() const; 65 66 const InputSlot* GetInputSlot(unsigned int index) const; 67 InputSlot* GetInputSlot(unsigned int index); 68 69 const OutputSlot* GetOutputSlot(unsigned int index) const; 70 OutputSlot* GetOutputSlot(unsigned int index); 71 72 unsigned int GetNumInputSlots() const; 73 unsigned int GetNumOutputSlots() const; 74 75 Iterator begin(); 76 Iterator end(); 77 78 ConstIterator begin() const; 79 ConstIterator end() const; 80 81 ConstIterator cbegin() const; 82 ConstIterator cend() const; 83 84 void Clear(); 85 86 private: 87 void CheckSubgraph(); 88 89 /// The list of pointers to the input slots of the parent graph. 90 InputSlots m_InputSlots; 91 92 /// The list of pointers to the output slots of the parent graph. 93 OutputSlots m_OutputSlots; 94 95 /// The list of pointers to the layers of the parent graph. 96 Layers m_Layers; 97 }; 98 99 /// 100 /// Old SubGraph definition kept for backward compatibility only. 101 /// 102 using SubGraph ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead") = SubgraphView; 103 104 } // namespace armnn 105