Compute Library
 21.02
Utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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  */
25 
29 
30 namespace arm_compute
31 {
32 namespace graph
33 {
35 {
37 }
38 
40 {
42  {
43  return Target::NEON;
44  }
46  {
47  return Target::CL;
48  }
50  {
51  return Target::GC;
52  }
53  ARM_COMPUTE_ERROR("No backend exists!");
54 }
55 
57 {
58  auto &nodes = g.nodes();
59  for(auto &node : nodes)
60  {
61  if(node)
62  {
63  node->set_assigned_target(target);
64  }
65  }
66 
67  auto &tensors = g.tensors();
68  for(auto &tensor : tensors)
69  {
70  if(tensor)
71  {
72  tensor->desc().target = target;
73  }
74  }
75 }
76 
78 {
79  PassManager pm;
80 
81  const bool is_target_gc = target == Target::GC;
82 
83  // Passes that mutate graph IR
84  if(cfg.convert_to_uint8)
85  {
86  pm.append(std::make_unique<SyntheticDataTypeMutator>(), !is_target_gc);
87  }
88  pm.append(std::make_unique<NodeFusionMutator>(), !is_target_gc);
89  pm.append(std::make_unique<GroupedConvolutionMutator>());
90  pm.append(std::make_unique<InPlaceOperationMutator>(), !is_target_gc);
91 
92  // Passes that mutate backend information
93  pm.append(std::make_unique<DepthConcatSubTensorMutator>(), !is_target_gc);
94  pm.append(std::make_unique<SplitLayerSubTensorMutator>(), !is_target_gc);
95  pm.append(std::make_unique<NodeExecutionMethodMutator>());
96 
97  return pm;
98 }
99 
101 {
102  for(const auto &backend : backends::BackendRegistry::get().backends())
103  {
104  if(backend.second->is_backend_supported())
105  {
106  backend.second->release_backend_context(ctx);
107  }
108  }
109 }
110 
112 {
113  if(backends::BackendRegistry::get().contains(target))
114  {
115  const auto &backend = backends::BackendRegistry::get().find_backend(target);
116  if(backend->is_backend_supported())
117  {
118  backend->setup_backend_context(ctx);
119  }
120  }
121 }
122 
123 size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
124 {
125  ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
126  return descriptor.shape[get_dimension_idx(descriptor.layout, data_layout_dimension)];
127 }
128 
129 size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
130 {
131  ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
132 
133  /* Return the index based on the data layout
134  * [N C H W]
135  * [3 2 1 0]
136  * [N H W C]
137  */
138  switch(data_layout_dimension)
139  {
141  return (data_layout == DataLayout::NCHW) ? 2 : 0;
142  break;
144  return (data_layout == DataLayout::NCHW) ? 1 : 2;
145  break;
147  return (data_layout == DataLayout::NCHW) ? 0 : 1;
148  break;
150  return 3;
151  break;
152  default:
153  break;
154  }
155  ARM_COMPUTE_ERROR("Data layout index not supported!");
156 }
157 
158 std::vector<NodeIdxPair> get_driving_nodes(const INode &node)
159 {
160  std::vector<NodeIdxPair> driving_nodes;
161 
162  const Graph *g = node.graph();
163  ARM_COMPUTE_ERROR_ON(g == nullptr);
164 
165  for(auto &output_edge_id : node.output_edges())
166  {
167  auto output_edge = g->edge(output_edge_id);
168  if(output_edge != nullptr)
169  {
170  ARM_COMPUTE_ERROR_ON(output_edge->consumer() == nullptr);
171  driving_nodes.push_back({ output_edge->consumer_id(), output_edge->consumer_idx() });
172  }
173  }
174 
175  return driving_nodes;
176 }
177 
179 {
180  if(tensor != nullptr && tensor->handle() == nullptr)
181  {
182  Target target = tensor->desc().target;
184  std::unique_ptr<ITensorHandle> handle = backend.create_tensor(*tensor);
185  ARM_COMPUTE_ERROR_ON_MSG(!handle, "Couldn't create backend handle!");
186  tensor->set_handle(std::move(handle));
187  }
188 }
189 } // namespace graph
190 } // namespace arm_compute
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:56
Neon capable target device.
Graph configuration structure Device target types.
Definition: Types.h:80
void configure_tensor(Tensor *tensor)
Configures tensor.
Definition: Utils.cpp:178
const std::set< EdgeID > & output_edges() const
Returns output edge set.
Definition: INode.cpp:132
std::vector< NodeIdxPair > get_driving_nodes(const INode &node)
Get the list of driving nodes of a given node.
Definition: Utils.cpp:158
void set_handle(std::unique_ptr< ITensorHandle > backend_tensor)
Sets the backend tensor.
Definition: Tensor.cpp:50
size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
Get size of a tensor&#39;s given dimension depending on its layout.
Definition: Utils.cpp:123
bool convert_to_uint8
Convert graph to a synthetic uint8 graph.
Definition: Types.h:86
ITensorHandle * handle()
Backend tensor handle accessor.
Definition: Tensor.cpp:55
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
DataLayoutDimension
[DataLayout enum definition]
Definition: Types.h:129
virtual bool is_backend_supported()=0
Checks if an instantiated backend is actually supported.
const Graph * graph() const
Returns node&#39;s Graph.
Definition: INode.cpp:112
IDeviceBackend & get_backend(Target target)
Get a backend from the registry.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const DataLayout data_layout
Definition: Im2Col.cpp:151
virtual void setup_backend_context(GraphContext &ctx)=0
Setups the given graph context.
Copyright (c) 2017-2021 Arm Limited.
virtual std::unique_ptr< ITensorHandle > create_tensor(const Tensor &tensor)=0
Create a backend Tensor.
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
Target get_default_target()
Returns default target for execution.
Definition: Utils.cpp:39
GLES compute capable target device.
std::vector< std::unique_ptr< Tensor > > & tensors()
Returns tensors of graph.
Definition: Graph.cpp:194
Node interface.
Definition: INode.h:45
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void setup_requested_backend_context(GraphContext &ctx, Target target)
Setups requested backend context if it exists, is supported and hasn&#39;t been initialized already...
Definition: Utils.cpp:111
bool is_target_supported(Target target)
Checks if a specific target is supported.
Definition: Utils.cpp:34
bool contains(Target target) const
Checks if a backend for a given target exists.
static BackendRegistry & get()
Gets backend registry instance.
Num samples, channels, height, width.
Graph class.
Definition: Graph.h:53
void append(std::unique_ptr< IGraphMutator > pass, bool conditional=true)
Appends a mutation pass.
Definition: PassManager.cpp:47
const std::vector< NodeID > & nodes(NodeType type)
Returns graph input nodes.
Definition: Graph.cpp:174
PassManager create_default_pass_manager(Target target, const GraphConfig &cfg)
Creates a default PassManager.
Definition: Utils.cpp:77
const Edge * edge(EdgeID id) const
Get edge object given its id.
Definition: Graph.cpp:214
IDeviceBackend * find_backend(Target target)
Finds a backend in the registry.
OpenCL capable target device.
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
void release_default_graph_context(GraphContext &ctx)
Default releases the graph context if not done manually.
Definition: Utils.cpp:100
size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get index of a tensor&#39;s given dimension depending on its layout.
Definition: Utils.cpp:129
Tensor object.
Definition: Tensor.h:41