ArmNN
 22.11
TosaRefBackend.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "TosaRefBackend.hpp"
7 #include "TosaRefBackendId.hpp"
11 
19 
20 #include <Optimizer.hpp>
21 
22 namespace armnn
23 {
24 
25 // Utility function to construct a valid Deleter for TosaSerializationHandler ptrs passed back to ArmNN
26 template <typename T>
27 void DeleteAsType(const void* const blob)
28 {
29  delete static_cast<const T*>(blob);
30 }
31 
33 {
34  static const BackendId s_Id{TosaRefBackendId()};
35  return s_Id;
36 }
37 
39  const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const
40 {
41  return std::make_unique<TosaRefWorkloadFactory>(PolymorphicPointerDowncast<TosaRefMemoryManager>(memoryManager));
42 }
43 
45  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const
46 {
47  auto memoryManager = std::make_shared<TosaRefMemoryManager>();
48 
49  tensorHandleFactoryRegistry.RegisterMemoryManager(memoryManager);
50 
51  auto factory = std::make_unique<TosaRefTensorHandleFactory>(memoryManager);
52  // Register copy and import factory pair
53  tensorHandleFactoryRegistry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
54  // Register the factory
55  tensorHandleFactoryRegistry.RegisterFactory(std::move(factory));
56 
57  return std::make_unique<TosaRefWorkloadFactory>(PolymorphicPointerDowncast<TosaRefMemoryManager>(memoryManager));
58 }
59 
61 {
62  return IBackendContextPtr{};
63 }
64 
67 {
69 }
70 
72 {
73  return std::make_unique<TosaRefMemoryManager>();
74 }
75 
77 {
78  static ILayerSupportSharedPtr layerSupport{new TosaRefLayerSupport};
79  return layerSupport;
80 }
81 
83  const ModelOptions& modelOptions) const
84 {
85  OptimizationViews optimizationViews(modelOptions);
86  auto handler = std::make_unique<TosaSerializationHandler>();
87 
88  // A main block should only be added once.
89  bool isMain = true;
90 
91  auto it = subgraph.endIConnectable();
92  while (it != subgraph.beginIConnectable())
93  {
94  --it;
95  Layer &base = *(PolymorphicDowncast<Layer*>(*it));
96 
97  if(base.GetType() == armnn::LayerType::Input ||
99  {
100  continue;
101  }
102 
103  tosa::TosaSerializationBasicBlock* mappings = GetTosaMappingFromLayer(&base, isMain);
104  handler.get()->GetBlocks().push_back(mappings);
105 
106  if(isMain)
107  {
108  isMain = false;
109  }
110  }
111 
112  auto compiledBlob =
113  std::make_unique<PreCompiledObjectPtr>(handler.release(), DeleteAsType<TosaSerializationHandler>);
114 
115  IConnectableLayer* preCompiledLayer = optimizationViews.GetINetwork()->AddPrecompiledLayer(
117  std::move(*compiledBlob),
119  "TOSA_Pre_Compiled_Layer");
120 
121  // Copy the output tensor infos from sub-graph
122  for (unsigned int i = 0; i < subgraph.GetNumOutputSlots(); i++)
123  {
124  preCompiledLayer->GetOutputSlot(i).SetTensorInfo(subgraph.GetIOutputSlot(i)->GetTensorInfo());
125  }
126 
127  optimizationViews.AddSubstitution({ std::move(subgraph), SubgraphView(preCompiledLayer) });
128  return optimizationViews;
129 }
130 
131 
132 std::vector<ITensorHandleFactory::FactoryId> TosaRefBackend::GetHandleFactoryPreferences() const
133 {
134  return std::vector<ITensorHandleFactory::FactoryId> { TosaRefTensorHandleFactory::GetIdStatic() };
135 }
136 
138 {
139  auto memoryManager = std::make_shared<TosaRefMemoryManager>();
140 
141  registry.RegisterMemoryManager(memoryManager);
142 
143  auto factory = std::make_unique<TosaRefTensorHandleFactory>(memoryManager);
144 
145  // Register copy and import factory pair
146  registry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
147  // Register the factory
148  registry.RegisterFactory(std::move(factory));
149 }
150 
151 std::unique_ptr<ICustomAllocator> TosaRefBackend::GetDefaultAllocator() const
152 {
153  return std::make_unique<DefaultAllocator>();
154 }
155 
156 } // namespace armnn
void RegisterMemoryManager(std::shared_ptr< IMemoryManager > memoryManger)
Register a memory manager with shared ownership.
IConnectableLayerIterator endIConnectable()
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:68
const BackendId & GetId() const override
std::vector< BackendOptions > ModelOptions
void RegisterFactory(std::unique_ptr< ITensorHandleFactory > allocator)
Register a TensorHandleFactory and transfer ownership.
TosaSerializationBasicBlock * GetTosaMappingFromLayer(Layer *layer, bool isMain=false)
unsigned int GetNumInputSlots() const
IConnectableLayerIterator beginIConnectable()
void AddSubstitution(SubstitutionPair &&substitution)
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
const IOutputSlot * GetIOutputSlot(unsigned int index) const
constexpr const char * TosaRefBackendId()
std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const override
Returns the default memory allocator for the backend.
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions &creationOptions, IBackendProfilingPtr &backendProfiling) override
Create context specifically used for profiling interaction from backends.
The SubgraphView class represents a subgraph of a Graph.
void RegisterCopyAndImportFactoryPair(ITensorHandleFactory::FactoryId copyFactoryId, ITensorHandleFactory::FactoryId importFactoryId)
Register a pair of TensorHandleFactory Id for Memory Copy and TensorHandleFactory Id for Memory Impor...
static const BackendId & GetIdStatic()
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:273
void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry &registry) override
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
std::shared_ptr< arm::pipe::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we&#39;ll keep it in the backend namespace...
void DeleteAsType(const void *const blob)
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
std::unique_ptr< arm::pipe::IBackendProfiling > IBackendProfilingPtr
IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions &) const override
Create the runtime context of the backend.
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override
OptimizationViews OptimizeSubgraphView(const SubgraphView &subgraph, const ModelOptions &modelOptions) const override
virtual const TensorInfo & GetTensorInfo() const =0
A PreCompiledDescriptor for the PreCompiledLayer.
std::vector< ITensorHandleFactory::FactoryId > GetHandleFactoryPreferences() const override
(Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
unsigned int GetNumOutputSlots() const
std::unique_ptr< IBackendContext > IBackendContextPtr