Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include "Layer.hpp" |
| 6 | |
| 7 | #include "Graph.hpp" |
janeil01 | 3fec1ea | 2019-11-07 09:47:20 +0000 | [diff] [blame] | 8 | #include <ProfilingService.hpp> |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 9 | #include <armnn/utility/NumericCast.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 10 | #include <armnn/backends/TensorHandle.hpp> |
| 11 | #include <armnn/backends/WorkloadData.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 13 | #include <fmt/format.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | |
| 15 | #include <numeric> |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
Jim Flynn | e466596 | 2022-01-31 16:08:53 +0000 | [diff] [blame] | 20 | // Instantiate the static member variable |
| 21 | NullDescriptor Layer::m_NullDescriptor; |
| 22 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | void InputSlot::Insert(Layer& layer) |
| 24 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 25 | ARMNN_ASSERT(layer.GetNumOutputSlots() == 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | |
| 27 | OutputSlot* const prevSlot = GetConnectedOutputSlot(); |
| 28 | |
| 29 | if (prevSlot != nullptr) |
| 30 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | // Disconnects parent from this. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | prevSlot->Disconnect(*this); |
| 33 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 34 | // Connects inserted layer to parent. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 35 | ARMNN_ASSERT(layer.GetNumInputSlots() == 1); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 36 | int idx = prevSlot->Connect(layer.GetInputSlot(0)); |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 37 | prevSlot->SetEdgeStrategy(armnn::numeric_cast<unsigned int>(idx), EdgeStrategy::Undefined); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 38 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | // Sets tensor info for inserted layer. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | const TensorInfo& tensorInfo = prevSlot->GetTensorInfo(); |
| 41 | layer.GetOutputHandler().SetTensorInfo(tensorInfo); |
| 42 | } |
| 43 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | // Connects inserted layer to this. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 45 | layer.GetOutputSlot(0).Connect(*this); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 46 | layer.GetOutputSlot(0).SetEdgeStrategy(0, EdgeStrategy::Undefined); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | const InputSlot* OutputSlot::GetConnection(unsigned int index) const |
| 50 | { |
| 51 | ValidateConnectionIndex(index); |
| 52 | return m_Connections[index]; |
| 53 | } |
| 54 | |
| 55 | InputSlot* OutputSlot::GetConnection(unsigned int index) |
| 56 | { |
| 57 | ValidateConnectionIndex(index); |
| 58 | return m_Connections[index]; |
| 59 | } |
| 60 | |
| 61 | void OutputSlot::SetTensorInfo(const TensorInfo& tensorInfo) |
| 62 | { |
| 63 | GetOutputHandler().SetTensorInfo(tensorInfo); |
| 64 | } |
| 65 | |
| 66 | const TensorInfo& OutputSlot::GetTensorInfo() const |
| 67 | { |
| 68 | return GetOutputHandler().GetTensorInfo(); |
| 69 | } |
| 70 | |
| 71 | bool OutputSlot::IsTensorInfoSet() const |
| 72 | { |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 73 | if (GetOwningLayer().GetShapeInferenceMethod() == ShapeInferenceMethod::InferAndValidate) |
| 74 | { |
| 75 | GetOwningLayer().ValidateTensorShapesFromInputs(); |
| 76 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | return GetOutputHandler().IsTensorInfoSet(); |
| 78 | } |
| 79 | |
| 80 | bool OutputSlot::ValidateTensorShape(const TensorShape& shape) const |
| 81 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 82 | ARMNN_ASSERT_MSG(IsTensorInfoSet(), "TensorInfo must be set in order to validate the shape."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | return shape == m_OutputHandler.GetTensorInfo().GetShape(); |
| 84 | } |
| 85 | |
| 86 | int OutputSlot::Connect(InputSlot& destination) |
| 87 | { |
| 88 | destination.SetConnection(this); |
| 89 | m_Connections.push_back(&destination); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 90 | m_EdgeStrategies.push_back(EdgeStrategy::Undefined); |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 91 | return armnn::numeric_cast<int>(m_Connections.size() - 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void OutputSlot::Disconnect(InputSlot& slot) |
| 95 | { |
| 96 | slot.SetConnection(nullptr); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 97 | auto it = std::find(m_Connections.begin(), m_Connections.end(), &slot); |
| 98 | |
| 99 | if (it == m_Connections.end()) |
| 100 | { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | auto idx = std::distance(m_Connections.begin(), it); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | m_Connections.erase(std::remove(m_Connections.begin(), m_Connections.end(), &slot), m_Connections.end()); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 106 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 107 | m_EdgeStrategies.erase(m_EdgeStrategies.begin() + idx); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void OutputSlot::DisconnectAll() |
| 111 | { |
| 112 | while (GetNumConnections() > 0) |
| 113 | { |
| 114 | InputSlot& connection = *GetConnection(0); |
| 115 | Disconnect(connection); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void OutputSlot::MoveAllConnections(OutputSlot& destination) |
| 120 | { |
| 121 | while (GetNumConnections() > 0) |
| 122 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 123 | ARMNN_ASSERT_MSG(m_EdgeStrategies[0] == EdgeStrategy::Undefined, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 124 | "Cannot move connections once memory strategies have be established."); |
| 125 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | InputSlot& connection = *GetConnection(0); |
| 127 | Disconnect(connection); |
| 128 | destination.Connect(connection); |
Finn Williams | 3e6676d | 2020-05-14 13:41:48 +0100 | [diff] [blame] | 129 | destination.GetOutputHandler().SetTensorInfo(GetOutputHandler().GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Derek Lamberti | 27d8307 | 2019-02-05 16:00:08 +0000 | [diff] [blame] | 133 | unsigned int OutputSlot::CalculateIndexOnOwner() const |
| 134 | { |
Matteo Martincigh | 9c5d33a | 2019-02-07 17:52:41 +0000 | [diff] [blame] | 135 | for (unsigned int i = 0; i < GetOwningLayer().GetNumOutputSlots(); i++) |
Derek Lamberti | 27d8307 | 2019-02-05 16:00:08 +0000 | [diff] [blame] | 136 | { |
| 137 | if (GetOwningLayer().GetOutputSlot(i) == (*this)) |
| 138 | { |
| 139 | return i; |
| 140 | } |
| 141 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 142 | ARMNN_ASSERT_MSG(false, "Did not find slot on owner."); |
Derek Lamberti | 27d8307 | 2019-02-05 16:00:08 +0000 | [diff] [blame] | 143 | return 0; // Error |
| 144 | } |
| 145 | |
| 146 | bool OutputSlot::operator==(const OutputSlot& other) const |
| 147 | { |
| 148 | bool isSame = other.GetNumConnections() == GetNumConnections(); |
| 149 | if (!isSame) |
| 150 | { |
| 151 | return false; |
| 152 | } |
| 153 | |
Matteo Martincigh | 9c5d33a | 2019-02-07 17:52:41 +0000 | [diff] [blame] | 154 | for (unsigned int i = 0; i < GetNumConnections(); i++) |
Derek Lamberti | 27d8307 | 2019-02-05 16:00:08 +0000 | [diff] [blame] | 155 | { |
| 156 | isSame &= other.GetConnection(i) == GetConnection(i); |
| 157 | } |
| 158 | return isSame; |
| 159 | } |
| 160 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | void OutputSlot::ValidateConnectionIndex(unsigned int index) const |
| 162 | { |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 163 | if (armnn::numeric_cast<std::size_t>(index) >= m_Connections.size()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 165 | throw InvalidArgumentException((fmt::format("GetConnection: Invalid index {} provided", index))); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 169 | LayerGuid OutputSlot::GetOwningLayerGuid() const |
| 170 | { |
| 171 | return GetOwningLayer().GetGuid(); |
| 172 | } |
| 173 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 174 | void OutputSlot::SetTensorHandleFactory(const ITensorHandleFactory::FactoryId& id) |
| 175 | { |
| 176 | m_TensorHandleFactoryId = id; |
| 177 | } |
| 178 | |
| 179 | ITensorHandleFactory::FactoryId OutputSlot::GetTensorHandleFactoryId() const |
| 180 | { |
| 181 | return m_TensorHandleFactoryId; |
| 182 | } |
| 183 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 184 | void OutputSlot::SetEdgeStrategy(unsigned int connectionIndex, EdgeStrategy strategy) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 185 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 186 | m_EdgeStrategies[connectionIndex] = strategy; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 189 | EdgeStrategy OutputSlot::GetEdgeStrategyForConnection(unsigned int connectionIdx) const |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 190 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 191 | return m_EdgeStrategies[connectionIdx]; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 194 | Layer::Layer(unsigned int numInputSlots, |
| 195 | unsigned int numOutputSlots, |
| 196 | LayerType type, |
| 197 | DataLayout layout, |
| 198 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | : m_OutputHandlers(numOutputSlots) |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 200 | , m_ShapeInferenceMethod(ShapeInferenceMethod::ValidateOnly) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 201 | , m_LayerName(name ? name : "") |
| 202 | , m_Type(type) |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 203 | , m_BackendId() |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 204 | , m_BackendHint(EmptyOptional()) |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 205 | , m_Guid(profiling::ProfilingService::GetNextGuid()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 206 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 207 | IgnoreUnused(layout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 208 | m_InputSlots.reserve(numInputSlots); |
| 209 | for (unsigned int i = 0; i < numInputSlots; ++i) |
| 210 | { |
| 211 | m_InputSlots.emplace_back(*this, i); |
| 212 | } |
| 213 | |
| 214 | m_OutputSlots.reserve(numOutputSlots); |
| 215 | for (unsigned int i = 0; i < numOutputSlots; ++i) |
| 216 | { |
| 217 | m_OutputSlots.emplace_back(*this, m_OutputHandlers[i]); |
| 218 | } |
| 219 | } |
| 220 | |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 221 | Layer::Layer(unsigned int numInputSlots, |
| 222 | unsigned int numOutputSlots, |
| 223 | LayerType type, |
| 224 | const char* name) |
| 225 | : Layer(numInputSlots, numOutputSlots, type, DataLayout::NCHW, name) |
| 226 | { |
| 227 | } |
| 228 | |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 229 | void Layer::CollectWorkloadInputs(WorkloadDataCollector& dataCollector) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 230 | { |
| 231 | for (auto&& inputSlot : GetInputSlots()) |
| 232 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 233 | // The graph must be well-formed at this point. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 234 | ARMNN_ASSERT(inputSlot.GetConnection()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 235 | const OutputHandler& outputHandler = inputSlot.GetConnectedOutputSlot()->GetOutputHandler(); |
| 236 | dataCollector.Push(outputHandler.GetData(), outputHandler.GetTensorInfo()); |
| 237 | } |
| 238 | } |
| 239 | |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 240 | void Layer::CollectWorkloadOutputs(WorkloadDataCollector& dataCollector) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 241 | { |
| 242 | for (auto&& outputHandler : m_OutputHandlers) |
| 243 | { |
| 244 | outputHandler.CollectWorkloadOutputs(dataCollector); |
| 245 | } |
| 246 | } |
| 247 | |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 248 | void Layer::SetAdditionalInfo(QueueDescriptor& descriptor) const |
| 249 | { |
| 250 | descriptor.m_AdditionalInfoObject = m_AdditionalInfoObject.get(); |
| 251 | } |
| 252 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 253 | void Layer::CreateTensorHandles(const TensorHandleFactoryRegistry& registry, |
| 254 | const IWorkloadFactory& workloadFactory, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 255 | const bool IsMemoryManaged) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 256 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 257 | for (unsigned int idx=0; idx < GetNumOutputSlots(); idx++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 258 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 259 | |
| 260 | OutputSlot& slot = GetOutputSlot(idx); |
| 261 | ITensorHandleFactory::FactoryId factoryId = slot.GetTensorHandleFactoryId(); |
| 262 | |
| 263 | OutputHandler& handler = GetOutputHandler(idx); |
| 264 | if (factoryId == ITensorHandleFactory::LegacyFactoryId) |
| 265 | { |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 266 | handler.CreateTensorHandles(workloadFactory, IsMemoryManaged); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | { |
Francis Murtagh | 73d3e2e | 2021-04-29 14:23:04 +0100 | [diff] [blame] | 270 | ITensorHandleFactory* handleFactory; |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 271 | handleFactory = registry.GetFactory(factoryId); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 272 | ARMNN_ASSERT(handleFactory); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 273 | handler.CreateTensorHandles(*handleFactory, IsMemoryManaged); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 274 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 278 | void Layer::ReleaseConstantData() |
| 279 | { |
| 280 | // Now free up the static data. |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 281 | OperateOnConstantTensors([](std::shared_ptr<ConstTensorHandle>& handle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 282 | { |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 283 | handle.reset(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 284 | }); |
| 285 | } |
| 286 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 287 | DataType Layer::GetDataType() const |
| 288 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 289 | if (GetNumInputSlots() > 0) // Ignore the input layer. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 290 | { |
| 291 | return GetInputSlot(0).GetConnection()->GetTensorInfo().GetDataType(); |
| 292 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 293 | return GetOutputSlot(0).GetTensorInfo().GetDataType(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void Layer::ResetPriority() const |
| 297 | { |
| 298 | m_Priority = 0; |
| 299 | m_Visiting = false; |
| 300 | } |
| 301 | |
| 302 | LayerPriority Layer::GetPriority() const |
| 303 | { |
| 304 | constexpr LayerPriority inputPrio = std::numeric_limits<LayerPriority>::lowest(); |
| 305 | constexpr LayerPriority outputPrio = std::numeric_limits<LayerPriority>::max(); |
| 306 | |
| 307 | if (GetType() == LayerType::Input) |
| 308 | { |
| 309 | m_Priority = inputPrio; |
| 310 | } |
| 311 | else if (GetType() == LayerType::Output) |
| 312 | { |
| 313 | m_Priority = outputPrio; |
| 314 | } |
| 315 | else if (m_Priority == 0) |
| 316 | { |
| 317 | if (m_Visiting) |
| 318 | { |
| 319 | throw GraphValidationException("Graph has circular dependencies: cannot walk"); |
| 320 | } |
| 321 | |
| 322 | auto maxPrio = [](const LayerPriority prio, const InputSlot& slot) -> LayerPriority |
| 323 | { |
Matthew Bentham | d78b891d | 2019-04-30 10:17:40 +0100 | [diff] [blame] | 324 | const OutputSlot *outputSlot = slot.GetConnectedOutputSlot(); |
| 325 | if (outputSlot) |
| 326 | { |
| 327 | const Layer& input = outputSlot->GetOwningLayer(); |
| 328 | return std::max(prio, input.GetPriority()); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | // unconnected input slot |
| 333 | return prio; |
| 334 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | m_Visiting = true; |
| 338 | LayerPriority parentPrio = std::accumulate(GetInputSlots().cbegin(), GetInputSlots().cend(), 0U, maxPrio); |
| 339 | m_Visiting = false; |
| 340 | |
| 341 | if (parentPrio >= outputPrio) |
| 342 | { |
| 343 | throw GraphValidationException("Graph has too many edges"); |
| 344 | } |
| 345 | |
| 346 | m_Priority = parentPrio + 1U; |
| 347 | } |
| 348 | |
| 349 | return m_Priority; |
| 350 | } |
| 351 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 352 | void Layer::VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation& location) const |
| 353 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 354 | ARMNN_ASSERT(GetNumInputSlots() == expectedConnections); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 355 | |
| 356 | for (unsigned int i=0; i<expectedConnections; ++i) |
| 357 | { |
| 358 | if (GetInputSlot(i).GetConnection() == nullptr) |
| 359 | { |
| 360 | throw LayerValidationException( |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 361 | fmt::format("Input connection #{0} must be connected " |
| 362 | "for {1} layer {2} {3}", |
| 363 | i, |
| 364 | GetLayerTypeAsCString(this->GetType()), |
| 365 | GetNameStr(), |
| 366 | location.AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 367 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | std::vector<TensorShape> Layer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const |
| 372 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 373 | ARMNN_ASSERT(GetNumInputSlots() != 0); |
| 374 | ARMNN_ASSERT(GetNumOutputSlots() != 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 375 | |
| 376 | // By default we return what we got, meaning the output shape(s) are the same as the input(s). |
| 377 | // This only works if the number of inputs and outputs are the same. Since we are in the Layer |
| 378 | // base class, this means the implementation needs to be overridden in the specific layers for |
| 379 | // the other cases. So the missing implementation justifies the UnimplementedException. |
| 380 | |
| 381 | if (GetNumInputSlots() != GetNumOutputSlots()) |
| 382 | { |
| 383 | throw UnimplementedException( |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 384 | fmt::format("Default implementation for InferOutputShapes can only be used for " |
| 385 | "layers with the same number of input and output slots. This doesn't " |
| 386 | "hold for {0} layer {1} (#inputs={2} #outputs={3}) {4}", |
| 387 | GetLayerTypeAsCString(this->GetType()), |
| 388 | GetNameStr(), |
| 389 | GetNumInputSlots(), |
| 390 | GetNumOutputSlots(), |
| 391 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 392 | } |
| 393 | return inputShapes; |
| 394 | } |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 395 | |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 396 | void Layer::ValidateAndCopyShape(const TensorShape& outputShape, |
| 397 | const TensorShape& inferredShape, |
| 398 | const ShapeInferenceMethod shapeInferenceMethod, |
| 399 | const std::string& layerName, |
| 400 | const unsigned int outputSlotIndex) |
| 401 | { |
| 402 | if (shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly) |
| 403 | { |
| 404 | ConditionalThrowIfNotEqual<LayerValidationException>( |
| 405 | layerName + ": TensorShape set on OutputSlot[0] does not match the inferred shape.", |
| 406 | outputShape, |
| 407 | inferredShape); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | if (outputShape.GetDimensionality() == Dimensionality::Specified) |
| 412 | { |
| 413 | for (unsigned int i = 0; i < outputShape.GetNumDimensions(); ++i) |
| 414 | { |
| 415 | if (outputShape.GetDimensionSpecificity(i) && outputShape[i] != inferredShape[i]) |
| 416 | { |
| 417 | std::stringstream ss; |
| 418 | ss << layerName << ": TensorShape set on OutputSlot[" << outputSlotIndex << |
| 419 | "] does not match the inferred shape at dimension index ["; |
| 420 | ss << i << "] " << outputShape << " != " << inferredShape; |
| 421 | throw LayerValidationException(ss.str()); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | TensorInfo info = GetOutputSlot(outputSlotIndex).GetTensorInfo(); |
| 427 | |
| 428 | armnn::TensorInfo inferredTensorInfo(inferredShape, |
| 429 | info.GetDataType(), |
| 430 | info.GetQuantizationScale(), |
| 431 | info.GetQuantizationOffset()); |
| 432 | |
| 433 | GetOutputSlot(outputSlotIndex).SetTensorInfo(inferredTensorInfo); |
| 434 | } |
| 435 | |
| 436 | void Layer::VerifyShapeInferenceType(const TensorShape& outputShape, ShapeInferenceMethod shapeInferenceMethod) |
| 437 | { |
| 438 | if (shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly) |
| 439 | { |
| 440 | ConditionalThrow<LayerValidationException>( |
| 441 | outputShape.GetDimensionality() != Dimensionality::NotSpecified, |
| 442 | "Dimensionality can not be NotSpecified while using ShapeInferenceMethod::ValidateOnly"); |
| 443 | |
| 444 | ConditionalThrow<LayerValidationException>( |
| 445 | outputShape.AreAllDimensionsSpecified(), |
| 446 | "Unspecified dimension while using ShapeInferenceMethod::ValidateOnly"); |
| 447 | } |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 450 | void Layer::SerializeLayerParameters(ParameterStringifyFunction& fn) const |
| 451 | { |
Rob Hughes | b17220d | 2020-08-28 11:48:35 +0100 | [diff] [blame] | 452 | std::string guid = std::to_string(m_Guid); |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 453 | std::string layerType = GetLayerTypeAsCString(m_Type); |
| 454 | std::string backendId = std::string(m_BackendId); |
Rob Hughes | b17220d | 2020-08-28 11:48:35 +0100 | [diff] [blame] | 455 | if (!(guid.compare("") == 0) && !guid.empty()) |
| 456 | { |
| 457 | fn("Guid", guid); |
| 458 | } |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 459 | if(!(m_LayerName.compare("") == 0) && !m_LayerName.empty()) |
| 460 | { |
| 461 | fn("LayerName",m_LayerName); |
| 462 | } |
| 463 | if(!(layerType.compare("") == 0) && !layerType.empty()) |
| 464 | { |
| 465 | fn("LayerType",layerType); |
| 466 | } |
| 467 | if(!(backendId.compare("") == 0) && !backendId.empty()) |
| 468 | { |
| 469 | fn("BackendID",backendId); |
| 470 | } |
Mike Kelly | 7a0efa5 | 2020-11-17 13:55:01 +0000 | [diff] [blame] | 471 | std::shared_ptr<ActivationDescriptor> |
| 472 | activationDescPtr = GetAdditionalInformation<ActivationDescriptor>(); |
| 473 | |
| 474 | if (activationDescPtr) |
| 475 | { |
| 476 | StringifyLayerParameters<ActivationDescriptor>::Serialize(fn, *activationDescPtr.get()); |
| 477 | } |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 480 | // default implementation of ExecuteStrategy |
| 481 | void Layer::ExecuteStrategy(IStrategy& strategy) const |
| 482 | { |
| 483 | strategy.ExecuteStrategy(this, BaseDescriptor(), {}, GetName()); |
| 484 | } |
| 485 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 486 | const IConnectableLayer& OutputSlot::GetOwningIConnectableLayer() const |
| 487 | { |
| 488 | return m_OwningLayer; |
| 489 | } |
| 490 | |
Francis Murtagh | 9d74ba6 | 2022-01-19 16:31:58 +0000 | [diff] [blame] | 491 | const IConnectableLayer& InputSlot::GetOwningIConnectableLayer() const |
| 492 | { |
| 493 | return m_OwningLayer; |
| 494 | } |
| 495 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 496 | } // namespace armnn |