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