Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 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 | |
| 22 | MockImportBackendInitialiser::MockImportBackendInitialiser() |
| 23 | { |
| 24 | BackendRegistryInstance().Register(MockImportBackend::GetIdStatic(), |
| 25 | []() |
| 26 | { |
| 27 | return IBackendInternalUniquePtr(new MockImportBackend); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | MockImportBackendInitialiser::~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 | |
| 43 | const BackendId& MockImportBackend::GetIdStatic() |
| 44 | { |
| 45 | static const BackendId s_Id{ MockImportBackendId() }; |
| 46 | return s_Id; |
| 47 | } |
| 48 | |
| 49 | IBackendInternal::IWorkloadFactoryPtr MockImportBackend::CreateWorkloadFactory( |
| 50 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const |
| 51 | { |
| 52 | return std::make_unique<RefWorkloadFactory>(PolymorphicPointerDowncast<RefMemoryManager>(memoryManager)); |
| 53 | } |
| 54 | |
| 55 | IBackendInternal::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 | |
| 66 | IBackendInternal::IBackendContextPtr MockImportBackend::CreateBackendContext(const IRuntime::CreationOptions&) const |
| 67 | { |
| 68 | return IBackendContextPtr{}; |
| 69 | } |
| 70 | |
| 71 | IBackendInternal::IBackendProfilingContextPtr MockImportBackend::CreateBackendProfilingContext( |
| 72 | const IRuntime::CreationOptions&, IBackendProfilingPtr&) |
| 73 | { |
| 74 | return IBackendProfilingContextPtr{}; |
| 75 | } |
| 76 | |
| 77 | IBackendInternal::IMemoryManagerUniquePtr MockImportBackend::CreateMemoryManager() const |
| 78 | { |
| 79 | return std::make_unique<RefMemoryManager>(); |
| 80 | } |
| 81 | |
| 82 | IBackendInternal::Optimizations MockImportBackend::GetOptimizations() const |
| 83 | { |
| 84 | return Optimizations{}; |
| 85 | } |
| 86 | |
| 87 | IBackendInternal::ILayerSupportSharedPtr MockImportBackend::GetLayerSupport() const |
| 88 | { |
| 89 | static ILayerSupportSharedPtr layerSupport{new MockImportLayerSupport}; |
| 90 | return layerSupport; |
| 91 | } |
| 92 | |
| 93 | OptimizationViews MockImportBackend::OptimizeSubgraphView(const SubgraphView& subgraph) const |
| 94 | { |
| 95 | OptimizationViews optimizationViews; |
| 96 | |
| 97 | optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 98 | |
| 99 | return optimizationViews; |
| 100 | } |
| 101 | |
| 102 | std::vector<ITensorHandleFactory::FactoryId> MockImportBackend::GetHandleFactoryPreferences() const |
| 103 | { |
| 104 | return std::vector<ITensorHandleFactory::FactoryId> { RefTensorHandleFactory::GetIdStatic() }; |
| 105 | } |
| 106 | |
| 107 | void MockImportBackend::RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) |
| 108 | { |
| 109 | auto memoryManager = std::make_shared<RefMemoryManager>(); |
| 110 | |
| 111 | registry.RegisterMemoryManager(memoryManager); |
| 112 | registry.RegisterFactory(std::make_unique<RefTensorHandleFactory>(memoryManager)); |
| 113 | } |
| 114 | |
| 115 | } // namespace armnn |