1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #pragma once
7
8 #include <armnn/Types.hpp>
9 #include <armnn/IRuntime.hpp>
10 #include <armnn/Deprecated.hpp>
11
12 #include <ISubgraphViewConverter.hpp>
13 #include <SubgraphView.hpp>
14 #include <optimizations/Optimization.hpp>
15
16 #include "IBackendContext.hpp"
17 #include "armnn/backends/profiling/IBackendProfiling.hpp"
18 #include "armnn/backends/profiling/IBackendProfilingContext.hpp"
19 #include "IMemoryManager.hpp"
20 #include "ITensorHandleFactory.hpp"
21 #include "OptimizationViews.hpp"
22
23 #include <vector>
24 #include <memory>
25
26 namespace armnn
27 {
28 class IWorkloadFactory;
29 class IMemoryManager;
30 class ILayerSupport;
31
32 struct BackendVersion
33 {
34 uint32_t m_Major;
35 uint32_t m_Minor;
36
BackendVersionarmnn::BackendVersion37 constexpr BackendVersion()
38 : m_Major(0)
39 , m_Minor(0)
40 {}
BackendVersionarmnn::BackendVersion41 constexpr BackendVersion(uint32_t major, uint32_t minor)
42 : m_Major(major)
43 , m_Minor(minor)
44 {}
45
operator ==armnn::BackendVersion46 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
operator <=armnn::BackendVersion53 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 }
59 };
60
operator <<(std::ostream & os,const BackendVersion & backendVersion)61 inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
62 {
63 os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
64
65 return os;
66 }
67
68 class IBackendInternal : public IBackend
69 {
70 protected:
71 /// Creation must be done through a specific
72 /// backend interface.
73 IBackendInternal() = default;
74
75 public:
76 /// Allow backends created by the factory function
77 /// to be destroyed through IBackendInternal.
78 ~IBackendInternal() override = default;
79
80 using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
81 using IBackendContextPtr = std::unique_ptr<IBackendContext>;
82 /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
83 using IBackendProfilingContextPtr = std::shared_ptr<armnn::profiling::IBackendProfilingContext>;
84 using IBackendProfilingPtr = std::unique_ptr<armnn::profiling::IBackendProfiling>;
85 using OptimizationPtr = std::unique_ptr<Optimization>;
86 using Optimizations = std::vector<OptimizationPtr>;
87 using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
88
89 using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>;
90
91 using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
92 using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
93
94 using GraphUniquePtr = std::unique_ptr<Graph>;
95 using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
96
97 ARMNN_NO_DEPRECATE_WARN_BEGIN
98 using ISubGraphConverterPtr ARMNN_DEPRECATED_MSG("This type is no longer supported")
99 = std::unique_ptr<ISubGraphConverter>;
100 using SubGraphUniquePtr ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead")
101 = std::unique_ptr<SubGraph>;
102
103 ARMNN_DEPRECATED_MSG("This method is no longer supported")
104 virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const;
105
106 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
107 virtual Optimizations GetOptimizations() const;
108
109 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
110 virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const;
111 ARMNN_NO_DEPRECATE_WARN_END
112
113 virtual IMemoryManagerUniquePtr CreateMemoryManager() const;
114
115 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
116 const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
117
118 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
119 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const;
120
121 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
122 const IMemoryManagerSharedPtr& memoryManager,
123 const ModelOptions& modelOptions) const;
124
125 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
126 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
127 const ModelOptions& modelOptions) const;
128
129 /// Create the runtime context of the backend
130 ///
131 /// Implementations may return a default-constructed IBackendContextPtr if
132 /// no context is needed at runtime.
133 /// Implementations must throw BackendUnavailableException if the backend
134 /// cannot be used (for example, necessary accelerator hardware is not present).
135 /// The default implementation always returns a default-constructed pointer.
136 virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const;
137
138 virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const;
139
140 /// Create context specifically used for profiling interaction from backends.
141 virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
142 IBackendProfilingPtr& backendProfiling);
143
144 virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
145
146 virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const;
147
148 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const;
149
150 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph,
151 const ModelOptions& modelOptions) const;
152
153 bool SupportsTensorAllocatorAPI() const;
154
155 ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory();
156
157 /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
158 virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const;
159
160 /// (Optional) Register TensorHandleFactories
161 /// Either this method or CreateMemoryManager() and
162 /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
RegisterTensorHandleFactories(class TensorHandleFactoryRegistry &)163 virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {}
164
165 /// Returns the version of the Backend API
GetApiVersion()166 static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); }
167 };
168
169 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
170
171 } // namespace armnn
172