2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include <armnn/Types.hpp>
8 #include <armnn/IRuntime.hpp>
9 #include <armnn/Deprecated.hpp>
11 #include <ISubgraphViewConverter.hpp>
12 #include <SubgraphView.hpp>
13 #include <optimizations/Optimization.hpp>
15 #include "IBackendContext.hpp"
16 #include "IMemoryManager.hpp"
17 #include "ITensorHandleFactory.hpp"
18 #include "OptimizationViews.hpp"
24 class IWorkloadFactory;
37 BackendVersion(uint32_t major, uint32_t minor)
42 bool operator==(const BackendVersion& other) const
44 return this == &other ||
45 (this->m_Major == other.m_Major &&
46 this->m_Minor == other.m_Minor);
49 bool operator<=(const BackendVersion& other) const
51 return this->m_Major < other.m_Major ||
52 (this->m_Major == other.m_Major &&
53 this->m_Minor <= other.m_Minor);
57 inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
59 os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
64 class IBackendInternal : public IBackend
67 // Creation must be done through a specific
69 IBackendInternal() = default;
72 // Allow backends created by the factory function
73 // to be destroyed through IBackendInternal.
74 ~IBackendInternal() override = default;
76 using IWorkloadFactoryPtr = std::unique_ptr<IWorkloadFactory>;
77 using IBackendContextPtr = std::unique_ptr<IBackendContext>;
78 using OptimizationPtr = std::unique_ptr<Optimization>;
79 using Optimizations = std::vector<OptimizationPtr>;
80 using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
82 using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
83 using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
85 using GraphUniquePtr = std::unique_ptr<Graph>;
86 using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
88 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>;
94 ARMNN_DEPRECATED_MSG("This method is no longer supported")
95 virtual ISubGraphConverterPtr CreateSubGraphConverter(const std::shared_ptr<SubGraph>& subGraph) const
97 return ISubGraphConverterPtr{};
100 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
101 virtual Optimizations GetOptimizations() const
103 return Optimizations{};
106 ARMNN_DEPRECATED_MSG("Use \"OptimizationViews OptimizeSubgraphView(const SubgraphView&)\" instead")
107 virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const
109 optimizationAttempted = false;
112 ARMNN_NO_DEPRECATE_WARN_END
115 virtual IMemoryManagerUniquePtr CreateMemoryManager() const
117 return IMemoryManagerUniquePtr();
120 virtual IWorkloadFactoryPtr CreateWorkloadFactory(
121 const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
123 virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const
125 return IBackendContextPtr{};
128 virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
130 // Default implementation of OptimizeSubgraphView for backward compatibility with the old API.
131 // Override this method with a custom optimization implementation.
132 virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const
134 bool optimizationAttempted = false;
136 ARMNN_NO_DEPRECATE_WARN_BEGIN
137 SubGraphUniquePtr optSubgraph = OptimizeSubGraph(subgraph, optimizationAttempted);
138 ARMNN_NO_DEPRECATE_WARN_END
140 OptimizationViews result;
141 if (!optimizationAttempted)
143 result.AddUntouchedSubgraph(SubgraphView(subgraph));
149 result.AddSubstitution({subgraph, SubgraphView(*optSubgraph.get())});
153 result.AddFailedSubgraph(SubgraphView(subgraph));
159 bool SupportsTensorAllocatorAPI() const { return GetHandleFactoryPreferences().empty() == false; }
161 ITensorHandleFactory::FactoryId GetBackwardCompatibleFavoriteHandleFactory()
163 auto favorites = GetHandleFactoryPreferences();
164 if (favorites.empty())
166 return ITensorHandleFactory::LegacyFactoryId;
171 /// (Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
172 virtual std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const
174 return std::vector<ITensorHandleFactory::FactoryId>();
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) {}
182 /// Returns the version of the Backend API
183 static BackendVersion GetApiVersion() { return { 1, 0 }; }
186 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;