ArmNN
 24.02
GraphUtils.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "GraphUtils.hpp"
7 
9 
10 bool GraphHasNamedLayer(const armnn::Graph& graph, const std::string& name)
11 {
12  for (auto&& layer : graph)
13  {
14  if (layer->GetName() == name)
15  {
16  return true;
17  }
18  }
19  return false;
20 }
21 
22 armnn::Layer* GetFirstLayerWithName(armnn::Graph& graph, const std::string& name)
23 {
24  for (auto&& layer : graph)
25  {
26  if (layer->GetNameStr() == name)
27  {
28  return layer;
29  }
30  }
31  return nullptr;
32 }
33 
34 bool CheckNumberOfInputSlot(armnn::Layer* layer, unsigned int num)
35 {
36  return layer->GetNumInputSlots() == num;
37 }
38 
39 bool CheckNumberOfOutputSlot(armnn::Layer* layer, unsigned int num)
40 {
41  return layer->GetNumOutputSlots() == num;
42 }
43 
44 bool IsConnected(armnn::Layer* srcLayer, armnn::Layer* destLayer,
45  unsigned int srcSlot, unsigned int destSlot,
46  const armnn::TensorInfo& expectedTensorInfo)
47 {
48  const armnn::IOutputSlot& outputSlot = srcLayer->GetOutputSlot(srcSlot);
49  const armnn::TensorInfo& tensorInfo = outputSlot.GetTensorInfo();
50  if (expectedTensorInfo != tensorInfo)
51  {
52  return false;
53  }
54  const unsigned int numConnections = outputSlot.GetNumConnections();
55  for (unsigned int c = 0; c < numConnections; ++c)
56  {
57  auto inputSlot = armnn::PolymorphicDowncast<const armnn::InputSlot*>(outputSlot.GetConnection(c));
58  if (inputSlot->GetOwningLayer().GetNameStr() == destLayer->GetNameStr() &&
59  inputSlot->GetSlotIndex() == destSlot)
60  {
61  return true;
62  }
63  }
64  return false;
65 }
66 
67 /// Checks that first comes before second in the order.
68 bool CheckOrder(const armnn::Graph& graph, const armnn::Layer* first, const armnn::Layer* second)
69 {
70  graph.Print();
71 
72  const auto& order = graph.TopologicalSort();
73 
74  auto firstPos = std::find(order.begin(), order.end(), first);
75  auto secondPos = std::find(firstPos, order.end(), second);
76 
77  return (secondPos != order.end());
78 }
armnn::TensorInfo
Definition: Tensor.hpp:152
GraphUtils.hpp
GraphHasNamedLayer
bool GraphHasNamedLayer(const armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:10
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
GetFirstLayerWithName
armnn::Layer * GetFirstLayerWithName(armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:22
CheckNumberOfOutputSlot
bool CheckNumberOfOutputSlot(armnn::Layer *layer, unsigned int num)
Definition: GraphUtils.cpp:39
armnn::IOutputSlot::GetTensorInfo
virtual const TensorInfo & GetTensorInfo() const =0
armnn::Layer
Definition: Layer.hpp:230
CheckNumberOfInputSlot
bool CheckNumberOfInputSlot(armnn::Layer *layer, unsigned int num)
Definition: GraphUtils.cpp:34
armnn::IOutputSlot
An output connection slot for a layer.
Definition: INetwork.hpp:53
armnn::IOutputSlot::GetConnection
virtual const IInputSlot * GetConnection(unsigned int index) const =0
PolymorphicDowncast.hpp
IsConnected
bool IsConnected(armnn::Layer *srcLayer, armnn::Layer *destLayer, unsigned int srcSlot, unsigned int destSlot, const armnn::TensorInfo &expectedTensorInfo)
Definition: GraphUtils.cpp:44
armnn::Graph::Print
Status Print(bool extended=false) const
Definition: Graph.cpp:68
armnn::Layer::GetNumOutputSlots
unsigned int GetNumOutputSlots() const override
Returns the number of connectable output slots.
Definition: Layer.hpp:335
armnn::Layer::GetNameStr
const std::string & GetNameStr() const
Definition: Layer.hpp:240
armnn::Layer::GetNumInputSlots
unsigned int GetNumInputSlots() const override
Returns the number of connectable input slots.
Definition: Layer.hpp:334
armnn::Graph::TopologicalSort
Graph & TopologicalSort()
Sorts layers in topological order and return this.
Definition: Graph.hpp:184
armnn::IOutputSlot::GetNumConnections
virtual unsigned int GetNumConnections() const =0
CheckOrder
bool CheckOrder(const armnn::Graph &graph, const armnn::Layer *first, const armnn::Layer *second)
Checks that first comes before second in the order.
Definition: GraphUtils.cpp:68
armnn::Graph
Definition: Graph.hpp:30