ArmNN
 21.11
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 
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  virtual IMemoryManagerUniquePtr CreateMemoryManager() const;
102 
103  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
104  const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
105 
106  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
107  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const;
108 
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 
117  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
118  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
119  const ModelOptions& modelOptions,
120  MemorySourceFlags inputFlags,
121  MemorySourceFlags outputFlags) const;
122 
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.
130  virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const;
131 
132  virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const;
133 
134  /// Create context specifically used for profiling interaction from backends.
135  virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
136  IBackendProfilingPtr& backendProfiling);
137 
138  virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
139 
140  virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const;
141 
142  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const;
143 
144  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph,
145  const ModelOptions& modelOptions) const;
146 
147  bool SupportsTensorAllocatorAPI() const;
148 
149  ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory();
150 
151  /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
152  virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const;
153 
154  /// (Optional) Register TensorHandleFactories
155  /// Either this method or CreateMemoryManager() and
156  /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
157  virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {}
158 
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 
166  /// Returns the version of the Backend API
167  static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); }
168 
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
173  {
174  return BackendCapabilities("IBackendInternal NullCapabilities");
175  };
176 
177  /// Returns true if backend support the capability false otherwise
178  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated in favour of GetCapability", "22.05")
179  virtual bool HasCapability(BackendCapability /*capabilityClass*/) const { return false; }
180 
181  /// Signals the backend to use a custom memory allocator provided by the user
182  ///
183  /// \param allocator - a pointer to the provided ICustomAllocator to use with this backend
184  /// \param errMsg - Optional string variable to return error messages
185  /// \return - Returns true if switching to custom allocator was successful
186  virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator,
188  {
189  IgnoreUnused(allocator);
190  if (errMsg)
191  {
192  std::stringstream message;
193  message << "The backend " << GetId() << " doesn't support using a custom allocator. This error might"
194  " be related with the protected mode if the backend doesn't"
195  " fully support it.";
196 
197  errMsg.value() = message.str();
198  }
199  return false;
200  }
201 
202  /// Returns the default memory allocator for the backend
203  ///
204  /// \return - Returns unique pointer to the Default Allocator of the Backend
205  virtual std::unique_ptr<ICustomAllocator> GetDefaultAllocator() const
206  {
207  throw armnn::Exception("GetDefaultAllocator: Function has not been implemented in backend.");
208  }
209 
210  /// Returns the number of files cached if backend supports caching
211  ///
212  /// \return - Returns 0 if backend does not support caching otherwise number of files cached
213  virtual unsigned int GetNumberOfCacheFiles() const { return 0; }
214 };
215 
216 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
217 
218 } // namespace armnn
virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry &)
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
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)
std::vector< BackendOptions > ModelOptions
virtual std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const
Returns the default memory allocator for the backend.
Each backend should implement an IBackend.
Definition: Types.hpp:240
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 &&...)
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
virtual unsigned int GetNumberOfCacheFiles() const
Returns the number of files cached if backend supports caching.
std::shared_ptr< IBackendModelContext > IBackendSpecificModelContextPtr
BackendCapability
BackendCapability class.
Definition: Types.hpp:254
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
class ARMNN_DEPRECATED_MSG_REMOVAL_DATE("Use ABI stable IStrategy instead.", "22.05") ILayerVisitor
BackendOptions BackendCapabilities
bool operator==(const BackendVersion &other) const
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
Struct for the users to pass backend specific options.
virtual BackendCapabilities GetCapabilities() const
Returns a BackendCapability if the backend lists the capability The BackendCapability must then be in...
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
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