1 // 2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #include "SampleDynamicBackend.hpp" 7 #include "SampleDynamicLayerSupport.hpp" 8 #include "SampleDynamicWorkloadFactory.hpp" 9 #include "SampleMemoryManager.hpp" 10 #include "SampleDynamicTensorHandleFactory.hpp" 11 12 #include <armnn/backends/IBackendInternal.hpp> 13 #include <armnn/backends/OptimizationViews.hpp> 14 #include <armnn/utility/PolymorphicDowncast.hpp> 15 #include <backendsCommon/TensorHandleFactoryRegistry.hpp> 16 17 namespace sdb // sample dynamic backend 18 { 19 SampleDynamicBackendId()20constexpr const char * SampleDynamicBackendId() { return "SampleDynamic"; } 21 22 class SampleDynamicBackend : public armnn::IBackendInternal 23 { 24 public: 25 SampleDynamicBackend() = default; 26 ~SampleDynamicBackend() = default; 27 GetIdStatic()28 static const armnn::BackendId& GetIdStatic() 29 { 30 static const armnn::BackendId s_Id{SampleDynamicBackendId()}; 31 return s_Id; 32 } 33 GetId() const34 const armnn::BackendId& GetId() const override { return GetIdStatic(); } 35 CreateMemoryManager() const36 IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override 37 { 38 return std::make_unique<SampleMemoryManager>(); 39 } 40 CreateWorkloadFactory(const IMemoryManagerSharedPtr & memoryManager) const41 IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( 42 const IMemoryManagerSharedPtr& memoryManager) const override 43 { 44 return std::make_unique<SampleDynamicWorkloadFactory>( 45 armnn::PolymorphicPointerDowncast<SampleMemoryManager>(memoryManager)); 46 } 47 CreateWorkloadFactory(class armnn::TensorHandleFactoryRegistry & tensorHandleFactoryRegistry) const48 IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( 49 class armnn::TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const override 50 { 51 auto memoryManager = std::make_shared<SampleMemoryManager>(); 52 53 tensorHandleFactoryRegistry.RegisterMemoryManager(memoryManager); 54 tensorHandleFactoryRegistry.RegisterFactory(std::make_unique<SampleDynamicTensorHandleFactory>(memoryManager)); 55 56 return std::make_unique<SampleDynamicWorkloadFactory>( 57 armnn::PolymorphicPointerDowncast<SampleMemoryManager>(memoryManager)); 58 } 59 CreateBackendProfilingContext(const armnn::IRuntime::CreationOptions &,IBackendProfilingPtr &)60 IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext( 61 const armnn::IRuntime::CreationOptions&, IBackendProfilingPtr&) override 62 { 63 return IBackendProfilingContextPtr{}; 64 } 65 GetLayerSupport() const66 IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override 67 { 68 static ILayerSupportSharedPtr layerSupport{new SampleDynamicLayerSupport}; 69 return layerSupport; 70 } 71 GetHandleFactoryPreferences() const72 std::vector<armnn::ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const override 73 { 74 return std::vector<armnn::ITensorHandleFactory::FactoryId> { SampleDynamicTensorHandleFactory::GetIdStatic() }; 75 } 76 CreateBackendContext(const armnn::IRuntime::CreationOptions &) const77 IBackendInternal::IBackendContextPtr CreateBackendContext(const armnn::IRuntime::CreationOptions&) const override 78 { 79 return IBackendContextPtr{}; 80 } 81 OptimizeSubgraphView(const armnn::SubgraphView & subgraph) const82 armnn::OptimizationViews OptimizeSubgraphView(const armnn::SubgraphView& subgraph) const override 83 { 84 armnn::OptimizationViews optimizationViews; 85 86 optimizationViews.AddUntouchedSubgraph(armnn::SubgraphView(subgraph)); 87 88 return optimizationViews; 89 } 90 RegisterTensorHandleFactories(class armnn::TensorHandleFactoryRegistry & registry)91 void RegisterTensorHandleFactories(class armnn::TensorHandleFactoryRegistry& registry) override 92 { 93 auto memoryManager = std::make_shared<SampleMemoryManager>(); 94 95 registry.RegisterMemoryManager(memoryManager); 96 registry.RegisterFactory(std::make_unique<SampleDynamicTensorHandleFactory>(memoryManager)); 97 } 98 99 }; 100 101 } // namespace sdb 102 GetBackendId()103const char* GetBackendId() 104 { 105 return sdb::SampleDynamicBackend::GetIdStatic().Get().c_str(); 106 } 107 GetVersion(uint32_t * outMajor,uint32_t * outMinor)108void GetVersion(uint32_t* outMajor, uint32_t* outMinor) 109 { 110 if (!outMajor || !outMinor) 111 { 112 return; 113 } 114 115 armnn::BackendVersion apiVersion = armnn::IBackendInternal::GetApiVersion(); 116 117 *outMajor = apiVersion.m_Major; 118 *outMinor = apiVersion.m_Minor; 119 } 120 BackendFactory()121void* BackendFactory() 122 { 123 return new sdb::SampleDynamicBackend(); 124 } 125 126 127