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 | // |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 5 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 6 | #pragma once |
| 7 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 8 | #include <armnn/Types.hpp> |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 9 | #include <armnn/IRuntime.hpp> |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 10 | #include <armnn/Deprecated.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 11 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 12 | #include <ISubgraphViewConverter.hpp> |
| 13 | #include <SubgraphView.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 14 | #include <optimizations/Optimization.hpp> |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 15 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 16 | #include "IBackendContext.hpp" |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 17 | #include "armnn/backends/profiling/IBackendProfiling.hpp" |
| 18 | #include "armnn/backends/profiling/IBackendProfilingContext.hpp" |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 19 | #include "IMemoryManager.hpp" |
| 20 | #include "ITensorHandleFactory.hpp" |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 21 | #include "OptimizationViews.hpp" |
| 22 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 23 | #include <vector> |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 24 | #include <memory> |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 25 | |
| 26 | namespace armnn |
| 27 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 28 | class IWorkloadFactory; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 29 | class IMemoryManager; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 30 | class ILayerSupport; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 31 | |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 32 | struct BackendVersion |
| 33 | { |
| 34 | uint32_t m_Major; |
| 35 | uint32_t m_Minor; |
| 36 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 37 | constexpr BackendVersion() |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 38 | : m_Major(0) |
| 39 | , m_Minor(0) |
| 40 | {} |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 41 | constexpr BackendVersion(uint32_t major, uint32_t minor) |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 42 | : m_Major(major) |
| 43 | , m_Minor(minor) |
| 44 | {} |
| 45 | |
| 46 | bool operator==(const BackendVersion& other) const |
| 47 | { |
| 48 | return this == &other || |
| 49 | (this->m_Major == other.m_Major && |
| 50 | this->m_Minor == other.m_Minor); |
| 51 | } |
| 52 | |
| 53 | bool operator<=(const BackendVersion& other) const |
| 54 | { |
| 55 | return this->m_Major < other.m_Major || |
| 56 | (this->m_Major == other.m_Major && |
| 57 | this->m_Minor <= other.m_Minor); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion) |
| 62 | { |
| 63 | os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]"; |
| 64 | |
| 65 | return os; |
| 66 | } |
| 67 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 68 | class IBackendInternal : public IBackend |
| 69 | { |
| 70 | protected: |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 71 | /// Creation must be done through a specific |
| 72 | /// backend interface. |
David Beck | 6b779f0 | 2018-10-09 17:20:21 +0100 | [diff] [blame] | 73 | IBackendInternal() = default; |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 74 | |
| 75 | public: |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 76 | /// Allow backends created by the factory function |
| 77 | /// to be destroyed through IBackendInternal. |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 78 | ~IBackendInternal() override = default; |
| 79 | |
| 80 | using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 81 | using IBackendContextPtr = std::unique_ptr<IBackendContext>; |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 82 | /// This is the bridge between backend and backend profiling we'll keep it in the backend namespace. |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 83 | using IBackendProfilingContextPtr = std::shared_ptr<armnn::profiling::IBackendProfilingContext>; |
| 84 | using IBackendProfilingPtr = std::unique_ptr<armnn::profiling::IBackendProfiling>; |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 85 | using OptimizationPtr = std::unique_ptr<Optimization>; |
| 86 | using Optimizations = std::vector<OptimizationPtr>; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 87 | using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 88 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 89 | using IBackendSpecificModelContextPtr = std::shared_ptr<IBackendModelContext>; |
| 90 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 91 | using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>; |
| 92 | using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>; |
| 93 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 94 | using GraphUniquePtr = std::unique_ptr<Graph>; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 95 | using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 96 | |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 97 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
| 98 | using ISubGraphConverterPtr ARMNN_DEPRECATED_MSG("This type is no longer supported") |
| 99 | = std::unique_ptr<ISubGraphConverter>; |
| 100 | using SubGraphUniquePtr ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead") |
| 101 | = std::unique_ptr<SubGraph>; |
| 102 | |
| 103 | ARMNN_DEPRECATED_MSG("This method is no longer supported") |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 104 | virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const; |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 105 | |
| 106 | ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead") |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 107 | virtual Optimizations GetOptimizations() const; |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 108 | |
| 109 | ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead") |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 110 | virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const; |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 111 | ARMNN_NO_DEPRECATE_WARN_END |
| 112 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 113 | virtual IMemoryManagerUniquePtr CreateMemoryManager() const; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 114 | |
| 115 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 116 | const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0; |
| 117 | |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 118 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 119 | class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const; |
| 120 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 121 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 122 | const IMemoryManagerSharedPtr& memoryManager, |
| 123 | const ModelOptions& modelOptions) const; |
| 124 | |
| 125 | virtual IWorkloadFactoryPtr CreateWorkloadFactory( |
| 126 | class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 127 | const ModelOptions& modelOptions) const; |
| 128 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 129 | /// Create the runtime context of the backend |
| 130 | /// |
| 131 | /// Implementations may return a default-constructed IBackendContextPtr if |
| 132 | /// no context is needed at runtime. |
| 133 | /// Implementations must throw BackendUnavailableException if the backend |
| 134 | /// cannot be used (for example, necessary accelerator hardware is not present). |
| 135 | /// The default implementation always returns a default-constructed pointer. |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 136 | virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 137 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 138 | virtual IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions& modelOptions) const; |
| 139 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 140 | /// Create context specifically used for profiling interaction from backends. |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 141 | virtual IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions, |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 142 | IBackendProfilingPtr& backendProfiling); |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 143 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 144 | virtual ILayerSupportSharedPtr GetLayerSupport() const = 0; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 145 | |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 146 | virtual ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 147 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 148 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const; |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 149 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 150 | virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph, |
| 151 | const ModelOptions& modelOptions) const; |
| 152 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 153 | bool SupportsTensorAllocatorAPI() const; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 154 | |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 155 | ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory(); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 156 | |
| 157 | /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order. |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 158 | virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 159 | |
| 160 | /// (Optional) Register TensorHandleFactories |
| 161 | /// Either this method or CreateMemoryManager() and |
| 162 | /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented. |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 163 | virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& /*registry*/) {} |
Matteo Martincigh | ac60d28 | 2019-07-25 15:25:44 +0100 | [diff] [blame] | 164 | |
| 165 | /// Returns the version of the Backend API |
Matteo Martincigh | d0dc770 | 2019-08-01 17:09:03 +0100 | [diff] [blame] | 166 | static constexpr BackendVersion GetApiVersion() { return BackendVersion(1, 0); } |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 169 | using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>; |
| 170 | |
David Beck | e97c6e0 | 2018-10-03 13:09:28 +0100 | [diff] [blame] | 171 | } // namespace armnn |