blob: c548683e3e947f516a5856271ae365eea8e4407b [file] [log] [blame]
David Becke97c6e02018-10-03 13:09:28 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
David Beck9efb57d2018-11-05 13:40:33 +00007#include <armnn/Types.hpp>
David Beck1b61be52018-11-08 09:19:14 +00008#include <armnn/IRuntime.hpp>
Matteo Martincighfc598e12019-05-14 10:36:13 +01009#include <armnn/Deprecated.hpp>
Matteo Martincigh49124022019-01-11 13:25:59 +000010
Derek Lambertiff05cc52019-04-26 13:05:17 +010011#include <ISubgraphViewConverter.hpp>
12#include <SubgraphView.hpp>
Derek Lamberti84da38b2019-06-13 11:40:08 +010013#include <optimizations/Optimization.hpp>
Matteo Martincigh49124022019-01-11 13:25:59 +000014
Derek Lamberti84da38b2019-06-13 11:40:08 +010015#include "IBackendContext.hpp"
16#include "IMemoryManager.hpp"
17#include "ITensorHandleFactory.hpp"
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010018#include "OptimizationViews.hpp"
19
David Beck263e3492018-11-09 14:46:40 +000020#include <vector>
David Becke97c6e02018-10-03 13:09:28 +010021
22namespace armnn
23{
David Beck29c75de2018-10-23 13:35:58 +010024class IWorkloadFactory;
Aron Virginas-Tar56055192018-11-12 18:10:43 +000025class IMemoryManager;
David Beck111b5d92018-11-12 14:59:37 +000026class ILayerSupport;
David Becke97c6e02018-10-03 13:09:28 +010027
Matteo Martincighac60d282019-07-25 15:25:44 +010028struct BackendVersion
29{
30 uint32_t m_Major;
31 uint32_t m_Minor;
32
33 BackendVersion()
34 : m_Major(0)
35 , m_Minor(0)
36 {}
37 BackendVersion(uint32_t major, uint32_t minor)
38 : m_Major(major)
39 , m_Minor(minor)
40 {}
41
42 bool operator==(const BackendVersion& other) const
43 {
44 return this == &other ||
45 (this->m_Major == other.m_Major &&
46 this->m_Minor == other.m_Minor);
47 }
48
49 bool operator<=(const BackendVersion& other) const
50 {
51 return this->m_Major < other.m_Major ||
52 (this->m_Major == other.m_Major &&
53 this->m_Minor <= other.m_Minor);
54 }
55};
56
57inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
58{
59 os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
60
61 return os;
62}
63
David Becke97c6e02018-10-03 13:09:28 +010064class IBackendInternal : public IBackend
65{
66protected:
David Beck9efb57d2018-11-05 13:40:33 +000067 // Creation must be done through a specific
68 // backend interface.
David Beck6b779f02018-10-09 17:20:21 +010069 IBackendInternal() = default;
David Becke97c6e02018-10-03 13:09:28 +010070
71public:
David Beck29c75de2018-10-23 13:35:58 +010072 // Allow backends created by the factory function
73 // to be destroyed through IBackendInternal.
74 ~IBackendInternal() override = default;
75
76 using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
David Beck1b61be52018-11-08 09:19:14 +000077 using IBackendContextPtr = std::unique_ptr<IBackendContext>;
David Beck263e3492018-11-09 14:46:40 +000078 using OptimizationPtr = std::unique_ptr<Optimization>;
79 using Optimizations = std::vector<OptimizationPtr>;
David Beck111b5d92018-11-12 14:59:37 +000080 using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
David Beck1b61be52018-11-08 09:19:14 +000081
Aron Virginas-Tar56055192018-11-12 18:10:43 +000082 using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
83 using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
84
Matteo Martincigh602af092019-05-01 10:31:27 +010085 using GraphUniquePtr = std::unique_ptr<Graph>;
Derek Lambertiff05cc52019-04-26 13:05:17 +010086 using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
Matteo Martincighadddddb2019-01-24 14:06:23 +000087
Matteo Martincighc3ba50e2019-05-22 14:28:16 +010088 ARMNN_NO_DEPRECATE_WARN_BEGIN
89 using ISubGraphConverterPtr ARMNN_DEPRECATED_MSG("This type is no longer supported")
90 = std::unique_ptr<ISubGraphConverter>;
91 using SubGraphUniquePtr ARMNN_DEPRECATED_MSG("SubGraph is deprecated, use SubgraphView instead")
92 = std::unique_ptr<SubGraph>;
93
94 ARMNN_DEPRECATED_MSG("This method is no longer supported")
95 virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const
96 {
97 return ISubGraphConverterPtr{};
98 }
99
100 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
Derek Lamberti84da38b2019-06-13 11:40:08 +0100101 virtual Optimizations GetOptimizations() const
102 {
103 return Optimizations{};
104 }
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100105
106 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
107 virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const
108 {
109 optimizationAttempted = false;
110 return nullptr;
111 }
112 ARMNN_NO_DEPRECATE_WARN_END
113
Derek Lamberti84da38b2019-06-13 11:40:08 +0100114
115 virtual IMemoryManagerUniquePtr CreateMemoryManager() const
116 {
117 return IMemoryManagerUniquePtr();
Matteo Martincighac60d282019-07-25 15:25:44 +0100118 }
Aron Virginas-Tar56055192018-11-12 18:10:43 +0000119
120 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
121 const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
122
Derek Lamberti84da38b2019-06-13 11:40:08 +0100123 virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const
124 {
125 return IBackendContextPtr{};
126 }
Aron Virginas-Tar56055192018-11-12 18:10:43 +0000127
David Beck111b5d92018-11-12 14:59:37 +0000128 virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
Matteo Martincighadddddb2019-01-24 14:06:23 +0000129
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100130 // Default implementation of OptimizeSubgraphView for backward compatibility with the old API.
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100131 // Override this method with a custom optimization implementation.
Derek Lamberti8106b7c2019-05-07 21:33:30 +0100132 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100133 {
Matteo Martincigh84924332019-05-09 12:46:16 +0100134 bool optimizationAttempted = false;
Matteo Martincighfc598e12019-05-14 10:36:13 +0100135
136 ARMNN_NO_DEPRECATE_WARN_BEGIN
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100137 SubGraphUniquePtr optSubgraph = OptimizeSubGraph(subgraph, optimizationAttempted);
Matteo Martincighfc598e12019-05-14 10:36:13 +0100138 ARMNN_NO_DEPRECATE_WARN_END
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100139
140 OptimizationViews result;
Matteo Martincigh84924332019-05-09 12:46:16 +0100141 if (!optimizationAttempted)
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100142 {
143 result.AddUntouchedSubgraph(SubgraphView(subgraph));
144 }
145 else
146 {
147 if (optSubgraph)
148 {
Matteo Martincigh88054f82019-05-17 12:15:30 +0100149 result.AddSubstitution({subgraph, SubgraphView(*optSubgraph.get())});
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100150 }
151 else
152 {
153 result.AddFailedSubgraph(SubgraphView(subgraph));
154 }
155 }
156 return result;
157 }
Derek Lamberti84da38b2019-06-13 11:40:08 +0100158
159 bool SupportsTensorAllocatorAPI() const { return GetHandleFactoryPreferences().empty() == false; }
160
161 ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory()
162 {
163 auto favorites = GetHandleFactoryPreferences();
164 if (favorites.empty())
165 {
166 return ITensorHandleFactory::LegacyFactoryId;
167 }
168 return favorites[0];
169 }
170
171 /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
172 virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const
173 {
174 return std::vector<ITensorHandleFactory::FactoryId>();
175 }
176
177 /// (Optional) Register TensorHandleFactories
178 /// Either this method or CreateMemoryManager() and
179 /// IWorkloadFactory::CreateTensor()/IWorkloadFactory::CreateSubtensor() methods must be implemented.
180 virtual void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) {}
Matteo Martincighac60d282019-07-25 15:25:44 +0100181
182 /// Returns the version of the Backend API
183 static BackendVersion GetApiVersion() { return { 1, 0 }; }
David Becke97c6e02018-10-03 13:09:28 +0100184};
185
David Beck29c75de2018-10-23 13:35:58 +0100186using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
187
David Becke97c6e02018-10-03 13:09:28 +0100188} // namespace armnn