David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 6 | #include "SubgraphView.hpp" |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 7 | #include "Graph.hpp" |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 8 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 9 | #include <armnn/utility/IgnoreUnused.hpp> |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 10 | #include <armnn/utility/NumericCast.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 11 | #include <armnn/utility/PolymorphicDowncast.hpp> |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 12 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 15 | namespace armnn |
| 16 | { |
| 17 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 18 | namespace |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 19 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 20 | |
| 21 | template <class C> |
| 22 | void AssertIfNullsOrDuplicates(const C& container, const std::string& errorMessage) |
| 23 | { |
| 24 | using T = typename C::value_type; |
| 25 | std::unordered_set<T> duplicateSet; |
| 26 | std::for_each(container.begin(), container.end(), [&duplicateSet, &errorMessage](const T& i) |
| 27 | { |
| 28 | // Ignore unused for release builds |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 29 | IgnoreUnused(errorMessage); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 30 | |
| 31 | // Check if the item is valid |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 32 | ARMNN_ASSERT_MSG(i, errorMessage.c_str()); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 33 | |
| 34 | // Check if a duplicate has been found |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 35 | ARMNN_ASSERT_MSG(duplicateSet.find(i) == duplicateSet.end(), errorMessage.c_str()); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 36 | |
| 37 | duplicateSet.insert(i); |
| 38 | }); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 41 | } // anonymous namespace |
| 42 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 43 | SubgraphView::SubgraphView(Graph& graph) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 44 | : m_InputSlots{} |
| 45 | , m_OutputSlots{} |
| 46 | , m_Layers(graph.begin(), graph.end()) |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 47 | { |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame^] | 48 | ArrangeBySortOrder(); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 49 | CheckSubgraph(); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 52 | SubgraphView::SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 53 | : m_InputSlots{inputs} |
| 54 | , m_OutputSlots{outputs} |
| 55 | , m_Layers{layers} |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 56 | { |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame^] | 57 | ArrangeBySortOrder(); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 58 | CheckSubgraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 61 | SubgraphView::SubgraphView(const SubgraphView& subgraph) |
| 62 | : m_InputSlots(subgraph.m_InputSlots.begin(), subgraph.m_InputSlots.end()) |
| 63 | , m_OutputSlots(subgraph.m_OutputSlots.begin(), subgraph.m_OutputSlots.end()) |
| 64 | , m_Layers(subgraph.m_Layers.begin(), subgraph.m_Layers.end()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 65 | { |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame^] | 66 | ArrangeBySortOrder(); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 67 | CheckSubgraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 70 | SubgraphView::SubgraphView(SubgraphView&& subgraph) |
| 71 | : m_InputSlots(std::move(subgraph.m_InputSlots)) |
| 72 | , m_OutputSlots(std::move(subgraph.m_OutputSlots)) |
| 73 | , m_Layers(std::move(subgraph.m_Layers)) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 74 | { |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame^] | 75 | ArrangeBySortOrder(); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 76 | CheckSubgraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 79 | SubgraphView::SubgraphView(IConnectableLayer* layer) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 80 | : m_InputSlots{} |
| 81 | , m_OutputSlots{} |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 82 | , m_Layers{PolymorphicDowncast<Layer*>(layer)} |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 83 | { |
| 84 | unsigned int numInputSlots = layer->GetNumInputSlots(); |
| 85 | m_InputSlots.resize(numInputSlots); |
| 86 | for (unsigned int i = 0; i < numInputSlots; i++) |
| 87 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 88 | m_InputSlots.at(i) = PolymorphicDowncast<InputSlot*>(&(layer->GetInputSlot(i))); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | unsigned int numOutputSlots = layer->GetNumOutputSlots(); |
| 92 | m_OutputSlots.resize(numOutputSlots); |
| 93 | for (unsigned int i = 0; i < numOutputSlots; i++) |
| 94 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 95 | m_OutputSlots.at(i) = PolymorphicDowncast<OutputSlot*>(&(layer->GetOutputSlot(i))); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 98 | CheckSubgraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 101 | SubgraphView& SubgraphView::operator=(SubgraphView&& other) |
| 102 | { |
| 103 | m_InputSlots = std::move(other.m_InputSlots); |
| 104 | m_OutputSlots = std::move(other.m_OutputSlots); |
| 105 | m_Layers = std::move(other.m_Layers); |
| 106 | |
| 107 | CheckSubgraph(); |
| 108 | |
| 109 | return *this; |
| 110 | } |
| 111 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 112 | void SubgraphView::CheckSubgraph() |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 113 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 114 | // Check for invalid or duplicate input slots |
| 115 | AssertIfNullsOrDuplicates(m_InputSlots, "Sub-graphs cannot contain null or duplicate input slots"); |
| 116 | |
| 117 | // Check for invalid or duplicate output slots |
| 118 | AssertIfNullsOrDuplicates(m_OutputSlots, "Sub-graphs cannot contain null or duplicate output slots"); |
| 119 | |
| 120 | // Check for invalid or duplicate layers |
| 121 | AssertIfNullsOrDuplicates(m_Layers, "Sub-graphs cannot contain null or duplicate layers"); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 124 | const SubgraphView::InputSlots& SubgraphView::GetInputSlots() const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 125 | { |
| 126 | return m_InputSlots; |
| 127 | } |
| 128 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 129 | const SubgraphView::OutputSlots& SubgraphView::GetOutputSlots() const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 130 | { |
| 131 | return m_OutputSlots; |
| 132 | } |
| 133 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 134 | const InputSlot* SubgraphView::GetInputSlot(unsigned int index) const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 135 | { |
| 136 | return m_InputSlots.at(index); |
| 137 | } |
| 138 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 139 | InputSlot* SubgraphView::GetInputSlot(unsigned int index) |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 140 | { |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 141 | return m_InputSlots.at(index); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 144 | const OutputSlot* SubgraphView::GetOutputSlot(unsigned int index) const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 145 | { |
| 146 | return m_OutputSlots.at(index); |
| 147 | } |
| 148 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 149 | OutputSlot* SubgraphView::GetOutputSlot(unsigned int index) |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 150 | { |
| 151 | return m_OutputSlots.at(index); |
| 152 | } |
| 153 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 154 | unsigned int SubgraphView::GetNumInputSlots() const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 155 | { |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 156 | return armnn::numeric_cast<unsigned int>(m_InputSlots.size()); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 159 | unsigned int SubgraphView::GetNumOutputSlots() const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 160 | { |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 161 | return armnn::numeric_cast<unsigned int>(m_OutputSlots.size()); |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 164 | const SubgraphView::Layers& SubgraphView::GetLayers() const |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 165 | { |
| 166 | return m_Layers; |
| 167 | } |
| 168 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 169 | SubgraphView::Iterator SubgraphView::begin() |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 170 | { |
| 171 | return m_Layers.begin(); |
| 172 | } |
| 173 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 174 | SubgraphView::Iterator SubgraphView::end() |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 175 | { |
| 176 | return m_Layers.end(); |
| 177 | } |
| 178 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 179 | SubgraphView::ConstIterator SubgraphView::begin() const |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 180 | { |
| 181 | return m_Layers.begin(); |
| 182 | } |
| 183 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 184 | SubgraphView::ConstIterator SubgraphView::end() const |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 185 | { |
| 186 | return m_Layers.end(); |
| 187 | } |
| 188 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 189 | SubgraphView::ConstIterator SubgraphView::cbegin() const |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 190 | { |
| 191 | return begin(); |
| 192 | } |
| 193 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 194 | SubgraphView::ConstIterator SubgraphView::cend() const |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 195 | { |
| 196 | return end(); |
| 197 | } |
| 198 | |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 199 | void SubgraphView::Clear() |
| 200 | { |
| 201 | m_InputSlots.clear(); |
| 202 | m_OutputSlots.clear(); |
| 203 | m_Layers.clear(); |
| 204 | } |
| 205 | |
Derek Lamberti | 161d29c | 2020-12-07 13:54:12 +0000 | [diff] [blame^] | 206 | void SubgraphView::ArrangeBySortOrder() |
| 207 | { |
| 208 | using LayerList = std::list<Layer*>; |
| 209 | auto compareLayerPriority = [](const LayerList::value_type& layerA, const LayerList::value_type& layerB) |
| 210 | { |
| 211 | return layerA->GetPriority() < layerB->GetPriority(); |
| 212 | }; |
| 213 | |
| 214 | m_Layers.sort(compareLayerPriority); |
| 215 | } |
| 216 | |
David Beck | f98d21a | 2018-10-26 16:03:03 +0100 | [diff] [blame] | 217 | } // namespace armnn |