telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [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 | #pragma once |
| 6 | |
| 7 | #include "LayerFwd.hpp" |
| 8 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 9 | #include <armnn/backends/ITensorHandleFactory.hpp> |
| 10 | #include <OutputHandler.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 11 | #include <backendsCommon/TensorHandleFactoryRegistry.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <backendsCommon/WorkloadDataCollector.hpp> |
| 13 | #include <backendsCommon/WorkloadInfo.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | #include "InternalTypes.hpp" |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 15 | #include "SerializeLayerParameters.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | |
| 17 | #include <armnn/Types.hpp> |
| 18 | #include <armnn/Tensor.hpp> |
| 19 | #include <armnn/INetwork.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 20 | #include <armnn/utility/IgnoreUnused.hpp> |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 21 | #include <armnn/utility/NumericCast.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 22 | #include <armnn/utility/PolymorphicDowncast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 25 | #include <functional> |
| 26 | #include <iostream> |
| 27 | #include <list> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | #include <memory> |
| 29 | #include <string> |
| 30 | #include <vector> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 31 | #include <armnn/backends/WorkloadData.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | namespace armnn |
| 34 | { |
| 35 | |
| 36 | class IWorkload; |
| 37 | class IWorkloadFactory; |
| 38 | class Layer; |
| 39 | class Graph; |
| 40 | |
| 41 | class InputSlot final : public IInputSlot |
| 42 | { |
| 43 | public: |
| 44 | explicit InputSlot(Layer& owner, unsigned int slotIndex) |
| 45 | : m_OwningLayer(owner) |
| 46 | , m_Connection(nullptr) |
| 47 | , m_SlotIndex(slotIndex) |
| 48 | {} |
| 49 | |
| 50 | ~InputSlot(); |
| 51 | |
| 52 | Layer& GetOwningLayer() const { return m_OwningLayer; } |
| 53 | unsigned int GetSlotIndex() const { return m_SlotIndex; } |
| 54 | |
| 55 | const OutputSlot* GetConnectedOutputSlot() const { return m_Connection; } |
| 56 | OutputSlot* GetConnectedOutputSlot() { return m_Connection; } |
| 57 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | /// Links the slot to an output slot or breaks an existing link if passing nullptr. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | void SetConnection(OutputSlot* source) |
| 60 | { |
| 61 | if (m_Connection != nullptr && source != nullptr) |
| 62 | { |
| 63 | throw InvalidArgumentException("Tried to connect an output slot to an input slot, " |
| 64 | "but the latter already has a connection"); |
| 65 | } |
| 66 | m_Connection = source; |
| 67 | } |
| 68 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | // Inserts single-output existing layer at this point in the graph. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | void Insert(Layer& layer); |
| 71 | |
| 72 | // IInputSlot |
| 73 | |
| 74 | const IOutputSlot* GetConnection() const override; |
| 75 | IOutputSlot* GetConnection() override; |
| 76 | |
| 77 | private: |
| 78 | Layer& m_OwningLayer; |
| 79 | OutputSlot* m_Connection; |
| 80 | const unsigned int m_SlotIndex; |
| 81 | }; |
| 82 | |
| 83 | class OutputSlot final : public IOutputSlot |
| 84 | { |
| 85 | public: |
| 86 | explicit OutputSlot(Layer& owner, OutputHandler& outputHandler) |
| 87 | : m_OwningLayer(owner) |
| 88 | , m_OutputHandler(outputHandler) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 89 | , m_TensorHandleFactoryId(ITensorHandleFactory::LegacyFactoryId) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | {} |
| 91 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 92 | OutputSlot(const OutputSlot&) = delete; |
| 93 | OutputSlot& operator=(const OutputSlot&) = delete; |
Matthew Bentham | 13757bd | 2020-02-10 17:23:39 +0000 | [diff] [blame] | 94 | OutputSlot& operator=(OutputSlot&&) = delete; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 95 | |
| 96 | OutputSlot(OutputSlot&&) = default; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 97 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 98 | ~OutputSlot() |
| 99 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 100 | try |
| 101 | { |
| 102 | // Coverity fix: DisconnectAll() may throw uncaught exceptions. |
| 103 | DisconnectAll(); |
| 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
| 107 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 108 | // exception of type std::length_error. |
| 109 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 110 | std::cerr << "WARNING: An error has occurred when disconnecting all output slots: " |
| 111 | << e.what() << std::endl; |
| 112 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Layer& GetOwningLayer() const { return m_OwningLayer; } |
| 116 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 117 | const IConnectableLayer& GetOwningIConnectableLayer() const override; |
| 118 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 119 | LayerGuid GetOwningLayerGuid() const override; |
| 120 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | const OutputHandler& GetOutputHandler() const { return m_OutputHandler; } |
| 122 | OutputHandler& GetOutputHandler() { return m_OutputHandler; } |
| 123 | |
| 124 | int Connect(InputSlot& destination); |
| 125 | void Disconnect(InputSlot& slot); |
| 126 | |
| 127 | const std::vector<InputSlot*>& GetConnections() const { return m_Connections; } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 128 | const std::vector<EdgeStrategy>& GetEdgeStrategies() const { return m_EdgeStrategies; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | |
| 130 | bool ValidateTensorShape(const TensorShape& shape) const; |
| 131 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | // Disconnect all conections. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 133 | void DisconnectAll(); |
| 134 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | /// Moves all connections to another OutputSlot. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | void MoveAllConnections(OutputSlot& destination); |
| 137 | |
| 138 | // IOutputSlot |
| 139 | |
Matthew Sloyan | 0663d66 | 2020-09-14 11:47:26 +0100 | [diff] [blame] | 140 | unsigned int GetNumConnections() const override { return armnn::numeric_cast<unsigned int>(m_Connections.size()); } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | const InputSlot* GetConnection(unsigned int index) const override; |
| 142 | InputSlot* GetConnection(unsigned int index) override; |
| 143 | |
| 144 | void SetTensorInfo(const TensorInfo& tensorInfo) override; |
| 145 | const TensorInfo& GetTensorInfo() const override; |
| 146 | bool IsTensorInfoSet() const override; |
| 147 | |
| 148 | int Connect(IInputSlot& destination) override |
| 149 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 150 | return Connect(*PolymorphicDowncast<InputSlot*>(&destination)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void Disconnect(IInputSlot& slot) override |
| 154 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 155 | return Disconnect(*PolymorphicDowncast<InputSlot*>(&slot)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 158 | unsigned int CalculateIndexOnOwner() const override; |
Derek Lamberti | 27d8307 | 2019-02-05 16:00:08 +0000 | [diff] [blame] | 159 | |
| 160 | bool operator==(const OutputSlot& other) const; |
| 161 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 162 | void SetTensorHandleFactory(const ITensorHandleFactory::FactoryId& id); |
| 163 | ITensorHandleFactory::FactoryId GetTensorHandleFactoryId() const; |
| 164 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 165 | void SetEdgeStrategy(unsigned int connectionIndex, EdgeStrategy strategy); |
| 166 | EdgeStrategy GetEdgeStrategyForConnection(unsigned int connectionIdx) const; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 167 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | private: |
| 169 | void ValidateConnectionIndex(unsigned int index) const; |
| 170 | |
| 171 | Layer& m_OwningLayer; |
| 172 | OutputHandler& m_OutputHandler; |
| 173 | std::vector<InputSlot*> m_Connections; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 174 | |
| 175 | ITensorHandleFactory::FactoryId m_TensorHandleFactoryId; |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 176 | std::vector<EdgeStrategy> m_EdgeStrategies; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | // InputSlot inlines that need OutputSlot declaration. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 180 | |
| 181 | inline InputSlot::~InputSlot() |
| 182 | { |
| 183 | if (m_Connection != nullptr) |
| 184 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 185 | try |
| 186 | { |
| 187 | // Coverity fix: Disconnect() may throw uncaught exceptions. |
| 188 | m_Connection->Disconnect(*this); |
| 189 | } |
| 190 | catch (const std::exception& e) |
| 191 | { |
| 192 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 193 | // exception of type std::length_error. |
| 194 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 195 | std::cerr << "WARNING: An error has occurred when disconnecting an input slot: " |
| 196 | << e.what() << std::endl; |
| 197 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | inline const IOutputSlot* InputSlot::GetConnection() const { return GetConnectedOutputSlot(); } |
| 202 | inline IOutputSlot* InputSlot::GetConnection() { return GetConnectedOutputSlot(); } |
| 203 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 204 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 205 | class ScopedTensorHandle; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 207 | // Base layer class |
| 208 | |
| 209 | using LayerPriority = unsigned int; |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 210 | using AdditionalInfoObjectPtr = std::shared_ptr<void>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 211 | |
| 212 | class Layer : public IConnectableLayer |
| 213 | { |
| 214 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 215 | /// @param name - Optional name for the layer (may be nullptr). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 216 | Layer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char* name); |
Derek Lamberti | 0cff163 | 2018-09-18 16:02:25 +0100 | [diff] [blame] | 217 | Layer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, DataLayout layout, const char* name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 218 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 219 | void ExecuteStrategy(IStrategy& strategy) const override; |
| 220 | |
| 221 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 222 | const std::string& GetNameStr() const |
| 223 | { |
| 224 | return m_LayerName; |
| 225 | } |
| 226 | |
| 227 | const OutputHandler& GetOutputHandler(unsigned int i = 0) const |
| 228 | { |
| 229 | return m_OutputHandlers[i]; |
| 230 | } |
| 231 | |
| 232 | OutputHandler& GetOutputHandler(unsigned int i = 0) |
| 233 | { |
| 234 | return const_cast<OutputHandler&>(const_cast<const Layer*>(this)->GetOutputHandler(i)); |
| 235 | } |
| 236 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 237 | ShapeInferenceMethod GetShapeInferenceMethod() const { return m_ShapeInferenceMethod; }; |
| 238 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 239 | const std::vector<InputSlot>& GetInputSlots() const { return m_InputSlots; } |
| 240 | const std::vector<OutputSlot>& GetOutputSlots() const { return m_OutputSlots; } |
| 241 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 242 | // Allows non-const access to input slots, but don't expose vector (vector size is fixed at layer construction). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 243 | std::vector<InputSlot>::iterator BeginInputSlots() { return m_InputSlots.begin(); } |
| 244 | std::vector<InputSlot>::iterator EndInputSlots() { return m_InputSlots.end(); } |
| 245 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 246 | // Allows non-const access to output slots, but don't expose vector (vector size is fixed at layer construction). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 247 | std::vector<OutputSlot>::iterator BeginOutputSlots() { return m_OutputSlots.begin(); } |
| 248 | std::vector<OutputSlot>::iterator EndOutputSlots() { return m_OutputSlots.end(); } |
| 249 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 250 | // Checks whether the outputs of this layer don't have any connection. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 251 | bool IsOutputUnconnected() |
| 252 | { |
| 253 | unsigned int numConnections = 0; |
| 254 | |
| 255 | for (auto&& output : GetOutputSlots()) |
| 256 | { |
| 257 | numConnections += output.GetNumConnections(); |
| 258 | } |
| 259 | |
| 260 | return (GetNumOutputSlots() > 0) && (numConnections == 0); |
| 261 | } |
| 262 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 263 | // Used for sorting. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 264 | void ResetPriority() const; |
| 265 | LayerPriority GetPriority() const; |
| 266 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 267 | LayerType GetType() const override { return m_Type; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 268 | |
| 269 | DataType GetDataType() const; |
| 270 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 271 | const BackendId& GetBackendId() const { return m_BackendId; } |
| 272 | void SetBackendId(const BackendId& id) { m_BackendId = id; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 273 | |
| 274 | // Virtuals |
| 275 | |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 276 | virtual std::unique_ptr<IWorkload> CreateWorkload(const IWorkloadFactory& factory) const = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 277 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 278 | virtual void CreateTensorHandles(const TensorHandleFactoryRegistry& registry, |
| 279 | const IWorkloadFactory& factory, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 280 | const bool IsMemoryManaged = true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 281 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 282 | /// Creates a dynamically-allocated copy of this layer. |
| 283 | /// @param graph - The Graph into which this Layer is being cloned. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 284 | virtual Layer* Clone(Graph& graph) const = 0; |
| 285 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 286 | void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation& location) const; |
| 287 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 288 | virtual void ValidateTensorShapesFromInputs() = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 289 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 290 | std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const override; |
| 291 | |
| 292 | /// Helper to serialize the layer parameters to string. |
| 293 | /// (currently used in DotSerializer and company). |
Andre Ghattas | 23ae2ea | 2019-08-07 12:18:38 +0100 | [diff] [blame] | 294 | virtual void SerializeLayerParameters(ParameterStringifyFunction& fn) const; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 295 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 296 | // Free up the constant source data |
| 297 | virtual void ReleaseConstantData(); |
| 298 | |
| 299 | template<typename Op> |
| 300 | void OperateOnConstantTensors(Op op) |
| 301 | { |
| 302 | for (auto constant : GetConstantTensorsByRef()) |
| 303 | { |
| 304 | if (constant.get()) |
| 305 | { |
| 306 | op(constant); |
| 307 | } |
| 308 | } |
| 309 | }; |
| 310 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 311 | // IConnectableLayer |
| 312 | |
| 313 | const char* GetName() const override { return m_LayerName.c_str(); } |
| 314 | |
| 315 | unsigned int GetNumInputSlots() const override { return static_cast<unsigned int>(m_InputSlots.size()); } |
| 316 | unsigned int GetNumOutputSlots() const override { return static_cast<unsigned int>(m_OutputSlots.size()); } |
| 317 | |
| 318 | const InputSlot& GetInputSlot(unsigned int index) const override { return m_InputSlots.at(index); } |
| 319 | InputSlot& GetInputSlot(unsigned int index) override { return m_InputSlots.at(index); } |
| 320 | const OutputSlot& GetOutputSlot(unsigned int index = 0) const override { return m_OutputSlots.at(index); } |
| 321 | OutputSlot& GetOutputSlot(unsigned int index = 0) override { return m_OutputSlots.at(index); } |
| 322 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 323 | void SetGuid(LayerGuid guid) { m_Guid = guid; } |
| 324 | LayerGuid GetGuid() const final { return m_Guid; } |
| 325 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 326 | void AddRelatedLayerName(const std::string layerName) { m_RelatedLayerNames.emplace_back(layerName); } |
| 327 | |
| 328 | const std::list<std::string>& GetRelatedLayerNames() { return m_RelatedLayerNames; } |
| 329 | |
Derek Lamberti | 8106b7c | 2019-05-07 21:33:30 +0100 | [diff] [blame] | 330 | virtual void Reparent(Graph& dest, std::list<Layer*>::const_iterator iterator) = 0; |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 331 | |
| 332 | void BackendSelectionHint(Optional<BackendId> backend) final |
| 333 | { |
| 334 | m_BackendHint = backend; |
| 335 | } |
| 336 | Optional<BackendId> GetBackendHint() const { return m_BackendHint; } |
| 337 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 338 | void SetShapeInferenceMethod(ShapeInferenceMethod shapeInferenceMethod) |
| 339 | { |
| 340 | m_ShapeInferenceMethod = shapeInferenceMethod; |
| 341 | } |
| 342 | |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 343 | template<typename T> |
Mike Kelly | 7a0efa5 | 2020-11-17 13:55:01 +0000 | [diff] [blame] | 344 | std::shared_ptr<T> GetAdditionalInformation() const |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 345 | { |
| 346 | return std::static_pointer_cast<T>(m_AdditionalInfoObject); |
| 347 | } |
| 348 | |
| 349 | void SetAdditionalInfoForObject(const AdditionalInfoObjectPtr& additionalInfo) |
| 350 | { |
| 351 | m_AdditionalInfoObject = additionalInfo; |
| 352 | } |
| 353 | |
Jim Flynn | e466596 | 2022-01-31 16:08:53 +0000 | [diff] [blame^] | 354 | virtual const BaseDescriptor& GetParameters() const override { return m_NullDescriptor; } |
| 355 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 356 | protected: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 357 | // Graph needs access to the virtual destructor. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 358 | friend class Graph; |
| 359 | virtual ~Layer() = default; |
| 360 | |
| 361 | template <typename QueueDescriptor> |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 362 | void CollectQueueDescriptorInputs(QueueDescriptor& descriptor, WorkloadInfo& info) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 363 | { |
| 364 | WorkloadDataCollector dataCollector(descriptor.m_Inputs, info.m_InputTensorInfos); |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 365 | CollectWorkloadInputs(dataCollector); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | template <typename QueueDescriptor> |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 369 | void CollectQueueDescriptorOutputs(QueueDescriptor& descriptor, WorkloadInfo& info) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 370 | { |
| 371 | WorkloadDataCollector dataCollector(descriptor.m_Outputs, info.m_OutputTensorInfos); |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 372 | CollectWorkloadOutputs(dataCollector); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 375 | void ValidateAndCopyShape(const TensorShape& outputShape, |
| 376 | const TensorShape& inferredShape, |
| 377 | const ShapeInferenceMethod shapeInferenceMethod, |
| 378 | const std::string& layerName, |
| 379 | const unsigned int outputSlotIndex = 0); |
| 380 | |
| 381 | void VerifyShapeInferenceType(const TensorShape& outputShape, ShapeInferenceMethod shapeInferenceMethod); |
| 382 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 383 | /// Helper function to reduce duplication in *Layer::CreateWorkload. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 384 | template <typename QueueDescriptor> |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 385 | WorkloadInfo PrepInfoAndDesc(QueueDescriptor& descriptor) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 386 | { |
| 387 | WorkloadInfo info; |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 388 | CollectQueueDescriptorInputs(descriptor, info); |
| 389 | CollectQueueDescriptorOutputs(descriptor, info); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 390 | return info; |
| 391 | } |
| 392 | |
| 393 | template <typename LayerType, typename ... Params> |
| 394 | LayerType* CloneBase(Graph& graph, Params&& ... params) const; |
| 395 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 396 | // Retrieve the Handles to the constants |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 397 | using ConstantTensors = std::vector<std::reference_wrapper<std::shared_ptr<ConstTensorHandle>>>; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 398 | virtual ConstantTensors GetConstantTensorsByRef() {return ConstantTensors(); }; |
| 399 | |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 400 | // "Blob" |
| 401 | AdditionalInfoObjectPtr m_AdditionalInfoObject; |
| 402 | |
| 403 | // Utility method to set a pointer in the queueDescriptor to the "blob" location in the layer |
| 404 | void SetAdditionalInfo(QueueDescriptor& descriptor) const; |
| 405 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 406 | private: |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 407 | void CollectWorkloadInputs(WorkloadDataCollector& dataCollector) const; |
| 408 | void CollectWorkloadOutputs(WorkloadDataCollector& dataCollector) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 409 | |
| 410 | protected: |
| 411 | std::vector<OutputHandler> m_OutputHandlers; |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 412 | ShapeInferenceMethod m_ShapeInferenceMethod; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 413 | |
| 414 | private: |
| 415 | const std::string m_LayerName; |
| 416 | |
| 417 | std::vector<InputSlot> m_InputSlots; |
| 418 | std::vector<OutputSlot> m_OutputSlots; |
| 419 | |
| 420 | const LayerType m_Type; |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 421 | BackendId m_BackendId; |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 422 | Optional<BackendId> m_BackendHint; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 423 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 424 | /// Used for sorting. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 425 | mutable LayerPriority m_Priority = 0; |
| 426 | mutable bool m_Visiting = false; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 427 | |
| 428 | LayerGuid m_Guid; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 429 | |
| 430 | std::list<std::string> m_RelatedLayerNames; |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 431 | |
Jim Flynn | e466596 | 2022-01-31 16:08:53 +0000 | [diff] [blame^] | 432 | /// returned by layers which have no parameters associated with them. |
| 433 | /// has to be a member as it is returned as a const reference |
| 434 | /// declared static so that there is only ever one of them in memory |
| 435 | static NullDescriptor m_NullDescriptor; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 438 | // A layer user-provided data can be bound to (e.g. inputs, outputs). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 439 | class BindableLayer : public Layer |
| 440 | { |
| 441 | public: |
| 442 | BindableLayer(unsigned int numInputSlots, |
| 443 | unsigned int numOutputSlots, |
| 444 | LayerType type, |
| 445 | const char* name, |
| 446 | LayerBindingId id) |
| 447 | : Layer(numInputSlots, numOutputSlots, type, name) |
| 448 | , m_Id(id) |
| 449 | { |
| 450 | } |
| 451 | |
| 452 | LayerBindingId GetBindingId() const { return m_Id; }; |
| 453 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 454 | void ExecuteStrategy(IStrategy& strategy) const override |
| 455 | { |
| 456 | strategy.ExecuteStrategy(this, BaseDescriptor(), {}, GetName(), GetBindingId()); |
| 457 | } |
| 458 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 459 | protected: |
| 460 | ~BindableLayer() = default; |
| 461 | |
| 462 | private: |
| 463 | LayerBindingId m_Id; |
| 464 | }; |
| 465 | |
| 466 | } |