IVGCVSW-3543 Implement the backend versioning algorithm
[platform/upstream/armnn.git] / src / backends / backendsCommon / IBackendInternal.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include <armnn/Types.hpp>
8 #include <armnn/IRuntime.hpp>
9 #include <armnn/Deprecated.hpp>
10
11 #include <ISubgraphViewConverter.hpp>
12 #include <SubgraphView.hpp>
13 #include <optimizations/Optimization.hpp>
14
15 #include "IBackendContext.hpp"
16 #include "IMemoryManager.hpp"
17 #include "ITensorHandleFactory.hpp"
18 #include "OptimizationViews.hpp"
19
20 #include <vector>
21
22 namespace armnn
23 {
24 class IWorkloadFactory;
25 class IMemoryManager;
26 class ILayerSupport;
27
28 struct 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
57 inline std::ostream& operator<<(std::ostream& os, const BackendVersion& backendVersion)
58 {
59     os << "[" << backendVersion.m_Major << "." << backendVersion.m_Minor << "]";
60
61     return os;
62 }
63
64 class IBackendInternal : public IBackend
65 {
66 protected:
67     // Creation must be done through a specific
68     // backend interface.
69     IBackendInternal() = default;
70
71 public:
72     // 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>;
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>;
81
82     using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
83     using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
84
85     using GraphUniquePtr = std::unique_ptr<Graph>;
86     using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
87
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>;
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")
101     virtual Optimizations GetOptimizations() const
102     {
103         return Optimizations{};
104     }
105
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
114
115     virtual IMemoryManagerUniquePtr CreateMemoryManager() const
116     {
117         return IMemoryManagerUniquePtr();
118     }
119
120     virtual IWorkloadFactoryPtr CreateWorkloadFactory(
121         const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
122
123     virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const
124     {
125         return IBackendContextPtr{};
126     }
127
128     virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
129
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
133     {
134         bool optimizationAttempted = false;
135
136         ARMNN_NO_DEPRECATE_WARN_BEGIN
137         SubGraphUniquePtr optSubgraph = OptimizeSubGraph(subgraph, optimizationAttempted);
138         ARMNN_NO_DEPRECATE_WARN_END
139
140         OptimizationViews result;
141         if (!optimizationAttempted)
142         {
143             result.AddUntouchedSubgraph(SubgraphView(subgraph));
144         }
145         else
146         {
147             if (optSubgraph)
148             {
149                 result.AddSubstitution({subgraph, SubgraphView(*optSubgraph.get())});
150             }
151             else
152             {
153                 result.AddFailedSubgraph(SubgraphView(subgraph));
154             }
155         }
156         return result;
157     }
158
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) {}
181
182     /// Returns the version of the Backend API
183     static BackendVersion GetApiVersion() { return { 1, 0 }; }
184 };
185
186 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
187
188 } // namespace armnn