xref: /aosp_15_r20/external/ComputeLibrary/src/graph/GraphManager.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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/GraphManager.h"
25 
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/TypePrinter.h"
31 #include "arm_compute/graph/Utils.h"
32 #include "arm_compute/graph/algorithms/TopologicalSort.h"
33 #include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h"
34 #include "arm_compute/graph/detail/ExecutionHelpers.h"
35 
36 #include "src/common/utils/Log.h"
37 
38 namespace arm_compute
39 {
40 namespace graph
41 {
GraphManager()42 GraphManager::GraphManager()
43     : _workloads()
44 {
45 }
46 
finalize_graph(Graph & graph,GraphContext & ctx,PassManager & pm,Target target)47 void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
48 {
49     ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph configuration!");
50 
51     // Check if graph has been registered
52     if(_workloads.find(graph.id()) != std::end(_workloads))
53     {
54         ARM_COMPUTE_ERROR("Graph is already registered!");
55     }
56 
57     // Apply IR mutating passes
58     pm.run_type(graph, IGraphMutator::MutationType::IR);
59 
60     // Force target to all graph construct
61     Target forced_target = target;
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 
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     }
79     force_target_to_graph(graph, forced_target);
80 
81     // Setup backend context
82     setup_requested_backend_context(ctx, forced_target);
83 
84     // Configure all tensors
85     detail::configure_all_tensors(graph);
86 
87     // Apply backend mutating passes
88     pm.run_type(graph, IGraphMutator::MutationType::Backend);
89 
90     // Perform topological sort
91     std::vector<NodeID> topological_sorted_nodes = dfs(graph);
92 
93     // Validate all nodes
94     detail::validate_all_nodes(graph);
95 
96     // Configure all nodes
97     auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes);
98     ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
99 
100     // Allocate const tensors and call accessors
101     detail::allocate_const_tensors(graph);
102     detail::call_all_const_node_accessors(graph);
103 
104     // Prepare graph
105     detail::prepare_all_tasks(workload);
106 
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);
111     }
112     else
113     {
114         detail::allocate_all_tensors(graph);
115     }
116 
117     // Finalize Graph context
118     ctx.finalize();
119 
120     // Register graph
121     _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
122     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl);
123 }
124 
execute_graph(Graph & graph)125 void GraphManager::execute_graph(Graph &graph)
126 {
127     ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph execution!");
128 
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 
133     while(true)
134     {
135         // Call input accessors
136         if(!detail::call_all_input_node_accessors(it->second))
137         {
138             return;
139         }
140 
141         // Run graph
142         detail::call_all_tasks(it->second);
143 
144         // Call output accessors
145         if(!detail::call_all_output_node_accessors(it->second))
146         {
147             return;
148         }
149     }
150 }
151 
invalidate_graph(Graph & graph)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 }
159 } // namespace graph
160 } // namespace arm_compute