blob: 9813ff05c7720c098fba9839b98ff10b9b926678 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_GRAPH_UTILS_H
25#define ARM_COMPUTE_GRAPH_UTILS_H
Georgios Pinitasd8734b52017-12-22 15:27:52 +000026
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Graph.h"
28#include "arm_compute/graph/PassManager.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000029
30namespace arm_compute
31{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033{
34// Forward Declaration
35class GraphContext;
36
Giorgio Arena6e9d0e02020-01-03 15:02:04 +000037inline bool is_utility_node(INode *node)
38{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039 std::set<NodeType> utility_node_types = {NodeType::PrintLayer};
Giorgio Arena6e9d0e02020-01-03 15:02:04 +000040 return utility_node_types.find(node->type()) != utility_node_types.end();
41}
42
Georgios Pinitasd8734b52017-12-22 15:27:52 +000043/** Returns the tensor descriptor of a given tensor
44 *
45 * @param[in] g Graph that the tensor belongs to
46 * @param[in] tid Tensor ID
47 *
48 * @return Tensor descriptor if tensor was found else empty descriptor
49 */
50inline TensorDescriptor get_tensor_descriptor(const Graph &g, TensorID tid)
51{
52 const Tensor *tensor = g.tensor(tid);
53 return (tensor != nullptr) ? tensor->desc() : TensorDescriptor();
54}
55/** Sets an accessor on a given tensor
56 *
57 * @param[in] tensor Tensor to set the accessor to
58 * @param[in] accessor Accessor to set
59 *
60 * @return True if accessor was set else false
61 */
62inline Status set_tensor_accessor(Tensor *tensor, std::unique_ptr<ITensorAccessor> accessor)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON(tensor == nullptr);
65 tensor->set_accessor(std::move(accessor));
66
67 return Status{};
68}
69/** Checks if a specific target is supported
70 *
71 * @param[in] target Target to check
72 *
73 * @return True if target is support else false
74 */
75bool is_target_supported(Target target);
76/** Returns default target for execution
77 *
78 * @note If an OpenCL backend exists then OpenCL is returned,
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000079 * else if the CPU backend exists returns @ref Target::NEON as target.
Georgios Pinitasd8734b52017-12-22 15:27:52 +000080 * If no backends are registered an error is raised.
81 *
82 * @return Default target
83 */
84Target get_default_target();
85/** Forces a single target to all graph constructs
86 *
87 * @param[in] g Graph to force target on
88 * @param[in] target Target to force
89 */
90void force_target_to_graph(Graph &g, Target target);
91/** Creates a default @ref PassManager
92 *
Georgios Pinitasfbb80542018-03-27 17:15:49 +010093 * @param[in] target Target to create the pass manager for
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000094 * @param[in] cfg Graph configuration meta-data
Georgios Pinitasfbb80542018-03-27 17:15:49 +010095 *
Georgios Pinitasd8734b52017-12-22 15:27:52 +000096 * @return A PassManager with default mutating passes
97 */
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000098PassManager create_default_pass_manager(Target target, const GraphConfig &cfg);
Georgios Pinitas7097e3c2019-02-20 18:11:42 +000099/** Setups requested backend context if it exists, is supported and hasn't been initialized already.
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000100 *
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000101 * @param[in,out] ctx Graph Context.
102 * @param[in] target Target to setup the backend for.
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000103 */
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000104void setup_requested_backend_context(GraphContext &ctx, Target target);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100105/** Default releases the graph context if not done manually
106 *
107 * @param[in,out] ctx Graph Context
108 */
109void release_default_graph_context(GraphContext &ctx);
Giorgio Arena9c67d382021-08-20 15:24:03 +0100110/** Synchronize kernels execution on the backends. On GPU, this results in a blocking call waiting for all kernels to be completed. */
111void sync_backends();
Georgios Pinitascac13b12018-04-27 19:07:19 +0100112/** Get size of a tensor's given dimension depending on its layout
113 *
114 * @param[in] descriptor Descriptor
115 * @param[in] data_layout_dimension Tensor data layout dimension
116 *
117 * @return Size of requested dimension
118 */
119size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension);
120/** Get index of a tensor's given dimension depending on its layout
121 *
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100122 * @param[in] data_layout Data layout of the tensor
Georgios Pinitascac13b12018-04-27 19:07:19 +0100123 * @param[in] data_layout_dimension Tensor data layout dimension
124 *
125 * @return Idx of given dimension
126 */
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100127size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100128/** Get the list of driving nodes of a given node
129 *
130 * @param[in] node Node to find the driving node of
131 *
132 * @return A list with the driving node of a given node
133 */
134std::vector<NodeIdxPair> get_driving_nodes(const INode &node);
Gunes Bayir814bddf2021-09-01 16:20:54 +0100135/** Get the list of driver nodes of a given node
136 *
137 * @param[in] node Node to find the driver node of
138 *
139 * @return A list with the driver node of a given node
140 */
141std::vector<NodeIdxPair> get_driver_nodes(const INode &node);
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100142/** Configures tensor
143 *
144 * @param[in, out] tensor Tensor to configure
145 */
146void configure_tensor(Tensor *tensor);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100147} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000148} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000149#endif /* ARM_COMPUTE_GRAPH_UTILS_H */