blob: 669913ce30a4643196bf6e0976fcaa00a251ef64 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
2 * Copyright (c) 2022 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 "GpuKernelComponentGraph.h"
25
26#include "arm_compute/dynamic_fusion/sketch/MemoryDescriptor.h"
27
28namespace arm_compute
29{
30namespace experimental
31{
32namespace dynamic_fusion
33{
34namespace
35{
36/** Automatically create memory descriptors for all tensors in the graph
37 *
38 * @param[in] tensors @ref ITensorInfo map
39 * @param[in] graph @ref DependencyGraph of which the @p tensors are a part
40 *
41 * @return MemoryDescriptorMap An assignment map of @ref MemoryDescriptors for each ITensorInfo in the graph
42 */
43MemoryDescriptorMap assign_memory_descriptors(const std::map<ITensorInfo::Id, const ITensorInfo *> tensors, const DependencyGraph &graph)
44{
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000045 const auto all_tensors = graph.all_tensors();
46 const auto src_tensors = graph.global_src_tensors();
47 const auto dst_tensors = graph.global_dst_tensors();
48 const auto interm_tensors = graph.intermediate_tensors();
49
SiCong Lif44bbc52022-08-29 18:25:51 +010050 MemoryDescriptorMap mem_map{};
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000051 for(auto t_id : all_tensors)
SiCong Lif44bbc52022-08-29 18:25:51 +010052 {
53 const auto &tensor = tensors.at(t_id);
54 // Only global src and dst tensors to the entire component graph are "User" tensors, which are user-specified memories
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000055 if(is_in(t_id, src_tensors) || is_in(t_id, dst_tensors))
SiCong Lif44bbc52022-08-29 18:25:51 +010056 {
57 mem_map[t_id] = MemoryDescriptor{ MemoryType::User };
58 }
Viet-Hoa Dob84e2532022-12-13 13:09:10 +000059 else if(is_in(t_id, interm_tensors))
60 {
61 mem_map[t_id] = MemoryDescriptor { MemoryType::NoAlloc };
62 }
SiCong Lif44bbc52022-08-29 18:25:51 +010063 else
64 {
65 AuxMemoryInfo aux_mem_info{ tensor->total_size() };
66 mem_map[t_id] = MemoryDescriptor{ MemoryType::Auxiliary, aux_mem_info };
67 }
68 }
69 return mem_map;
70}
71
72} // namespace
73
74std::vector<DependencyGraph::TensorId> GpuKernelComponentGraph::get_tensor_ids(const std::vector<const ITensorInfo *> tensors)
75{
76 std::vector<DependencyGraph::TensorId> tensor_ids{};
77 std::transform(
78 std::begin(tensors), std::end(tensors),
79 std::back_inserter(tensor_ids),
80 [](const auto & t)
81 {
82 return t->id();
83 });
84 return tensor_ids;
85}
86
87GpuKernelComponentGraph::GpuKernelComponentGraph(GpuComponentServices *services)
88 : _services{ services }, _components{}, _tensors{}, _dependency_graph{}
89{
90}
91
92GpuKernelComponentStream GpuKernelComponentGraph::fuse() const
93{
94 // Obtain memory descriptor map
95 const auto mem_map = assign_memory_descriptors(_tensors, _dependency_graph);
SiCong Lif44bbc52022-08-29 18:25:51 +010096
97 GpuKernelComponentStream stream{ _services, mem_map };
Viet-Hoa Do04f46202022-12-14 14:49:56 +000098 const auto op_seq = _dependency_graph.build_operators_sequence();
SiCong Lif44bbc52022-08-29 18:25:51 +010099
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000100 stream.new_component_group();
101 for(auto op : op_seq)
SiCong Lif44bbc52022-08-29 18:25:51 +0100102 {
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000103 const auto component = _components.at(op.op).get();
104 const auto success = stream.add_component(component);
105 ARM_COMPUTE_ERROR_ON(!success);
106 ARM_COMPUTE_UNUSED(success);
SiCong Lif44bbc52022-08-29 18:25:51 +0100107 }
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000108
SiCong Lif44bbc52022-08-29 18:25:51 +0100109 return stream;
110}
111} // namespace dynamic_fusion
112} // namespace experimental
113} // namespace arm_compute