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/detail/ExecutionHelpers.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/Graph.h" |
| 27 | #include "arm_compute/graph/GraphContext.h" |
| 28 | #include "arm_compute/graph/GraphManager.h" |
| 29 | #include "arm_compute/graph/Tensor.h" |
| 30 | #include "arm_compute/graph/backends/BackendRegistry.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 34 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | { |
| 36 | namespace detail |
| 37 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 38 | void validate_all_nodes(Graph &g) |
| 39 | { |
| 40 | auto &nodes = g.nodes(); |
| 41 | |
| 42 | // Create tasks |
| 43 | for(auto &node : nodes) |
| 44 | { |
| 45 | if(node != nullptr) |
| 46 | { |
| 47 | Target assigned_target = node->assigned_target(); |
| 48 | auto backend = backends::BackendRegistry::get().find_backend(assigned_target); |
| 49 | ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!"); |
| 50 | Status status = backend->validate_node(*node); |
| 51 | ARM_COMPUTE_ERROR_ON_MSG(!bool(status), status.error_description().c_str()); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 56 | void configure_all_tensors(Graph &g) |
| 57 | { |
| 58 | auto &tensors = g.tensors(); |
| 59 | |
| 60 | for(auto &tensor : tensors) |
| 61 | { |
| 62 | if(tensor) |
| 63 | { |
| 64 | Target target = tensor->desc().target; |
| 65 | auto backend = backends::BackendRegistry::get().find_backend(target); |
| 66 | ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!"); |
| 67 | auto handle = backend->create_tensor(*tensor); |
| 68 | ARM_COMPUTE_ERROR_ON_MSG(!backend, "Couldn't create backend handle!"); |
| 69 | tensor->set_handle(std::move(handle)); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 74 | void allocate_all_input_tensors(INode &node) |
| 75 | { |
| 76 | for(unsigned int i = 0; i < node.num_inputs(); ++i) |
| 77 | { |
| 78 | Tensor *tensor = node.input(i); |
| 79 | if(tensor != nullptr && !tensor->bound_edges().empty()) |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR_ON_MSG(!tensor->handle(), "Tensor handle is not configured!"); |
| 82 | tensor->handle()->allocate(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void allocate_all_output_tensors(INode &node) |
| 88 | { |
| 89 | for(unsigned int i = 0; i < node.num_outputs(); ++i) |
| 90 | { |
| 91 | Tensor *tensor = node.output(i); |
| 92 | if(tensor != nullptr && !tensor->bound_edges().empty()) |
| 93 | { |
| 94 | ARM_COMPUTE_ERROR_ON_MSG(!tensor->handle(), "Tensor handle is not configured!"); |
| 95 | tensor->handle()->allocate(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void allocate_const_tensors(Graph &g) |
| 101 | { |
| 102 | for(auto &node : g.nodes()) |
| 103 | { |
| 104 | if(node != nullptr) |
| 105 | { |
| 106 | switch(node->type()) |
| 107 | { |
| 108 | case NodeType::Const: |
| 109 | case NodeType::Input: |
| 110 | allocate_all_output_tensors(*node); |
| 111 | break; |
| 112 | case NodeType::Output: |
| 113 | allocate_all_input_tensors(*node); |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 121 | void allocate_all_tensors(Graph &g) |
| 122 | { |
| 123 | auto &tensors = g.tensors(); |
| 124 | |
| 125 | for(auto &tensor : tensors) |
| 126 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 127 | if(tensor && !tensor->bound_edges().empty() && tensor->handle() != nullptr && tensor->handle()->tensor().info()->is_resizable() && tensor->handle()->tensor().is_used()) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 128 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 129 | tensor->handle()->allocate(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 134 | ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx) |
| 135 | { |
| 136 | ExecutionWorkload workload; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 137 | workload.graph = &g; |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 138 | workload.ctx = &ctx; |
| 139 | |
| 140 | auto &nodes = g.nodes(); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 141 | |
| 142 | // Create tasks |
| 143 | for(auto &node : nodes) |
| 144 | { |
| 145 | if(node != nullptr) |
| 146 | { |
| 147 | Target assigned_target = node->assigned_target(); |
| 148 | auto backend = backends::BackendRegistry::get().find_backend(assigned_target); |
| 149 | ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!"); |
| 150 | auto func = backend->configure_node(*node, ctx); |
| 151 | if(func != nullptr) |
| 152 | { |
| 153 | ExecutionTask task; |
| 154 | task.task = std::move(func); |
| 155 | task.node = node.get(); |
| 156 | workload.tasks.push_back(std::move(task)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Add inputs and outputs |
| 162 | for(auto &node : nodes) |
| 163 | { |
| 164 | if(node != nullptr && node->type() == NodeType::Input) |
| 165 | { |
| 166 | workload.inputs.push_back(node->output(0)); |
| 167 | } |
| 168 | |
| 169 | if(node != nullptr && node->type() == NodeType::Output) |
| 170 | { |
| 171 | workload.outputs.push_back(node->input(0)); |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return workload; |
| 177 | } |
| 178 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 179 | void release_unused_tensors(Graph &g) |
| 180 | { |
| 181 | for(auto &tensor : g.tensors()) |
| 182 | { |
| 183 | if(tensor != nullptr && tensor->handle() != nullptr) |
| 184 | { |
| 185 | tensor->handle()->release_if_unused(); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 190 | void call_tensor_accessor(Tensor *tensor) |
| 191 | { |
| 192 | ARM_COMPUTE_ERROR_ON(!tensor); |
| 193 | tensor->call_accessor(); |
| 194 | } |
| 195 | |
| 196 | void call_all_const_node_accessors(Graph &g) |
| 197 | { |
| 198 | auto &nodes = g.nodes(); |
| 199 | |
| 200 | for(auto &node : nodes) |
| 201 | { |
| 202 | if(node != nullptr && node->type() == NodeType::Const) |
| 203 | { |
| 204 | call_tensor_accessor(node->output(0)); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 209 | bool call_all_input_node_accessors(ExecutionWorkload &workload) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 210 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 211 | return !std::any_of(std::begin(workload.inputs), std::end(workload.inputs), [](Tensor * input_tensor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 212 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 213 | return (input_tensor == nullptr) || !input_tensor->call_accessor(); |
| 214 | }); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 217 | void prepare_all_tasks(ExecutionWorkload &workload) |
| 218 | { |
| 219 | ARM_COMPUTE_ERROR_ON(workload.graph == nullptr); |
| 220 | for(auto &task : workload.tasks) |
| 221 | { |
| 222 | task.prepare(); |
| 223 | release_unused_tensors(*workload.graph); |
| 224 | } |
| 225 | } |
| 226 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 227 | void call_all_tasks(ExecutionWorkload &workload) |
| 228 | { |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 229 | ARM_COMPUTE_ERROR_ON(workload.ctx == nullptr); |
| 230 | |
| 231 | // Acquire memory for the transition buffers |
| 232 | for(auto &mm_ctx : workload.ctx->memory_managers()) |
| 233 | { |
| 234 | if(mm_ctx.second.cross_group != nullptr) |
| 235 | { |
| 236 | mm_ctx.second.cross_group->acquire(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Execute tasks |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 241 | for(auto &task : workload.tasks) |
| 242 | { |
| 243 | task(); |
| 244 | } |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 245 | |
| 246 | // Release memory for the transition buffers |
| 247 | for(auto &mm_ctx : workload.ctx->memory_managers()) |
| 248 | { |
| 249 | if(mm_ctx.second.cross_group != nullptr) |
| 250 | { |
| 251 | mm_ctx.second.cross_group->release(); |
| 252 | } |
| 253 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 256 | bool call_all_output_node_accessors(ExecutionWorkload &workload) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 257 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 258 | bool is_valid = true; |
| 259 | std::for_each(std::begin(workload.outputs), std::end(workload.outputs), [&](Tensor * output_tensor) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 260 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame^] | 261 | is_valid = is_valid && (output_tensor != nullptr) && output_tensor->call_accessor(); |
| 262 | }); |
| 263 | |
| 264 | return is_valid; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 265 | } |
| 266 | } // namespace detail |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 267 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 268 | } // namespace arm_compute |