Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 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" |
| 8 | #include "TosaRefWorkloadFactory.hpp" |
| 9 | #include "TosaRefLayerSupport.hpp" |
| 10 | #include "TosaRefTensorHandleFactory.hpp" |
| 11 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 12 | #include <tosaCommon/TosaMappings.hpp> |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 13 | #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 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 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 | |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 32 | const BackendId& TosaRefBackend::GetIdStatic() |
| 33 | { |
| 34 | static const BackendId s_Id{TosaRefBackendId()}; |
| 35 | return s_Id; |
| 36 | } |
| 37 | |
| 38 | IBackendInternal::IWorkloadFactoryPtr TosaRefBackend::CreateWorkloadFactory( |
| 39 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const |
| 40 | { |
| 41 | return std::make_unique<TosaRefWorkloadFactory>(PolymorphicPointerDowncast<TosaRefMemoryManager>(memoryManager)); |
| 42 | } |
| 43 | |
| 44 | IBackendInternal::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 | |
| 60 | IBackendInternal::IBackendContextPtr TosaRefBackend::CreateBackendContext(const IRuntime::CreationOptions&) const |
| 61 | { |
| 62 | return IBackendContextPtr{}; |
| 63 | } |
| 64 | |
| 65 | IBackendInternal::IBackendProfilingContextPtr TosaRefBackend::CreateBackendProfilingContext( |
| 66 | const IRuntime::CreationOptions&, IBackendProfilingPtr&) |
| 67 | { |
| 68 | return IBackendProfilingContextPtr{}; |
| 69 | } |
| 70 | |
| 71 | IBackendInternal::IMemoryManagerUniquePtr TosaRefBackend::CreateMemoryManager() const |
| 72 | { |
| 73 | return std::make_unique<TosaRefMemoryManager>(); |
| 74 | } |
| 75 | |
| 76 | IBackendInternal::ILayerSupportSharedPtr TosaRefBackend::GetLayerSupport() const |
| 77 | { |
| 78 | static ILayerSupportSharedPtr layerSupport{new TosaRefLayerSupport}; |
| 79 | return layerSupport; |
| 80 | } |
| 81 | |
| 82 | OptimizationViews TosaRefBackend::OptimizeSubgraphView(const SubgraphView& subgraph, |
| 83 | const ModelOptions& modelOptions) const |
| 84 | { |
| 85 | OptimizationViews optimizationViews(modelOptions); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 86 | auto handler = std::make_unique<TosaSerializationHandler>(); |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 87 | |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame^] | 88 | // A main block should only be added once. |
| 89 | bool isMain = true; |
| 90 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 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 || |
| 98 | base.GetType() == armnn::LayerType::Output) |
| 99 | { |
| 100 | continue; |
| 101 | } |
| 102 | |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame^] | 103 | tosa::TosaSerializationBasicBlock* mappings = GetTosaMappingFromLayer(&base, isMain); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 104 | handler.get()->GetBlocks().push_back(mappings); |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame^] | 105 | |
| 106 | if(isMain) |
| 107 | { |
| 108 | isMain = false; |
| 109 | } |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | auto compiledBlob = |
| 113 | std::make_unique<PreCompiledObjectPtr>(handler.release(), DeleteAsType<TosaSerializationHandler>); |
| 114 | |
| 115 | IConnectableLayer* preCompiledLayer = optimizationViews.GetINetwork()->AddPrecompiledLayer( |
| 116 | PreCompiledDescriptor(subgraph.GetNumInputSlots(), subgraph.GetNumOutputSlots()), |
| 117 | std::move(*compiledBlob), |
| 118 | armnn::Optional<BackendId>(GetId()), |
| 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) }); |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 128 | return optimizationViews; |
| 129 | } |
| 130 | |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 131 | |
Francis Murtagh | 9270d9e | 2022-08-12 13:54:17 +0100 | [diff] [blame] | 132 | std::vector<ITensorHandleFactory::FactoryId> TosaRefBackend::GetHandleFactoryPreferences() const |
| 133 | { |
| 134 | return std::vector<ITensorHandleFactory::FactoryId> { TosaRefTensorHandleFactory::GetIdStatic() }; |
| 135 | } |
| 136 | |
| 137 | void TosaRefBackend::RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) |
| 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 |