Compute Library
 21.05
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 {
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  */
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 /** 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  */
117 size_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  *
120  * @param[in] data_layout Data layout of the tensor
121  * @param[in] data_layout_dimension Tensor data layout dimension
122  *
123  * @return Idx of given dimension
124  */
125 size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension);
126 /** 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  */
132 std::vector<NodeIdxPair> get_driving_nodes(const INode &node);
133 /** Configures tensor
134  *
135  * @param[in, out] tensor Tensor to configure
136  */
137 void configure_tensor(Tensor *tensor);
138 } // namespace graph
139 } // namespace arm_compute
140 #endif /* ARM_COMPUTE_GRAPH_UTILS_H */
TensorDescriptor get_tensor_descriptor(const Graph &g, TensorID tid)
Returns the tensor descriptor of a given tensor.
Definition: Utils.h:50
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:52
void configure_tensor(Tensor *tensor)
Configures tensor.
Definition: Utils.cpp:186
std::vector< NodeIdxPair > get_driving_nodes(const INode &node)
Get the list of driving nodes of a given node.
Definition: Utils.cpp:166
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:131
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:123
bool is_utility_node(INode *node)
Definition: Utils.h:37
const DataLayout data_layout
Definition: Im2Col.cpp:151
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void set_accessor(std::unique_ptr< ITensorAccessor > accessor)
Sets the backend tensor accessor.
Definition: Tensor.cpp:60
Copyright (c) 2017-2021 Arm Limited.
Status set_tensor_accessor(Tensor *tensor, std::unique_ptr< ITensorAccessor > accessor)
Sets an accessor on a given tensor.
Definition: Utils.h:62
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
Target get_default_target()
Returns default target for execution.
Definition: Utils.cpp:39
Node interface.
Definition: INode.h:45
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:119
bool is_target_supported(Target target)
Checks if a specific target is supported.
Definition: Utils.cpp:34
Basic implementation of the tensor interface.
Definition: Tensor.h:37
Graph class.
Definition: Graph.h:53
PassManager create_default_pass_manager(Target target, const GraphConfig &cfg)
Creates a default PassManager.
Definition: Utils.cpp:73
virtual NodeType type() const =0
Returns node's type.
unsigned int TensorID
Definition: Types.h:67
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
void release_default_graph_context(GraphContext &ctx)
Default releases the graph context if not done manually.
Definition: Utils.cpp:108
const Tensor * tensor(TensorID id) const
Get tensor object given its id.
Definition: Graph.cpp:224
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:137
Tensor object.
Definition: Tensor.h:41