blob: 030fa2df59f469e1bfdb296e88a1c1e8dc08ec9c [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 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 */
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 Pinitasfbb80542018-03-27 17:15:49 +010081 if(target != Target::GC)
82 {
83 pm.append(support::cpp14::make_unique<InPlaceOperationMutator>());
84 pm.append(support::cpp14::make_unique<NodeFusionMutator>());
85 pm.append(support::cpp14::make_unique<SplitLayerSubTensorMutator>());
86 pm.append(support::cpp14::make_unique<DepthConcatSubTensorMutator>());
87 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000088
89 return pm;
90}
91
Georgios Pinitasd8734b52017-12-22 15:27:52 +000092void setup_default_graph_context(GraphContext &ctx)
93{
94 for(const auto &backend : backends::BackendRegistry::get().backends())
95 {
96 backend.second->setup_backend_context(ctx);
97 }
98}
Georgios Pinitascac13b12018-04-27 19:07:19 +010099
100size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
101{
102 ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
103 return descriptor.shape[get_dimension_idx(descriptor, data_layout_dimension)];
104}
105
106size_t get_dimension_idx(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
107{
108 ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
109
110 /* Return the index based on the data layout
111 * [N C H W]
112 * [3 2 1 0]
113 * [N H W C]
114 */
115 switch(data_layout_dimension)
116 {
117 case DataLayoutDimension::CHANNEL:
118 return (descriptor.layout == DataLayout::NCHW) ? 2 : 0;
119 break;
120 case DataLayoutDimension::HEIGHT:
121 return (descriptor.layout == DataLayout::NCHW) ? 1 : 2;
122 break;
123 case DataLayoutDimension::WIDTH:
124 return (descriptor.layout == DataLayout::NCHW) ? 0 : 1;
125 break;
126 case DataLayoutDimension::BATCHES:
127 return 3;
128 break;
129 default:
130 ARM_COMPUTE_ERROR("Data layout index not supported!");
131 break;
132 }
133}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100134} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000135} // namespace arm_compute