20 #include <unordered_map> 21 #include <unordered_set> 32 template <
typename LayerType>
35 return PolymorphicDowncast<LayerType*>(layer);
38 template <
typename Func>
41 for (
auto it = m_Layers.begin(); it != m_Layers.end(); )
43 auto next = std::next(it);
64 return {
m_Graph.m_Layers.begin(), &(PtrCast<const InputLayer>) };
70 &(PtrCast<const InputLayer>) };
84 &(PtrCast<const OutputLayer>) };
89 return {
m_Graph.m_Layers.end(), &(PtrCast<const OutputLayer>) };
95 Graph(
bool shapeInferenceMethod =
false)
96 : m_LayersInOrder(
true)
107 *
this = std::move(other);
112 m_InputIds = std::move(other.m_InputIds);
113 m_OutputIds = std::move(other.m_OutputIds);
114 m_LayersInOrder = std::move(other.m_LayersInOrder);
115 m_Views = std::move(other.m_Views);
119 otherLayer->
Reparent(*
this, m_Layers.end());
141 template <
typename LayerT,
typename... Args>
146 template <
typename LayerT,
typename... Args>
150 template <
typename LayerT,
typename... Args>
158 template <
typename LayerT>
209 m_Views[notifyOnEvent].emplace_back(observable);
213 m_Views[notifyOnEvent].remove(observable);
220 template <
typename LayerT>
221 class LayerInGraphBase;
223 template <
typename LayerT>
237 while ((it != m_Layers.begin()) && ((*std::prev(it))->GetType() ==
LayerType::Output))
247 for (
auto& observable : m_Views[event])
249 observable->Update(graphState);
253 std::unordered_set<LayerBindingId> m_InputIds;
254 std::unordered_set<LayerBindingId> m_OutputIds;
255 std::unordered_map<const Layer*, Iterator> m_PosInGraphMap;
263 mutable bool m_LayersInOrder;
265 std::map<const GraphEvent, std::list<IGraphObservable*>> m_Views;
270 template <
typename LayerT>
271 class Graph::LayerInGraphBase :
public LayerT
274 template <
typename... Args>
275 LayerInGraphBase(
Graph& graph,
Iterator insertBefore, Args&&... args)
276 : LayerT(std::forward<Args>(args)...),
m_Graph(&graph)
278 Insert(*
m_Graph, insertBefore);
285 void Reparent(
Graph& destGraph,
Iterator insertBefore)
override 287 Insert(destGraph, insertBefore);
296 graph.m_PosInGraphMap.emplace(
this, graph.m_Layers.emplace(insertBefore,
this));
299 void Remove(
Graph& graph)
302 graph.m_Layers.erase(layerIt);
304 const size_t numErased = graph.m_PosInGraphMap.erase(
this);
314 template <
typename LayerT>
315 class Graph::LayerInGraph final :
public LayerInGraphBase<LayerT>
318 template <
typename... Args>
319 LayerInGraph(
Graph& graph, Args&&... args)
320 : LayerInGraphBase<LayerT>(graph,
323 std::forward<Args>(args)...)
326 template <
typename... Args>
327 LayerInGraph(
Graph& graph,
Iterator insertBefore, Args&&... args)
328 : LayerInGraphBase<LayerT>(graph,
330 graph.ForwardToEndOfInputs(graph.RewindToBeginOfOutputs(insertBefore)),
331 std::forward<Args>(args)...)
338 class Graph::LayerInGraph<
InputLayer> final :
public LayerInGraphBase<InputLayer>
341 template <
typename... Args>
346 std::forward<Args>(args)...)
348 const bool isNewId =
m_Graph->m_InputIds.emplace(GetBindingId()).second;
354 template <
typename... Args>
357 : LayerInGraph(graph,
std::forward<Args>(args)...)
362 const size_t numErased =
m_Graph->m_InputIds.erase(GetBindingId());
370 class Graph::LayerInGraph<
OutputLayer> final :
public LayerInGraphBase<OutputLayer>
373 template <
typename... Args>
378 std::forward<Args>(args)...)
380 const bool isNewId =
m_Graph->m_OutputIds.emplace(GetBindingId()).second;
388 const size_t numErased =
m_Graph->m_OutputIds.erase(GetBindingId());
396 auto it = m_PosInGraphMap.find(&layer);
401 template <
typename LayerT,
typename... Args>
404 m_LayersInOrder = m_LayersInOrder &&
406 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, std::forward<Args>(args)...);
408 layer->SetShapeInferenceMethod(m_ShapeInferenceMethod);
415 template <
typename LayerT,
typename... Args>
420 const Iterator pos = (parentOut !=
nullptr)
423 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, pos, std::forward<Args>(args)...);
424 insertBefore.
Insert(*layer);
431 template <
typename LayerT,
typename... Args>
437 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, pos, std::forward<Args>(args)...);
442 insertAfter.
Connect(layer->GetInputSlot(0));
456 template <
typename LayerT>
Graph & operator=(const Graph &other)=delete
Iterator begin()
Returns iterator pointing to the beginning of the list. Lowercase for range-based for loops...
Graph(bool shapeInferenceMethod=false)
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Status SerializeToDot(std::ostream &stream)
void AttachObservable(IGraphObservable *const observable, GraphEvent notifyOnEvent)
virtual void Reparent(Graph &dest, std::list< Layer *>::const_iterator iterator)=0
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
LayerInGraph(Graph &graph, Args &&... args)
ConstIterator cbegin() const
Returns const iterator pointing to the beginning of the list. Lowercase for range-based for loops...
Layer & GetOwningLayer() const
int Connect(InputSlot &destination)
void EraseLayer(Iterator pos)
Deletes the layer at the specified position.
static LayerType * PtrCast(Layer *const layer)
size_t GetNumOutputs() const
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
LayerList::const_iterator Iterator
OutputLayersAccessor(const Graph &graph)
ConstIteratorOutputs begin() const
The SubgraphView class represents a subgraph of a Graph.
Iterator GetPosInGraph(Layer &layer)
Gets the position of a layer in the graph.
A layer user-provided data can be bound to (e.g. inputs, outputs).
void ForEachLayer(Func func) const
ConstIterator end() const
Returns const iterator pointing to the end of the list. Lowercase for range-based for loops...
Validate all output shapes.
#define ARMNN_ASSERT(COND)
Iterator::difference_type IteratorDifference
Wrapper class returned by Graph::GetOutputLayers()
void SubstituteSubgraph(SubgraphView &subgraph, IConnectableLayer *substituteLayer)
Substitutes the given sub-graph with either a new layer or a new sub-graph.
OutputLayersAccessor GetOutputLayers() const
Returns a wrapper object with begin(), end() methods to iterate over the output layers in a range-bas...
Graph & operator=(Graph &&other)
std::list< Layer * > LayerList
ConstIterator begin() const
Returns const iterator pointing to the beginning of the list. Lowercase for range-based for loops...
ConstIteratorOutputs end() const
Iterator end()
Returns iterator pointing to the end of the list. Lowercase for range-based for loops.
Infer missing output shapes and validate all output shapes.
ConstIterator cend() const
Returns const iterator pointing to the end of the list. Lowercase for range-based for loops...
Graph & TopologicalSort()
Sorts layers in topological order and return this.
InputLayersAccessor GetInputLayers() const
Returns a wrapper object with begin(), end() methods to iterate over the input layers in a range-base...
size_t GetNumLayers() const
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Status AllocateDynamicBuffers()
Allocates memory for all tensors under output tensor handers of each layer.
size_t GetNumInputs() const
ShapeInferenceMethod
The ShapeInferenceMethod modify how the output shapes are treated.
void AddCompatibilityLayers(std::map< BackendId, std::unique_ptr< class IBackendInternal >> &backends, TensorHandleFactoryRegistry ®istry)
Modifies the graph in-place, removing edges connecting layers using different compute devices...
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
void DetachObservable(IGraphObservable *const observable, GraphEvent notifyOnEvent)
void MoveAllConnections(OutputSlot &destination)
Moves all connections to another OutputSlot.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...