Compute Library
 23.08
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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_UTILS_H
25 #define ARM_COMPUTE_GRAPH_UTILS_H
26 
29 
30 namespace arm_compute
31 {
32 namespace graph
33 {
34 // Forward Declaration
35 class GraphContext;
36 
37 inline 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 
43 /** 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  */
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  */
62 inline Status set_tensor_accessor(Tensor *tensor, std::unique_ptr<ITensorAccessor> accessor)
63 {
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  */
75 bool 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 CPU backend exists returns @ref Target::NEON as target.
80  * If no backends are registered an error is raised.
81  *
82  * @return Default target
83  */
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  */
90 void force_target_to_graph(Graph &g, Target target);
91 /** Creates a default @ref PassManager
92  *
93  * @param[in] target Target to create the pass manager for
94  * @param[in] cfg Graph configuration meta-data
95  *
96  * @return A PassManager with default mutating passes
97  */
98 PassManager create_default_pass_manager(Target target, const GraphConfig &cfg);
99 /** Setups requested backend context if it exists, is supported and hasn't been initialized already.
100  *
101  * @param[in,out] ctx Graph Context.
102  * @param[in] target Target to setup the backend for.
103  */
104 void setup_requested_backend_context(GraphContext &ctx, Target target);
105 /** Default releases the graph context if not done manually
106  *
107  * @param[in,out] ctx Graph Context
108  */
109 void release_default_graph_context(GraphContext &ctx);
110 /** Synchronize kernels execution on the backends. On GPU, this results in a blocking call waiting for all kernels to be completed. */
111 void sync_backends();
112 /** 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  */
119 size_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  *
122  * @param[in] data_layout Data layout of the tensor
123  * @param[in] data_layout_dimension Tensor data layout dimension
124  *
125  * @return Idx of given dimension
126  */
127 size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
128 /** 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  */
134 std::vector<NodeIdxPair> get_driving_nodes(const INode &node);
135 /** 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  */
141 std::vector<NodeIdxPair> get_driver_nodes(const INode &node);
142 /** Configures tensor
143  *
144  * @param[in, out] tensor Tensor to configure
145  */
147 } // namespace graph
148 } // namespace arm_compute
149 #endif /* ARM_COMPUTE_GRAPH_UTILS_H */
arm_compute::graph::get_driving_nodes
std::vector< NodeIdxPair > get_driving_nodes(const INode &node)
Get the list of driving nodes of a given node.
Definition: Utils.cpp:177
arm_compute::graph::force_target_to_graph
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:52
arm_compute::DataLayout
DataLayout
[DataLayout enum definition]
Definition: CoreTypes.h:109
arm_compute::DataLayoutDimension
DataLayoutDimension
[DataLayout enum definition]
Definition: CoreTypes.h:120
arm_compute::graph::setup_requested_backend_context
void setup_requested_backend_context(GraphContext &ctx, Target target)
Setups requested backend context if it exists, is supported and hasn't been initialized already.
Definition: Utils.cpp:130
arm_compute::graph::Tensor
Tensor object.
Definition: Tensor.h:41
arm_compute::graph::get_dimension_size
size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
Get size of a tensor's given dimension depending on its layout.
Definition: Utils.cpp:142
arm_compute::test::validation::data_layout
const auto data_layout
Definition: ConvolutionLayer.cpp:406
arm_compute::graph::is_utility_node
bool is_utility_node(INode *node)
Definition: Utils.h:37
arm_compute::graph::get_driver_nodes
std::vector< NodeIdxPair > get_driver_nodes(const INode &node)
Get the list of driver nodes of a given node.
Definition: Utils.cpp:197
PassManager.h
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:297
arm_compute::graph::INode
Node interface.
Definition: INode.h:46
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::graph::is_target_supported
bool is_target_supported(Target target)
Checks if a specific target is supported.
Definition: Utils.cpp:34
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:66
arm_compute::graph::configure_tensor
void configure_tensor(Tensor *tensor)
Configures tensor.
Definition: Utils.cpp:217
arm_compute::graph::INode::type
virtual NodeType type() const =0
Returns node's type.
arm_compute::graph::get_dimension_idx
size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get index of a tensor's given dimension depending on its layout.
Definition: Utils.cpp:148
arm_compute::graph::create_default_pass_manager
PassManager create_default_pass_manager(Target target, const GraphConfig &cfg)
Creates a default PassManager.
Definition: Utils.cpp:73
arm_compute::graph::get_default_target
Target get_default_target()
Returns default target for execution.
Definition: Utils.cpp:39
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::graph::Graph::tensor
const Tensor * tensor(TensorID id) const
Get tensor object given its id.
Definition: Graph.cpp:224
arm_compute::graph::TensorDescriptor
Tensor metadata class.
Definition: TensorDescriptor.h:38
arm_compute::graph::NodeType::PrintLayer
@ PrintLayer
arm_compute::graph::sync_backends
void sync_backends()
Synchronize kernels execution on the backends.
Definition: Utils.cpp:119
arm_compute::graph::get_tensor_descriptor
TensorDescriptor get_tensor_descriptor(const Graph &g, TensorID tid)
Returns the tensor descriptor of a given tensor.
Definition: Utils.h:50
arm_compute::graph::Graph
Graph class.
Definition: Graph.h:52
arm_compute::graph::release_default_graph_context
void release_default_graph_context(GraphContext &ctx)
Default releases the graph context if not done manually.
Definition: Utils.cpp:108
arm_compute::Tensor
Basic implementation of the tensor interface.
Definition: Tensor.h:37
arm_compute::Target
Target
Definition: Types.h:45
Graph.h
arm_compute::graph::TensorID
unsigned int TensorID
Definition: Types.h:72
arm_compute::graph::set_tensor_accessor
Status set_tensor_accessor(Tensor *tensor, std::unique_ptr< ITensorAccessor > accessor)
Sets an accessor on a given tensor.
Definition: Utils.h:62