ArmNN
 24.08
TosaRefBackend.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2024 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 
87  auto handler = std::make_unique<TosaSerializationHandler>();
88 
89  std::vector<std::string> graphInputs;
90  std::vector<std::string> graphOutputs;
91 
92  std::vector<TosaSerializationOperator*> operators;
93  std::vector<TosaSerializationTensor*> tensors;
94 
95  auto it = subgraph.begin();
96  while (it != subgraph.end())
97  {
98  Layer& base = *(PolymorphicDowncast<Layer*>(*it));
99 
100  if(base.GetType() == armnn::LayerType::Input ||
102  {
103  ++it;
104  continue;
105  }
106 
107  tosa::TosaSerializationBasicBlock* mappings = GetTosaMappingFromLayer(&base);
108 
109  // Loop through inputs to see if there are any graph inputs, if so save them.
110  // If it's an input to the graph "input" can be found in the string.
111  for (const std::string& blockInputName : mappings->GetInputs())
112  {
113  if (blockInputName.find("input") != std::string::npos)
114  {
115  graphInputs.push_back(blockInputName);
116  }
117  }
118 
119  // Loop through outputs to see if there are any graph outputs, if so save them.
120  // If it's an output to the graph "output" can be found in the string.
121  for (const std::string& blockOutputName : mappings->GetOutputs())
122  {
123  if (blockOutputName.find("output") != std::string::npos)
124  {
125  graphOutputs.push_back(blockOutputName);
126  }
127  }
128 
129  auto blockOperators = mappings->GetOperators();
130  operators.insert(operators.end(), blockOperators.begin(), blockOperators.end());
131 
132  auto blockTensors = mappings->GetTensors();
133  tensors.insert(tensors.end(), blockTensors.begin(), blockTensors.end());
134 
135  ++it;
136  }
137 
138  // Add all mappings to main block.
139  auto* block = new TosaSerializationBasicBlock("main", "main", operators, tensors, graphInputs, graphOutputs);
140 
141  std::vector<TosaSerializationBasicBlock*> blocks;
142  blocks.emplace_back(block);
143 
144  // Add blocks to the main region.
145  auto* region = new TosaSerializationRegion("main", blocks);
146  handler->GetRegions().emplace_back(region);
147 
148  auto compiledBlob =
149  std::make_unique<PreCompiledObjectPtr>(handler.release(), DeleteAsType<TosaSerializationHandler>);
150 
151  IConnectableLayer* preCompiledLayer = optimizationViews.GetINetwork()->AddPrecompiledLayer(
153  std::move(*compiledBlob),
155  "TOSA_Pre_Compiled_Layer");
156 
157  // Copy the output tensor infos from sub-graph
158  for (unsigned int i = 0; i < subgraph.GetNumOutputSlots(); i++)
159  {
160  preCompiledLayer->GetOutputSlot(i).SetTensorInfo(subgraph.GetIOutputSlot(i)->GetTensorInfo());
161  }
162 
163  optimizationViews.AddSubstitution({ std::move(subgraph), SubgraphView(preCompiledLayer) });
164  return optimizationViews;
165 }
166 
167 
168 std::vector<ITensorHandleFactory::FactoryId> TosaRefBackend::GetHandleFactoryPreferences() const
169 {
170  return std::vector<ITensorHandleFactory::FactoryId> { TosaRefTensorHandleFactory::GetIdStatic() };
171 }
172 
174 {
175  auto memoryManager = std::make_shared<TosaRefMemoryManager>();
176 
177  registry.RegisterMemoryManager(memoryManager);
178 
179  auto factory = std::make_unique<TosaRefTensorHandleFactory>(memoryManager);
180 
181  // Register copy and import factory pair
182  registry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
183  // Register the factory
184  registry.RegisterFactory(std::move(factory));
185 }
186 
187 std::unique_ptr<ICustomAllocator> TosaRefBackend::GetDefaultAllocator() const
188 {
189  return std::make_unique<DefaultAllocator>();
190 }
191 
192 } // namespace armnn
armnn::TosaRefBackend::GetHandleFactoryPreferences
std::vector< ITensorHandleFactory::FactoryId > GetHandleFactoryPreferences() const override
(Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
Definition: TosaRefBackend.cpp:168
armnn::TosaRefBackendId
constexpr const char * TosaRefBackendId()
Definition: TosaRefBackendId.hpp:10
armnn::Optional
Definition: Optional.hpp:270
DefaultAllocator.hpp
armnn::IBackendInternal::IMemoryManagerSharedPtr
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
Definition: IBackendInternal.hpp:99
armnn::TensorHandleFactoryRegistry
Definition: TensorHandleFactoryRegistry.hpp:23
armnn::TosaRefBackend::CreateWorkloadFactory
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
Definition: TosaRefBackend.cpp:38
armnn::TosaRefBackend::OptimizeSubgraphView
OptimizationViews OptimizeSubgraphView(const SubgraphView &subgraph, const ModelOptions &modelOptions) const override
Definition: TosaRefBackend.cpp:82
armnn::SubgraphView::GetIOutputSlot
const IOutputSlot * GetIOutputSlot(unsigned int index) const
Definition: SubgraphView.cpp:256
armnn::TensorHandleFactoryRegistry::RegisterMemoryManager
void RegisterMemoryManager(std::shared_ptr< IMemoryManager > memoryManger)
Register a memory manager with shared ownership.
Definition: TensorHandleFactoryRegistry.cpp:34
armnn::SubgraphView::GetNumInputSlots
unsigned int GetNumInputSlots() const
Definition: SubgraphView.cpp:271
BackendRegistry.hpp
armnn::IOutputSlot::GetTensorInfo
virtual const TensorInfo & GetTensorInfo() const =0
armnn::TosaRefLayerSupport
Definition: TosaRefLayerSupport.hpp:12
armnn::IBackendInternal::IBackendContextPtr
std::unique_ptr< IBackendContext > IBackendContextPtr
Definition: IBackendInternal.hpp:90
Optimizer.hpp
armnn::TosaRefBackend::GetLayerSupport
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override
Definition: TosaRefBackend.cpp:76
armnn::Layer
Definition: Layer.hpp:230
GetTosaMappingFromLayer
TosaSerializationBasicBlock * GetTosaMappingFromLayer(const Layer *layer)
Definition: TosaMappings.cpp:213
armnn::TosaRefBackend::RegisterTensorHandleFactories
void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry &registry) override
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
Definition: TosaRefBackend.cpp:173
armnn::TosaRefBackend::GetId
const BackendId & GetId() const override
Definition: TosaRefBackend.hpp:19
armnn::SubgraphView::begin
IConnectableLayerIterator begin()
Definition: SubgraphView.cpp:286
armnn::INetwork::AddPrecompiledLayer
IConnectableLayer * AddPrecompiledLayer(const PreCompiledDescriptor &preCompiledDescriptor, CompiledBlobPtr compiledBlobPtr, const Optional< BackendId > &backend, const char *name=nullptr)
Adds a Precompiled layer to the network.
Definition: Network.cpp:368
armnn::IOutputSlot::SetTensorInfo
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
IBackendContext.hpp
PolymorphicDowncast.hpp
TosaRefTensorHandleFactory.hpp
armnn::TosaRefBackend::GetDefaultAllocator
std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const override
Returns the default memory allocator for the backend.
Definition: TosaRefBackend.cpp:187
armnn::SubgraphView
The SubgraphView class represents a subgraph of a Graph.
Definition: SubgraphView.hpp:31
armnn::OptimizationViews
Definition: OptimizationViews.hpp:17
armnn::TosaRefBackend::CreateBackendProfilingContext
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions &creationOptions, IBackendProfilingPtr &backendProfiling) override
Create context specifically used for profiling interaction from backends.
Definition: TosaRefBackend.cpp:65
armnn::TosaRefTensorHandleFactory::GetIdStatic
static const FactoryId & GetIdStatic()
Definition: TosaRefTensorHandleFactory.cpp:16
TosaRefBackend.hpp
armnn::OptimizationViews::AddSubstitution
void AddSubstitution(SubstitutionPair &&substitution)
Definition: OptimizationViews.hpp:38
armnn::TosaRefBackend::GetIdStatic
static const BackendId & GetIdStatic()
Definition: TosaRefBackend.cpp:32
armnn::TosaRefBackend::CreateBackendContext
IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions &) const override
Create the runtime context of the backend.
Definition: TosaRefBackend.cpp:60
armnn::IBackendInternal::IBackendProfilingContextPtr
std::shared_ptr< arm::pipe::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
Definition: IBackendInternal.hpp:92
TosaRefWorkloadFactory.hpp
armnn::DeleteAsType
void DeleteAsType(const void *const blob)
Definition: GpuFsaBackend.cpp:37
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::SubgraphView::GetNumOutputSlots
unsigned int GetNumOutputSlots() const
Definition: SubgraphView.cpp:276
armnn::IBackendInternal::IBackendProfilingPtr
std::unique_ptr< arm::pipe::IBackendProfiling > IBackendProfilingPtr
Definition: IBackendInternal.hpp:93
armnn::IRuntime::CreationOptions
Definition: IRuntime.hpp:78
armnn::IBackendInternal::IMemoryManagerUniquePtr
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
Definition: IBackendInternal.hpp:98
armnn::SubgraphView::end
IConnectableLayerIterator end()
Definition: SubgraphView.cpp:291
armnn::BackendId
Definition: BackendId.hpp:75
armnn::IConnectableLayer::GetOutputSlot
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::TensorHandleFactoryRegistry::RegisterFactory
void RegisterFactory(std::unique_ptr< ITensorHandleFactory > allocator)
Register a TensorHandleFactory and transfer ownership.
Definition: TensorHandleFactoryRegistry.cpp:12
armnn::OptimizationViews::GetINetwork
INetwork * GetINetwork()
Definition: OptimizationViews.hpp:69
armnn::IBackendInternal::ILayerSupportSharedPtr
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
Definition: IBackendInternal.hpp:94
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:80
armnn::TensorHandleFactoryRegistry::RegisterCopyAndImportFactoryPair
void RegisterCopyAndImportFactoryPair(ITensorHandleFactory::FactoryId copyFactoryId, ITensorHandleFactory::FactoryId importFactoryId)
Register a pair of TensorHandleFactory Id for Memory Copy and TensorHandleFactory Id for Memory Impor...
Definition: TensorHandleFactoryRegistry.cpp:66
armnn::LayerType::Input
@ Input
armnn::ModelOptions
std::vector< BackendOptions > ModelOptions
Definition: BackendOptions.hpp:18
TosaRefLayerSupport.hpp
armnn::PreCompiledDescriptor
A PreCompiledDescriptor for the PreCompiledLayer.
Definition: Descriptors.hpp:1367
armnn::IBackendInternal::IWorkloadFactoryPtr
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
Definition: IBackendInternal.hpp:89
SubgraphUtils.hpp
TosaMappings.hpp
IMemoryManager.hpp
armnn::TosaRefBackend::CreateMemoryManager
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override
Definition: TosaRefBackend.cpp:71
armnn::LayerType::Output
@ Output
TosaRefBackendId.hpp