xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/graph/Utils.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_GRAPH_UTILS_H
25*c217d954SCole Faust #define ARM_COMPUTE_GRAPH_UTILS_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/graph/Graph.h"
28*c217d954SCole Faust #include "arm_compute/graph/PassManager.h"
29*c217d954SCole Faust 
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace graph
33*c217d954SCole Faust {
34*c217d954SCole Faust // Forward Declaration
35*c217d954SCole Faust class GraphContext;
36*c217d954SCole Faust 
is_utility_node(INode * node)37*c217d954SCole Faust inline bool is_utility_node(INode *node)
38*c217d954SCole Faust {
39*c217d954SCole Faust     std::set<NodeType> utility_node_types = { NodeType::PrintLayer };
40*c217d954SCole Faust     return utility_node_types.find(node->type()) != utility_node_types.end();
41*c217d954SCole Faust }
42*c217d954SCole Faust 
43*c217d954SCole Faust /** Returns the tensor descriptor of a given tensor
44*c217d954SCole Faust  *
45*c217d954SCole Faust  * @param[in] g   Graph that the tensor belongs to
46*c217d954SCole Faust  * @param[in] tid Tensor ID
47*c217d954SCole Faust  *
48*c217d954SCole Faust  * @return Tensor descriptor if tensor was found else empty descriptor
49*c217d954SCole Faust  */
get_tensor_descriptor(const Graph & g,TensorID tid)50*c217d954SCole Faust inline TensorDescriptor get_tensor_descriptor(const Graph &g, TensorID tid)
51*c217d954SCole Faust {
52*c217d954SCole Faust     const Tensor *tensor = g.tensor(tid);
53*c217d954SCole Faust     return (tensor != nullptr) ? tensor->desc() : TensorDescriptor();
54*c217d954SCole Faust }
55*c217d954SCole Faust /** Sets an accessor on a given tensor
56*c217d954SCole Faust  *
57*c217d954SCole Faust  * @param[in] tensor   Tensor to set the accessor to
58*c217d954SCole Faust  * @param[in] accessor Accessor to set
59*c217d954SCole Faust  *
60*c217d954SCole Faust  * @return True if accessor was set else false
61*c217d954SCole Faust  */
set_tensor_accessor(Tensor * tensor,std::unique_ptr<ITensorAccessor> accessor)62*c217d954SCole Faust inline Status set_tensor_accessor(Tensor *tensor, std::unique_ptr<ITensorAccessor> accessor)
63*c217d954SCole Faust {
64*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(tensor == nullptr);
65*c217d954SCole Faust     tensor->set_accessor(std::move(accessor));
66*c217d954SCole Faust 
67*c217d954SCole Faust     return Status{};
68*c217d954SCole Faust }
69*c217d954SCole Faust /** Checks if a specific target is supported
70*c217d954SCole Faust  *
71*c217d954SCole Faust  * @param[in] target Target to check
72*c217d954SCole Faust  *
73*c217d954SCole Faust  * @return True if target is support else false
74*c217d954SCole Faust  */
75*c217d954SCole Faust bool is_target_supported(Target target);
76*c217d954SCole Faust /** Returns default target for execution
77*c217d954SCole Faust  *
78*c217d954SCole Faust  * @note If an OpenCL backend exists then OpenCL is returned,
79*c217d954SCole Faust  *       else if the CPU backend exists returns @ref Target::NEON as target.
80*c217d954SCole Faust  *       If no backends are registered an error is raised.
81*c217d954SCole Faust  *
82*c217d954SCole Faust  * @return Default target
83*c217d954SCole Faust  */
84*c217d954SCole Faust Target get_default_target();
85*c217d954SCole Faust /** Forces a single target to all graph constructs
86*c217d954SCole Faust  *
87*c217d954SCole Faust  * @param[in] g      Graph to force target on
88*c217d954SCole Faust  * @param[in] target Target to force
89*c217d954SCole Faust  */
90*c217d954SCole Faust void force_target_to_graph(Graph &g, Target target);
91*c217d954SCole Faust /** Creates a default @ref PassManager
92*c217d954SCole Faust  *
93*c217d954SCole Faust  * @param[in] target Target to create the pass manager for
94*c217d954SCole Faust  * @param[in] cfg    Graph configuration meta-data
95*c217d954SCole Faust  *
96*c217d954SCole Faust  * @return A PassManager with default mutating passes
97*c217d954SCole Faust  */
98*c217d954SCole Faust PassManager create_default_pass_manager(Target target, const GraphConfig &cfg);
99*c217d954SCole Faust /** Setups requested backend context if it exists, is supported and hasn't been initialized already.
100*c217d954SCole Faust  *
101*c217d954SCole Faust  * @param[in,out] ctx    Graph Context.
102*c217d954SCole Faust  * @param[in]     target Target to setup the backend for.
103*c217d954SCole Faust  */
104*c217d954SCole Faust void setup_requested_backend_context(GraphContext &ctx, Target target);
105*c217d954SCole Faust /** Default releases the graph context if not done manually
106*c217d954SCole Faust  *
107*c217d954SCole Faust  * @param[in,out] ctx Graph Context
108*c217d954SCole Faust  */
109*c217d954SCole Faust void release_default_graph_context(GraphContext &ctx);
110*c217d954SCole Faust /** Synchronize kernels execution on the backends. On GPU, this results in a blocking call waiting for all kernels to be completed. */
111*c217d954SCole Faust void sync_backends();
112*c217d954SCole Faust /** Get size of a tensor's given dimension depending on its layout
113*c217d954SCole Faust  *
114*c217d954SCole Faust  * @param[in] descriptor            Descriptor
115*c217d954SCole Faust  * @param[in] data_layout_dimension Tensor data layout dimension
116*c217d954SCole Faust  *
117*c217d954SCole Faust  * @return Size of requested dimension
118*c217d954SCole Faust  */
119*c217d954SCole Faust size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension);
120*c217d954SCole Faust /** Get index of a tensor's given dimension depending on its layout
121*c217d954SCole Faust  *
122*c217d954SCole Faust  * @param[in] data_layout           Data layout of the tensor
123*c217d954SCole Faust  * @param[in] data_layout_dimension Tensor data layout dimension
124*c217d954SCole Faust  *
125*c217d954SCole Faust  * @return Idx of given dimension
126*c217d954SCole Faust  */
127*c217d954SCole Faust size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
128*c217d954SCole Faust /** Get the list of driving nodes of a given node
129*c217d954SCole Faust  *
130*c217d954SCole Faust  * @param[in] node Node to find the driving node of
131*c217d954SCole Faust  *
132*c217d954SCole Faust  * @return A list with the driving node of a given node
133*c217d954SCole Faust  */
134*c217d954SCole Faust std::vector<NodeIdxPair> get_driving_nodes(const INode &node);
135*c217d954SCole Faust /** Get the list of driver nodes of a given node
136*c217d954SCole Faust  *
137*c217d954SCole Faust  * @param[in] node Node to find the driver node of
138*c217d954SCole Faust  *
139*c217d954SCole Faust  * @return A list with the driver node of a given node
140*c217d954SCole Faust  */
141*c217d954SCole Faust std::vector<NodeIdxPair> get_driver_nodes(const INode &node);
142*c217d954SCole Faust /** Configures tensor
143*c217d954SCole Faust  *
144*c217d954SCole Faust  * @param[in, out] tensor Tensor to configure
145*c217d954SCole Faust  */
146*c217d954SCole Faust void configure_tensor(Tensor *tensor);
147*c217d954SCole Faust } // namespace graph
148*c217d954SCole Faust } // namespace arm_compute
149*c217d954SCole Faust #endif /* ARM_COMPUTE_GRAPH_UTILS_H */
150