Compute Library
 23.11
GraphManager.cpp
Go to the documentation of this file.
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  */
25 
35 
36 #include "src/common/utils/Log.h"
37 
38 namespace arm_compute
39 {
40 namespace graph
41 {
43 {
44 }
45 
47 {
48  ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph configuration!");
49 
50  // Check if graph has been registered
51  if (_workloads.find(graph.id()) != std::end(_workloads))
52  {
53  ARM_COMPUTE_ERROR("Graph is already registered!");
54  }
55 
56  // Apply IR mutating passes
58 
59  // Force target to all graph construct
60  Target forced_target = target;
61 
62  // In case CLVK is selected, use the CL backend and
63  // update config
64  if (target == Target::CLVK)
65  {
66  forced_target = Target::CL;
67  GraphConfig config = ctx.config();
69 
70  ctx.set_config(config);
71  }
72 
73  if (!is_target_supported(target))
74  {
75  forced_target = get_default_target();
76  ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
77  }
78  force_target_to_graph(graph, forced_target);
79 
80  // Setup backend context
81  setup_requested_backend_context(ctx, forced_target);
82 
83  // Configure all tensors
85 
86  // Apply backend mutating passes
88 
89  // Perform topological sort
90  std::vector<NodeID> topological_sorted_nodes = dfs(graph);
91 
92  // Validate all nodes
94 
95  // Configure all nodes
96  auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes);
97  ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
98 
99  // Allocate const tensors and call accessors
102 
103  // Prepare graph
104  detail::prepare_all_tasks(workload);
105 
106  // Setup tensor memory (Allocate all tensors or setup transition manager)
108  {
109  detail::configure_transition_manager(graph, ctx, workload);
110  }
111  else
112  {
114  }
115 
116  // Finalize Graph context
117  ctx.finalize();
118 
119  // Register graph
120  _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
121  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl);
122 }
123 
125 {
126  ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph execution!");
127 
128  // Check if graph is finalized
129  auto it = _workloads.find(graph.id());
130  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
131 
132  while (true)
133  {
134  // Call input accessors
135  if (!detail::call_all_input_node_accessors(it->second))
136  {
137  return;
138  }
139 
140  // Run graph
141  detail::call_all_tasks(it->second);
142 
143  // Call output accessors
145  {
146  return;
147  }
148  }
149 }
150 
152 {
153  auto it = _workloads.find(graph.id());
154  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
155 
156  _workloads.erase(it);
157 }
158 } // namespace graph
159 } // namespace arm_compute
ARM_COMPUTE_LOG_GRAPH_INFO
#define ARM_COMPUTE_LOG_GRAPH_INFO(x)
Definition: Logger.h:54
arm_compute::graph::GraphManager::GraphManager
GraphManager()
Default Constructor.
Definition: GraphManager.cpp:42
ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL
#define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg)
Definition: Log.h:34
arm_compute::graph::GraphContext::config
const GraphConfig & config() const
Graph configuration accessor.
Definition: GraphContext.cpp:45
arm_compute::graph::Target::CL
@ CL
OpenCL capable target device.
arm_compute::graph::force_target_to_graph
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:53
arm_compute::graph::GraphManager::invalidate_graph
void invalidate_graph(Graph &graph)
Invalidates the graph execution workload.
Definition: GraphManager.cpp:151
arm_compute::graph::detail::allocate_const_tensors
void allocate_const_tensors(Graph &g)
Allocates const tensor of a given graph.
Definition: ExecutionHelpers.cpp:99
TopologicalSort.h
arm_compute::graph::detail::call_all_output_node_accessors
bool call_all_output_node_accessors(ExecutionWorkload &workload)
Call all output node accessors.
Definition: ExecutionHelpers.cpp:261
arm_compute::graph::setup_requested_backend_context
void setup_requested_backend_context(GraphContext &ctx, Target target)
Setups requested backend context if it exists, is supported and hasn't been initialized already.
Definition: Utils.cpp:131
arm_compute::graph::PassManager
Pass manager.
Definition: PassManager.h:43
arm_compute::graph::IGraphMutator::MutationType::IR
@ IR
ARM_COMPUTE_ERROR
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:354
arm_compute::graph::detail::prepare_all_tasks
void prepare_all_tasks(ExecutionWorkload &workload)
Prepares all tasks for execution.
Definition: ExecutionHelpers.cpp:222
ExecutionHelpers.h
GraphContext.h
arm_compute::graph::GraphManager::finalize_graph
void finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
Finalizes a given graph.
Definition: GraphManager.cpp:46
arm_compute::graph::Target::CLVK
@ CLVK
CLVK capable target device.
arm_compute::graph::Graph::id
GraphID id() const
Returns graph id.
Definition: Graph.cpp:173
arm_compute::graph::GraphContext::finalize
void finalize()
Finalizes memory managers in graph context.
Definition: GraphContext.cpp:101
arm_compute::graph::detail::configure_all_tensors
void configure_all_tensors(Graph &g)
Configures all nodes of a graph.
Definition: ExecutionHelpers.cpp:56
ARM_COMPUTE_LOG_GRAPH_VERBOSE
#define ARM_COMPUTE_LOG_GRAPH_VERBOSE(x)
Definition: Logger.h:50
arm_compute::graph::detail::call_all_input_node_accessors
bool call_all_input_node_accessors(ExecutionWorkload &workload)
Call all input node accessors.
Definition: ExecutionHelpers.cpp:210
arm_compute::graph::GraphContext::set_config
void set_config(const GraphConfig &config)
Sets graph configuration.
Definition: GraphContext.cpp:50
arm_compute::CLBackendType::Clvk
@ Clvk
CLVK backend.
arm_compute::graph::GraphConfig
Graph configuration structure.
Definition: Types.h:87
arm_compute::graph::Target
Target
Definition: Types.h:104
TypePrinter.h
PassManager.h
arm_compute::graph::detail::allocate_all_tensors
void allocate_all_tensors(Graph &g)
Allocates all tensors of a graph.
Definition: ExecutionHelpers.cpp:120
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
arm_compute::graph::PassManager::run_type
void run_type(Graph &g, IGraphMutator::MutationType type)
Runs a mutation passes of a specific type on a given graph.
Definition: PassManager.cpp:72
arm_compute::graph::dfs
std::vector< NodeID > dfs(Graph &g)
Depth first search traversal.
Definition: TopologicalSort.cpp:127
arm_compute::graph::is_target_supported
bool is_target_supported(Target target)
Checks if a specific target is supported.
Definition: Utils.cpp:34
arm_compute::graph::IGraphMutator::MutationType::Backend
@ Backend
IR specific mutation.
arm_compute::graph::GraphConfig::backend_type
CLBackendType backend_type
CL backend type to use.
Definition: Types.h:100
Logger.h
arm_compute::graph::GraphManager::execute_graph
void execute_graph(Graph &graph)
Executes a graph.
Definition: GraphManager.cpp:124
arm_compute::graph::detail::validate_all_nodes
void validate_all_nodes(Graph &g)
Validates all nodes.
Definition: ExecutionHelpers.cpp:39
arm_compute::graph::get_default_target
Target get_default_target()
Returns default target for execution.
Definition: Utils.cpp:40
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::graph::GraphConfig::use_transition_memory_manager
bool use_transition_memory_manager
Use a memory manager to manager transition buffer memory.
Definition: Types.h:91
arm_compute::mlgo::parser::end
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:283
Log.h
arm_compute::graph::detail::configure_all_nodes
ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx, const std::vector< NodeID > &node_order)
Configures all nodes of graph.
Definition: ExecutionHelpers.cpp:134
arm_compute::graph::detail::call_all_tasks
void call_all_tasks(ExecutionWorkload &workload)
Executes all tasks of a workload.
Definition: ExecutionHelpers.cpp:232
arm_compute::graph::Graph
Graph class.
Definition: Graph.h:52
Utils.h
arm_compute::graph::GraphContext
Graph context.
Definition: GraphContext.h:56
arm_compute::graph::detail::configure_transition_manager
void configure_transition_manager(Graph &g, GraphContext &ctx, ExecutionWorkload &workload)
Configures transition manager and execution workload.
Definition: CrossLayerMemoryManagerHelpers.cpp:237
Graph.h
GraphManager.h
CrossLayerMemoryManagerHelpers.h
arm_compute::graph::detail::call_all_const_node_accessors
void call_all_const_node_accessors(Graph &g)
Call all const node accessors.
Definition: ExecutionHelpers.cpp:194