blob: e357f10401aed2d445dcdc43ec4510c0e58f4d77 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michalis Spyrou402740d2021-04-20 11:26:21 +01002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/GraphManager.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#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 Pinitas12be7ab2018-07-03 12:06:23 +010030#include "arm_compute/graph/TypePrinter.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031#include "arm_compute/graph/Utils.h"
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010032#include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033#include "arm_compute/graph/detail/ExecutionHelpers.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034
Georgios Pinitas2a2db592018-08-15 12:14:46 +010035#include "arm_compute/graph/algorithms/TopologicalSort.h"
36
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037namespace arm_compute
38{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010039namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000040{
41GraphManager::GraphManager()
42 : _workloads()
43{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000044}
45
46void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
47{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000048 // Check if graph has been registered
Georgios Pinitasae700722018-04-27 18:10:01 +010049 if(_workloads.find(graph.id()) != std::end(_workloads))
50 {
51 ARM_COMPUTE_ERROR("Graph is already registered!");
52 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000053
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000054 // Apply IR mutating passes
55 pm.run_type(graph, IGraphMutator::MutationType::IR);
56
Georgios Pinitasd8734b52017-12-22 15:27:52 +000057 // Force target to all graph construct
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058 Target forced_target = target;
Michalis Spyrou402740d2021-04-20 11:26:21 +010059
60 // In case CLVK is selected, use the CL backend and
61 // update config
62 if(target == Target::CLVK)
63 {
64 forced_target = Target::CL;
65 GraphConfig config = ctx.config();
66 config.backend_type = CLBackendType::Clvk;
67
68 ctx.set_config(config);
69 }
70
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010071 if(!is_target_supported(target))
72 {
73 forced_target = get_default_target();
74 ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
75 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000076 force_target_to_graph(graph, forced_target);
77
Georgios Pinitas7097e3c2019-02-20 18:11:42 +000078 // Setup backend context
Georgios Pinitas7097e3c2019-02-20 18:11:42 +000079 setup_requested_backend_context(ctx, forced_target);
80
Georgios Pinitasd8734b52017-12-22 15:27:52 +000081 // Configure all tensors
82 detail::configure_all_tensors(graph);
83
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000084 // Apply backend mutating passes
85 pm.run_type(graph, IGraphMutator::MutationType::Backend);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000086
Georgios Pinitasd8734b52017-12-22 15:27:52 +000087 // Perform topological sort
Georgios Pinitas2a2db592018-08-15 12:14:46 +010088 std::vector<NodeID> topological_sorted_nodes = dfs(graph);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000089
Georgios Pinitas28705162018-03-21 20:10:53 +000090 // Validate all nodes
91 detail::validate_all_nodes(graph);
92
Georgios Pinitasd8734b52017-12-22 15:27:52 +000093 // Configure all nodes
Georgios Pinitas2a2db592018-08-15 12:14:46 +010094 auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000095 ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
96
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010097 // Allocate const tensors and call accessors
98 detail::allocate_const_tensors(graph);
99 detail::call_all_const_node_accessors(graph);
100
Georgios Pinitas72219332018-06-05 14:56:06 +0100101 // Prepare graph
102 detail::prepare_all_tasks(workload);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000103
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100104 // Setup tensor memory (Allocate all tensors or setup transition manager)
105 if(ctx.config().use_transition_memory_manager)
106 {
107 detail::configure_transition_manager(graph, ctx, workload);
Georgios Pinitase0437672018-05-02 14:07:55 +0100108 }
109 else
110 {
Georgios Pinitase0437672018-05-02 14:07:55 +0100111 detail::allocate_all_tensors(graph);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100112 }
Georgios Pinitas1562be32018-03-08 19:09:19 +0000113
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100114 // Finalize Graph context
115 ctx.finalize();
Georgios Pinitase0437672018-05-02 14:07:55 +0100116
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100117 // Register graph
118 _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
Georgios Pinitas54c92fd2018-11-05 12:16:15 +0000119 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000120}
121
122void GraphManager::execute_graph(Graph &graph)
123{
124 // Check if graph is finalized
125 auto it = _workloads.find(graph.id());
126 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
127
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100128 while(true)
129 {
130 // Call input accessors
131 if(!detail::call_all_input_node_accessors(it->second))
132 {
133 return;
134 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000135
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100136 // Run graph
137 detail::call_all_tasks(it->second);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000138
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100139 // Call output accessors
140 if(!detail::call_all_output_node_accessors(it->second))
141 {
142 return;
143 }
144 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000145}
146
147void GraphManager::invalidate_graph(Graph &graph)
148{
149 auto it = _workloads.find(graph.id());
150 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
151
152 _workloads.erase(it);
153}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100154} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000155} // namespace arm_compute