1 // 2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #include "MockImportBackend.hpp" 7 #include "MockImportLayerSupport.hpp" 8 9 #include <armnn/BackendRegistry.hpp> 10 #include <armnn/backends/IBackendContext.hpp> 11 #include <armnn/backends/IMemoryManager.hpp> 12 #include <armnn/utility/PolymorphicDowncast.hpp> 13 14 #include <reference/RefWorkloadFactory.hpp> 15 #include <reference/RefTensorHandleFactory.hpp> 16 17 #include <Optimizer.hpp> 18 19 namespace armnn 20 { 21 MockImportBackendInitialiser()22MockImportBackendInitialiser::MockImportBackendInitialiser() 23 { 24 BackendRegistryInstance().Register(MockImportBackend::GetIdStatic(), 25 []() 26 { 27 return IBackendInternalUniquePtr(new MockImportBackend); 28 }); 29 } 30 ~MockImportBackendInitialiser()31MockImportBackendInitialiser::~MockImportBackendInitialiser() 32 { 33 try 34 { 35 BackendRegistryInstance().Deregister(MockImportBackend::GetIdStatic()); 36 } 37 catch (...) 38 { 39 std::cerr << "could not deregister mock import backend" << std::endl; 40 } 41 } 42 GetIdStatic()43const BackendId& MockImportBackend::GetIdStatic() 44 { 45 static const BackendId s_Id{ MockImportBackendId() }; 46 return s_Id; 47 } 48 CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr & memoryManager) const49IBackendInternal::IWorkloadFactoryPtr MockImportBackend::CreateWorkloadFactory( 50 const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const 51 { 52 return std::make_unique<RefWorkloadFactory>(PolymorphicPointerDowncast<RefMemoryManager>(memoryManager)); 53 } 54 CreateWorkloadFactory(class TensorHandleFactoryRegistry & tensorHandleFactoryRegistry) const55IBackendInternal::IWorkloadFactoryPtr MockImportBackend::CreateWorkloadFactory( 56 class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const 57 { 58 auto memoryManager = std::make_shared<RefMemoryManager>(); 59 60 tensorHandleFactoryRegistry.RegisterMemoryManager(memoryManager); 61 tensorHandleFactoryRegistry.RegisterFactory(std::make_unique<RefTensorHandleFactory>(memoryManager)); 62 63 return std::make_unique<RefWorkloadFactory>(PolymorphicPointerDowncast<RefMemoryManager>(memoryManager)); 64 } 65 CreateBackendContext(const IRuntime::CreationOptions &) const66IBackendInternal::IBackendContextPtr MockImportBackend::CreateBackendContext(const IRuntime::CreationOptions&) const 67 { 68 return IBackendContextPtr{}; 69 } 70 CreateBackendProfilingContext(const IRuntime::CreationOptions &,IBackendProfilingPtr &)71IBackendInternal::IBackendProfilingContextPtr MockImportBackend::CreateBackendProfilingContext( 72 const IRuntime::CreationOptions&, IBackendProfilingPtr&) 73 { 74 return IBackendProfilingContextPtr{}; 75 } 76 CreateMemoryManager() const77IBackendInternal::IMemoryManagerUniquePtr MockImportBackend::CreateMemoryManager() const 78 { 79 return std::make_unique<RefMemoryManager>(); 80 } 81 GetLayerSupport() const82IBackendInternal::ILayerSupportSharedPtr MockImportBackend::GetLayerSupport() const 83 { 84 static ILayerSupportSharedPtr layerSupport{new MockImportLayerSupport}; 85 return layerSupport; 86 } 87 OptimizeSubgraphView(const SubgraphView & subgraph) const88OptimizationViews MockImportBackend::OptimizeSubgraphView(const SubgraphView& subgraph) const 89 { 90 OptimizationViews optimizationViews; 91 92 optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph)); 93 94 return optimizationViews; 95 } 96 GetHandleFactoryPreferences() const97std::vector<ITensorHandleFactory::FactoryId> MockImportBackend::GetHandleFactoryPreferences() const 98 { 99 return std::vector<ITensorHandleFactory::FactoryId> { RefTensorHandleFactory::GetIdStatic() }; 100 } 101 RegisterTensorHandleFactories(class TensorHandleFactoryRegistry & registry)102void MockImportBackend::RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) 103 { 104 auto memoryManager = std::make_shared<RefMemoryManager>(); 105 106 registry.RegisterMemoryManager(memoryManager); 107 registry.RegisterFactory(std::make_unique<RefTensorHandleFactory>(memoryManager)); 108 } 109 110 } // namespace armnn 111