SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace experimental |
| 31 | { |
| 32 | namespace dynamic_fusion |
| 33 | { |
| 34 | namespace |
| 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 | */ |
| 43 | MemoryDescriptorMap assign_memory_descriptors(const std::map<ITensorInfo::Id, const ITensorInfo *> tensors, const DependencyGraph &graph) |
| 44 | { |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 45 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 50 | MemoryDescriptorMap mem_map{}; |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 51 | for(auto t_id : all_tensors) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 52 | { |
| 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 Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 55 | if(is_in(t_id, src_tensors) || is_in(t_id, dst_tensors)) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 56 | { |
| 57 | mem_map[t_id] = MemoryDescriptor{ MemoryType::User }; |
| 58 | } |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 59 | else if(is_in(t_id, interm_tensors)) |
| 60 | { |
| 61 | mem_map[t_id] = MemoryDescriptor { MemoryType::NoAlloc }; |
| 62 | } |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 63 | 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 | |
| 74 | std::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 | |
| 87 | GpuKernelComponentGraph::GpuKernelComponentGraph(GpuComponentServices *services) |
| 88 | : _services{ services }, _components{}, _tensors{}, _dependency_graph{} |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | GpuKernelComponentStream GpuKernelComponentGraph::fuse() const |
| 93 | { |
| 94 | // Obtain memory descriptor map |
| 95 | const auto mem_map = assign_memory_descriptors(_tensors, _dependency_graph); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 96 | |
| 97 | GpuKernelComponentStream stream{ _services, mem_map }; |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 98 | const auto op_seq = _dependency_graph.build_operators_sequence(); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 99 | |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 100 | stream.new_component_group(); |
| 101 | for(auto op : op_seq) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 102 | { |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 103 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 107 | } |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 108 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 109 | return stream; |
| 110 | } |
| 111 | } // namespace dynamic_fusion |
| 112 | } // namespace experimental |
| 113 | } // namespace arm_compute |