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