blob: 4c34dd85a5547e12b78006c3a8ba4bc97d1aa738 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Georgios Pinitas7097e3c2019-02-20 18:11:42 +00002 * Copyright (c) 2018-2019 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/Utils.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/GraphContext.h"
27#include "arm_compute/graph/backends/BackendRegistry.h"
28#include "arm_compute/graph/mutators/GraphMutators.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{
34bool is_target_supported(Target target)
35{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010036 return backends::BackendRegistry::get().contains(target) && backends::BackendRegistry::get().find_backend(target)->is_backend_supported();
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037}
38
39Target get_default_target()
40{
41 if(is_target_supported(Target::NEON))
42 {
43 return Target::NEON;
44 }
45 if(is_target_supported(Target::CL))
46 {
47 return Target::CL;
48 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +010049 if(is_target_supported(Target::GC))
50 {
51 return Target::GC;
52 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000053 ARM_COMPUTE_ERROR("No backend exists!");
54}
55
56void force_target_to_graph(Graph &g, Target target)
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
Georgios Pinitasfbb80542018-03-27 17:15:49 +010077PassManager create_default_pass_manager(Target target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000078{
79 PassManager pm;
80
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000081 const bool is_target_gc = target == Target::GC;
82
Georgios Pinitas2a2db592018-08-15 12:14:46 +010083 // Passes that mutate graph IR
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000084 pm.append(support::cpp14::make_unique<NodeFusionMutator>(), !is_target_gc);
Georgios Pinitas2a2db592018-08-15 12:14:46 +010085 pm.append(support::cpp14::make_unique<GroupedConvolutionMutator>());
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000086 pm.append(support::cpp14::make_unique<InPlaceOperationMutator>(), !is_target_gc);
Georgios Pinitas2a2db592018-08-15 12:14:46 +010087
88 // Passes that mutate backend information
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000089 pm.append(support::cpp14::make_unique<DepthConcatSubTensorMutator>(), !is_target_gc);
90 pm.append(support::cpp14::make_unique<SplitLayerSubTensorMutator>(), !is_target_gc);
Georgios Pinitas2a2db592018-08-15 12:14:46 +010091 pm.append(support::cpp14::make_unique<NodeExecutionMethodMutator>());
Georgios Pinitasd8734b52017-12-22 15:27:52 +000092
93 return pm;
94}
95
Anthony Barbierb6eb3532018-08-08 13:20:04 +010096void release_default_graph_context(GraphContext &ctx)
97{
98 for(const auto &backend : backends::BackendRegistry::get().backends())
99 {
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100100 if(backend.second->is_backend_supported())
101 {
102 backend.second->release_backend_context(ctx);
103 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100104 }
105}
106
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000107void setup_requested_backend_context(GraphContext &ctx, Target target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000108{
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000109 if(backends::BackendRegistry::get().contains(target))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000110 {
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000111 const auto &backend = backends::BackendRegistry::get().find_backend(target);
112 if(backend->is_backend_supported())
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100113 {
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000114 backend->setup_backend_context(ctx);
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100115 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000116 }
117}
Georgios Pinitascac13b12018-04-27 19:07:19 +0100118
119size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
120{
121 ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100122 return descriptor.shape[get_dimension_idx(descriptor.layout, data_layout_dimension)];
Georgios Pinitascac13b12018-04-27 19:07:19 +0100123}
124
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100125size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Georgios Pinitascac13b12018-04-27 19:07:19 +0100126{
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100127 ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
Georgios Pinitascac13b12018-04-27 19:07:19 +0100128
129 /* Return the index based on the data layout
130 * [N C H W]
131 * [3 2 1 0]
132 * [N H W C]
133 */
134 switch(data_layout_dimension)
135 {
136 case DataLayoutDimension::CHANNEL:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100137 return (data_layout == DataLayout::NCHW) ? 2 : 0;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100138 break;
139 case DataLayoutDimension::HEIGHT:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100140 return (data_layout == DataLayout::NCHW) ? 1 : 2;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100141 break;
142 case DataLayoutDimension::WIDTH:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100143 return (data_layout == DataLayout::NCHW) ? 0 : 1;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100144 break;
145 case DataLayoutDimension::BATCHES:
146 return 3;
147 break;
148 default:
149 ARM_COMPUTE_ERROR("Data layout index not supported!");
150 break;
151 }
152}
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100153
154std::vector<NodeIdxPair> get_driving_nodes(const INode &node)
155{
156 std::vector<NodeIdxPair> driving_nodes;
157
158 const Graph *g = node.graph();
159 ARM_COMPUTE_ERROR_ON(g == nullptr);
160
161 for(auto &output_edge_id : node.output_edges())
162 {
163 auto output_edge = g->edge(output_edge_id);
164 if(output_edge != nullptr)
165 {
166 ARM_COMPUTE_ERROR_ON(output_edge->consumer() == nullptr);
167 driving_nodes.push_back({ output_edge->consumer_id(), output_edge->consumer_idx() });
168 }
169 }
170
171 return driving_nodes;
172}
173
174void configure_tensor(Tensor *tensor)
175{
176 if(tensor != nullptr && tensor->handle() == nullptr)
177 {
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100178 Target target = tensor->desc().target;
179 backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(target);
180 std::unique_ptr<ITensorHandle> handle = backend.create_tensor(*tensor);
181 ARM_COMPUTE_ERROR_ON_MSG(!handle, "Couldn't create backend handle!");
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100182 tensor->set_handle(std::move(handle));
183 }
184}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100185} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100186} // namespace arm_compute