ArmNN
 21.08
IBackendInternal.hpp
Go to the documentation of this file.
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 
13 #include <SubgraphView.hpp>
15 
16 #include "IBackendContext.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 
33 {
34  uint32_t m_Major;
35  uint32_t m_Minor;
36 
37  constexpr BackendVersion()
38  : m_Major(0)
39  , m_Minor(0)
40  {}
41  constexpr BackendVersion(uint32_t major, uint32_t minor)
42  : 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  }
59 
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  }
66 };
67 
68 inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
69 {
70  os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
71 
72  return os;
73 }
74 
75 class IBackendInternal : public IBackend
76 {
77 protected:
78  /// Creation must be done through a specific
79  /// backend interface.
80  IBackendInternal() = default;
81 
82 public:
83  /// Allow backends created by the factory function
84  /// to be destroyed through IBackendInternal.
85  ~IBackendInternal() override = default;
86 
87  using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
88  using IBackendContextPtr = std::unique_ptr<IBackendContext>;
89  /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
90  using IBackendProfilingContextPtr = std::shared_ptr<armnn::profiling::IBackendProfilingContext>;
91  using IBackendProfilingPtr = std::unique_ptr<armnn::profiling::IBackendProfiling>;
92  using OptimizationPtr = std::unique_ptr<Optimization>;
93  using Optimizations = std::vector<OptimizationPtr>;
94  using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
95 
96  using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>;
97 
98  using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
99  using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
100 
101  using GraphUniquePtr = std::unique_ptr<Graph>;
102  using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
103 
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")
111  virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const;
112 
113  ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
114  virtual Optimizations GetOptimizations() const;
115 
116  ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
117  virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const;
119 
120  virtual IMemoryManagerUniquePtr CreateMemoryManager() const;
121 
122  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
123  const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
124 
125  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
126  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const;
127 
128  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 
136  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
137  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
138  const ModelOptions& modelOptions,
139  MemorySourceFlags inputFlags,
140  MemorySourceFlags outputFlags) const;
141 
142  /// 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.
149  virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const;
150 
151  virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const;
152 
153  /// Create context specifically used for profiling interaction from backends.
154  virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
155  IBackendProfilingPtr& backendProfiling);
156 
157  virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
158 
159  virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const;
160 
161  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const;
162 
163  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph,
164  const ModelOptions& modelOptions) const;
165 
166  bool SupportsTensorAllocatorAPI() const;
167 
168  ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory();
169 
170  /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
171  virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const;
172 
173  /// (Optional) Register TensorHandleFactories
174  /// Either this method or CreateMemoryManager() and
175  /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
176  virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {}
177 
178  /// (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 
185  /// Returns the version of the Backend API
186  static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); }
187 
188  /// 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
192  {
193  return BackendCapabilities("IBackendInternal NullCapabilities");
194  };
195 
196  /// Returns true if backend support the capability false otherwise
197  ARMNN_DEPRECATED_MSG("This function has been deprecated in favour of GetCapability")
198  virtual bool HasCapability(BackendCapability /*capabilityClass*/) const { return false; }
199 
200  /// Signals the backend to use a custom memory allocator provided by the user
201  ///
202  /// \param allocator - a pointer to the provided ICustomAllocator to use with this backend
203  /// \param errMsg - Optional string variable to return error messages
204  /// \return - Returns true if switching to custom allocator was successful
205  virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator,
207  {
208  IgnoreUnused(allocator);
209  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  }
220 };
221 
222 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
223 
224 } // namespace armnn
bool HasCapability(const std::string &name, const BackendCapabilities &capabilities)
Convenience function to check if a capability exists in a BackendCapabilites struct.
bool operator>=(const BackendVersion &other) const
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
std::vector< OptimizationPtr > Optimizations
bool operator<=(const BackendVersion &other) const
std::unique_ptr< Optimization > OptimizationPtr
constexpr BackendVersion(uint32_t major, uint32_t minor)
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
std::vector< BackendOptions > ModelOptions
Each backend should implement an IBackend.
Definition: Types.hpp:207
std::ostream & operator<<(std::ostream &os, const std::vector< Compute > &compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:47
virtual bool UseCustomMemoryAllocator(std::shared_ptr< ICustomAllocator > allocator, armnn::Optional< std::string &> errMsg)
Signals the backend to use a custom memory allocator provided by the user.
unsigned int MemorySourceFlags
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
void IgnoreUnused(Ts &&...)
std::unique_ptr< SubgraphView > SubgraphViewUniquePtr
The SubgraphView class represents a subgraph of a Graph.
static constexpr BackendVersion GetApiVersion()
Returns the version of the Backend API.
std::unique_ptr< armnn::profiling::IBackendProfiling > IBackendProfilingPtr
ITensorHandleFactory::FactoryId FactoryId
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
std::shared_ptr< IBackendModelContext > IBackendSpecificModelContextPtr
BackendCapability
BackendCapability class.
Definition: Types.hpp:221
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
BackendOptions BackendCapabilities
bool operator==(const BackendVersion &other) const
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
Struct for the users to pass backend specific options.
std::unique_ptr< SubGraph > instead
virtual BackendCapabilities GetCapabilities() const
Returns a BackendCapability if the backend lists the capability The BackendCapability must then be in...
#define ARMNN_DEPRECATED_MSG(message)
Definition: Deprecated.hpp:43
std::unique_ptr< Graph > GraphUniquePtr
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr
std::shared_ptr< armnn::profiling::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we&#39;ll keep it in the backend namespace...
std::unique_ptr< IBackendContext > IBackendContextPtr