Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | #include "arm_compute/graph/GraphContext.h" |
| 27 | #include "arm_compute/graph/backends/BackendRegistry.h" |
| 28 | #include "arm_compute/graph/mutators/GraphMutators.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | { |
| 34 | bool is_target_supported(Target target) |
| 35 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | return backends::BackendRegistry::get().contains(target) && backends::BackendRegistry::get().find_backend(target)->is_backend_supported(); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Target 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 Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 49 | if(is_target_supported(Target::GC)) |
| 50 | { |
| 51 | return Target::GC; |
| 52 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR("No backend exists!"); |
| 54 | } |
| 55 | |
| 56 | void 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 Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 77 | PassManager create_default_pass_manager(Target target) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 78 | { |
| 79 | PassManager pm; |
| 80 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 81 | // Passes that mutate graph IR |
| 82 | pm.append(support::cpp14::make_unique<GroupedConvolutionMutator>()); |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 83 | if(target != Target::GC) |
| 84 | { |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 85 | pm.append(support::cpp14::make_unique<NodeFusionMutator>()); |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 86 | pm.append(support::cpp14::make_unique<InPlaceOperationMutator>()); |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Passes that mutate backend information |
| 90 | if(target != Target::GC) |
| 91 | { |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 92 | pm.append(support::cpp14::make_unique<DepthConcatSubTensorMutator>()); |
Georgios Pinitas | b7a2023 | 2018-07-02 16:12:54 +0100 | [diff] [blame] | 93 | pm.append(support::cpp14::make_unique<SplitLayerSubTensorMutator>()); |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 94 | } |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 95 | pm.append(support::cpp14::make_unique<NodeExecutionMethodMutator>()); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 96 | |
| 97 | return pm; |
| 98 | } |
| 99 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 100 | void release_default_graph_context(GraphContext &ctx) |
| 101 | { |
| 102 | for(const auto &backend : backends::BackendRegistry::get().backends()) |
| 103 | { |
Anthony Barbier | 890ad1b | 2018-08-22 13:44:36 +0100 | [diff] [blame] | 104 | if(backend.second->is_backend_supported()) |
| 105 | { |
| 106 | backend.second->release_backend_context(ctx); |
| 107 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 111 | void setup_default_graph_context(GraphContext &ctx) |
| 112 | { |
| 113 | for(const auto &backend : backends::BackendRegistry::get().backends()) |
| 114 | { |
Anthony Barbier | 890ad1b | 2018-08-22 13:44:36 +0100 | [diff] [blame] | 115 | if(backend.second->is_backend_supported()) |
| 116 | { |
| 117 | backend.second->setup_backend_context(ctx); |
| 118 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 121 | |
| 122 | size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension) |
| 123 | { |
| 124 | ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!"); |
| 125 | return descriptor.shape[get_dimension_idx(descriptor, data_layout_dimension)]; |
| 126 | } |
| 127 | |
| 128 | size_t get_dimension_idx(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension) |
| 129 | { |
| 130 | ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!"); |
| 131 | |
| 132 | /* Return the index based on the data layout |
| 133 | * [N C H W] |
| 134 | * [3 2 1 0] |
| 135 | * [N H W C] |
| 136 | */ |
| 137 | switch(data_layout_dimension) |
| 138 | { |
| 139 | case DataLayoutDimension::CHANNEL: |
| 140 | return (descriptor.layout == DataLayout::NCHW) ? 2 : 0; |
| 141 | break; |
| 142 | case DataLayoutDimension::HEIGHT: |
| 143 | return (descriptor.layout == DataLayout::NCHW) ? 1 : 2; |
| 144 | break; |
| 145 | case DataLayoutDimension::WIDTH: |
| 146 | return (descriptor.layout == DataLayout::NCHW) ? 0 : 1; |
| 147 | break; |
| 148 | case DataLayoutDimension::BATCHES: |
| 149 | return 3; |
| 150 | break; |
| 151 | default: |
| 152 | ARM_COMPUTE_ERROR("Data layout index not supported!"); |
| 153 | break; |
| 154 | } |
| 155 | } |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 156 | |
| 157 | std::vector<NodeIdxPair> get_driving_nodes(const INode &node) |
| 158 | { |
| 159 | std::vector<NodeIdxPair> driving_nodes; |
| 160 | |
| 161 | const Graph *g = node.graph(); |
| 162 | ARM_COMPUTE_ERROR_ON(g == nullptr); |
| 163 | |
| 164 | for(auto &output_edge_id : node.output_edges()) |
| 165 | { |
| 166 | auto output_edge = g->edge(output_edge_id); |
| 167 | if(output_edge != nullptr) |
| 168 | { |
| 169 | ARM_COMPUTE_ERROR_ON(output_edge->consumer() == nullptr); |
| 170 | driving_nodes.push_back({ output_edge->consumer_id(), output_edge->consumer_idx() }); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return driving_nodes; |
| 175 | } |
| 176 | |
| 177 | void configure_tensor(Tensor *tensor) |
| 178 | { |
| 179 | if(tensor != nullptr && tensor->handle() == nullptr) |
| 180 | { |
Anthony Barbier | 890ad1b | 2018-08-22 13:44:36 +0100 | [diff] [blame] | 181 | Target target = tensor->desc().target; |
| 182 | backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(target); |
| 183 | std::unique_ptr<ITensorHandle> handle = backend.create_tensor(*tensor); |
| 184 | ARM_COMPUTE_ERROR_ON_MSG(!handle, "Couldn't create backend handle!"); |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 185 | tensor->set_handle(std::move(handle)); |
| 186 | } |
| 187 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 188 | } // namespace graph |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 189 | } // namespace arm_compute |