narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 1 | // |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "GraphUtils.hpp" |
| 7 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 8 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 9 | |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 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 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 57 | auto inputSlot = armnn::PolymorphicDowncast<const armnn::InputSlot*>(outputSlot.GetConnection(c)); |
narpra01 | 6f37f83 | 2018-12-21 18:30:00 +0000 | [diff] [blame] | 58 | if (inputSlot->GetOwningLayer().GetNameStr() == destLayer->GetNameStr() && |
| 59 | inputSlot->GetSlotIndex() == destSlot) |
| 60 | { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 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 | } |