ArmNN
 21.11
MockBackend Class Reference

#include <MockBackend.hpp>

Inheritance diagram for MockBackend:
IBackendInternal IBackend

Public Member Functions

 MockBackend ()=default
 
 ~MockBackend ()=default
 
const BackendIdGetId () const override
 
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager () const override
 
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory (const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
 
IBackendInternal::IBackendContextPtr CreateBackendContext (const IRuntime::CreationOptions &) const override
 Create the runtime context of the backend. More...
 
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext (const IRuntime::CreationOptions &creationOptions, IBackendProfilingPtr &backendProfiling) override
 Create context specifically used for profiling interaction from backends. More...
 
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport () const override
 
OptimizationViews OptimizeSubgraphView (const SubgraphView &subgraph) const override
 
std::unique_ptr< ICustomAllocatorGetDefaultAllocator () const override
 Returns the default memory allocator for the backend. More...
 
- Public Member Functions inherited from IBackendInternal
 ~IBackendInternal () override=default
 Allow backends created by the factory function to be destroyed through IBackendInternal. More...
 
virtual IWorkloadFactoryPtr CreateWorkloadFactory (class TensorHandleFactoryRegistry &tensorHandleFactoryRegistry) const
 
virtual IWorkloadFactoryPtr CreateWorkloadFactory (const IMemoryManagerSharedPtr &memoryManager, const ModelOptions &modelOptions) const
 
virtual IWorkloadFactoryPtr CreateWorkloadFactory (class TensorHandleFactoryRegistry &tensorHandleFactoryRegistry, const ModelOptions &modelOptions) const
 
virtual IWorkloadFactoryPtr CreateWorkloadFactory (class TensorHandleFactoryRegistry &tensorHandleFactoryRegistry, const ModelOptions &modelOptions, MemorySourceFlags inputFlags, MemorySourceFlags outputFlags) const
 
virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext (const ModelOptions &modelOptions) const
 
virtual ILayerSupportSharedPtr GetLayerSupport (const ModelOptions &modelOptions) const
 
virtual OptimizationViews OptimizeSubgraphView (const SubgraphView &subgraph, const ModelOptions &modelOptions) const
 
bool SupportsTensorAllocatorAPI () const
 
ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory ()
 
virtual std::vector< ITensorHandleFactory::FactoryIdGetHandleFactoryPreferences () const
 (Optional) Returns a vector of supported TensorHandleFactory ids in preference order. More...
 
virtual void RegisterTensorHandleFactories (class TensorHandleFactoryRegistry &)
 (Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. More...
 
virtual void RegisterTensorHandleFactories (class TensorHandleFactoryRegistry &registry, MemorySourceFlags inputFlags, MemorySourceFlags outputFlags)
 (Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. More...
 
virtual BackendCapabilities GetCapabilities () const
 Returns a BackendCapability if the backend lists the capability The BackendCapability must then be inspected to check whether or not that BackendCapability is supported Otherwise returns an EmptyOptional if the BackendCapability is unlisted. More...
 
virtual bool HasCapability (BackendCapability) const
 Returns true if backend support the capability false otherwise. More...
 
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. More...
 
virtual unsigned int GetNumberOfCacheFiles () const
 Returns the number of files cached if backend supports caching. More...
 

Static Public Member Functions

static const BackendIdGetIdStatic ()
 
- Static Public Member Functions inherited from IBackendInternal
static constexpr BackendVersion GetApiVersion ()
 Returns the version of the Backend API. More...
 

Additional Inherited Members

- Public Types inherited from IBackendInternal
using IWorkloadFactoryPtr = std::unique_ptr< IWorkloadFactory >
 
using IBackendContextPtr = std::unique_ptr< IBackendContext >
 
using IBackendProfilingContextPtr = std::shared_ptr< armnn::profiling::IBackendProfilingContext >
 This is the bridge between backend and backend profiling we'll keep it in the backend namespace. More...
 
using IBackendProfilingPtr = std::unique_ptr< armnn::profiling::IBackendProfiling >
 
using OptimizationPtr = std::unique_ptr< Optimization >
 
using Optimizations = std::vector< OptimizationPtr >
 
using ILayerSupportSharedPtr = std::shared_ptr< ILayerSupport >
 
using IBackendSpecificModelContextPtr = std::shared_ptr< IBackendModelContext >
 
using IMemoryManagerUniquePtr = std::unique_ptr< IMemoryManager >
 
using IMemoryManagerSharedPtr = std::shared_ptr< IMemoryManager >
 
- Protected Member Functions inherited from IBackendInternal
 IBackendInternal ()=default
 Creation must be done through a specific backend interface. More...
 
- Protected Member Functions inherited from IBackend
 IBackend ()
 
virtual ~IBackend ()
 

Detailed Description

Definition at line 143 of file MockBackend.hpp.

Constructor & Destructor Documentation

◆ MockBackend()

MockBackend ( )
default

◆ ~MockBackend()

~MockBackend ( )
default

Member Function Documentation

◆ CreateBackendContext()

IBackendInternal::IBackendContextPtr CreateBackendContext ( const IRuntime::CreationOptions ) const
overridevirtual

Create the runtime context of the backend.

Implementations may return a default-constructed IBackendContextPtr if no context is needed at runtime. Implementations must throw BackendUnavailableException if the backend cannot be used (for example, necessary accelerator hardware is not present). The default implementation always returns a default-constructed pointer.

Reimplemented from IBackendInternal.

Definition at line 101 of file MockBackend.cpp.

102 {
103  return IBackendContextPtr{};
104 }
std::unique_ptr< IBackendContext > IBackendContextPtr

◆ CreateBackendProfilingContext()

IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext ( const IRuntime::CreationOptions creationOptions,
IBackendProfilingPtr backendProfiling 
)
overridevirtual

Create context specifically used for profiling interaction from backends.

Reimplemented from IBackendInternal.

Definition at line 106 of file MockBackend.cpp.

References armnn::IgnoreUnused(), MockBackendProfilingService::Instance(), and MockBackendProfilingService::SetProfilingContextPtr().

108 {
109  IgnoreUnused(options);
110  std::shared_ptr<armnn::MockBackendProfilingContext> context =
111  std::make_shared<MockBackendProfilingContext>(backendProfiling);
113  return context;
114 }
void SetProfilingContextPtr(std::shared_ptr< MockBackendProfilingContext > shared)
void IgnoreUnused(Ts &&...)
static MockBackendProfilingService & Instance()

◆ CreateMemoryManager()

IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager ( ) const
overridevirtual

Reimplemented from IBackendInternal.

Definition at line 116 of file MockBackend.cpp.

117 {
118  return IMemoryManagerUniquePtr{};
119 }
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr

◆ CreateWorkloadFactory()

IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory ( const IBackendInternal::IMemoryManagerSharedPtr memoryManager = nullptr) const
overridevirtual

Implements IBackendInternal.

Definition at line 95 of file MockBackend.cpp.

97 {
98  return IWorkloadFactoryPtr{};
99 }
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr

◆ GetDefaultAllocator()

std::unique_ptr< ICustomAllocator > GetDefaultAllocator ( ) const
overridevirtual

Returns the default memory allocator for the backend.

Returns
- Returns unique pointer to the Default Allocator of the Backend

Reimplemented from IBackendInternal.

Definition at line 262 of file MockBackend.cpp.

263 {
264  return std::make_unique<DefaultAllocator>();
265 }

◆ GetId()

const BackendId& GetId ( ) const
inlineoverridevirtual

Implements IBackend.

Definition at line 150 of file MockBackend.hpp.

151  {
152  return GetIdStatic();
153  }
static const BackendId & GetIdStatic()
Definition: MockBackend.cpp:89

◆ GetIdStatic()

const BackendId & GetIdStatic ( )
static

Definition at line 89 of file MockBackend.cpp.

References armnn::MockBackendId().

Referenced by MockBackendInitialiser::MockBackendInitialiser(), TEST_SUITE(), and MockBackendInitialiser::~MockBackendInitialiser().

90 {
91  static const BackendId s_Id{MockBackendId()};
92  return s_Id;
93 }
constexpr const char * MockBackendId()

◆ GetLayerSupport()

IBackendInternal::ILayerSupportSharedPtr GetLayerSupport ( ) const
overridevirtual

Implements IBackendInternal.

Definition at line 121 of file MockBackend.cpp.

122 {
123  static ILayerSupportSharedPtr layerSupport{new MockLayerSupport};
124  return layerSupport;
125 }
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr

◆ OptimizeSubgraphView()

OptimizationViews OptimizeSubgraphView ( const SubgraphView subgraph) const
overridevirtual

Reimplemented from IBackendInternal.

Definition at line 127 of file MockBackend.cpp.

References OptimizationViews::AddFailedSubgraph(), Graph::AddLayer(), OptimizationViews::AddSubstitution(), OptimizationViews::AddUntouchedSubgraph(), ARMNN_ASSERT, OptimizationViews::GetGraph(), SubgraphView::GetLayers(), Layer::GetType(), armnn::Input, armnn::MockBackendId(), armnn::Output, SubgraphViewSelector::SelectSubgraphs(), and Layer::SetBackendId().

128 {
129  // Prepare the optimization views
130  OptimizationViews optimizationViews;
131 
132  // Get the layers of the input sub-graph
133  const SubgraphView::Layers& subgraphLayers = subgraph.GetLayers();
134 
135  // Parse the layers
136  SubgraphView::Layers supportedLayers;
137  SubgraphView::Layers unsupportedLayers;
138  SubgraphView::Layers untouchedLayers;
139  std::for_each(subgraphLayers.begin(),
140  subgraphLayers.end(),
141  [&](Layer* layer)
142  {
143  bool supported = IsLayerSupported(layer);
144  if (supported)
145  {
146  // Layer supported, check if it's optimizable
147  bool optimizable = IsLayerOptimizable(layer);
148  if (optimizable)
149  {
150  // Layer fully supported
151  supportedLayers.push_back(layer);
152  }
153  else
154  {
155  // Layer supported but not optimizable
156  untouchedLayers.push_back(layer);
157  }
158  }
159  else
160  {
161  // Layer unsupported
162  unsupportedLayers.push_back(layer);
163  }
164  });
165 
166  // Check if there are supported layers
167  if (!supportedLayers.empty())
168  {
169  // Select the layers that are neither inputs or outputs, but that are optimizable
170  auto supportedSubgraphSelector = [](const Layer& layer)
171  {
172  return layer.GetType() != LayerType::Input &&
173  layer.GetType() != LayerType::Output &&
174  IsLayerSupported(layer) &&
175  IsLayerOptimizable(layer);
176  };
177 
178  // Apply the subgraph selector to the supported layers to group them into sub-graphs were appropriate
179  SubgraphView mutableSubgraph(subgraph);
180  SubgraphViewSelector::Subgraphs supportedSubgraphs =
181  SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, supportedSubgraphSelector);
182 
183  // Create a substitution pair for each supported sub-graph
184  std::for_each(supportedSubgraphs.begin(),
185  supportedSubgraphs.end(),
186  [&optimizationViews](const SubgraphView::SubgraphViewPtr& supportedSubgraph)
187  {
188  ARMNN_ASSERT(supportedSubgraph != nullptr);
189 
190  PreCompiledLayer* preCompiledLayer =
191  optimizationViews.GetGraph().AddLayer<PreCompiledLayer>(
192  PreCompiledDescriptor(supportedSubgraph->GetNumInputSlots(),
193  supportedSubgraph->GetNumOutputSlots()),
194  "pre-compiled");
195  preCompiledLayer->SetBackendId(MockBackendId());
196 
197  SubgraphView substitutionSubgraph(*supportedSubgraph);
198  SubgraphView replacementSubgraph(preCompiledLayer);
199 
200  optimizationViews.AddSubstitution({ substitutionSubgraph, replacementSubgraph });
201  });
202  }
203 
204  // Check if there are unsupported layers
205  if (!unsupportedLayers.empty())
206  {
207  // Select the layers that are neither inputs or outputs, and are not optimizable
208  auto unsupportedSubgraphSelector = [](const Layer& layer)
209  {
210  return layer.GetType() != LayerType::Input &&
211  layer.GetType() != LayerType::Output &&
212  !IsLayerSupported(layer);
213  };
214 
215  // Apply the subgraph selector to the unsupported layers to group them into sub-graphs were appropriate
216  SubgraphView mutableSubgraph(subgraph);
217  SubgraphViewSelector::Subgraphs unsupportedSubgraphs =
218  SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, unsupportedSubgraphSelector);
219 
220  // Add each unsupported sub-graph to the list of failed sub-graphs in the optimizization views
221  std::for_each(unsupportedSubgraphs.begin(),
222  unsupportedSubgraphs.end(),
223  [&optimizationViews](const SubgraphView::SubgraphViewPtr& unsupportedSubgraph)
224  {
225  ARMNN_ASSERT(unsupportedSubgraph != nullptr);
226 
227  optimizationViews.AddFailedSubgraph(SubgraphView(*unsupportedSubgraph));
228  });
229  }
230 
231  // Check if there are untouched layers
232  if (!untouchedLayers.empty())
233  {
234  // Select the layers that are neither inputs or outputs, that are supported but that and are not optimizable
235  auto untouchedSubgraphSelector = [](const Layer& layer)
236  {
237  return layer.GetType() != LayerType::Input &&
238  layer.GetType() != LayerType::Output &&
239  IsLayerSupported(layer) &&
240  !IsLayerOptimizable(layer);
241  };
242 
243  // Apply the subgraph selector to the untouched layers to group them into sub-graphs were appropriate
244  SubgraphView mutableSubgraph(subgraph);
245  SubgraphViewSelector::Subgraphs untouchedSubgraphs =
246  SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, untouchedSubgraphSelector);
247 
248  // Add each untouched sub-graph to the list of untouched sub-graphs in the optimizization views
249  std::for_each(untouchedSubgraphs.begin(),
250  untouchedSubgraphs.end(),
251  [&optimizationViews](const SubgraphView::SubgraphViewPtr& untouchedSubgraph)
252  {
253  ARMNN_ASSERT(untouchedSubgraph != nullptr);
254 
255  optimizationViews.AddUntouchedSubgraph(SubgraphView(*untouchedSubgraph));
256  });
257  }
258 
259  return optimizationViews;
260 }
constexpr const char * MockBackendId()
std::unique_ptr< SubgraphView > SubgraphViewPtr
std::vector< SubgraphViewPtr > Subgraphs
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
static Subgraphs SelectSubgraphs(Graph &graph, const LayerSelectorFunction &selector)
Selects subgraphs from a graph based on the selector function and the algorithm.
std::list< Layer * > Layers

The documentation for this class was generated from the following files: