xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/graph/IDeviceBackend.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2019,2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_GRAPH_IDEVICEBACKEND_H
25 #define ARM_COMPUTE_GRAPH_IDEVICEBACKEND_H
26 
27 #include "arm_compute/graph/ITensorHandle.h"
28 #include "arm_compute/graph/Types.h"
29 #include "arm_compute/runtime/IFunction.h"
30 #include "arm_compute/runtime/IMemoryManager.h"
31 #include "arm_compute/runtime/IWeightsManager.h"
32 
33 #include <memory>
34 
35 namespace arm_compute
36 {
37 namespace graph
38 {
39 // Forward declarations
40 class Graph;
41 class GraphContext;
42 class Tensor;
43 class INode;
44 
45 namespace backends
46 {
47 /** Device backend interface */
48 class IDeviceBackend
49 {
50 public:
51     /** Virtual Destructor */
52     virtual ~IDeviceBackend() = default;
53     /** Initializes the backend */
54     virtual void initialize_backend() = 0;
55     /** Setups the given graph context
56      *
57      * @param[in,out] ctx Graph context
58      */
59     virtual void setup_backend_context(GraphContext &ctx) = 0;
60     /** Release the backend specific resources associated to a given graph context
61      *
62      * @param[in,out] ctx Graph context
63      */
64     virtual void release_backend_context(GraphContext &ctx) = 0;
65     /** Checks if an instantiated backend is actually supported
66      *
67      * @return True if the backend is supported else false
68      */
69     virtual bool is_backend_supported() = 0;
70     /** Gets a backend memory allocator
71      *
72      * @return Backend memory allocator
73      */
74     virtual IAllocator *backend_allocator() = 0;
75     /** Create a backend Tensor
76      *
77      * @param[in] tensor The tensor we want to create a backend tensor for
78      *
79      * @return Backend tensor handle
80      */
81     virtual std::unique_ptr<ITensorHandle> create_tensor(const Tensor &tensor) = 0;
82     /** Create a backend Sub-Tensor
83      *
84      * @param[in] parent        Parent sub-tensor handle
85      * @param[in] shape         Shape of the sub-tensor
86      * @param[in] coords        Starting coordinates of the sub-tensor
87      * @param[in] extend_parent Extends parent shape if true
88      *
89      * @return Backend sub-tensor handle
90      */
91     virtual std::unique_ptr<ITensorHandle> create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) = 0;
92     /** Configure a backend Node
93      *
94      * @note This creates an appropriate configured backend function for the given node
95      *
96      * @param[in] node The node we want to configure
97      * @param[in] ctx  Context to use
98      *
99      * @return Backend execution function
100      */
101     virtual std::unique_ptr<arm_compute::IFunction> configure_node(INode &node, GraphContext &ctx) = 0;
102     /** Validate a node
103      *
104      * @param[in] node The node we want to validate
105      *
106      * @return An error status
107      */
108     virtual Status validate_node(INode &node) = 0;
109     /** Create a backend memory manager given its affinity
110      *
111      * @param[in] affinity Memory Manager affinity
112      *
113      * @return Memory manager
114      */
115     virtual std::shared_ptr<arm_compute::IMemoryManager> create_memory_manager(MemoryManagerAffinity affinity) = 0;
116     /** Create a backend weights manager
117      *
118      * @return Weights manager
119      */
120     virtual std::shared_ptr<arm_compute::IWeightsManager> create_weights_manager() = 0;
121     /** Synchronize kernels execution on the backend. On GPU, this results in a blocking call waiting for all kernels to be completed. */
122     virtual void sync() = 0;
123 };
124 } // namespace backends
125 } // namespace graph
126 } // namespace arm_compute
127 #endif //ARM_COMPUTE_GRAPH_IDEVICEBACKEND_H
128