David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 1 | // |
Matthew Sloyan | 21a6a1a | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 5 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 6 | #pragma once |
| 7 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 8 | #include <armnn/Types.hpp> |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 9 | #include <armnn/IRuntime.hpp> |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 10 | #include <armnn/Deprecated.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 11 | |
Matthew Sloyan | 21a6a1a | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 12 | #include <ExecutionData.hpp> |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 13 | #include <ISubgraphViewConverter.hpp> |
Matthew Sloyan | 21a6a1a | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 14 | #include <WorkingMemDescriptor.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 15 | |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 16 | #include <armnn/backends/IBackendContext.hpp> |
| 17 | #include <armnn/backends/IMemoryManager.hpp> |
| 18 | #include <armnn/backends/ITensorHandleFactory.hpp> |
| 19 | #include <armnn/backends/OptimizationViews.hpp> |
Francis Murtagh | a49ff08 | 2022-01-17 17:08:01 +0000 | [diff] [blame] | 20 | #include <armnn/backends/SubgraphView.hpp> |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 21 | |
| 22 | #include <client/include/backends/IBackendProfiling.hpp> |
| 23 | #include <client/include/backends/IBackendProfilingContext.hpp> |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 24 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 25 | #include <vector> |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 26 | #include <memory> |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 27 | |
| 28 | namespace armnn |
| 29 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 30 | class IWorkloadFactory; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 31 | class IMemoryManager; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 32 | class ILayerSupport; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 33 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 34 | struct BackendVersion |
| 35 | { |
| 36 | uint32_t m_Major; |
| 37 | uint32_t m_Minor; |
| 38 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 39 | constexpr BackendVersion() |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 40 | : m_Major(0) |
| 41 | , m_Minor(0) |
| 42 | {} |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 43 | constexpr BackendVersion(uint32_t major, uint32_t minor) |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 44 | : m_Major(major) |
| 45 | , m_Minor(minor) |
| 46 | {} |
| 47 | |
| 48 | bool operator==(const BackendVersion& other) const |
| 49 | { |
| 50 | return this == &other || |
| 51 | (this->m_Major == other.m_Major && |
| 52 | this->m_Minor == other.m_Minor); |
| 53 | } |
| 54 | |
| 55 | bool operator<=(const BackendVersion& other) const |
| 56 | { |
| 57 | return this->m_Major < other.m_Major || |
| 58 | (this->m_Major == other.m_Major && |
| 59 | this->m_Minor <= other.m_Minor); |
| 60 | } |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 61 | |
| 62 | bool operator>=(const BackendVersion& other) const |
| 63 | { |
| 64 | return this->m_Major > other.m_Major || |
| 65 | (this->m_Major == other.m_Major && |
| 66 | this->m_Minor >= other.m_Minor); |
| 67 | } |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion) |
| 71 | { |
| 72 | os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]"; |
| 73 | |
| 74 | return os; |
| 75 | } |
| 76 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 77 | class IBackendInternal : public IBackend |
| 78 | { |
| 79 | protected: |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 80 | /// Creation must be done through a specific |
| 81 | /// backend interface. |
David Beck | 6b779f0 | 2018-10-09 17:20:21 +0100 | [diff] [blame] | 82 | IBackendInternal() = default; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 83 | |
| 84 | public: |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 85 | /// Allow backends created by the factory function |
| 86 | /// to be destroyed through IBackendInternal. |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 87 | ~IBackendInternal() override = default; |
| 88 | |
| 89 | using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 90 | using IBackendContextPtr = std::unique_ptr<IBackendContext>; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 91 | /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace. |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 92 | using IBackendProfilingContextPtr = std::shared_ptr<arm::pipe::IBackendProfilingContext>; |
| 93 | using IBackendProfilingPtr = std::unique_ptr<arm::pipe::IBackendProfiling>; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 94 | using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 95 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 96 | using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>; |
| 97 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 98 | using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>; |
| 99 | using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>; |
| 100 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 101 | virtual IMemoryManagerUniquePtr CreateMemoryManager() const; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 102 | |
| 103 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 104 | const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0; |
| 105 | |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 106 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 107 | class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const; |
| 108 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 109 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 110 | const IMemoryManagerSharedPtr& memoryManager, |
| 111 | const ModelOptions& modelOptions) const; |
| 112 | |
| 113 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 114 | class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 115 | const ModelOptions& modelOptions) const; |
| 116 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 117 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 118 | class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 119 | const ModelOptions& modelOptions, |
| 120 | MemorySourceFlags inputFlags, |
| 121 | MemorySourceFlags outputFlags) const; |
| 122 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 123 | /// Create the runtime context of the backend |
| 124 | /// |
| 125 | /// Implementations may return a default-constructed IBackendContextPtr if |
| 126 | /// no context is needed at runtime. |
| 127 | /// Implementations must throw BackendUnavailableException if the backend |
| 128 | /// cannot be used (for example, necessary accelerator hardware is not present). |
| 129 | /// The default implementation always returns a default-constructed pointer. |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 130 | virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 131 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 132 | virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const; |
| 133 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 134 | /// Create context specifically used for profiling interaction from backends. |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 135 | virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions, |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 136 | IBackendProfilingPtr& backendProfiling); |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 137 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 138 | virtual ILayerSupportSharedPtr GetLayerSupport() const = 0; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 139 | |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 140 | virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 141 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 142 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const; |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 143 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 144 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph, |
| 145 | const ModelOptions& modelOptions) const; |
| 146 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 147 | bool SupportsTensorAllocatorAPI() const; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 148 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 149 | ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory(); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 150 | |
| 151 | /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order. |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 152 | virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 153 | |
| 154 | /// (Optional) Register TensorHandleFactories |
| 155 | /// Either this method or CreateMemoryManager() and |
| 156 | /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 157 | virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {} |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 158 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 159 | /// (Optional) Register TensorHandleFactories |
| 160 | /// Either this method or CreateMemoryManager() and |
| 161 | /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. |
| 162 | virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry, |
| 163 | MemorySourceFlags inputFlags, |
| 164 | MemorySourceFlags outputFlags); |
| 165 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 166 | /// Returns the version of the Backend API |
Francis Murtagh | 7293066 | 2021-08-17 16:46:41 +0100 | [diff] [blame] | 167 | static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); } |
Sadik Armagan | f0a6dec | 2021-03-25 07:46:55 +0000 | [diff] [blame] | 168 | |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 169 | /// Returns a BackendCapability if the backend lists the capability |
| 170 | /// The BackendCapability must then be inspected to check whether or not that BackendCapability is supported |
| 171 | /// Otherwise returns an EmptyOptional if the BackendCapability is unlisted |
| 172 | virtual BackendCapabilities GetCapabilities() const |
| 173 | { |
| 174 | return BackendCapabilities("IBackendInternal NullCapabilities"); |
| 175 | }; |
| 176 | |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 177 | /// Signals the backend to use a custom memory allocator provided by the user |
| 178 | /// |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 179 | /// \param allocator - a pointer to the provided ICustomAllocator to use with this backend |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 180 | /// \param errMsg - Optional string variable to return error messages |
| 181 | /// \return - Returns true if switching to custom allocator was successful |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 182 | virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator, |
| 183 | armnn::Optional<std::string&> errMsg) |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 184 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 185 | IgnoreUnused(allocator); |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 186 | if (errMsg) |
| 187 | { |
| 188 | std::stringstream message; |
| 189 | message << "The backend " << GetId() << " doesn't support using a custom allocator. This error might" |
| 190 | " be related with the protected mode if the backend doesn't" |
| 191 | " fully support it."; |
| 192 | |
| 193 | errMsg.value() = message.str(); |
| 194 | } |
| 195 | return false; |
| 196 | } |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 197 | |
| 198 | /// Returns the default memory allocator for the backend |
| 199 | /// |
| 200 | /// \return - Returns unique pointer to the Default Allocator of the Backend |
| 201 | virtual std::unique_ptr<ICustomAllocator> GetDefaultAllocator() const |
| 202 | { |
| 203 | throw armnn::Exception("GetDefaultAllocator: Function has not been implemented in backend."); |
| 204 | } |
Sadik Armagan | b7851f9 | 2021-10-06 16:37:02 +0100 | [diff] [blame] | 205 | |
| 206 | /// Returns the number of files cached if backend supports caching |
| 207 | /// |
| 208 | /// \return - Returns 0 if backend does not support caching otherwise number of files cached |
| 209 | virtual unsigned int GetNumberOfCacheFiles() const { return 0; } |
Matthew Sloyan | 21a6a1a | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 210 | |
| 211 | /// Returns ExecutionData for the backend |
| 212 | /// |
| 213 | /// \param workingMemDescriptor - Vectors of input and output TensorHandles for a layer |
| 214 | /// \return - Returns backend specific ExecutionData generated for a layer |
| 215 | virtual ExecutionData CreateExecutionData(WorkingMemDescriptor& workingMemDescriptor) const |
| 216 | { |
| 217 | IgnoreUnused(workingMemDescriptor); |
| 218 | throw armnn::Exception("CreateExecutionData: Function has not been implemented in backend."); |
| 219 | }; |
| 220 | |
| 221 | /// Update the ExecutionData for a layer. It is used to swap in pre-imported tensor handles |
| 222 | /// |
| 223 | /// \param executionData - Backend specific ExecutionData generated for a layer |
| 224 | /// \param workingMemDescriptor - Vectors of input and output TensorHandles for a layer |
| 225 | virtual void UpdateExecutionData(ExecutionData& executionData, WorkingMemDescriptor& workingMemDescriptor) const |
| 226 | { |
| 227 | IgnoreUnused(executionData); |
| 228 | IgnoreUnused(workingMemDescriptor); |
| 229 | throw armnn::Exception("UpdateExecutionData: Function has not been implemented in backend."); |
| 230 | }; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 231 | }; |
| 232 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 233 | using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>; |
| 234 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 235 | } // namespace armnn |