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