19 #include <unordered_map> 20 #include <unordered_set> 23 #include <boost/iterator/transform_iterator.hpp> 33 template <
typename LayerType>
36 return PolymorphicDowncast<LayerType*>(layer);
39 template <
typename Func>
42 for (
auto it = m_Layers.begin(); it != m_Layers.end(); )
44 auto next = std::next(it);
65 return {
m_Graph.m_Layers.begin(), &(PtrCast<const InputLayer>) };
71 &(PtrCast<const InputLayer>) };
85 &(PtrCast<const OutputLayer>) };
90 return {
m_Graph.m_Layers.end(), &(PtrCast<const OutputLayer>) };
104 *
this = std::move(other);
109 m_InputIds = std::move(other.m_InputIds);
110 m_OutputIds = std::move(other.m_OutputIds);
111 m_LayersInOrder = std::move(other.m_LayersInOrder);
112 m_Views = std::move(other.m_Views);
116 otherLayer->
Reparent(*
this, m_Layers.end());
138 template <
typename LayerT,
typename... Args>
143 template <
typename LayerT,
typename... Args>
147 template <
typename LayerT,
typename... Args>
155 template <
typename LayerT>
206 m_Views[notifyOnEvent].emplace_back(observable);
210 m_Views[notifyOnEvent].remove(observable);
217 template <
typename LayerT>
218 class LayerInGraphBase;
220 template <
typename LayerT>
234 while ((it != m_Layers.begin()) && ((*std::prev(it))->GetType() ==
LayerType::Output))
244 for (
auto& observable : m_Views[event])
246 observable->Update(graphState);
250 std::unordered_set<LayerBindingId> m_InputIds;
251 std::unordered_set<LayerBindingId> m_OutputIds;
252 std::unordered_map<const Layer*, Iterator> m_PosInGraphMap;
260 mutable bool m_LayersInOrder;
262 std::map<const GraphEvent, std::list<IGraphObservable*>> m_Views;
266 template <
typename LayerT>
267 class Graph::LayerInGraphBase :
public LayerT
270 template <
typename... Args>
271 LayerInGraphBase(
Graph& graph,
Iterator insertBefore, Args&&... args)
272 : LayerT(std::forward<Args>(args)...),
m_Graph(&graph)
274 Insert(*
m_Graph, insertBefore);
281 void Reparent(
Graph& destGraph,
Iterator insertBefore)
override 283 Insert(destGraph, insertBefore);
292 graph.m_PosInGraphMap.emplace(
this, graph.m_Layers.emplace(insertBefore,
this));
298 graph.m_Layers.erase(layerIt);
300 const size_t numErased = graph.m_PosInGraphMap.erase(
this);
310 template <
typename LayerT>
311 class Graph::LayerInGraph final :
public LayerInGraphBase<LayerT>
314 template <
typename... Args>
315 LayerInGraph(
Graph& graph, Args&&... args)
316 : LayerInGraphBase<LayerT>(graph,
319 std::forward<Args>(args)...)
322 template <
typename... Args>
323 LayerInGraph(
Graph& graph,
Iterator insertBefore, Args&&... args)
324 : LayerInGraphBase<LayerT>(graph,
326 graph.ForwardToEndOfInputs(graph.RewindToBeginOfOutputs(insertBefore)),
327 std::forward<Args>(args)...)
334 class Graph::LayerInGraph<
InputLayer> final :
public LayerInGraphBase<InputLayer>
337 template <
typename... Args>
342 std::forward<Args>(args)...)
344 const bool isNewId =
m_Graph->m_InputIds.emplace(GetBindingId()).second;
350 template <
typename... Args>
353 : LayerInGraph(graph,
std::forward<Args>(args)...)
358 const size_t numErased =
m_Graph->m_InputIds.erase(GetBindingId());
366 class Graph::LayerInGraph<
OutputLayer> final :
public LayerInGraphBase<OutputLayer>
369 template <
typename... Args>
374 std::forward<Args>(args)...)
376 const bool isNewId =
m_Graph->m_OutputIds.emplace(GetBindingId()).second;
384 const size_t numErased =
m_Graph->m_OutputIds.erase(GetBindingId());
392 auto it = m_PosInGraphMap.find(&layer);
397 template <
typename LayerT,
typename... Args>
400 m_LayersInOrder = m_LayersInOrder &&
402 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, std::forward<Args>(args)...);
409 template <
typename LayerT,
typename... Args>
414 const Iterator pos = (parentOut !=
nullptr)
417 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, pos, std::forward<Args>(args)...);
418 insertBefore.
Insert(*layer);
425 template <
typename LayerT,
typename... Args>
431 LayerT*
const layer =
new LayerInGraph<LayerT>(*
this, pos, std::forward<Args>(args)...);
436 insertAfter.
Connect(layer->GetInputSlot(0));
450 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...
boost::transform_iterator< decltype(&PtrCast< const OutputLayer >), Iterator > ConstIteratorOutputs
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)
boost::transform_iterator< decltype(&PtrCast< const Layer >), Iterator > ConstIterator
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) 2020 ARM Limited.
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...
bool Remove(const char *path)
#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.
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...
boost::transform_iterator< decltype(&PtrCast< const InputLayer >), Iterator > ConstIteratorInputs
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
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.