blob: 2045ba2fc04bfb2d3701a938647b50fe44ec2ceb [file] [log] [blame]
David Becke97c6e02018-10-03 13:09:28 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
Matteo Martincighd0dc7702019-08-01 17:09:03 +01005
David Becke97c6e02018-10-03 13:09:28 +01006#pragma once
7
David Beck9efb57d2018-11-05 13:40:33 +00008#include <armnn/Types.hpp>
David Beck1b61be52018-11-08 09:19:14 +00009#include <armnn/IRuntime.hpp>
Matteo Martincighfc598e12019-05-14 10:36:13 +010010#include <armnn/Deprecated.hpp>
Matteo Martincigh49124022019-01-11 13:25:59 +000011
Derek Lambertiff05cc52019-04-26 13:05:17 +010012#include <ISubgraphViewConverter.hpp>
13#include <SubgraphView.hpp>
Derek Lamberti84da38b2019-06-13 11:40:08 +010014#include <optimizations/Optimization.hpp>
Matteo Martincigh49124022019-01-11 13:25:59 +000015
Derek Lamberti84da38b2019-06-13 11:40:08 +010016#include "IBackendContext.hpp"
Colm Donelane49755b2020-01-29 15:22:43 +000017#include "armnn/backends/profiling/IBackendProfiling.hpp"
18#include "armnn/backends/profiling/IBackendProfilingContext.hpp"
Derek Lamberti84da38b2019-06-13 11:40:08 +010019#include "IMemoryManager.hpp"
20#include "ITensorHandleFactory.hpp"
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010021#include "OptimizationViews.hpp"
22
David Beck263e3492018-11-09 14:46:40 +000023#include <vector>
Matteo Martincighd0dc7702019-08-01 17:09:03 +010024#include <memory>
David Becke97c6e02018-10-03 13:09:28 +010025
26namespace armnn
27{
David Beck29c75de2018-10-23 13:35:58 +010028class IWorkloadFactory;
Aron Virginas-Tar56055192018-11-12 18:10:43 +000029class IMemoryManager;
David Beck111b5d92018-11-12 14:59:37 +000030class ILayerSupport;
David Becke97c6e02018-10-03 13:09:28 +010031
Matteo Martincighac60d282019-07-25 15:25:44 +010032struct BackendVersion
33{
34 uint32_t m_Major;
35 uint32_t m_Minor;
36
Matteo Martincighd0dc7702019-08-01 17:09:03 +010037 constexpr BackendVersion()
Matteo Martincighac60d282019-07-25 15:25:44 +010038 : m_Major(0)
39 , m_Minor(0)
40 {}
Matteo Martincighd0dc7702019-08-01 17:09:03 +010041 constexpr BackendVersion(uint32_t major, uint32_t minor)
Matteo Martincighac60d282019-07-25 15:25:44 +010042 : m_Major(major)
43 , m_Minor(minor)
44 {}
45
46 bool operator==(const BackendVersion& other) const
47 {
48 return this == &other ||
49 (this->m_Major == other.m_Major &&
50 this->m_Minor == other.m_Minor);
51 }
52
53 bool operator<=(const BackendVersion& other) const
54 {
55 return this->m_Major < other.m_Major ||
56 (this->m_Major == other.m_Major &&
57 this->m_Minor <= other.m_Minor);
58 }
Jan Eilers15fcc7e2021-07-14 13:50:15 +010059
60 bool operator>=(const BackendVersion& other) const
61 {
62 return this->m_Major > other.m_Major ||
63 (this->m_Major == other.m_Major &&
64 this->m_Minor >= other.m_Minor);
65 }
Matteo Martincighac60d282019-07-25 15:25:44 +010066};
67
68inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
69{
70 os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
71
72 return os;
73}
74
David Becke97c6e02018-10-03 13:09:28 +010075class IBackendInternal : public IBackend
76{
77protected:
Ryan OShea2bbfaa72020-02-12 16:15:27 +000078 /// Creation must be done through a specific
79 /// backend interface.
David Beck6b779f02018-10-09 17:20:21 +010080 IBackendInternal() = default;
David Becke97c6e02018-10-03 13:09:28 +010081
82public:
Ryan OShea2bbfaa72020-02-12 16:15:27 +000083 /// Allow backends created by the factory function
84 /// to be destroyed through IBackendInternal.
David Beck29c75de2018-10-23 13:35:58 +010085 ~IBackendInternal() override = default;
86
87 using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
David Beck1b61be52018-11-08 09:19:14 +000088 using IBackendContextPtr = std::unique_ptr<IBackendContext>;
Ryan OShea2bbfaa72020-02-12 16:15:27 +000089 /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
Colm Donelan1aff3932020-02-05 17:48:59 +000090 using IBackendProfilingContextPtr = std::shared_ptr<armnn::profiling::IBackendProfilingContext>;
91 using IBackendProfilingPtr = std::unique_ptr<armnn::profiling::IBackendProfiling>;
David Beck263e3492018-11-09 14:46:40 +000092 using OptimizationPtr = std::unique_ptr<Optimization>;
93 using Optimizations = std::vector<OptimizationPtr>;
David Beck111b5d92018-11-12 14:59:37 +000094 using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
David Beck1b61be52018-11-08 09:19:14 +000095
Sadik Armagan045f6be2020-09-10 13:37:32 +010096 using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>;
97
Aron Virginas-Tar56055192018-11-12 18:10:43 +000098 using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
99 using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
100
Matteo Martincigh602af092019-05-01 10:31:27 +0100101 using GraphUniquePtr = std::unique_ptr<Graph>;
Derek Lambertiff05cc52019-04-26 13:05:17 +0100102 using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
Matteo Martincighadddddb2019-01-24 14:06:23 +0000103
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100104 ARMNN_NO_DEPRECATE_WARN_BEGIN
105 using ISubGraphConverterPtr ARMNN_DEPRECATED_MSG("This type is no longer supported")
106 = std::unique_ptr<ISubGraphConverter>;
107 using SubGraphUniquePtr ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead")
108 = std::unique_ptr<SubGraph>;
109
110 ARMNN_DEPRECATED_MSG("This method is no longer supported")
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100111 virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const;
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100112
113 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100114 virtual Optimizations GetOptimizations() const;
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100115
116 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100117 virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const;
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100118 ARMNN_NO_DEPRECATE_WARN_END
119
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100120 virtual IMemoryManagerUniquePtr CreateMemoryManager() const;
Aron Virginas-Tar56055192018-11-12 18:10:43 +0000121
122 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
123 const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
124
Narumol Prangnawarat11bd2612019-08-13 10:26:53 +0100125 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
126 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const;
127
Sadik Armagan04a72972020-09-14 15:44:18 +0100128 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
129 const IMemoryManagerSharedPtr& memoryManager,
130 const ModelOptions& modelOptions) const;
131
132 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
133 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
134 const ModelOptions& modelOptions) const;
135
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100136 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
137 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
138 const ModelOptions& modelOptions,
139 MemorySourceFlags inputFlags,
140 MemorySourceFlags outputFlags) const;
141
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000142 /// Create the runtime context of the backend
143 ///
144 /// Implementations may return a default-constructed IBackendContextPtr if
145 /// no context is needed at runtime.
146 /// Implementations must throw BackendUnavailableException if the backend
147 /// cannot be used (for example, necessary accelerator hardware is not present).
148 /// The default implementation always returns a default-constructed pointer.
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100149 virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const;
Aron Virginas-Tar56055192018-11-12 18:10:43 +0000150
Sadik Armagan045f6be2020-09-10 13:37:32 +0100151 virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const;
152
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000153 /// Create context specifically used for profiling interaction from backends.
Colm Donelane49755b2020-01-29 15:22:43 +0000154 virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
Colm Donelan1aff3932020-02-05 17:48:59 +0000155 IBackendProfilingPtr& backendProfiling);
Colm Donelane49755b2020-01-29 15:22:43 +0000156
David Beck111b5d92018-11-12 14:59:37 +0000157 virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
Matteo Martincighadddddb2019-01-24 14:06:23 +0000158
Sadik Armagana25886e2020-09-15 17:17:08 +0100159 virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const;
Sadik Armagan045f6be2020-09-10 13:37:32 +0100160
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100161 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const;
Matteo Martincighfc598e12019-05-14 10:36:13 +0100162
Mike Kelly07810fc2020-11-12 10:58:48 +0000163 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph,
164 const ModelOptions& modelOptions) const;
165
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100166 bool SupportsTensorAllocatorAPI() const;
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100167
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100168 ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory();
Derek Lamberti84da38b2019-06-13 11:40:08 +0100169
170 /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
Matteo Martincighd0dc7702019-08-01 17:09:03 +0100171 virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100172
173 /// (Optional) Register TensorHandleFactories
174 /// Either this method or CreateMemoryManager() and
175 /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
Derek Lamberti901ea112019-12-10 22:07:09 +0000176 virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {}
Matteo Martincighac60d282019-07-25 15:25:44 +0100177
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100178 /// (Optional) Register TensorHandleFactories
179 /// Either this method or CreateMemoryManager() and
180 /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
181 virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry,
182 MemorySourceFlags inputFlags,
183 MemorySourceFlags outputFlags);
184
Matteo Martincighac60d282019-07-25 15:25:44 +0100185 /// Returns the version of the Backend API
Francis Murtagh72930662021-08-17 16:46:41 +0100186 static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); }
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000187
Finn Williamsb9af86e2021-05-26 18:38:12 +0100188 /// Returns a BackendCapability if the backend lists the capability
189 /// The BackendCapability must then be inspected to check whether or not that BackendCapability is supported
190 /// Otherwise returns an EmptyOptional if the BackendCapability is unlisted
191 virtual BackendCapabilities GetCapabilities() const
192 {
193 return BackendCapabilities("IBackendInternal NullCapabilities");
194 };
195
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000196 /// Returns true if backend support the capability false otherwise
Finn Williamsb9af86e2021-05-26 18:38:12 +0100197 ARMNN_DEPRECATED_MSG("This function has been deprecated in favour of GetCapability")
Sadik Armaganf0a6dec2021-03-25 07:46:55 +0000198 virtual bool HasCapability(BackendCapability /*capabilityClass*/) const { return false; }
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100199
200 /// Signals the backend to use a custom memory allocator provided by the user
201 ///
Jan Eilersc1c872f2021-07-22 13:17:04 +0100202 /// \param allocator - a pointer to the provided ICustomAllocator to use with this backend
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100203 /// \param errMsg - Optional string variable to return error messages
204 /// \return - Returns true if switching to custom allocator was successful
Jan Eilersc1c872f2021-07-22 13:17:04 +0100205 virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator,
206 armnn::Optional<std::string&> errMsg)
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100207 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100208 IgnoreUnused(allocator);
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100209 if (errMsg)
210 {
211 std::stringstream message;
212 message << "The backend " << GetId() << " doesn't support using a custom allocator. This error might"
213 " be related with the protected mode if the backend doesn't"
214 " fully support it.";
215
216 errMsg.value() = message.str();
217 }
218 return false;
219 }
David Becke97c6e02018-10-03 13:09:28 +0100220};
221
David Beck29c75de2018-10-23 13:35:58 +0100222using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
223
David Becke97c6e02018-10-03 13:09:28 +0100224} // namespace armnn