Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 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/GraphManager.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/Logger.h" |
| 29 | #include "arm_compute/graph/PassManager.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/graph/TypePrinter.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | 2b147ee | 2021-07-08 18:14:45 +0100 | [diff] [blame] | 32 | #include "arm_compute/graph/algorithms/TopologicalSort.h" |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 33 | #include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 34 | #include "arm_compute/graph/detail/ExecutionHelpers.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | |
Georgios Pinitas | 2b147ee | 2021-07-08 18:14:45 +0100 | [diff] [blame] | 36 | #include "src/common/utils/Log.h" |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 37 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 40 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 41 | { |
| 42 | GraphManager::GraphManager() |
| 43 | : _workloads() |
| 44 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target) |
| 48 | { |
Georgios Pinitas | 2b147ee | 2021-07-08 18:14:45 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph configuration!"); |
| 50 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 51 | // Check if graph has been registered |
Georgios Pinitas | ae70072 | 2018-04-27 18:10:01 +0100 | [diff] [blame] | 52 | if(_workloads.find(graph.id()) != std::end(_workloads)) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR("Graph is already registered!"); |
| 55 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 56 | |
Georgios Pinitas | f4261ad | 2019-12-02 11:58:19 +0000 | [diff] [blame] | 57 | // Apply IR mutating passes |
| 58 | pm.run_type(graph, IGraphMutator::MutationType::IR); |
| 59 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 60 | // Force target to all graph construct |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 61 | Target forced_target = target; |
Michalis Spyrou | 402740d | 2021-04-20 11:26:21 +0100 | [diff] [blame] | 62 | |
| 63 | // In case CLVK is selected, use the CL backend and |
| 64 | // update config |
| 65 | if(target == Target::CLVK) |
| 66 | { |
| 67 | forced_target = Target::CL; |
| 68 | GraphConfig config = ctx.config(); |
| 69 | config.backend_type = CLBackendType::Clvk; |
| 70 | |
| 71 | ctx.set_config(config); |
| 72 | } |
| 73 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 74 | if(!is_target_supported(target)) |
| 75 | { |
| 76 | forced_target = get_default_target(); |
| 77 | ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl); |
| 78 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 79 | force_target_to_graph(graph, forced_target); |
| 80 | |
Georgios Pinitas | 7097e3c | 2019-02-20 18:11:42 +0000 | [diff] [blame] | 81 | // Setup backend context |
Georgios Pinitas | 7097e3c | 2019-02-20 18:11:42 +0000 | [diff] [blame] | 82 | setup_requested_backend_context(ctx, forced_target); |
| 83 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 84 | // Configure all tensors |
| 85 | detail::configure_all_tensors(graph); |
| 86 | |
Georgios Pinitas | f4261ad | 2019-12-02 11:58:19 +0000 | [diff] [blame] | 87 | // Apply backend mutating passes |
| 88 | pm.run_type(graph, IGraphMutator::MutationType::Backend); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 89 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 90 | // Perform topological sort |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 91 | std::vector<NodeID> topological_sorted_nodes = dfs(graph); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 92 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 93 | // Validate all nodes |
| 94 | detail::validate_all_nodes(graph); |
| 95 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 96 | // Configure all nodes |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 97 | auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!"); |
| 99 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 100 | // Allocate const tensors and call accessors |
| 101 | detail::allocate_const_tensors(graph); |
| 102 | detail::call_all_const_node_accessors(graph); |
| 103 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 104 | // Prepare graph |
| 105 | detail::prepare_all_tasks(workload); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 106 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 107 | // Setup tensor memory (Allocate all tensors or setup transition manager) |
| 108 | if(ctx.config().use_transition_memory_manager) |
| 109 | { |
| 110 | detail::configure_transition_manager(graph, ctx, workload); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 114 | detail::allocate_all_tensors(graph); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 115 | } |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 116 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 117 | // Finalize Graph context |
| 118 | ctx.finalize(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 119 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 120 | // Register graph |
| 121 | _workloads.insert(std::make_pair(graph.id(), std::move(workload))); |
Georgios Pinitas | 54c92fd | 2018-11-05 12:16:15 +0000 | [diff] [blame] | 122 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void GraphManager::execute_graph(Graph &graph) |
| 126 | { |
Georgios Pinitas | 2b147ee | 2021-07-08 18:14:45 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph execution!"); |
| 128 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 129 | // Check if graph is finalized |
| 130 | auto it = _workloads.find(graph.id()); |
| 131 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 132 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 133 | while(true) |
| 134 | { |
| 135 | // Call input accessors |
| 136 | if(!detail::call_all_input_node_accessors(it->second)) |
| 137 | { |
| 138 | return; |
| 139 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 140 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 141 | // Run graph |
| 142 | detail::call_all_tasks(it->second); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 143 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 144 | // Call output accessors |
| 145 | if(!detail::call_all_output_node_accessors(it->second)) |
| 146 | { |
| 147 | return; |
| 148 | } |
| 149 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void GraphManager::invalidate_graph(Graph &graph) |
| 153 | { |
| 154 | auto it = _workloads.find(graph.id()); |
| 155 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 156 | |
| 157 | _workloads.erase(it); |
| 158 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 159 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 160 | } // namespace arm_compute |