David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 7 | #include <armnn/Types.hpp> |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 8 | #include <armnn/IRuntime.hpp> |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 9 | #include <armnn/Deprecated.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 10 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 11 | #include <ISubgraphViewConverter.hpp> |
| 12 | #include <SubgraphView.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 13 | #include <optimizations/Optimization.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 14 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 15 | #include "IBackendContext.hpp" |
| 16 | #include "IMemoryManager.hpp" |
| 17 | #include "ITensorHandleFactory.hpp" |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 18 | #include "OptimizationViews.hpp" |
| 19 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 20 | #include <vector> |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 21 | |
| 22 | namespace armnn |
| 23 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 24 | class IWorkloadFactory; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 25 | class IMemoryManager; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 26 | class ILayerSupport; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 27 | |
| 28 | class IBackendInternal : public IBackend |
| 29 | { |
| 30 | protected: |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 31 | // Creation must be done through a specific |
| 32 | // backend interface. |
David Beck | 6b779f0 | 2018-10-09 17:20:21 +0100 | [diff] [blame] | 33 | IBackendInternal() = default; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 34 | |
| 35 | public: |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 36 | // Allow backends created by the factory function |
| 37 | // to be destroyed through IBackendInternal. |
| 38 | ~IBackendInternal() override = default; |
| 39 | |
| 40 | using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 41 | using IBackendContextPtr = std::unique_ptr<IBackendContext>; |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 42 | using OptimizationPtr = std::unique_ptr<Optimization>; |
| 43 | using Optimizations = std::vector<OptimizationPtr>; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 44 | using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 45 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 46 | using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>; |
| 47 | using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>; |
| 48 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 49 | using GraphUniquePtr = std::unique_ptr<Graph>; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 50 | using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 51 | |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 52 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
| 53 | using ISubGraphConverterPtr ARMNN_DEPRECATED_MSG("This type is no longer supported") |
| 54 | = std::unique_ptr<ISubGraphConverter>; |
| 55 | using SubGraphUniquePtr ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead") |
| 56 | = std::unique_ptr<SubGraph>; |
| 57 | |
| 58 | ARMNN_DEPRECATED_MSG("This method is no longer supported") |
| 59 | virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const |
| 60 | { |
| 61 | return ISubGraphConverterPtr{}; |
| 62 | } |
| 63 | |
| 64 | ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead") |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 65 | virtual Optimizations GetOptimizations() const |
| 66 | { |
| 67 | return Optimizations{}; |
| 68 | } |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 69 | |
| 70 | ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead") |
| 71 | virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const |
| 72 | { |
| 73 | optimizationAttempted = false; |
| 74 | return nullptr; |
| 75 | } |
| 76 | ARMNN_NO_DEPRECATE_WARN_END |
| 77 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 78 | |
| 79 | virtual IMemoryManagerUniquePtr CreateMemoryManager() const |
| 80 | { |
| 81 | return IMemoryManagerUniquePtr(); |
| 82 | }; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 83 | |
| 84 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 85 | const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0; |
| 86 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 87 | virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const |
| 88 | { |
| 89 | return IBackendContextPtr{}; |
| 90 | } |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 91 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 92 | virtual ILayerSupportSharedPtr GetLayerSupport() const = 0; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 93 | |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 94 | // Default implementation of OptimizeSubgraphView for backward compatibility with the old API. |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 95 | // Override this method with a custom optimization implementation. |
Derek Lamberti | 8106b7c | 2019-05-07 21:33:30 +0100 | [diff] [blame] | 96 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 97 | { |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 98 | bool optimizationAttempted = false; |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 99 | |
| 100 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 101 | SubGraphUniquePtr optSubgraph = OptimizeSubGraph(subgraph, optimizationAttempted); |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 102 | ARMNN_NO_DEPRECATE_WARN_END |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 103 | |
| 104 | OptimizationViews result; |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 105 | if (!optimizationAttempted) |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 106 | { |
| 107 | result.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | if (optSubgraph) |
| 112 | { |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 113 | result.AddSubstitution({subgraph, SubgraphView(*optSubgraph.get())}); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
| 117 | result.AddFailedSubgraph(SubgraphView(subgraph)); |
| 118 | } |
| 119 | } |
| 120 | return result; |
| 121 | } |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame^] | 122 | |
| 123 | bool SupportsTensorAllocatorAPI() const { return GetHandleFactoryPreferences().empty() == false; } |
| 124 | |
| 125 | ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory() |
| 126 | { |
| 127 | auto favorites = GetHandleFactoryPreferences(); |
| 128 | if (favorites.empty()) |
| 129 | { |
| 130 | return ITensorHandleFactory::LegacyFactoryId; |
| 131 | } |
| 132 | return favorites[0]; |
| 133 | } |
| 134 | |
| 135 | /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order. |
| 136 | virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const |
| 137 | { |
| 138 | return std::vector<ITensorHandleFactory::FactoryId>(); |
| 139 | } |
| 140 | |
| 141 | /// (Optional) Register TensorHandleFactories |
| 142 | /// Either this method or CreateMemoryManager() and |
| 143 | /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. |
| 144 | virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) {} |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 147 | using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>; |
| 148 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 149 | } // namespace armnn |