blob: aaac07c27b908ff3ef59eec766e74f82c72bb0ac [file] [log] [blame]
Francis Murtagh9270d9e2022-08-12 13:54:17 +01001//
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"
8#include "TosaRefWorkloadFactory.hpp"
9#include "TosaRefLayerSupport.hpp"
10#include "TosaRefTensorHandleFactory.hpp"
11
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010012#include <tosaCommon/TosaMappings.hpp>
Francis Murtagh9270d9e2022-08-12 13:54:17 +010013#include <armnn/BackendRegistry.hpp>
14#include <armnn/backends/IBackendContext.hpp>
15#include <armnn/backends/IMemoryManager.hpp>
16#include <armnn/utility/PolymorphicDowncast.hpp>
17#include <backendsCommon/DefaultAllocator.hpp>
18#include <backendsCommon/SubgraphUtils.hpp>
19
20#include <Optimizer.hpp>
21
22namespace armnn
23{
24
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010025// Utility function to construct a valid Deleter for TosaSerializationHandler ptrs passed back to ArmNN
26template <typename T>
27void DeleteAsType(const void* const blob)
28{
29 delete static_cast<const T*>(blob);
30}
31
Francis Murtagh9270d9e2022-08-12 13:54:17 +010032const BackendId& TosaRefBackend::GetIdStatic()
33{
34 static const BackendId s_Id{TosaRefBackendId()};
35 return s_Id;
36}
37
38IBackendInternal::IWorkloadFactoryPtr TosaRefBackend::CreateWorkloadFactory(
39 const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const
40{
41 return std::make_unique<TosaRefWorkloadFactory>(PolymorphicPointerDowncast<TosaRefMemoryManager>(memoryManager));
42}
43
44IBackendInternal::IWorkloadFactoryPtr TosaRefBackend::CreateWorkloadFactory(
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
60IBackendInternal::IBackendContextPtr TosaRefBackend::CreateBackendContext(const IRuntime::CreationOptions&) const
61{
62 return IBackendContextPtr{};
63}
64
65IBackendInternal::IBackendProfilingContextPtr TosaRefBackend::CreateBackendProfilingContext(
66 const IRuntime::CreationOptions&, IBackendProfilingPtr&)
67{
68 return IBackendProfilingContextPtr{};
69}
70
71IBackendInternal::IMemoryManagerUniquePtr TosaRefBackend::CreateMemoryManager() const
72{
73 return std::make_unique<TosaRefMemoryManager>();
74}
75
76IBackendInternal::ILayerSupportSharedPtr TosaRefBackend::GetLayerSupport() const
77{
78 static ILayerSupportSharedPtr layerSupport{new TosaRefLayerSupport};
79 return layerSupport;
80}
81
82OptimizationViews TosaRefBackend::OptimizeSubgraphView(const SubgraphView& subgraph,
83 const ModelOptions& modelOptions) const
84{
85 OptimizationViews optimizationViews(modelOptions);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000086
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010087 auto handler = std::make_unique<TosaSerializationHandler>();
Francis Murtagh9270d9e2022-08-12 13:54:17 +010088
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000089 std::vector<std::string> graphInputs;
90 std::vector<std::string> graphOutputs;
91
92 std::vector<TosaSerializationOperator*> operators;
93 std::vector<TosaSerializationTensor*> tensors;
Matthew Sloyan5c54c382022-11-09 16:28:51 +000094
Francis Murtagh0f3e9a02023-07-28 14:29:46 +010095 auto it = subgraph.end();
96 while (it != subgraph.begin())
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010097 {
98 --it;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000099 Layer& base = *(PolymorphicDowncast<Layer*>(*it));
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100100
101 if(base.GetType() == armnn::LayerType::Input ||
102 base.GetType() == armnn::LayerType::Output)
103 {
104 continue;
105 }
106
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000107 tosa::TosaSerializationBasicBlock* mappings = GetTosaMappingFromLayer(&base);
Matthew Sloyan5c54c382022-11-09 16:28:51 +0000108
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000109 // 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 (uint32_t i = 0; i < mappings->GetInputs().size(); i++)
Matthew Sloyan5c54c382022-11-09 16:28:51 +0000112 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000113 std::basic_string<char> blockInputName = mappings->GetInputs()[i];
114
115 if (blockInputName.find("input") != std::string::npos)
116 {
117 graphInputs.push_back(blockInputName);
118 }
Matthew Sloyan5c54c382022-11-09 16:28:51 +0000119 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000120
121 // Loop through outputs to see if there are any graph outputs, if so save them.
122 // If it's an output to the graph "output" can be found in the string.
123 for (uint32_t i = 0; i < mappings->GetOutputs().size(); i++)
124 {
125 std::basic_string<char> blockOutputName = mappings->GetOutputs()[i];
126
127 if (blockOutputName.find("output") != std::string::npos)
128 {
129 graphOutputs.push_back(blockOutputName);
130 }
131 }
132
133 auto blockOperators = mappings->GetOperators();
134 operators.insert(operators.end(), blockOperators.begin(), blockOperators.end());
135
136 auto blockTensors = mappings->GetTensors();
137 tensors.insert(tensors.end(), blockTensors.begin(), blockTensors.end());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100138 }
139
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100140 // Add all mappings to main block.
141 auto* block = new TosaSerializationBasicBlock("main", "main", operators, tensors, graphInputs, graphOutputs);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000142
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100143 std::vector<TosaSerializationBasicBlock*> blocks;
144 blocks.emplace_back(block);
145
146 // Add blocks to the main region.
147 auto* region = new TosaSerializationRegion("main", blocks);
148 handler->GetRegions().emplace_back(region);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000149
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100150 auto compiledBlob =
151 std::make_unique<PreCompiledObjectPtr>(handler.release(), DeleteAsType<TosaSerializationHandler>);
152
153 IConnectableLayer* preCompiledLayer = optimizationViews.GetINetwork()->AddPrecompiledLayer(
154 PreCompiledDescriptor(subgraph.GetNumInputSlots(), subgraph.GetNumOutputSlots()),
155 std::move(*compiledBlob),
156 armnn::Optional<BackendId>(GetId()),
157 "TOSA_Pre_Compiled_Layer");
158
159 // Copy the output tensor infos from sub-graph
160 for (unsigned int i = 0; i < subgraph.GetNumOutputSlots(); i++)
161 {
162 preCompiledLayer->GetOutputSlot(i).SetTensorInfo(subgraph.GetIOutputSlot(i)->GetTensorInfo());
163 }
164
165 optimizationViews.AddSubstitution({ std::move(subgraph), SubgraphView(preCompiledLayer) });
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100166 return optimizationViews;
167}
168
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100169
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100170std::vector<ITensorHandleFactory::FactoryId> TosaRefBackend::GetHandleFactoryPreferences() const
171{
172 return std::vector<ITensorHandleFactory::FactoryId> { TosaRefTensorHandleFactory::GetIdStatic() };
173}
174
175void TosaRefBackend::RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry)
176{
177 auto memoryManager = std::make_shared<TosaRefMemoryManager>();
178
179 registry.RegisterMemoryManager(memoryManager);
180
181 auto factory = std::make_unique<TosaRefTensorHandleFactory>(memoryManager);
182
183 // Register copy and import factory pair
184 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
185 // Register the factory
186 registry.RegisterFactory(std::move(factory));
187}
188
189std::unique_ptr<ICustomAllocator> TosaRefBackend::GetDefaultAllocator() const
190{
191 return std::make_unique<DefaultAllocator>();
192}
193
194} // namespace armnn