ArmNN
 22.02
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 
21 
22 #include <vector>
23 #include <memory>
24 
25 namespace armnn
26 {
27 class IWorkloadFactory;
28 class IMemoryManager;
29 class ILayerSupport;
30 
32 {
33  uint32_t m_Major;
34  uint32_t m_Minor;
35 
36  constexpr BackendVersion()
37  : m_Major(0)
38  , m_Minor(0)
39  {}
40  constexpr BackendVersion(uint32_t major, uint32_t minor)
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  }
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  }
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 
74 class IBackendInternal : public IBackend
75 {
76 protected:
77  /// Creation must be done through a specific
78  /// backend interface.
79  IBackendInternal() = default;
80 
81 public:
82  /// Allow backends created by the factory function
83  /// to be destroyed through IBackendInternal.
84  ~IBackendInternal() override = default;
85 
86  using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
87  using IBackendContextPtr = std::unique_ptr<IBackendContext>;
88  /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
89  using IBackendProfilingContextPtr = std::shared_ptr<armnn::profiling::IBackendProfilingContext>;
90  using IBackendProfilingPtr = std::unique_ptr<armnn::profiling::IBackendProfiling>;
91  using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
92 
93  using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>;
94 
95  using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
96  using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
97 
98  virtual IMemoryManagerUniquePtr CreateMemoryManager() const;
99 
100  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
101  const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
102 
103  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
104  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const;
105 
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 
114  virtual IWorkloadFactoryPtr CreateWorkloadFactory(
115  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry,
116  const ModelOptions& modelOptions,
117  MemorySourceFlags inputFlags,
118  MemorySourceFlags outputFlags) const;
119 
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.
127  virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const;
128 
129  virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const;
130 
131  /// Create context specifically used for profiling interaction from backends.
132  virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
133  IBackendProfilingPtr& backendProfiling);
134 
135  virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
136 
137  virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const;
138 
139  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const;
140 
141  virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph,
142  const ModelOptions& modelOptions) const;
143 
144  bool SupportsTensorAllocatorAPI() const;
145 
146  ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory();
147 
148  /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
149  virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const;
150 
151  /// (Optional) Register TensorHandleFactories
152  /// Either this method or CreateMemoryManager() and
153  /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
154  virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {}
155 
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 
163  /// Returns the version of the Backend API
164  static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); }
165 
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
170  {
171  return BackendCapabilities("IBackendInternal NullCapabilities");
172  };
173 
174  /// Returns true if backend support the capability false otherwise
175  ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated in favour of GetCapability", "22.05")
176  virtual bool HasCapability(BackendCapability /*capabilityClass*/) const { return false; }
177 
178  /// Signals the backend to use a custom memory allocator provided by the user
179  ///
180  /// \param allocator - a pointer to the provided ICustomAllocator to use with this backend
181  /// \param errMsg - Optional string variable to return error messages
182  /// \return - Returns true if switching to custom allocator was successful
183  virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator,
185  {
186  IgnoreUnused(allocator);
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  }
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  }
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; }
211 };
212 
213 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
214 
215 } // 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
bool operator<=(const BackendVersion &other) const
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
ARMNN_NO_DEPRECATE_WARN_BEGIN struct ARMNN_DEPRECATED_MSG_REMOVAL_DATE("ResizeBilinearQueueDescriptor is deprecated use ResizeQueueDescriptor instead", "22.08") ResizeBilinearQueueDescriptor
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