blob: b1be51ee301701bbcd3da2e322ac7aac0cb13443 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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{
39 std::set<NodeType> utility_node_types = { NodeType::PrintLayer };
40 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,
79 * else if the NEON backend exists returns NEON as target.
80 * 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);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100110/** Get size of a tensor's given dimension depending on its layout
111 *
112 * @param[in] descriptor Descriptor
113 * @param[in] data_layout_dimension Tensor data layout dimension
114 *
115 * @return Size of requested dimension
116 */
117size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension);
118/** Get index of a tensor's given dimension depending on its layout
119 *
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100120 * @param[in] data_layout Data layout of the tensor
Georgios Pinitascac13b12018-04-27 19:07:19 +0100121 * @param[in] data_layout_dimension Tensor data layout dimension
122 *
123 * @return Idx of given dimension
124 */
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100125size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100126/** Get the list of driving nodes of a given node
127 *
128 * @param[in] node Node to find the driving node of
129 *
130 * @return A list with the driving node of a given node
131 */
132std::vector<NodeIdxPair> get_driving_nodes(const INode &node);
133/** Configures tensor
134 *
135 * @param[in, out] tensor Tensor to configure
136 */
137void configure_tensor(Tensor *tensor);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100138} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000139} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000140#endif /* ARM_COMPUTE_GRAPH_UTILS_H */