blob: dcab177a3bb05fd4dc8cffed92e1247fcaed5ea5 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
SiCongLif466d752021-03-01 15:26:18 +00002 * Copyright (c) 2018-2021 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 Pinitasd8734b52017-12-22 15:27:52 +000049 ARM_COMPUTE_ERROR("No backend exists!");
50}
51
52void force_target_to_graph(Graph &g, Target target)
53{
54 auto &nodes = g.nodes();
55 for(auto &node : nodes)
56 {
57 if(node)
58 {
59 node->set_assigned_target(target);
60 }
61 }
62
63 auto &tensors = g.tensors();
64 for(auto &tensor : tensors)
65 {
66 if(tensor)
67 {
68 tensor->desc().target = target;
69 }
70 }
71}
72
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000073PassManager create_default_pass_manager(Target target, const GraphConfig &cfg)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000074{
Michele Di Giorgio40efd532021-03-18 17:32:00 +000075 ARM_COMPUTE_UNUSED(target);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000076 PassManager pm;
77
Georgios Pinitas2a2db592018-08-15 12:14:46 +010078 // Passes that mutate graph IR
SiCongLif466d752021-03-01 15:26:18 +000079 if(cfg.use_synthetic_type)
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000080 {
SiCongLif466d752021-03-01 15:26:18 +000081 switch(cfg.synthetic_type)
82 {
83 case DataType::QASYMM8:
84 case DataType::QASYMM8_SIGNED:
85 {
Michele Di Giorgio40efd532021-03-18 17:32:00 +000086 pm.append(std::make_unique<SyntheticDataTypeMutator>(cfg.synthetic_type));
SiCongLif466d752021-03-01 15:26:18 +000087 break;
88 }
89 default:
90 {
91 ARM_COMPUTE_ERROR("Unsupported DataType for SyntheticDataTypeMutator");
92 break;
93 }
94 }
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000095 }
Michele Di Giorgio40efd532021-03-18 17:32:00 +000096 pm.append(std::make_unique<NodeFusionMutator>());
Georgios Pinitas40f51a62020-11-21 03:04:18 +000097 pm.append(std::make_unique<GroupedConvolutionMutator>());
Michele Di Giorgio40efd532021-03-18 17:32:00 +000098 pm.append(std::make_unique<InPlaceOperationMutator>());
Georgios Pinitas2a2db592018-08-15 12:14:46 +010099
100 // Passes that mutate backend information
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000101 pm.append(std::make_unique<DepthConcatSubTensorMutator>());
102 pm.append(std::make_unique<SplitLayerSubTensorMutator>());
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000103 pm.append(std::make_unique<NodeExecutionMethodMutator>());
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104
105 return pm;
106}
107
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100108void release_default_graph_context(GraphContext &ctx)
109{
110 for(const auto &backend : backends::BackendRegistry::get().backends())
111 {
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100112 if(backend.second->is_backend_supported())
113 {
114 backend.second->release_backend_context(ctx);
115 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100116 }
117}
118
Giorgio Arena9c67d382021-08-20 15:24:03 +0100119void sync_backends()
120{
121 for(const auto &backend : backends::BackendRegistry::get().backends())
122 {
Giorgio Arenaa46c9c92021-08-25 12:12:09 +0100123 if(backend.second->backend_allocator())
Giorgio Arena9c67d382021-08-20 15:24:03 +0100124 {
125 backend.second->sync();
126 }
127 }
128}
129
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000130void setup_requested_backend_context(GraphContext &ctx, Target target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000131{
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000132 if(backends::BackendRegistry::get().contains(target))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000133 {
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000134 const auto &backend = backends::BackendRegistry::get().find_backend(target);
135 if(backend->is_backend_supported())
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100136 {
Georgios Pinitas7097e3c2019-02-20 18:11:42 +0000137 backend->setup_backend_context(ctx);
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100138 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000139 }
140}
Georgios Pinitascac13b12018-04-27 19:07:19 +0100141
142size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
143{
144 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 +0100145 return descriptor.shape[get_dimension_idx(descriptor.layout, data_layout_dimension)];
Georgios Pinitascac13b12018-04-27 19:07:19 +0100146}
147
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100148size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Georgios Pinitascac13b12018-04-27 19:07:19 +0100149{
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100150 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 +0100151
152 /* Return the index based on the data layout
153 * [N C H W]
154 * [3 2 1 0]
155 * [N H W C]
156 */
157 switch(data_layout_dimension)
158 {
159 case DataLayoutDimension::CHANNEL:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100160 return (data_layout == DataLayout::NCHW) ? 2 : 0;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100161 break;
162 case DataLayoutDimension::HEIGHT:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100163 return (data_layout == DataLayout::NCHW) ? 1 : 2;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100164 break;
165 case DataLayoutDimension::WIDTH:
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100166 return (data_layout == DataLayout::NCHW) ? 0 : 1;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100167 break;
168 case DataLayoutDimension::BATCHES:
169 return 3;
170 break;
171 default:
Georgios Pinitascac13b12018-04-27 19:07:19 +0100172 break;
173 }
Matthew Bentham0082c362020-02-03 12:05:18 +0000174 ARM_COMPUTE_ERROR("Data layout index not supported!");
Georgios Pinitascac13b12018-04-27 19:07:19 +0100175}
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100176
177std::vector<NodeIdxPair> get_driving_nodes(const INode &node)
178{
179 std::vector<NodeIdxPair> driving_nodes;
180
181 const Graph *g = node.graph();
182 ARM_COMPUTE_ERROR_ON(g == nullptr);
183
184 for(auto &output_edge_id : node.output_edges())
185 {
186 auto output_edge = g->edge(output_edge_id);
187 if(output_edge != nullptr)
188 {
189 ARM_COMPUTE_ERROR_ON(output_edge->consumer() == nullptr);
190 driving_nodes.push_back({ output_edge->consumer_id(), output_edge->consumer_idx() });
191 }
192 }
193
194 return driving_nodes;
195}
196
Gunes Bayir814bddf2021-09-01 16:20:54 +0100197std::vector<NodeIdxPair> get_driver_nodes(const INode &node)
198{
199 std::vector<NodeIdxPair> driver_nodes;
200
201 const Graph *g = node.graph();
202 ARM_COMPUTE_ERROR_ON(g == nullptr);
203
204 for(auto &input_edge_id : node.input_edges())
205 {
206 auto input_edge = g->edge(input_edge_id);
207 if(input_edge != nullptr)
208 {
209 ARM_COMPUTE_ERROR_ON(input_edge->producer() == nullptr);
210 driver_nodes.push_back({ input_edge->producer_id(), input_edge->producer_idx() });
211 }
212 }
213
214 return driver_nodes;
215}
216
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100217void configure_tensor(Tensor *tensor)
218{
219 if(tensor != nullptr && tensor->handle() == nullptr)
220 {
Anthony Barbier890ad1b2018-08-22 13:44:36 +0100221 Target target = tensor->desc().target;
222 backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(target);
223 std::unique_ptr<ITensorHandle> handle = backend.create_tensor(*tensor);
224 ARM_COMPUTE_ERROR_ON_MSG(!handle, "Couldn't create backend handle!");
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100225 tensor->set_handle(std::move(handle));
226 }
227}
Gunes Bayircc171f92021-09-13 13:38:29 +0100228
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100229} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100230} // namespace arm_compute