blob: e4f498b130025a45bf874e08db533bdf9739beec [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#ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGRAPH
25#define SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGRAPH
26
27#include "src/dynamic_fusion/sketch/ArgumentPack.h"
28#include "src/dynamic_fusion/sketch/gpu/GpuComponentServices.h"
29#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentStream.h"
30#include "src/dynamic_fusion/sketch/utils/DependencyGraph.h"
31
32#include <vector>
33
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40class IGpuKernelComponent;
41
42/** A multi-input (tensors), multi-output (tensors) acyclic directed graph of gpu kernel components
43 * Its main purposes are:
44 * - Perform "graph-level" optimizations like fusion of kernel components (not the fusion of operators)
45 * - Automatically assign memory descriptions @ref MemoryDescriptor of all tensors based on graph topology
46 */
47class GpuKernelComponentGraph
48{
49public:
50 /** Constructor
51 *
52 * @param[in] services @ref GpuComponentServices to be used by the graph
53 */
54 GpuKernelComponentGraph(GpuComponentServices *services);
55 /** Prevent instances of this class from being copy constructed */
56 GpuKernelComponentGraph(const GpuKernelComponentGraph &graph) = delete;
57 /** Prevent instances of this class from being copied */
58 GpuKernelComponentGraph &operator=(const GpuKernelComponentGraph &graph) = delete;
59 /** Allow instances of this class to be move constructed */
60 GpuKernelComponentGraph(GpuKernelComponentGraph &&graph) = default;
61 /** Allow instances of this class to be moved */
62 GpuKernelComponentGraph &operator=(GpuKernelComponentGraph &&graph) = default;
63 /** Create a new component and add it to the component graph
64 * Component id is automatically allocated
65 *
66 * @tparam T Component type
67 * @tparam Args Component argument types
68 *
69 * @param[in] args Component arguments except for component id, which is auto-allocated
70 */
71 template <typename T, typename... Args>
72 void add_new_component(Args &&... args)
73 {
74 auto comp = _services->component_factory().create<T>(std::forward<Args>(args)...);
75 ArgumentPack<ITensorInfo> tensors = comp->tensors();
76 const auto src_tensor_ids = get_tensor_ids(tensors.get_const_src_tensors());
77 const auto dst_tensor_ids = get_tensor_ids(tensors.get_const_dst_tensors());
78 bool success = _dependency_graph.add_operator(comp->id(), src_tensor_ids, dst_tensor_ids);
SiCong Lifd766112022-11-09 16:01:44 +000079 ARM_COMPUTE_UNUSED(success);
SiCong Lif44bbc52022-08-29 18:25:51 +010080 ARM_COMPUTE_ERROR_ON(!success);
81 _components[comp->id()] = std::move(comp);
82 for(auto t : tensors.get_const_src_tensors())
83 {
84 _tensors[t->id()] = t;
85 }
86 for(auto t : tensors.get_const_dst_tensors())
87 {
88 _tensors[t->id()] = t;
89 }
90 }
91 /** Perform component fusion and serialize the graph into a stream of component groups
92 */
93 GpuKernelComponentStream fuse() const;
94
95private:
96 static std::vector<DependencyGraph::TensorId> get_tensor_ids(const std::vector<const ITensorInfo *> tensors);
97 GpuComponentServices *_services;
98 std::map<ComponentId, std::unique_ptr<IGpuKernelComponent>> _components;
99 std::map<ITensorInfo::Id, const ITensorInfo *> _tensors;
100 DependencyGraph _dependency_graph{};
101};
102} // namespace dynamic_fusion
103} // namespace experimental
104} // namespace arm_compute
105#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_GPUKERNELCOMPONENTGRAPH */