a0d6569949052123de837fc9e94d00ffe953c4c3
[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 class IBackendInternal : public IBackend
29 {
30 protected:
31     // Creation must be done through a specific
32     // backend interface.
33     IBackendInternal() = default;
34
35 public:
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>;
41     using IBackendContextPtr = std::unique_ptr<IBackendContext>;
42     using OptimizationPtr = std::unique_ptr<Optimization>;
43     using Optimizations = std::vector<OptimizationPtr>;
44     using ILayerSupportSharedPtr = std::shared_ptr<ILayerSupport>;
45
46     using IMemoryManagerUniquePtr = std::unique_ptr<IMemoryManager>;
47     using IMemoryManagerSharedPtr = std::shared_ptr<IMemoryManager>;
48
49     using GraphUniquePtr = std::unique_ptr<Graph>;
50     using SubgraphViewUniquePtr = std::unique_ptr<SubgraphView>;
51
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")
65     virtual Optimizations GetOptimizations() const
66     {
67         return Optimizations{};
68     }
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
78
79     virtual IMemoryManagerUniquePtr CreateMemoryManager() const
80     {
81         return IMemoryManagerUniquePtr();
82     };
83
84     virtual IWorkloadFactoryPtr CreateWorkloadFactory(
85         const IMemoryManagerSharedPtr& memoryManager = nullptr) const = 0;
86
87     virtual IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const
88     {
89         return IBackendContextPtr{};
90     }
91
92     virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
93
94     // Default implementation of OptimizeSubgraphView for backward compatibility with the old API.
95     // Override this method with a custom optimization implementation.
96     virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const
97     {
98         bool optimizationAttempted = false;
99
100         ARMNN_NO_DEPRECATE_WARN_BEGIN
101         SubGraphUniquePtr optSubgraph = OptimizeSubGraph(subgraph, optimizationAttempted);
102         ARMNN_NO_DEPRECATE_WARN_END
103
104         OptimizationViews result;
105         if (!optimizationAttempted)
106         {
107             result.AddUntouchedSubgraph(SubgraphView(subgraph));
108         }
109         else
110         {
111             if (optSubgraph)
112             {
113                 result.AddSubstitution({subgraph, SubgraphView(*optSubgraph.get())});
114             }
115             else
116             {
117                 result.AddFailedSubgraph(SubgraphView(subgraph));
118             }
119         }
120         return result;
121     }
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) {}
145 };
146
147 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
148
149 } // namespace armnn