Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [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 | */ |
| 24 | #include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h" |
| 25 | |
| 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/INode.h" |
| 30 | #include "arm_compute/graph/Tensor.h" |
| 31 | #include "arm_compute/graph/Types.h" |
| 32 | #include "arm_compute/graph/backends/BackendRegistry.h" |
| 33 | |
| 34 | #include "arm_compute/core/ITensor.h" |
| 35 | #include "arm_compute/core/utils/misc/Cast.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | #include <map> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace graph |
| 43 | { |
| 44 | namespace detail |
| 45 | { |
| 46 | namespace |
| 47 | { |
| 48 | using HandleCountPair = std::pair<ITensorHandle *, unsigned int>; |
| 49 | using HandleCounter = std::map<HandleCountPair::first_type, HandleCountPair::second_type>; |
| 50 | using TargetHandleCounter = std::map<Target, HandleCounter>; |
| 51 | |
| 52 | /** Holds managed IO tensor handles if a task */ |
| 53 | struct TaskHandles |
| 54 | { |
| 55 | std::vector<std::pair<ITensorHandle *, IMemoryGroup *>> input_handles = {}; /**< Input handles to a task */ |
| 56 | std::vector<std::pair<ITensorHandle *, IMemoryGroup *>> output_handles = {}; /**< Output handles of a task */ |
| 57 | }; |
| 58 | |
| 59 | /** Returns memory group depending on handle backend type |
| 60 | * |
| 61 | * @param[in] ctx Graph context |
| 62 | * @param[in] handle Tensor handle |
| 63 | * |
| 64 | * @return Memory groupb |
| 65 | */ |
| 66 | IMemoryGroup *get_memory_group_from_handle(GraphContext &ctx, ITensorHandle *handle) |
| 67 | { |
| 68 | ARM_COMPUTE_ERROR_ON(handle == nullptr); |
| 69 | return ctx.memory_management_ctx(handle->target())->cross_group.get(); |
| 70 | } |
| 71 | |
| 72 | /** Get handles of const tensors of graph |
| 73 | * |
| 74 | * @param[in] g Graph |
| 75 | * |
| 76 | * @return Handles of const tensors of graph |
| 77 | */ |
| 78 | std::set<ITensorHandle *> get_const_handles(const Graph &g) |
| 79 | { |
| 80 | std::set<NodeType> const_node_types = { NodeType::Input, NodeType::Output, NodeType::Const }; |
| 81 | |
| 82 | std::set<ITensorHandle *> const_tensors; |
| 83 | |
| 84 | auto &nodes = g.nodes(); |
| 85 | for(auto &node : nodes) |
| 86 | { |
| 87 | // If its a const node: |
| 88 | if(node != nullptr && const_node_types.find(node->type()) != std::end(const_node_types)) |
| 89 | { |
| 90 | // TODO (geopin01) : Create IO iterator wrappers |
| 91 | // Add all its inputs / outputs to the list of constant handles |
| 92 | for(unsigned int i = 0; i < node->num_inputs(); ++i) |
| 93 | { |
| 94 | if(node->input(i) != nullptr) |
| 95 | { |
| 96 | const_tensors.insert(node->input(i)->handle()->parent_handle()); |
| 97 | } |
| 98 | } |
| 99 | for(unsigned int i = 0; i < node->num_outputs(); ++i) |
| 100 | { |
| 101 | if(node->output(i) != nullptr) |
| 102 | { |
| 103 | const_tensors.insert(node->output(i)->handle()->parent_handle()); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return const_tensors; |
| 110 | } |
| 111 | |
| 112 | /** Builds a list of all the transition handles (Handles that are used to link two nodes) |
| 113 | * |
| 114 | * @param[in] ctx Graph context |
| 115 | * @param[in] task Workload task |
| 116 | * @param[in] const_tensors Constant tensors |
| 117 | * |
| 118 | * @return List of transition handles |
| 119 | */ |
| 120 | TaskHandles get_transition_handles(GraphContext &ctx, |
| 121 | ExecutionTask &task, |
| 122 | const std::set<ITensorHandle *> &const_tensors) |
| 123 | { |
| 124 | ARM_COMPUTE_ERROR_ON(task.node == nullptr || task.task == nullptr); |
| 125 | INode &node = *task.node; |
| 126 | |
| 127 | TaskHandles transition_handles; |
| 128 | |
| 129 | // Add input handles |
| 130 | for(unsigned int i = 0; i < node.input_edges().size(); ++i) |
| 131 | { |
| 132 | Edge *input_edge = node.input_edge(i); |
| 133 | // If this input is the output of another node |
| 134 | if(input_edge != nullptr && input_edge->tensor() != nullptr && const_tensors.find(input_edge->tensor()->handle()->parent_handle()) == std::end(const_tensors)) |
| 135 | { |
| 136 | // Then add it to the list of transition buffers |
| 137 | ITensorHandle *tensor_handle = input_edge->tensor()->handle()->parent_handle(); |
| 138 | IMemoryGroup *mm_group = get_memory_group_from_handle(ctx, tensor_handle); |
| 139 | transition_handles.input_handles.push_back(std::make_pair(tensor_handle, mm_group)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Add output handles |
| 144 | for(unsigned int i = 0; i < node.num_outputs(); ++i) |
| 145 | { |
| 146 | Tensor *output_tensor = node.output(i); |
| 147 | // If this output is used as an input for another node |
| 148 | if(output_tensor != nullptr && const_tensors.find(output_tensor->handle()->parent_handle()) == std::end(const_tensors)) |
| 149 | { |
| 150 | ITensorHandle *tensor_handle = output_tensor->handle()->parent_handle(); |
| 151 | IMemoryGroup *mm_group = get_memory_group_from_handle(ctx, tensor_handle); |
| 152 | transition_handles.output_handles.push_back(std::make_pair(tensor_handle, mm_group)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return transition_handles; |
| 157 | } |
| 158 | |
| 159 | /** Counts handles refcount for each input handle of each target |
| 160 | * |
| 161 | * @param[in] task Execution task containing the managed handles |
| 162 | * @param[in,out] handle_counter Data structure that keeps the handles reference count |
| 163 | */ |
| 164 | void count_input_handles_per_target(const TaskHandles &task_handles, TargetHandleCounter &handle_counter) |
| 165 | { |
| 166 | for(const auto &handle : task_handles.input_handles) |
| 167 | { |
| 168 | ITensorHandle *key = handle.first; |
| 169 | HandleCounter &target_counter = handle_counter[key->target()]; |
| 170 | if(target_counter.find(key) == std::end(target_counter)) |
| 171 | { |
| 172 | target_counter.emplace(std::make_pair(key, 1)); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | ++target_counter[key]; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** Calculates the lifetime of each tensor handle |
| 182 | * |
| 183 | * @param[in, out] tasks_handles Tensor handles for each task |
| 184 | * @param[in] hc Data structure that keeps the handles reference count |
| 185 | */ |
| 186 | void configure_handle_lifetime(std::vector<TaskHandles> &tasks_handles, const HandleCounter &hc) |
| 187 | { |
| 188 | // Identify max number of tensors in flight |
| 189 | HandleCounter tensors_in_flight; |
| 190 | |
| 191 | // Acquires the given handles and sets them as in flight if they aren't already |
| 192 | auto acquire = [&](std::vector<std::pair<ITensorHandle *, IMemoryGroup *>> &handles) |
| 193 | { |
| 194 | for(auto &handle : handles) |
| 195 | { |
| 196 | ITensorHandle *parent_handle = handle.first; |
| 197 | ARM_COMPUTE_ERROR_ON(parent_handle == nullptr); |
| 198 | // If the tensor is not already in flight: |
| 199 | if(tensors_in_flight.find(parent_handle) == std::end(tensors_in_flight)) |
| 200 | { |
| 201 | ARM_COMPUTE_ERROR_ON(hc.find(parent_handle) == std::end(hc)); |
| 202 | // Then add it to the list of in flight tensors |
| 203 | tensors_in_flight.insert(std::make_pair(parent_handle, hc.at(parent_handle))); |
| 204 | // Start of allocation's lifetime |
| 205 | parent_handle->manage(handle.second); |
| 206 | } |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | for(auto &task_handle : tasks_handles) |
| 211 | { |
| 212 | // Marking all the input and output tensors of the task as in flight |
| 213 | acquire(task_handle.input_handles); |
| 214 | acquire(task_handle.output_handles); |
| 215 | |
| 216 | // Releasing the input tensors |
| 217 | for(auto &input_handle : task_handle.input_handles) |
| 218 | { |
| 219 | ITensorHandle *ihandle = input_handle.first; |
| 220 | ARM_COMPUTE_ERROR_ON(ihandle == nullptr); |
| 221 | ARM_COMPUTE_ERROR_ON(tensors_in_flight.find(ihandle) == std::end(tensors_in_flight)); |
| 222 | --tensors_in_flight[ihandle]; |
| 223 | if(tensors_in_flight[ihandle] <= 0) |
| 224 | { |
| 225 | // Remove tensor for tensors in flight |
| 226 | tensors_in_flight.erase(ihandle); |
| 227 | // End of allocation's lifetime |
| 228 | ihandle->allocate(); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } // namespace |
| 234 | |
| 235 | void configure_transition_manager(Graph &g, GraphContext &ctx, ExecutionWorkload &workload) |
| 236 | { |
| 237 | // Get const tensors (un-managed) |
| 238 | std::set<ITensorHandle *> const_tensors = get_const_handles(g); |
| 239 | |
| 240 | std::vector<TaskHandles> tasks_handles; |
| 241 | TargetHandleCounter target_handle_count; |
| 242 | |
| 243 | // Count handles |
| 244 | for(auto &task : workload.tasks) |
| 245 | { |
| 246 | // Populates IO handles |
| 247 | tasks_handles.push_back(get_transition_handles(ctx, task, const_tensors)); |
| 248 | |
| 249 | // Count handles |
| 250 | count_input_handles_per_target(tasks_handles.back(), target_handle_count); |
| 251 | } |
| 252 | |
| 253 | // Setup memory managers |
| 254 | for(auto &hc : target_handle_count) |
| 255 | { |
| 256 | MemoryManagerContext *mm_ctx = ctx.memory_management_ctx(hc.first); |
| 257 | if(mm_ctx != nullptr) |
| 258 | { |
| 259 | if(mm_ctx->cross_mm != nullptr && mm_ctx->cross_group != nullptr) |
| 260 | { |
| 261 | // Manage and allocate tensors |
| 262 | configure_handle_lifetime(tasks_handles, hc.second); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } // namespace detail |
| 268 | } // namespace graph |
| 269 | } // namespace arm_compute |