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/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" |
| 30 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 31 | #include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | #include "arm_compute/graph/detail/ExecutionHelpers.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 36 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | { |
| 38 | GraphManager::GraphManager() |
| 39 | : _workloads() |
| 40 | { |
| 41 | detail::default_initialize_backends(); |
| 42 | } |
| 43 | |
| 44 | void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target) |
| 45 | { |
| 46 | // Setup graph context if not done manually |
| 47 | setup_default_graph_context(ctx); |
| 48 | |
| 49 | // Check if graph has been registered |
Georgios Pinitas | ae70072 | 2018-04-27 18:10:01 +0100 | [diff] [blame] | 50 | if(_workloads.find(graph.id()) != std::end(_workloads)) |
| 51 | { |
| 52 | ARM_COMPUTE_ERROR("Graph is already registered!"); |
| 53 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 54 | |
| 55 | // Force target to all graph construct |
| 56 | // TODO (geopin01) : Support heterogeneous execution |
| 57 | Target forced_target = is_target_supported(target) ? target : get_default_target(); |
| 58 | force_target_to_graph(graph, forced_target); |
| 59 | |
| 60 | // Configure all tensors |
| 61 | detail::configure_all_tensors(graph); |
| 62 | |
| 63 | // Apply all mutating passes |
| 64 | pm.run_all(graph); |
| 65 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 66 | // Perform topological sort |
| 67 | // FIXME : Sort nodes and pass sorted indices in configure all nodes |
| 68 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 69 | // Validate all nodes |
| 70 | detail::validate_all_nodes(graph); |
| 71 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 72 | // Configure all nodes |
| 73 | auto workload = detail::configure_all_nodes(graph, ctx); |
| 74 | ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!"); |
| 75 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 76 | // Allocate const tensors and call accessors |
| 77 | detail::allocate_const_tensors(graph); |
| 78 | detail::call_all_const_node_accessors(graph); |
| 79 | |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 80 | // TODO (COMPMID-920) : Update prepare for NEON/GC |
| 81 | if(forced_target == Target::CL) |
| 82 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 83 | // Prepare graph |
| 84 | detail::prepare_all_tasks(workload); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 85 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 86 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 87 | // Setup tensor memory (Allocate all tensors or setup transition manager) |
| 88 | if(ctx.config().use_transition_memory_manager) |
| 89 | { |
| 90 | detail::configure_transition_manager(graph, ctx, workload); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 94 | detail::allocate_all_tensors(graph); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 95 | } |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 96 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 97 | // Finalize Graph context |
| 98 | ctx.finalize(); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 99 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 100 | // Register graph |
| 101 | _workloads.insert(std::make_pair(graph.id(), std::move(workload))); |
| 102 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id().get() << std::endl); |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 103 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame^] | 104 | // TODO (COMPMID-920) : Update prepare for NEON/GC |
| 105 | if(forced_target != Target::CL) |
| 106 | { |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 107 | // Make first run |
| 108 | execute_graph(graph); |
| 109 | |
| 110 | // Release all unused const tensors |
| 111 | detail::release_unused_tensors(graph); |
| 112 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void GraphManager::execute_graph(Graph &graph) |
| 116 | { |
| 117 | // Check if graph is finalized |
| 118 | auto it = _workloads.find(graph.id()); |
| 119 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 120 | |
| 121 | // Call input accessors |
| 122 | detail::call_all_input_node_accessors(it->second); |
| 123 | |
| 124 | // Run graph |
| 125 | detail::call_all_tasks(it->second); |
| 126 | |
| 127 | // Call output accessors |
| 128 | detail::call_all_output_node_accessors(it->second); |
| 129 | } |
| 130 | |
| 131 | void GraphManager::invalidate_graph(Graph &graph) |
| 132 | { |
| 133 | auto it = _workloads.find(graph.id()); |
| 134 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 135 | |
| 136 | _workloads.erase(it); |
| 137 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 138 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 139 | } // namespace arm_compute |